2024-05-16 16:24:50 +02:00
|
|
|
<script lang="ts">
|
2025-02-02 11:17:22 +01:00
|
|
|
import * as Collapsible from '$lib/components/ui/collapsible';
|
|
|
|
|
import { Button } from '$lib/components/ui/button';
|
2025-06-21 21:07:36 +02:00
|
|
|
import { ChevronDown, ChevronLeft, ChevronRight } from '@lucide/svelte';
|
|
|
|
|
import { getContext, setContext, type Snippet } from 'svelte';
|
|
|
|
|
import type { ClassValue } from 'svelte/elements';
|
|
|
|
|
import type { CollapsibleTreeState } from './utils.svelte';
|
2024-05-16 16:24:50 +02:00
|
|
|
|
2025-06-21 21:07:36 +02:00
|
|
|
const props: {
|
|
|
|
|
id: string | number;
|
|
|
|
|
class?: ClassValue;
|
|
|
|
|
trigger: Snippet;
|
|
|
|
|
content: Snippet;
|
|
|
|
|
} = $props();
|
2024-05-16 16:24:50 +02:00
|
|
|
|
2025-06-21 21:07:36 +02:00
|
|
|
let state = getContext<CollapsibleTreeState>('collapsible-tree-state');
|
2025-02-02 11:17:22 +01:00
|
|
|
let side = getContext<'left' | 'right'>('collapsible-tree-side');
|
|
|
|
|
let nohover = getContext<boolean>('collapsible-tree-nohover');
|
|
|
|
|
let slotInsideTrigger = getContext<boolean>('collapsible-tree-slot-inside-trigger');
|
|
|
|
|
let parentId = getContext<string>('collapsible-tree-parent-id');
|
2024-05-22 16:05:31 +02:00
|
|
|
|
2025-06-21 21:07:36 +02:00
|
|
|
let fullId = `${parentId}.${props.id}`;
|
2025-02-02 11:17:22 +01:00
|
|
|
setContext('collapsible-tree-parent-id', fullId);
|
2024-05-16 16:24:50 +02:00
|
|
|
|
2025-06-21 21:07:36 +02:00
|
|
|
let open = state.get(fullId);
|
2024-05-24 13:16:41 +02:00
|
|
|
|
2025-02-02 11:17:22 +01:00
|
|
|
export function openNode() {
|
2025-06-21 21:07:36 +02:00
|
|
|
open.current = true;
|
2025-02-02 11:17:22 +01:00
|
|
|
}
|
2024-05-16 16:24:50 +02:00
|
|
|
</script>
|
|
|
|
|
|
2025-06-21 21:07:36 +02:00
|
|
|
<Collapsible.Root bind:open={open.current} class={props.class}>
|
2025-02-02 11:17:22 +01:00
|
|
|
{#if slotInsideTrigger}
|
|
|
|
|
<Collapsible.Trigger class="w-full">
|
|
|
|
|
<Button
|
|
|
|
|
variant="ghost"
|
|
|
|
|
class="w-full flex flex-row {side === 'right'
|
|
|
|
|
? 'justify-between'
|
2025-10-18 18:51:11 +02:00
|
|
|
: 'justify-start'} p-0 has-[>svg]:px-0 h-fit {nohover
|
2025-02-02 11:17:22 +01:00
|
|
|
? 'hover:bg-background'
|
|
|
|
|
: ''} pointer-events-none"
|
|
|
|
|
>
|
|
|
|
|
{#if side === 'left'}
|
2025-06-21 21:07:36 +02:00
|
|
|
{#if open.current}
|
2025-02-02 11:17:22 +01:00
|
|
|
<ChevronDown size="16" class="shrink-0" />
|
|
|
|
|
{:else}
|
|
|
|
|
<ChevronRight size="16" class="shrink-0" />
|
|
|
|
|
{/if}
|
|
|
|
|
{/if}
|
2025-06-21 21:07:36 +02:00
|
|
|
{@render props.trigger()}
|
2025-02-02 11:17:22 +01:00
|
|
|
{#if side === 'right'}
|
2025-06-21 21:07:36 +02:00
|
|
|
{#if open.current}
|
2025-02-02 11:17:22 +01:00
|
|
|
<ChevronDown size="16" class="shrink-0" />
|
|
|
|
|
{:else}
|
|
|
|
|
<ChevronLeft size="16" class="shrink-0" />
|
|
|
|
|
{/if}
|
|
|
|
|
{/if}
|
|
|
|
|
</Button>
|
|
|
|
|
</Collapsible.Trigger>
|
|
|
|
|
{:else}
|
|
|
|
|
<Button
|
|
|
|
|
variant="ghost"
|
|
|
|
|
class="w-full flex flex-row {side === 'right'
|
|
|
|
|
? 'justify-between'
|
2025-10-18 18:51:11 +02:00
|
|
|
: 'justify-start'} p-0 has-[>svg]:px-0 h-fit {nohover ? 'hover:bg-background' : ''}"
|
2025-02-02 11:17:22 +01:00
|
|
|
>
|
|
|
|
|
{#if side === 'left'}
|
|
|
|
|
<Collapsible.Trigger>
|
2025-06-21 21:07:36 +02:00
|
|
|
{#if open.current}
|
2025-02-02 11:17:22 +01:00
|
|
|
<ChevronDown size="16" class="shrink-0" />
|
|
|
|
|
{:else}
|
|
|
|
|
<ChevronRight size="16" class="shrink-0" />
|
|
|
|
|
{/if}
|
|
|
|
|
</Collapsible.Trigger>
|
|
|
|
|
{/if}
|
2025-06-21 21:07:36 +02:00
|
|
|
{@render props.trigger()}
|
2025-02-02 11:17:22 +01:00
|
|
|
{#if side === 'right'}
|
|
|
|
|
<Collapsible.Trigger>
|
2025-06-21 21:07:36 +02:00
|
|
|
{#if open.current}
|
2025-02-02 11:17:22 +01:00
|
|
|
<ChevronDown size="16" class="shrink-0" />
|
|
|
|
|
{:else}
|
|
|
|
|
<ChevronLeft size="16" class="shrink-0" />
|
|
|
|
|
{/if}
|
|
|
|
|
</Collapsible.Trigger>
|
|
|
|
|
{/if}
|
|
|
|
|
</Button>
|
|
|
|
|
{/if}
|
2024-06-05 14:51:32 +02:00
|
|
|
|
2025-10-18 18:51:11 +02:00
|
|
|
<Collapsible.Content>
|
2025-06-21 21:07:36 +02:00
|
|
|
{@render props.content()}
|
2025-02-02 11:17:22 +01:00
|
|
|
</Collapsible.Content>
|
2024-05-16 16:24:50 +02:00
|
|
|
</Collapsible.Root>
|