@hydrateless/svelte 0.5.0 → 0.7.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
@@ -10,7 +10,7 @@ automatically when the element is destroyed.
10
10
  npm install hydrateless @hydrateless/svelte
11
11
  ```
12
12
 
13
- Import the CSS once (e.g. in your root layout):
13
+ Import the CSS once (e.g., in your root layout):
14
14
 
15
15
  ```ts
16
16
  import 'hydrateless/hydrateless.css';
@@ -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.d.ts CHANGED
@@ -1,25 +1,25 @@
1
1
  import type { Action } from 'svelte/action';
2
- /** `use:accordion` — single-open behavior on a `[data-hl-accordion]` element. */
2
+ /** `use:accordion`: single-open behavior on a `[data-hl-accordion]` element. */
3
3
  export declare const accordion: Action<HTMLElement, undefined, Record<never, any>>;
4
- /** `use:disclosure` — groups `[data-hl-disclosure]` elements within the node. */
4
+ /** `use:disclosure`: groups `[data-hl-disclosure]` elements within the node. */
5
5
  export declare const disclosure: Action<HTMLElement, undefined, Record<never, any>>;
6
- /** `use:tabs` — ARIA + keyboard navigation on a `[data-hl-tabs]` element. */
6
+ /** `use:tabs`: ARIA + keyboard navigation on a `[data-hl-tabs]` element. */
7
7
  export declare const tabs: Action<HTMLElement, undefined, Record<never, any>>;
8
- /** `use:dropdown` — menu pattern on a `[data-hl-dropdown]` element. */
8
+ /** `use:dropdown`: menu pattern on a `[data-hl-dropdown]` element. */
9
9
  export declare const dropdown: Action<HTMLElement, undefined, Record<never, any>>;
10
- /** `use:modal` — wires modal openers/closers + focus trap within the node. */
10
+ /** `use:modal`: wires modal openers/closers + focus trap within the node. */
11
11
  export declare const modal: Action<HTMLElement, undefined, Record<never, any>>;
12
- /** `use:drawer` — wires drawer openers/closers within the node. */
12
+ /** `use:drawer`: wires drawer openers/closers within the node. */
13
13
  export declare const drawer: Action<HTMLElement, undefined, Record<never, any>>;
14
- /** `use:popover` — wires popover openers/closers within the node. */
14
+ /** `use:popover`: wires popover openers/closers within the node. */
15
15
  export declare const popover: Action<HTMLElement, undefined, Record<never, any>>;
16
- /** `use:tooltip` — wires `[data-hl-tooltip]` triggers within the node. */
16
+ /** `use:tooltip`: wires `[data-hl-tooltip]` triggers within the node. */
17
17
  export declare const tooltip: Action<HTMLElement, undefined, Record<never, any>>;
18
- /** `use:toc` — builds a table of contents from the surrounding document. */
18
+ /** `use:toc`: builds a table of contents from the surrounding document. */
19
19
  export declare const toc: Action<HTMLElement, undefined, Record<never, any>>;
20
- /** `use:menu` — menubar/navigation pattern on a `[data-hl-menu]` element. */
20
+ /** `use:menu`: menubar/navigation pattern on a `[data-hl-menu]` element. */
21
21
  export declare const menu: Action<HTMLElement, undefined, Record<never, any>>;
22
- /** `use:combobox` — editable combobox on a `[data-hl-combobox]` element. */
22
+ /** `use:combobox`: editable combobox on a `[data-hl-combobox]` element. */
23
23
  export declare const combobox: Action<HTMLElement, undefined, Record<never, any>>;
24
- /** `use:command` — command palette on a `[data-hl-command]` element. */
24
+ /** `use:command`: command palette on a `[data-hl-command]` element. */
25
25
  export declare const command: Action<HTMLElement, undefined, Record<never, any>>;
package/dist/actions.js CHANGED
@@ -1,35 +1,31 @@
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
- /** `use:accordion` — single-open behavior on a `[data-hl-accordion]` element. */
8
+ /** `use:accordion`: single-open behavior on a `[data-hl-accordion]` element. */
13
9
  export const accordion = actionFor((node) => enhanceAccordion(node));
14
- /** `use:disclosure` — groups `[data-hl-disclosure]` elements within the node. */
10
+ /** `use:disclosure`: groups `[data-hl-disclosure]` elements within the node. */
15
11
  export const disclosure = actionFor((node) => enhanceDisclosure(node));
16
- /** `use:tabs` — ARIA + keyboard navigation on a `[data-hl-tabs]` element. */
12
+ /** `use:tabs`: ARIA + keyboard navigation on a `[data-hl-tabs]` element. */
17
13
  export const tabs = actionFor((node) => enhanceTabs(node));
18
- /** `use:dropdown` — menu pattern on a `[data-hl-dropdown]` element. */
14
+ /** `use:dropdown`: menu pattern on a `[data-hl-dropdown]` element. */
19
15
  export const dropdown = actionFor((node) => enhanceDropdown(node));
20
- /** `use:modal` — wires modal openers/closers + focus trap within the node. */
16
+ /** `use:modal`: wires modal openers/closers + focus trap within the node. */
21
17
  export const modal = actionFor((node) => enhanceModal(node));
22
- /** `use:drawer` — wires drawer openers/closers within the node. */
18
+ /** `use:drawer`: wires drawer openers/closers within the node. */
23
19
  export const drawer = actionFor((node) => enhanceDrawer(node));
24
- /** `use:popover` — wires popover openers/closers within the node. */
20
+ /** `use:popover`: wires popover openers/closers within the node. */
25
21
  export const popover = actionFor((node) => enhancePopover(node));
26
- /** `use:tooltip` — wires `[data-hl-tooltip]` triggers within the node. */
22
+ /** `use:tooltip`: wires `[data-hl-tooltip]` triggers within the node. */
27
23
  export const tooltip = actionFor((node) => enhanceTooltip(node));
28
- /** `use:toc` — builds a table of contents from the surrounding document. */
24
+ /** `use:toc`: builds a table of contents from the surrounding document. */
29
25
  export const toc = actionFor((node) => enhanceToc(node.ownerDocument));
30
- /** `use:menu` — menubar/navigation pattern on a `[data-hl-menu]` element. */
26
+ /** `use:menu`: menubar/navigation pattern on a `[data-hl-menu]` element. */
31
27
  export const menu = actionFor((node) => enhanceMenu(node));
32
- /** `use:combobox` — editable combobox on a `[data-hl-combobox]` element. */
28
+ /** `use:combobox`: editable combobox on a `[data-hl-combobox]` element. */
33
29
  export const combobox = actionFor((node) => enhanceCombobox(node));
34
- /** `use:command` — command palette on a `[data-hl-command]` element. */
30
+ /** `use:command`: command palette on a `[data-hl-command]` element. */
35
31
  export const command = actionFor((node) => enhanceCommand(node));
@@ -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,36 @@
1
1
  <script lang="ts">
2
+ import { enhancePopover, type PopoverApi } from '@hydrateless/enhancers';
3
+ import { untrack } from 'svelte';
2
4
  import type { Snippet } from 'svelte';
3
5
  import type { HTMLAttributes } from 'svelte/elements';
4
6
 
5
7
  interface Props extends HTMLAttributes<HTMLDivElement> {
6
- open: boolean;
8
+ /** Open state; two-way bindable (`bind:open`). The browser's light-dismiss
9
+ * (Escape/outside click) closes it and updates the binding. */
10
+ open?: boolean;
7
11
  children?: Snippet;
8
12
  }
9
13
 
10
- let { open, children, ...rest }: Props = $props();
14
+ let { open = $bindable(false), children, ...rest }: Props = $props();
15
+ let host = $state<HTMLDivElement>();
16
+ let api = $state<PopoverApi | null>(null);
17
+
18
+ $effect(() => {
19
+ if (!host) return;
20
+ const handle = enhancePopover(host, { onOpenChange: (next) => (open = next) });
21
+ api = handle.api;
22
+ if (untrack(() => open)) handle.api?.setOpen(true);
23
+ return () => {
24
+ handle.destroy();
25
+ api = null;
26
+ };
27
+ });
28
+
29
+ $effect(() => {
30
+ api?.setOpen(open);
31
+ });
11
32
  </script>
12
33
 
13
- <div {...rest} data-hl-popover role="dialog" hidden={!open}>
34
+ <div {...rest} data-hl-popover popover="auto" role="dialog" bind:this={host}>
14
35
  {@render children?.()}
15
36
  </div>
@@ -1,9 +1,11 @@
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`). The browser's light-dismiss
5
+ * (Escape/outside click) closes it and updates the binding. */
6
+ open?: boolean;
5
7
  children?: Snippet;
6
8
  }
7
- declare const Popover: import("svelte").Component<Props, {}, "">;
9
+ declare const Popover: import("svelte").Component<Props, {}, "open">;
8
10
  type Popover = ReturnType<typeof Popover>;
9
11
  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/context.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ /** Shared field state a Field provides to its controls (ids and validity flags). */
1
2
  export interface FieldContext {
2
3
  readonly id: string;
3
4
  readonly helpId: string;
@@ -5,14 +6,19 @@ export interface FieldContext {
5
6
  readonly invalid: boolean;
6
7
  readonly required: boolean;
7
8
  }
9
+ /** Publish {@link FieldContext} to the descendant controls of a Field. */
8
10
  export declare function setFieldContext(ctx: FieldContext): void;
11
+ /** Read the nearest {@link FieldContext}, or `undefined` outside a Field. */
9
12
  export declare function getFieldContext(): FieldContext | undefined;
10
13
  /** Returns attributes (`id`, `aria-describedby`, `aria-invalid`) for a field control. */
11
14
  export declare function fieldBindings(): Record<string, string | boolean | undefined>;
15
+ /** Shared state a RadioGroup provides to its radios (name, selected value, and a setter). */
12
16
  export interface RadioGroupContext {
13
17
  readonly name: string | undefined;
14
18
  readonly value: string | undefined;
15
19
  select: (value: string) => void;
16
20
  }
21
+ /** Publish {@link RadioGroupContext} to the descendant radios of a group. */
17
22
  export declare function setRadioGroupContext(ctx: RadioGroupContext): void;
23
+ /** Read the nearest {@link RadioGroupContext}, or `undefined` outside a group. */
18
24
  export declare function getRadioGroupContext(): RadioGroupContext | undefined;
package/dist/context.js CHANGED
@@ -1,8 +1,10 @@
1
1
  import { getContext, setContext } from 'svelte';
2
2
  const FIELD_KEY = Symbol('hl-field');
3
+ /** Publish {@link FieldContext} to the descendant controls of a Field. */
3
4
  export function setFieldContext(ctx) {
4
5
  setContext(FIELD_KEY, ctx);
5
6
  }
7
+ /** Read the nearest {@link FieldContext}, or `undefined` outside a Field. */
6
8
  export function getFieldContext() {
7
9
  return getContext(FIELD_KEY);
8
10
  }
@@ -19,9 +21,11 @@ export function fieldBindings() {
19
21
  };
20
22
  }
21
23
  const RADIO_GROUP_KEY = Symbol('hl-radio-group');
24
+ /** Publish {@link RadioGroupContext} to the descendant radios of a group. */
22
25
  export function setRadioGroupContext(ctx) {
23
26
  setContext(RADIO_GROUP_KEY, ctx);
24
27
  }
28
+ /** Read the nearest {@link RadioGroupContext}, or `undefined` outside a group. */
25
29
  export function getRadioGroupContext() {
26
30
  return getContext(RADIO_GROUP_KEY);
27
31
  }
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,7 +1,7 @@
1
1
  {
2
2
  "name": "@hydrateless/svelte",
3
- "version": "0.5.0",
4
- "description": "Svelte 5 bindings for Hydrateless — first-class components, actions, and a toast store.",
3
+ "version": "0.7.0",
4
+ "description": "Svelte 5 bindings for Hydrateless: first-class components, actions, and a toast store.",
5
5
  "license": "MIT",
6
6
  "repository": {
7
7
  "type": "git",
@@ -32,7 +32,7 @@
32
32
  "test": "vitest run"
33
33
  },
34
34
  "dependencies": {
35
- "@hydrateless/enhancers": "^0.5.0"
35
+ "@hydrateless/enhancers": "^0.7.0"
36
36
  },
37
37
  "peerDependencies": {
38
38
  "svelte": ">=5"