Files
gpx.studio/website/src/lib/components/collapsible-tree/CollapsibleTreeNode.svelte

99 lines
2.8 KiB
Svelte
Raw Normal View History

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-06-05 23:37:55 +02:00
import { getContext, onMount, setContext } from 'svelte';
2024-05-24 13:16:41 +02:00
import { get, type Writable } from 'svelte/store';
2024-05-16 16:24:50 +02:00
2024-05-22 16:05:31 +02:00
export let id: string | number;
2024-05-16 16:24:50 +02:00
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-06-05 14:51:32 +02:00
let slotInsideTrigger = getContext<boolean>('collapsible-tree-slot-inside-trigger');
2024-05-22 16:05:31 +02:00
let parentId = getContext<string>('collapsible-tree-parent-id');
let fullId = `${parentId}.${id}`;
setContext('collapsible-tree-parent-id', fullId);
2024-05-16 16:24:50 +02:00
2024-06-05 23:37:55 +02:00
onMount(() => {
if (!get(open).hasOwnProperty(fullId)) {
open.update((value) => {
value[fullId] = defaultState === 'open';
return value;
});
2024-05-16 16:24:50 +02:00
}
});
2024-05-24 13:16:41 +02:00
export function openNode() {
open.update((value) => {
value[fullId] = true;
return value;
});
}
2024-05-16 16:24:50 +02:00
</script>
2024-05-22 16:05:31 +02:00
<Collapsible.Root bind:open={$open[fullId]} class={$$props.class ?? ''}>
2024-06-05 14:51:32 +02:00
{#if slotInsideTrigger}
<Collapsible.Trigger class="w-full">
<Button
variant="ghost"
class="w-full flex flex-row {side === 'right'
? 'justify-between'
2024-06-27 15:50:15 +02:00
: 'justify-start'} py-0 px-1 h-fit {nohover
? 'hover:bg-background'
: ''} pointer-events-none"
2024-06-05 14:51:32 +02:00
>
{#if side === 'left'}
{#if $open[fullId]}
<ChevronDown size="16" class="shrink-0" />
{:else}
<ChevronRight size="16" class="shrink-0" />
{/if}
{/if}
<slot name="trigger" />
{#if side === 'right'}
{#if $open[fullId]}
<ChevronDown size="16" class="shrink-0" />
{:else}
<ChevronLeft size="16" class="shrink-0" />
{/if}
{/if}
</Button>
</Collapsible.Trigger>
{:else}
2024-05-16 16:24:50 +02:00
<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-06-05 14:51:32 +02:00
<Collapsible.Trigger>
{#if $open[fullId]}
<ChevronDown size="16" class="shrink-0" />
{:else}
<ChevronRight size="16" class="shrink-0" />
{/if}
</Collapsible.Trigger>
2024-05-16 19:10:26 +02:00
{/if}
<slot name="trigger" />
{#if side === 'right'}
2024-06-05 14:51:32 +02:00
<Collapsible.Trigger>
{#if $open[fullId]}
<ChevronDown size="16" class="shrink-0" />
{:else}
<ChevronLeft size="16" class="shrink-0" />
{/if}
</Collapsible.Trigger>
2024-05-16 18:18:42 +02:00
{/if}
2024-05-16 16:24:50 +02:00
</Button>
2024-06-05 14:51:32 +02:00
{/if}
<Collapsible.Content class="ml-{margin} pl-{margin}">
2024-05-16 16:24:50 +02:00
<slot name="content" />
</Collapsible.Content>
</Collapsible.Root>