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

147 lines
5.3 KiB
Svelte
Raw Normal View History

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';
2026-10-17 16:39:41 +02:00
import { stickyKey, stickyZIndex, trackStuck, type StickyLevel } from './sticky';
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;
2026-10-17 14:42:49 +02:00
/** Gets whether the node is open, for contents that are only worth building then. */
content: Snippet<[boolean]>;
2025-06-21 21:07:36 +02:00
} = $props();
2024-05-16 16:24:50 +02:00
2026-10-17 16:39:41 +02:00
let treeState = 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');
2026-10-17 16:39:41 +02:00
// where the row sticks, under the rows of the nodes this one is in
let level = getContext<StickyLevel>(stickyKey);
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
2026-10-17 16:39:41 +02:00
let open = treeState.get(fullId);
// the rows of the nodes in this one stick under this row, whatever its height
let rowHeight = $state(0);
setContext<StickyLevel>(stickyKey, {
get enabled() {
return level.enabled;
},
get top() {
return level.top + rowHeight;
},
get depth() {
return level.depth + 1;
},
get stuckRows() {
return level.stuckRows;
},
});
// the row is on top of the content that goes under it
let stuck = $state(false);
// only the deepest row that is stuck has a border: the rows above it run into it
let bordered = $derived(stuck && level.stuckRows.deepest === level.depth);
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>
2026-10-17 16:39:41 +02:00
{#snippet row()}
2025-02-02 11:17:22 +01:00
{#if slotInsideTrigger}
2026-10-17 16:39:41 +02:00
<Collapsible.Trigger class="w-full">
2025-02-02 11:17:22 +01:00
<Button
variant="ghost"
2025-10-26 12:12:23 +01:00
size="icon"
2026-04-06 18:22:01 +02:00
class="w-full flex flex-row gap-1 border-none {side === 'right'
2025-02-02 11:17:22 +01:00
? 'justify-between'
2025-10-26 12:12:23 +01:00
: 'justify-start pl-1'} 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"
2025-10-26 12:12:23 +01:00
size="icon"
2026-04-06 18:22:01 +02:00
class="w-full flex flex-row gap-1 border-none {side === 'right'
2025-02-02 11:17:22 +01:00
? 'justify-between'
2025-10-26 12:12:23 +01:00
: 'justify-start pl-1'} 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}
2026-10-17 16:39:41 +02:00
{/snippet}
<Collapsible.Root bind:open={open.current} class={props.class}>
{#if level.enabled}
<!-- sticks while some of the content of the node is in view, with a border when it does -->
<div
class={['sticky bg-background', bordered && 'border-b']}
style="top: {level.top}px; z-index: {stickyZIndex(level.depth)}"
bind:clientHeight={rowHeight}
{@attach (row) => {
const stop = trackStuck(
row,
() => level.top,
(value) => {
stuck = value;
level.stuckRows.set(fullId, level.depth, value);
}
);
return () => {
stop?.();
level.stuckRows.set(fullId, level.depth, false);
};
}}
>
{@render row()}
</div>
{:else}
{@render row()}
{/if}
2025-10-18 18:51:11 +02:00
<Collapsible.Content>
2026-10-17 14:42:49 +02:00
{@render props.content(open.current)}
2025-02-02 11:17:22 +01:00
</Collapsible.Content>
2024-05-16 16:24:50 +02:00
</Collapsible.Root>