@hydrateless/svelte 0.5.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -46,19 +46,45 @@ Available actions: `accordion`, `disclosure`, `tabs`, `dropdown`, `modal`,
46
46
  drawer, popover, tooltip) put the action on a wrapper that contains the trigger
47
47
  and target.
48
48
 
49
+ ## Components
50
+
51
+ Interactive components support two-way binding on their state:
52
+
53
+ ```svelte
54
+ <script lang="ts">
55
+ import { Tabs, TabList, Tab, TabPanel, Modal, ModalBody } from '@hydrateless/svelte';
56
+
57
+ let tab = $state('overview');
58
+ let open = $state(false);
59
+ </script>
60
+
61
+ <Tabs bind:value={tab}>
62
+ <TabList>
63
+ <Tab value="overview">Overview</Tab>
64
+ <Tab value="install">Install</Tab>
65
+ </TabList>
66
+ <TabPanel>Zero runtime by default.</TabPanel>
67
+ <TabPanel><code>npm i hydrateless</code></TabPanel>
68
+ </Tabs>
69
+
70
+ <button onclick={() => (open = true)}>Open</button>
71
+ <Modal bind:open>
72
+ <ModalBody>Body content.</ModalBody>
73
+ </Modal>
74
+ ```
75
+
49
76
  ## Toasts
50
77
 
78
+ `useToast()` works anywhere — no setup required:
79
+
51
80
  ```svelte
52
81
  <script lang="ts">
53
- import { onMount, onDestroy } from 'svelte';
54
- import { createToast } from '@hydrateless/svelte';
82
+ import { useToast } from '@hydrateless/svelte';
55
83
 
56
- let toast: ReturnType<typeof createToast>;
57
- onMount(() => (toast = createToast()));
58
- onDestroy(() => toast?.destroy());
84
+ const toast = useToast();
59
85
  </script>
60
86
 
61
- <button on:click={() => toast.show('Saved')}>Save</button>
87
+ <button onclick={() => toast.show('Saved', { variant: 'success' })}>Save</button>
62
88
  ```
63
89
 
64
90
  ## License
package/dist/actions.js CHANGED
@@ -1,12 +1,8 @@
1
1
  import { enhanceAccordion, enhanceCombobox, enhanceCommand, enhanceDisclosure, enhanceDropdown, enhanceDrawer, enhanceMenu, enhanceModal, enhancePopover, enhanceTabs, enhanceToc, enhanceTooltip, } from '@hydrateless/enhancers';
2
2
  function actionFor(enhance) {
3
3
  return (node) => {
4
- const dispose = enhance(node);
5
- return {
6
- destroy() {
7
- dispose();
8
- },
9
- };
4
+ const { destroy } = enhance(node);
5
+ return { destroy };
10
6
  };
11
7
  }
12
8
  /** `use:accordion` — single-open behavior on a `[data-hl-accordion]` element. */
@@ -1,20 +1,45 @@
1
1
  <script lang="ts">
2
- import { enhanceAccordion } from '@hydrateless/enhancers';
2
+ import { enhanceAccordion, type AccordionApi } from '@hydrateless/enhancers';
3
+ import { untrack } from 'svelte';
3
4
  import type { Snippet } from 'svelte';
4
5
  import type { HTMLAttributes } from 'svelte/elements';
5
6
 
6
7
  interface Props extends HTMLAttributes<HTMLDivElement> {
7
8
  /** Allow more than one panel to stay open at a time. */
8
9
  allowMultiple?: boolean;
10
+ /** Open item values; two-way bindable (`bind:value`). */
11
+ value?: string[];
12
+ /** Initially open item values for uncontrolled usage. */
13
+ defaultValue?: string[];
9
14
  children?: Snippet;
10
15
  }
11
16
 
12
- let { allowMultiple = false, children, ...rest }: Props = $props();
17
+ let {
18
+ allowMultiple = false,
19
+ value = $bindable(),
20
+ defaultValue,
21
+ children,
22
+ ...rest
23
+ }: Props = $props();
13
24
  let host = $state<HTMLDivElement>();
25
+ let api = $state<AccordionApi | null>(null);
14
26
 
15
27
  $effect(() => {
16
28
  if (!host) return;
17
- return enhanceAccordion(host, { allowMultiple });
29
+ const handle = enhanceAccordion(host, {
30
+ allowMultiple,
31
+ defaultValue: untrack(() => value ?? defaultValue),
32
+ onValueChange: (next) => (value = next),
33
+ });
34
+ api = handle.api;
35
+ return () => {
36
+ handle.destroy();
37
+ api = null;
38
+ };
39
+ });
40
+
41
+ $effect(() => {
42
+ if (value != null) api?.setValue(value);
18
43
  });
19
44
  </script>
20
45
 
@@ -3,8 +3,12 @@ import type { HTMLAttributes } from 'svelte/elements';
3
3
  interface Props extends HTMLAttributes<HTMLDivElement> {
4
4
  /** Allow more than one panel to stay open at a time. */
5
5
  allowMultiple?: boolean;
6
+ /** Open item values; two-way bindable (`bind:value`). */
7
+ value?: string[];
8
+ /** Initially open item values for uncontrolled usage. */
9
+ defaultValue?: string[];
6
10
  children?: Snippet;
7
11
  }
8
- declare const Accordion: import("svelte").Component<Props, {}, "">;
12
+ declare const Accordion: import("svelte").Component<Props, {}, "value">;
9
13
  type Accordion = ReturnType<typeof Accordion>;
10
14
  export default Accordion;
@@ -5,11 +5,13 @@
5
5
  interface Props extends Omit<HTMLAttributes<HTMLDetailsElement>, 'title'> {
6
6
  /** The clickable summary/trigger row. */
7
7
  summary?: Snippet;
8
+ /** Stable value identifying this item; defaults to its index. */
9
+ value?: string;
8
10
  defaultOpen?: boolean;
9
11
  children?: Snippet;
10
12
  }
11
13
 
12
- let { summary, defaultOpen = false, children, ...rest }: Props = $props();
14
+ let { summary, value, defaultOpen = false, children, ...rest }: Props = $props();
13
15
  let el = $state<HTMLDetailsElement>();
14
16
 
15
17
  $effect(() => {
@@ -17,7 +19,7 @@
17
19
  });
18
20
  </script>
19
21
 
20
- <details {...rest} bind:this={el}>
22
+ <details {...rest} bind:this={el} data-hl-value={value}>
21
23
  <summary>{@render summary?.()}</summary>
22
- <div class="accordion-panel">{@render children?.()}</div>
24
+ <div class="hl-accordion-panel">{@render children?.()}</div>
23
25
  </details>
@@ -3,6 +3,8 @@ import type { HTMLAttributes } from 'svelte/elements';
3
3
  interface Props extends Omit<HTMLAttributes<HTMLDetailsElement>, 'title'> {
4
4
  /** The clickable summary/trigger row. */
5
5
  summary?: Snippet;
6
+ /** Stable value identifying this item; defaults to its index. */
7
+ value?: string;
6
8
  defaultOpen?: boolean;
7
9
  children?: Snippet;
8
10
  }
@@ -1,30 +1,58 @@
1
1
  <script lang="ts">
2
- import { enhanceCombobox, type EnhanceComboboxOptions } from '@hydrateless/enhancers';
2
+ import {
3
+ enhanceCombobox,
4
+ type ComboboxApi,
5
+ type EnhanceComboboxOptions,
6
+ } from '@hydrateless/enhancers';
7
+ import { untrack } from 'svelte';
3
8
  import type { Snippet } from 'svelte';
4
9
  import type { HTMLAttributes } from 'svelte/elements';
5
10
 
6
11
  interface Props extends Omit<HTMLAttributes<HTMLDivElement>, 'onchange'> {
7
12
  filter?: EnhanceComboboxOptions['filter'];
8
13
  autoHighlight?: EnhanceComboboxOptions['autoHighlight'];
14
+ /** Committed value; two-way bindable (`bind:value`). */
15
+ value?: string;
16
+ /** Initial committed value for uncontrolled usage. */
17
+ defaultValue?: string;
9
18
  /** Called with the committed value when an option is selected. */
10
19
  onValueChange?: (value: string) => void;
11
20
  children?: Snippet;
12
21
  }
13
22
 
14
- let { filter, autoHighlight, onValueChange, children, ...rest }: Props = $props();
23
+ let {
24
+ filter,
25
+ autoHighlight,
26
+ value = $bindable(),
27
+ defaultValue,
28
+ onValueChange,
29
+ children,
30
+ ...rest
31
+ }: Props = $props();
15
32
  let host = $state<HTMLDivElement>();
33
+ let api = $state<ComboboxApi | null>(null);
16
34
 
17
35
  $effect(() => {
18
36
  if (!host) return;
19
- const node = host;
20
- const dispose = enhanceCombobox(node, { filter, autoHighlight });
21
- const handler = (e: Event) => onValueChange?.((e as CustomEvent).detail.value);
22
- node.addEventListener('hl:select', handler);
37
+ const handle = enhanceCombobox(host, {
38
+ filter,
39
+ autoHighlight,
40
+ defaultValue: untrack(() => value ?? defaultValue),
41
+ onValueChange: (next) => {
42
+ value = next;
43
+ onValueChange?.(next);
44
+ },
45
+ });
46
+ api = handle.api;
23
47
  return () => {
24
- node.removeEventListener('hl:select', handler);
25
- dispose();
48
+ handle.destroy();
49
+ api = null;
26
50
  };
27
51
  });
52
+
53
+ $effect(() => {
54
+ if (value != null) api?.setValue(value);
55
+ });
28
56
  </script>
29
57
 
30
58
  <div {...rest} data-hl-combobox bind:this={host}>
@@ -4,10 +4,14 @@ import type { HTMLAttributes } from 'svelte/elements';
4
4
  interface Props extends Omit<HTMLAttributes<HTMLDivElement>, 'onchange'> {
5
5
  filter?: EnhanceComboboxOptions['filter'];
6
6
  autoHighlight?: EnhanceComboboxOptions['autoHighlight'];
7
+ /** Committed value; two-way bindable (`bind:value`). */
8
+ value?: string;
9
+ /** Initial committed value for uncontrolled usage. */
10
+ defaultValue?: string;
7
11
  /** Called with the committed value when an option is selected. */
8
12
  onValueChange?: (value: string) => void;
9
13
  children?: Snippet;
10
14
  }
11
- declare const Combobox: import("svelte").Component<Props, {}, "">;
15
+ declare const Combobox: import("svelte").Component<Props, {}, "value">;
12
16
  type Combobox = ReturnType<typeof Combobox>;
13
17
  export default Combobox;
@@ -16,14 +16,7 @@
16
16
 
17
17
  $effect(() => {
18
18
  if (!host) return;
19
- const node = host;
20
- const dispose = enhanceCommand(node, { hotkey });
21
- const handler = (e: Event) => onSelect?.((e as CustomEvent).detail.value);
22
- node.addEventListener('hl:command', handler);
23
- return () => {
24
- node.removeEventListener('hl:command', handler);
25
- dispose();
26
- };
19
+ return enhanceCommand(host, { hotkey, onCommand: (value) => onSelect?.(value) }).destroy;
27
20
  });
28
21
  </script>
29
22
 
@@ -17,7 +17,7 @@
17
17
  });
18
18
  </script>
19
19
 
20
- <details {...rest} bind:this={el} class={['hydrateless-disclosure', klass]} data-hl-disclosure>
20
+ <details {...rest} bind:this={el} class={['hl-disclosure', klass]} data-hl-disclosure>
21
21
  <summary>{@render summary?.()}</summary>
22
- <div class="disclosure-panel">{@render children?.()}</div>
22
+ <div class="hl-disclosure-panel">{@render children?.()}</div>
23
23
  </details>
@@ -1,48 +1,47 @@
1
1
  <script lang="ts">
2
+ import { enhanceDrawer, type DrawerApi } from '@hydrateless/enhancers';
3
+ import { untrack } from 'svelte';
2
4
  import type { Snippet } from 'svelte';
3
5
  import type { HTMLDialogAttributes } from 'svelte/elements';
4
6
 
5
7
  interface Props extends HTMLDialogAttributes {
6
- open: boolean;
8
+ /** Open state; two-way bindable (`bind:open`). */
9
+ open?: boolean;
7
10
  side?: 'left' | 'right';
8
11
  closeOnBackdrop?: boolean;
9
- onclose?: () => void;
10
12
  children?: Snippet;
11
13
  }
12
14
 
13
15
  let {
14
- open,
16
+ open = $bindable(false),
15
17
  side = 'right',
16
18
  closeOnBackdrop = true,
17
- onclose,
18
19
  class: klass,
19
20
  children,
20
21
  ...rest
21
22
  }: Props = $props();
22
23
  let dialog = $state<HTMLDialogElement>();
24
+ let api = $state<DrawerApi | null>(null);
23
25
 
24
26
  $effect(() => {
25
- const el = dialog;
26
- if (!el) return;
27
- if (open && !el.open) el.showModal();
28
- else if (!open && el.open) el.close();
27
+ if (!dialog) return;
28
+ const handle = enhanceDrawer(dialog, {
29
+ closeOnBackdrop,
30
+ onOpenChange: (next) => (open = next),
31
+ });
32
+ api = handle.api;
33
+ if (untrack(() => open)) handle.api?.setOpen(true);
34
+ return () => {
35
+ handle.destroy();
36
+ api = null;
37
+ };
29
38
  });
30
39
 
31
- function handleClose() {
32
- onclose?.();
33
- }
34
- function handleClick(e: MouseEvent) {
35
- if (closeOnBackdrop && e.target === dialog) onclose?.();
36
- }
40
+ $effect(() => {
41
+ api?.setOpen(open);
42
+ });
37
43
  </script>
38
44
 
39
- <dialog
40
- {...rest}
41
- bind:this={dialog}
42
- data-side={side}
43
- class={['hydrateless-drawer', klass]}
44
- onclose={handleClose}
45
- onclick={handleClick}
46
- >
45
+ <dialog {...rest} bind:this={dialog} data-side={side} class={['hl-drawer', klass]} data-hl-drawer>
47
46
  {@render children?.()}
48
47
  </dialog>
@@ -1,12 +1,12 @@
1
1
  import type { Snippet } from 'svelte';
2
2
  import type { HTMLDialogAttributes } from 'svelte/elements';
3
3
  interface Props extends HTMLDialogAttributes {
4
- open: boolean;
4
+ /** Open state; two-way bindable (`bind:open`). */
5
+ open?: boolean;
5
6
  side?: 'left' | 'right';
6
7
  closeOnBackdrop?: boolean;
7
- onclose?: () => void;
8
8
  children?: Snippet;
9
9
  }
10
- declare const Drawer: import("svelte").Component<Props, {}, "">;
10
+ declare const Drawer: import("svelte").Component<Props, {}, "open">;
11
11
  type Drawer = ReturnType<typeof Drawer>;
12
12
  export default Drawer;
@@ -1,19 +1,40 @@
1
1
  <script lang="ts">
2
- import { enhanceDropdown, type EnhanceDropdownOptions } from '@hydrateless/enhancers';
2
+ import {
3
+ enhanceDropdown,
4
+ type DropdownApi,
5
+ type EnhanceDropdownOptions,
6
+ } from '@hydrateless/enhancers';
7
+ import { untrack } from 'svelte';
3
8
  import type { Snippet } from 'svelte';
4
9
  import type { HTMLAttributes } from 'svelte/elements';
5
10
 
6
11
  interface Props extends HTMLAttributes<HTMLDivElement> {
7
12
  placement?: EnhanceDropdownOptions['placement'];
13
+ /** Open state; two-way bindable (`bind:open`). */
14
+ open?: boolean;
8
15
  children?: Snippet;
9
16
  }
10
17
 
11
- let { placement, children, ...rest }: Props = $props();
18
+ let { placement, open = $bindable(false), children, ...rest }: Props = $props();
12
19
  let host = $state<HTMLDivElement>();
20
+ let api = $state<DropdownApi | null>(null);
13
21
 
14
22
  $effect(() => {
15
23
  if (!host) return;
16
- return enhanceDropdown(host, { placement });
24
+ const handle = enhanceDropdown(host, {
25
+ placement,
26
+ defaultOpen: untrack(() => open),
27
+ onOpenChange: (next) => (open = next),
28
+ });
29
+ api = handle.api;
30
+ return () => {
31
+ handle.destroy();
32
+ api = null;
33
+ };
34
+ });
35
+
36
+ $effect(() => {
37
+ api?.setOpen(open);
17
38
  });
18
39
  </script>
19
40
 
@@ -3,8 +3,10 @@ import type { Snippet } from 'svelte';
3
3
  import type { HTMLAttributes } from 'svelte/elements';
4
4
  interface Props extends HTMLAttributes<HTMLDivElement> {
5
5
  placement?: EnhanceDropdownOptions['placement'];
6
+ /** Open state; two-way bindable (`bind:open`). */
7
+ open?: boolean;
6
8
  children?: Snippet;
7
9
  }
8
- declare const Dropdown: import("svelte").Component<Props, {}, "">;
10
+ declare const Dropdown: import("svelte").Component<Props, {}, "open">;
9
11
  type Dropdown = ReturnType<typeof Dropdown>;
10
12
  export default Dropdown;
@@ -13,7 +13,7 @@
13
13
 
14
14
  $effect(() => {
15
15
  if (!host) return;
16
- return enhanceMenu(host, { orientation });
16
+ return enhanceMenu(host, { orientation }).destroy;
17
17
  });
18
18
  </script>
19
19
 
@@ -1,38 +1,45 @@
1
1
  <script lang="ts">
2
+ import { enhanceModal, type ModalApi } from '@hydrateless/enhancers';
3
+ import { untrack } from 'svelte';
2
4
  import type { Snippet } from 'svelte';
3
5
  import type { HTMLDialogAttributes } from 'svelte/elements';
4
6
 
5
7
  interface Props extends HTMLDialogAttributes {
6
- open: boolean;
8
+ /** Open state; two-way bindable (`bind:open`). */
9
+ open?: boolean;
7
10
  closeOnBackdrop?: boolean;
8
- onclose?: () => void;
9
11
  children?: Snippet;
10
12
  }
11
13
 
12
- let { open, closeOnBackdrop = true, onclose, class: klass, children, ...rest }: Props = $props();
14
+ let {
15
+ open = $bindable(false),
16
+ closeOnBackdrop = true,
17
+ class: klass,
18
+ children,
19
+ ...rest
20
+ }: Props = $props();
13
21
  let dialog = $state<HTMLDialogElement>();
22
+ let api = $state<ModalApi | null>(null);
14
23
 
15
24
  $effect(() => {
16
- const el = dialog;
17
- if (!el) return;
18
- if (open && !el.open) el.showModal();
19
- else if (!open && el.open) el.close();
25
+ if (!dialog) return;
26
+ const handle = enhanceModal(dialog, {
27
+ closeOnBackdrop,
28
+ onOpenChange: (next) => (open = next),
29
+ });
30
+ api = handle.api;
31
+ if (untrack(() => open)) handle.api?.setOpen(true);
32
+ return () => {
33
+ handle.destroy();
34
+ api = null;
35
+ };
20
36
  });
21
37
 
22
- function handleClose() {
23
- onclose?.();
24
- }
25
- function handleClick(e: MouseEvent) {
26
- if (closeOnBackdrop && e.target === dialog) onclose?.();
27
- }
38
+ $effect(() => {
39
+ api?.setOpen(open);
40
+ });
28
41
  </script>
29
42
 
30
- <dialog
31
- {...rest}
32
- bind:this={dialog}
33
- class={['hydrateless-modal', klass]}
34
- onclose={handleClose}
35
- onclick={handleClick}
36
- >
43
+ <dialog {...rest} bind:this={dialog} class={['hl-modal', klass]} data-hl-modal>
37
44
  {@render children?.()}
38
45
  </dialog>
@@ -1,11 +1,11 @@
1
1
  import type { Snippet } from 'svelte';
2
2
  import type { HTMLDialogAttributes } from 'svelte/elements';
3
3
  interface Props extends HTMLDialogAttributes {
4
- open: boolean;
4
+ /** Open state; two-way bindable (`bind:open`). */
5
+ open?: boolean;
5
6
  closeOnBackdrop?: boolean;
6
- onclose?: () => void;
7
7
  children?: Snippet;
8
8
  }
9
- declare const Modal: import("svelte").Component<Props, {}, "">;
9
+ declare const Modal: import("svelte").Component<Props, {}, "open">;
10
10
  type Modal = ReturnType<typeof Modal>;
11
11
  export default Modal;
@@ -1,15 +1,25 @@
1
1
  <script lang="ts">
2
+ import { combine, onClickOutside, onEscape } from '@hydrateless/enhancers';
2
3
  import type { Snippet } from 'svelte';
3
4
  import type { HTMLAttributes } from 'svelte/elements';
4
5
 
5
6
  interface Props extends HTMLAttributes<HTMLDivElement> {
6
- open: boolean;
7
+ /** Open state; two-way bindable (`bind:open`). Escape/outside clicks close it. */
8
+ open?: boolean;
7
9
  children?: Snippet;
8
10
  }
9
11
 
10
- let { open, children, ...rest }: Props = $props();
12
+ let { open = $bindable(false), children, ...rest }: Props = $props();
13
+ let host = $state<HTMLDivElement>();
14
+
15
+ $effect(() => {
16
+ const el = host;
17
+ if (!el || !open) return;
18
+ const dismiss = () => (open = false);
19
+ return combine([onClickOutside(el, dismiss), onEscape(dismiss, el.ownerDocument)]);
20
+ });
11
21
  </script>
12
22
 
13
- <div {...rest} data-hl-popover role="dialog" hidden={!open}>
23
+ <div {...rest} data-hl-popover role="dialog" hidden={!open} bind:this={host}>
14
24
  {@render children?.()}
15
25
  </div>
@@ -1,9 +1,10 @@
1
1
  import type { Snippet } from 'svelte';
2
2
  import type { HTMLAttributes } from 'svelte/elements';
3
3
  interface Props extends HTMLAttributes<HTMLDivElement> {
4
- open: boolean;
4
+ /** Open state; two-way bindable (`bind:open`). Escape/outside clicks close it. */
5
+ open?: boolean;
5
6
  children?: Snippet;
6
7
  }
7
- declare const Popover: import("svelte").Component<Props, {}, "">;
8
+ declare const Popover: import("svelte").Component<Props, {}, "open">;
8
9
  type Popover = ReturnType<typeof Popover>;
9
10
  export default Popover;
@@ -9,6 +9,6 @@
9
9
  let { class: klass, children, ...rest }: Props = $props();
10
10
  </script>
11
11
 
12
- <a {...rest} class={['a11y-skip-link', klass]}>
12
+ <a {...rest} class={['hl-skip-link', klass]}>
13
13
  {#if children}{@render children()}{:else}Skip to content{/if}
14
14
  </a>
@@ -3,12 +3,14 @@
3
3
  import type { HTMLButtonAttributes } from 'svelte/elements';
4
4
 
5
5
  interface Props extends HTMLButtonAttributes {
6
+ /** Stable value identifying this tab; defaults to its index. */
7
+ value?: string;
6
8
  children?: Snippet;
7
9
  }
8
10
 
9
- let { children, ...rest }: Props = $props();
11
+ let { value, children, ...rest }: Props = $props();
10
12
  </script>
11
13
 
12
- <button type="button" {...rest} role="tab">
14
+ <button type="button" {...rest} role="tab" data-hl-value={value}>
13
15
  {@render children?.()}
14
16
  </button>
@@ -1,6 +1,8 @@
1
1
  import type { Snippet } from 'svelte';
2
2
  import type { HTMLButtonAttributes } from 'svelte/elements';
3
3
  interface Props extends HTMLButtonAttributes {
4
+ /** Stable value identifying this tab; defaults to its index. */
5
+ value?: string;
4
6
  children?: Snippet;
5
7
  }
6
8
  declare const Tab: import("svelte").Component<Props, {}, "">;
@@ -1,20 +1,47 @@
1
1
  <script lang="ts">
2
- import { enhanceTabs, type EnhanceTabsOptions } from '@hydrateless/enhancers';
2
+ import { enhanceTabs, type EnhanceTabsOptions, type TabsApi } from '@hydrateless/enhancers';
3
+ import { untrack } from 'svelte';
3
4
  import type { Snippet } from 'svelte';
4
5
  import type { HTMLAttributes } from 'svelte/elements';
5
6
 
6
7
  interface Props extends HTMLAttributes<HTMLDivElement> {
7
8
  activation?: EnhanceTabsOptions['activation'];
8
9
  orientation?: EnhanceTabsOptions['orientation'];
10
+ /** Selected tab value; two-way bindable (`bind:value`). */
11
+ value?: string;
12
+ /** Initial value for uncontrolled usage. */
13
+ defaultValue?: string;
9
14
  children?: Snippet;
10
15
  }
11
16
 
12
- let { activation, orientation, children, ...rest }: Props = $props();
17
+ let {
18
+ activation,
19
+ orientation,
20
+ value = $bindable(),
21
+ defaultValue,
22
+ children,
23
+ ...rest
24
+ }: Props = $props();
13
25
  let host = $state<HTMLDivElement>();
26
+ let api = $state<TabsApi | null>(null);
14
27
 
15
28
  $effect(() => {
16
29
  if (!host) return;
17
- return enhanceTabs(host, { activation, orientation });
30
+ const handle = enhanceTabs(host, {
31
+ activation,
32
+ orientation,
33
+ defaultValue: untrack(() => value ?? defaultValue),
34
+ onValueChange: (next) => (value = next),
35
+ });
36
+ api = handle.api;
37
+ return () => {
38
+ handle.destroy();
39
+ api = null;
40
+ };
41
+ });
42
+
43
+ $effect(() => {
44
+ if (value != null) api?.setValue(value);
18
45
  });
19
46
  </script>
20
47
 
@@ -4,8 +4,12 @@ import type { HTMLAttributes } from 'svelte/elements';
4
4
  interface Props extends HTMLAttributes<HTMLDivElement> {
5
5
  activation?: EnhanceTabsOptions['activation'];
6
6
  orientation?: EnhanceTabsOptions['orientation'];
7
+ /** Selected tab value; two-way bindable (`bind:value`). */
8
+ value?: string;
9
+ /** Initial value for uncontrolled usage. */
10
+ defaultValue?: string;
7
11
  children?: Snippet;
8
12
  }
9
- declare const Tabs: import("svelte").Component<Props, {}, "">;
13
+ declare const Tabs: import("svelte").Component<Props, {}, "value">;
10
14
  type Tabs = ReturnType<typeof Tabs>;
11
15
  export default Tabs;
@@ -13,7 +13,7 @@
13
13
 
14
14
  $effect(() => {
15
15
  if (!host) return;
16
- return enhanceToc(host.ownerDocument, { contentSelector, headings, scrollSpy });
16
+ return enhanceToc(host.ownerDocument, { contentSelector, headings, scrollSpy }).destroy;
17
17
  });
18
18
  </script>
19
19
 
@@ -24,7 +24,7 @@
24
24
  trigger.setAttribute('data-hl-tooltip', tipId);
25
25
  trigger.setAttribute('aria-describedby', tipId);
26
26
  }
27
- return enhanceTooltip(host);
27
+ return enhanceTooltip(host).destroy;
28
28
  });
29
29
  </script>
30
30
 
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  export { accordion, disclosure, tabs, dropdown, modal, drawer, popover, tooltip, toc, menu, combobox, command, } from './actions.js';
2
- export { createToast } from './toast.js';
2
+ export { useToast } from './toast.js';
3
3
  export { fieldBindings } from './context.js';
4
4
  export type { FieldContext, RadioGroupContext } from './context.js';
5
5
  export { default as Accordion } from './components/Accordion.svelte';
@@ -72,4 +72,4 @@ export { default as Separator } from './components/Separator.svelte';
72
72
  export { default as Pagination } from './components/Pagination.svelte';
73
73
  export { default as Toc } from './components/Toc.svelte';
74
74
  export { default as SkipLink } from './components/SkipLink.svelte';
75
- export type { Disposer } from '@hydrateless/enhancers';
75
+ export type { AccordionApi, ComboboxApi, Disposer, DrawerApi, DropdownApi, EnhancerHandle, ModalApi, PopoverApi, TabsApi, ToastApi, TooltipApi, } from '@hydrateless/enhancers';
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  // Actions + toast store (low-level layer)
2
2
  export { accordion, disclosure, tabs, dropdown, modal, drawer, popover, tooltip, toc, menu, combobox, command, } from './actions.js';
3
- export { createToast } from './toast.js';
3
+ export { useToast } from './toast.js';
4
4
  // Field/radio context helpers
5
5
  export { fieldBindings } from './context.js';
6
6
  // Disclosure
package/dist/toast.d.ts CHANGED
@@ -1,14 +1,14 @@
1
- import { type ToastApi } from '@hydrateless/enhancers';
1
+ import { type EnhanceToastOptions, type ToastApi } from '@hydrateless/enhancers';
2
2
  /**
3
- * Create a toast controller. Call in `onMount` and tear down in `onDestroy`:
3
+ * Imperative toast API backed by a lazily-created document-level region (or a
4
+ * `[data-hl-toast-region]` you render yourself, e.g. via `<ToastRegion>`).
5
+ * Safe to call from anywhere — no setup required:
4
6
  *
5
7
  * ```ts
6
- * import { onMount, onDestroy } from 'svelte';
7
- * import { createToast } from '@hydrateless/svelte';
8
+ * import { useToast } from '@hydrateless/svelte';
8
9
  *
9
- * let toast: ReturnType<typeof createToast>;
10
- * onMount(() => (toast = createToast()));
11
- * onDestroy(() => toast?.destroy());
10
+ * const toast = useToast();
11
+ * toast.show('Saved', { variant: 'success' });
12
12
  * ```
13
13
  */
14
- export declare function createToast(container?: Document | HTMLElement): ToastApi;
14
+ export declare function useToast(options?: EnhanceToastOptions): ToastApi;
package/dist/toast.js CHANGED
@@ -1,16 +1,26 @@
1
1
  import { enhanceToast } from '@hydrateless/enhancers';
2
+ let shared = null;
2
3
  /**
3
- * Create a toast controller. Call in `onMount` and tear down in `onDestroy`:
4
+ * Imperative toast API backed by a lazily-created document-level region (or a
5
+ * `[data-hl-toast-region]` you render yourself, e.g. via `<ToastRegion>`).
6
+ * Safe to call from anywhere — no setup required:
4
7
  *
5
8
  * ```ts
6
- * import { onMount, onDestroy } from 'svelte';
7
- * import { createToast } from '@hydrateless/svelte';
9
+ * import { useToast } from '@hydrateless/svelte';
8
10
  *
9
- * let toast: ReturnType<typeof createToast>;
10
- * onMount(() => (toast = createToast()));
11
- * onDestroy(() => toast?.destroy());
11
+ * const toast = useToast();
12
+ * toast.show('Saved', { variant: 'success' });
12
13
  * ```
13
14
  */
14
- export function createToast(container = document) {
15
- return enhanceToast(container);
15
+ export function useToast(options) {
16
+ return {
17
+ show(message, opts) {
18
+ // The toast enhancer always creates a region, so `api` is never null.
19
+ const api = (shared ?? (shared = enhanceToast(document, options).api));
20
+ return api.show(message, opts);
21
+ },
22
+ dismiss(toast) {
23
+ shared?.dismiss(toast);
24
+ },
25
+ };
16
26
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hydrateless/svelte",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
4
4
  "description": "Svelte 5 bindings for Hydrateless — first-class components, actions, and a toast store.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -32,7 +32,7 @@
32
32
  "test": "vitest run"
33
33
  },
34
34
  "dependencies": {
35
- "@hydrateless/enhancers": "^0.5.0"
35
+ "@hydrateless/enhancers": "^0.6.0"
36
36
  },
37
37
  "peerDependencies": {
38
38
  "svelte": ">=5"