-
-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathTab.svelte
More file actions
34 lines (28 loc) · 1 KB
/
Tab.svelte
File metadata and controls
34 lines (28 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<script lang="ts">
import type { Snippet, Component } from 'svelte';
import type { HTMLAttributes } from 'svelte/elements';
import { cls } from '@layerstack/tailwind';
import { getContext, untrack } from 'svelte';
interface Props extends HTMLAttributes<HTMLDivElement> {
children: Snippet;
label?: string;
icon?: Component;
}
const { children, label, icon, class: className, ...restProps }: Props = $props();
const tabsContext = getContext<{
activeTab: number;
setActiveTab: (index: number) => void;
registerTab: (label: string | undefined, icon: Component | undefined) => number;
}>('tabs');
// Register this tab and get its index
// Use untrack to capture the initial values without creating a reactive dependency
const tabIndex =
tabsContext?.registerTab(
untrack(() => label),
untrack(() => icon)
) ?? 0;
const isActive = $derived(tabsContext?.activeTab === tabIndex);
</script>
<div class={cls('tab', !isActive && 'hidden', className)} {...restProps}>
{@render children?.()}
</div>