2024-05-16 16:24:50 +02:00
|
|
|
<script lang="ts">
|
|
|
|
import * as Collapsible from '$lib/components/ui/collapsible';
|
|
|
|
import { Button } from '$lib/components/ui/button';
|
2024-05-16 19:10:26 +02:00
|
|
|
import { ChevronDown, ChevronLeft, ChevronRight } from 'lucide-svelte';
|
2024-05-16 16:24:50 +02:00
|
|
|
import { getContext } from 'svelte';
|
|
|
|
import type { Writable } from 'svelte/store';
|
|
|
|
|
|
|
|
export let id: string;
|
|
|
|
|
2024-05-17 12:20:46 +02:00
|
|
|
let defaultState = getContext<'open' | 'closed'>('collapsible-tree-default-state');
|
2024-05-16 16:24:50 +02:00
|
|
|
let open = getContext<Writable<Record<string, boolean>>>('collapsible-tree-state');
|
2024-05-16 18:18:42 +02:00
|
|
|
let side = getContext<'left' | 'right'>('collapsible-tree-side');
|
2024-05-16 19:10:26 +02:00
|
|
|
let margin = getContext<number>('collapsible-tree-margin');
|
|
|
|
let nohover = getContext<boolean>('collapsible-tree-nohover');
|
2024-05-16 16:24:50 +02:00
|
|
|
|
|
|
|
open.update((value) => {
|
|
|
|
if (!value.hasOwnProperty(id)) {
|
2024-05-17 12:20:46 +02:00
|
|
|
value[id] = defaultState === 'open';
|
2024-05-16 16:24:50 +02:00
|
|
|
}
|
|
|
|
return value;
|
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
2024-05-16 19:10:26 +02:00
|
|
|
<Collapsible.Root bind:open={$open[id]} class={$$props.class ?? ''}>
|
2024-05-16 16:24:50 +02:00
|
|
|
<Collapsible.Trigger class="w-full">
|
|
|
|
<Button
|
|
|
|
variant="ghost"
|
2024-05-16 18:18:42 +02:00
|
|
|
class="w-full flex flex-row {side === 'right'
|
|
|
|
? 'justify-between'
|
2024-05-16 19:10:26 +02:00
|
|
|
: 'justify-start'} py-0 px-1 h-fit {nohover ? 'hover:bg-background' : ''}"
|
2024-05-16 16:24:50 +02:00
|
|
|
>
|
2024-05-16 18:18:42 +02:00
|
|
|
{#if side === 'left'}
|
2024-05-16 19:10:26 +02:00
|
|
|
{#if $open[id]}
|
|
|
|
<ChevronDown size="16" class="shrink-0" />
|
|
|
|
{:else}
|
|
|
|
<ChevronRight size="16" class="shrink-0" />
|
|
|
|
{/if}
|
|
|
|
{/if}
|
|
|
|
<slot name="trigger" />
|
|
|
|
{#if side === 'right'}
|
|
|
|
{#if $open[id]}
|
|
|
|
<ChevronDown size="16" class="shrink-0" />
|
|
|
|
{:else}
|
|
|
|
<ChevronLeft size="16" class="shrink-0" />
|
|
|
|
{/if}
|
2024-05-16 18:18:42 +02:00
|
|
|
{/if}
|
2024-05-16 16:24:50 +02:00
|
|
|
</Button>
|
|
|
|
</Collapsible.Trigger>
|
2024-05-16 19:10:26 +02:00
|
|
|
<Collapsible.Content class="ml-{margin}">
|
2024-05-16 16:24:50 +02:00
|
|
|
<slot name="content" />
|
|
|
|
</Collapsible.Content>
|
|
|
|
</Collapsible.Root>
|