@hydrateless/svelte 0.7.0 → 0.8.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.
@@ -1,23 +1,57 @@
1
1
  <script lang="ts">
2
+ import { enhanceDisclosure, type DisclosureApi } 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 Omit<HTMLAttributes<HTMLDetailsElement>, 'title'> {
6
8
  /** The clickable summary/trigger row. */
7
9
  summary?: Snippet;
10
+ /** Open state; two-way bindable (`bind:open`). */
11
+ open?: boolean;
12
+ /** Open the disclosure initially for uncontrolled usage. */
8
13
  defaultOpen?: boolean;
14
+ /**
15
+ * Group name. Disclosures sharing a `name` are exclusive: the browser
16
+ * closes the others when one opens, with no JavaScript.
17
+ */
18
+ name?: string;
9
19
  children?: Snippet;
10
20
  }
11
21
 
12
- let { summary, defaultOpen = false, class: klass, children, ...rest }: Props = $props();
22
+ let {
23
+ summary,
24
+ defaultOpen = false,
25
+ open = $bindable(),
26
+ name,
27
+ class: klass,
28
+ children,
29
+ ...rest
30
+ }: Props = $props();
31
+ // svelte-ignore state_referenced_locally -- seeds the uncontrolled initial value once
32
+ if (open === undefined) open = defaultOpen;
13
33
  let el = $state<HTMLDetailsElement>();
34
+ let api = $state<DisclosureApi | null>(null);
14
35
 
15
36
  $effect(() => {
16
- if (el && defaultOpen) el.open = true;
37
+ if (!el) return;
38
+ const handle = enhanceDisclosure(el, {
39
+ defaultOpen: untrack(() => open),
40
+ onOpenChange: (next) => (open = next),
41
+ });
42
+ api = handle.api;
43
+ return () => {
44
+ handle.destroy();
45
+ api = null;
46
+ };
47
+ });
48
+
49
+ $effect(() => {
50
+ if (open != null) api?.setOpen(open);
17
51
  });
18
52
  </script>
19
53
 
20
- <details {...rest} bind:this={el} class={['hl-disclosure', klass]} data-hl-disclosure>
54
+ <details {...rest} bind:this={el} {name} class={['hl-disclosure', klass]} data-hl-disclosure>
21
55
  <summary>{@render summary?.()}</summary>
22
56
  <div class="hl-disclosure-panel">{@render children?.()}</div>
23
57
  </details>
@@ -3,9 +3,17 @@ 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
+ /** Open state; two-way bindable (`bind:open`). */
7
+ open?: boolean;
8
+ /** Open the disclosure initially for uncontrolled usage. */
6
9
  defaultOpen?: boolean;
10
+ /**
11
+ * Group name. Disclosures sharing a `name` are exclusive: the browser
12
+ * closes the others when one opens, with no JavaScript.
13
+ */
14
+ name?: string;
7
15
  children?: Snippet;
8
16
  }
9
- declare const Disclosure: import("svelte").Component<Props, {}, "">;
17
+ declare const Disclosure: import("svelte").Component<Props, {}, "open">;
10
18
  type Disclosure = ReturnType<typeof Disclosure>;
11
19
  export default Disclosure;
@@ -7,19 +7,24 @@
7
7
  interface Props extends HTMLDialogAttributes {
8
8
  /** Open state; two-way bindable (`bind:open`). */
9
9
  open?: boolean;
10
+ /** Open the drawer initially for uncontrolled usage. */
11
+ defaultOpen?: boolean;
10
12
  side?: 'left' | 'right';
11
13
  closeOnBackdrop?: boolean;
12
14
  children?: Snippet;
13
15
  }
14
16
 
15
17
  let {
16
- open = $bindable(false),
18
+ defaultOpen = false,
19
+ open = $bindable(),
17
20
  side = 'right',
18
21
  closeOnBackdrop = true,
19
22
  class: klass,
20
23
  children,
21
24
  ...rest
22
25
  }: Props = $props();
26
+ // svelte-ignore state_referenced_locally -- seeds the uncontrolled initial value once
27
+ if (open === undefined) open = defaultOpen;
23
28
  let dialog = $state<HTMLDialogElement>();
24
29
  let api = $state<DrawerApi | null>(null);
25
30
 
@@ -27,10 +32,10 @@
27
32
  if (!dialog) return;
28
33
  const handle = enhanceDrawer(dialog, {
29
34
  closeOnBackdrop,
35
+ defaultOpen: untrack(() => open),
30
36
  onOpenChange: (next) => (open = next),
31
37
  });
32
38
  api = handle.api;
33
- if (untrack(() => open)) handle.api?.setOpen(true);
34
39
  return () => {
35
40
  handle.destroy();
36
41
  api = null;
@@ -38,7 +43,7 @@
38
43
  });
39
44
 
40
45
  $effect(() => {
41
- api?.setOpen(open);
46
+ if (open != null) api?.setOpen(open);
42
47
  });
43
48
  </script>
44
49
 
@@ -3,6 +3,8 @@ import type { HTMLDialogAttributes } from 'svelte/elements';
3
3
  interface Props extends HTMLDialogAttributes {
4
4
  /** Open state; two-way bindable (`bind:open`). */
5
5
  open?: boolean;
6
+ /** Open the drawer initially for uncontrolled usage. */
7
+ defaultOpen?: boolean;
6
8
  side?: 'left' | 'right';
7
9
  closeOnBackdrop?: boolean;
8
10
  children?: Snippet;
@@ -12,10 +12,14 @@
12
12
  placement?: EnhanceDropdownOptions['placement'];
13
13
  /** Open state; two-way bindable (`bind:open`). */
14
14
  open?: boolean;
15
+ /** Open the menu initially for uncontrolled usage. */
16
+ defaultOpen?: boolean;
15
17
  children?: Snippet;
16
18
  }
17
19
 
18
- let { placement, open = $bindable(false), children, ...rest }: Props = $props();
20
+ let { placement, defaultOpen = false, open = $bindable(), children, ...rest }: Props = $props();
21
+ // svelte-ignore state_referenced_locally -- seeds the uncontrolled initial value once
22
+ if (open === undefined) open = defaultOpen;
19
23
  let host = $state<HTMLDivElement>();
20
24
  let api = $state<DropdownApi | null>(null);
21
25
 
@@ -34,7 +38,7 @@
34
38
  });
35
39
 
36
40
  $effect(() => {
37
- api?.setOpen(open);
41
+ if (open != null) api?.setOpen(open);
38
42
  });
39
43
  </script>
40
44
 
@@ -5,6 +5,8 @@ interface Props extends HTMLAttributes<HTMLDivElement> {
5
5
  placement?: EnhanceDropdownOptions['placement'];
6
6
  /** Open state; two-way bindable (`bind:open`). */
7
7
  open?: boolean;
8
+ /** Open the menu initially for uncontrolled usage. */
9
+ defaultOpen?: boolean;
8
10
  children?: Snippet;
9
11
  }
10
12
  declare const Dropdown: import("svelte").Component<Props, {}, "open">;
@@ -11,8 +11,9 @@
11
11
  }
12
12
 
13
13
  let { id, invalid = false, required = false, class: klass, children, ...rest }: Props = $props();
14
- const generatedId = `hl-field-${Math.random().toString(36).slice(2, 8)}`;
15
- const baseId = $derived(id ?? generatedId);
14
+ // SSR-safe: $props.id() is stable across server and client renders.
15
+ const generatedId = $props.id();
16
+ const baseId = $derived(id ?? `hl-field-${generatedId}`);
16
17
 
17
18
  setFieldContext({
18
19
  get id() {
@@ -1,19 +1,43 @@
1
1
  <script lang="ts">
2
- import { enhanceMenu, type EnhanceMenuOptions } from '@hydrateless/enhancers';
2
+ import { enhanceMenu, type EnhanceMenuOptions, type MenuApi } from '@hydrateless/enhancers';
3
3
  import type { Snippet } from 'svelte';
4
4
  import type { HTMLAttributes } from 'svelte/elements';
5
5
 
6
- interface Props extends HTMLAttributes<HTMLUListElement> {
6
+ interface Props extends Omit<HTMLAttributes<HTMLUListElement>, 'onselect'> {
7
7
  orientation?: EnhanceMenuOptions['orientation'];
8
+ /** Open submenu value (or `null`); two-way bindable (`bind:open`). */
9
+ open?: string | null;
10
+ /** Fires with the item's value when a leaf menu item is activated. */
11
+ onSelect?: (value: string) => void;
8
12
  children?: Snippet;
9
13
  }
10
14
 
11
- let { orientation = 'horizontal', children, ...rest }: Props = $props();
15
+ let {
16
+ orientation = 'horizontal',
17
+ open = $bindable(),
18
+ onSelect,
19
+ children,
20
+ ...rest
21
+ }: Props = $props();
12
22
  let host = $state<HTMLUListElement>();
23
+ let api = $state<MenuApi | null>(null);
13
24
 
14
25
  $effect(() => {
15
26
  if (!host) return;
16
- return enhanceMenu(host, { orientation }).destroy;
27
+ const handle = enhanceMenu(host, {
28
+ orientation,
29
+ onOpenChange: (value) => (open = value),
30
+ onSelect: (value) => onSelect?.(value),
31
+ });
32
+ api = handle.api;
33
+ return () => {
34
+ handle.destroy();
35
+ api = null;
36
+ };
37
+ });
38
+
39
+ $effect(() => {
40
+ if (open !== undefined) api?.setOpen(open);
17
41
  });
18
42
  </script>
19
43
 
@@ -1,10 +1,14 @@
1
1
  import { type EnhanceMenuOptions } from '@hydrateless/enhancers';
2
2
  import type { Snippet } from 'svelte';
3
3
  import type { HTMLAttributes } from 'svelte/elements';
4
- interface Props extends HTMLAttributes<HTMLUListElement> {
4
+ interface Props extends Omit<HTMLAttributes<HTMLUListElement>, 'onselect'> {
5
5
  orientation?: EnhanceMenuOptions['orientation'];
6
+ /** Open submenu value (or `null`); two-way bindable (`bind:open`). */
7
+ open?: string | null;
8
+ /** Fires with the item's value when a leaf menu item is activated. */
9
+ onSelect?: (value: string) => void;
6
10
  children?: Snippet;
7
11
  }
8
- declare const Menu: import("svelte").Component<Props, {}, "">;
12
+ declare const Menu: import("svelte").Component<Props, {}, "open">;
9
13
  type Menu = ReturnType<typeof Menu>;
10
14
  export default Menu;
@@ -2,9 +2,11 @@
2
2
  import type { Snippet } from 'svelte';
3
3
  import type { HTMLAnchorAttributes, HTMLButtonAttributes } from 'svelte/elements';
4
4
 
5
- interface Props extends Omit<HTMLButtonAttributes, 'type'> {
5
+ interface Props extends Omit<HTMLButtonAttributes, 'type' | 'value'> {
6
6
  /** Render as a link instead of a button. */
7
7
  href?: string;
8
+ /** Stable value identifying this item; defaults to its label text. */
9
+ value?: string;
8
10
  /** Convenience handler fired on activation. */
9
11
  onSelect?: () => void;
10
12
  /** Nested `<MenuItem>`s; renders a single-level submenu. */
@@ -14,17 +16,18 @@
14
16
  children?: Snippet;
15
17
  }
16
18
 
17
- let { href, onSelect, onclick, submenu, anchorProps, children, ...rest }: Props = $props();
19
+ let { href, value, onSelect, onclick, submenu, anchorProps, children, ...rest }: Props = $props();
18
20
  </script>
19
21
 
20
22
  <li role="none">
21
23
  {#if href && !submenu}
22
- <a {...anchorProps} role="menuitem" {href}>{@render children?.()}</a>
24
+ <a {...anchorProps} role="menuitem" {href} data-hl-value={value}>{@render children?.()}</a>
23
25
  {:else}
24
26
  <button
25
27
  {...rest}
26
28
  type="button"
27
29
  role="menuitem"
30
+ data-hl-value={value}
28
31
  onclick={(e) => {
29
32
  onSelect?.();
30
33
  onclick?.(e);
@@ -1,8 +1,10 @@
1
1
  import type { Snippet } from 'svelte';
2
2
  import type { HTMLAnchorAttributes, HTMLButtonAttributes } from 'svelte/elements';
3
- interface Props extends Omit<HTMLButtonAttributes, 'type'> {
3
+ interface Props extends Omit<HTMLButtonAttributes, 'type' | 'value'> {
4
4
  /** Render as a link instead of a button. */
5
5
  href?: string;
6
+ /** Stable value identifying this item; defaults to its label text. */
7
+ value?: string;
6
8
  /** Convenience handler fired on activation. */
7
9
  onSelect?: () => void;
8
10
  /** Nested `<MenuItem>`s; renders a single-level submenu. */
@@ -7,17 +7,22 @@
7
7
  interface Props extends HTMLDialogAttributes {
8
8
  /** Open state; two-way bindable (`bind:open`). */
9
9
  open?: boolean;
10
+ /** Open the dialog initially for uncontrolled usage. */
11
+ defaultOpen?: boolean;
10
12
  closeOnBackdrop?: boolean;
11
13
  children?: Snippet;
12
14
  }
13
15
 
14
16
  let {
15
- open = $bindable(false),
17
+ defaultOpen = false,
18
+ open = $bindable(),
16
19
  closeOnBackdrop = true,
17
20
  class: klass,
18
21
  children,
19
22
  ...rest
20
23
  }: Props = $props();
24
+ // svelte-ignore state_referenced_locally -- seeds the uncontrolled initial value once
25
+ if (open === undefined) open = defaultOpen;
21
26
  let dialog = $state<HTMLDialogElement>();
22
27
  let api = $state<ModalApi | null>(null);
23
28
 
@@ -25,10 +30,10 @@
25
30
  if (!dialog) return;
26
31
  const handle = enhanceModal(dialog, {
27
32
  closeOnBackdrop,
33
+ defaultOpen: untrack(() => open),
28
34
  onOpenChange: (next) => (open = next),
29
35
  });
30
36
  api = handle.api;
31
- if (untrack(() => open)) handle.api?.setOpen(true);
32
37
  return () => {
33
38
  handle.destroy();
34
39
  api = null;
@@ -36,7 +41,7 @@
36
41
  });
37
42
 
38
43
  $effect(() => {
39
- api?.setOpen(open);
44
+ if (open != null) api?.setOpen(open);
40
45
  });
41
46
  </script>
42
47
 
@@ -3,6 +3,8 @@ import type { HTMLDialogAttributes } from 'svelte/elements';
3
3
  interface Props extends HTMLDialogAttributes {
4
4
  /** Open state; two-way bindable (`bind:open`). */
5
5
  open?: boolean;
6
+ /** Open the dialog initially for uncontrolled usage. */
7
+ defaultOpen?: boolean;
6
8
  closeOnBackdrop?: boolean;
7
9
  children?: Snippet;
8
10
  }
@@ -8,18 +8,24 @@
8
8
  /** Open state; two-way bindable (`bind:open`). The browser's light-dismiss
9
9
  * (Escape/outside click) closes it and updates the binding. */
10
10
  open?: boolean;
11
+ /** Show the popover initially for uncontrolled usage. */
12
+ defaultOpen?: boolean;
11
13
  children?: Snippet;
12
14
  }
13
15
 
14
- let { open = $bindable(false), children, ...rest }: Props = $props();
16
+ let { defaultOpen = false, open = $bindable(), children, ...rest }: Props = $props();
17
+ // svelte-ignore state_referenced_locally -- seeds the uncontrolled initial value once
18
+ if (open === undefined) open = defaultOpen;
15
19
  let host = $state<HTMLDivElement>();
16
20
  let api = $state<PopoverApi | null>(null);
17
21
 
18
22
  $effect(() => {
19
23
  if (!host) return;
20
- const handle = enhancePopover(host, { onOpenChange: (next) => (open = next) });
24
+ const handle = enhancePopover(host, {
25
+ defaultOpen: untrack(() => open),
26
+ onOpenChange: (next) => (open = next),
27
+ });
21
28
  api = handle.api;
22
- if (untrack(() => open)) handle.api?.setOpen(true);
23
29
  return () => {
24
30
  handle.destroy();
25
31
  api = null;
@@ -27,7 +33,7 @@
27
33
  });
28
34
 
29
35
  $effect(() => {
30
- api?.setOpen(open);
36
+ if (open != null) api?.setOpen(open);
31
37
  });
32
38
  </script>
33
39
 
@@ -4,6 +4,8 @@ interface Props extends HTMLAttributes<HTMLDivElement> {
4
4
  /** Open state; two-way bindable (`bind:open`). The browser's light-dismiss
5
5
  * (Escape/outside click) closes it and updates the binding. */
6
6
  open?: boolean;
7
+ /** Show the popover initially for uncontrolled usage. */
8
+ defaultOpen?: boolean;
7
9
  children?: Snippet;
8
10
  }
9
11
  declare const Popover: import("svelte").Component<Props, {}, "open">;
@@ -8,15 +8,28 @@
8
8
  }
9
9
 
10
10
  interface Props extends HTMLAttributes<HTMLDivElement> {
11
+ /** Selected value; two-way bindable (`bind:value`). */
11
12
  value?: string;
13
+ /** Initially selected value for uncontrolled usage. */
14
+ defaultValue?: string;
12
15
  options?: SegmentedOption[];
13
16
  name?: string;
14
17
  size?: 'sm' | 'md' | 'lg';
15
18
  }
16
19
 
17
- let { value = $bindable(), options = [], name, size, class: klass, ...rest }: Props = $props();
18
- const generatedName = `hl-seg-${Math.random().toString(36).slice(2, 8)}`;
19
- const groupName = $derived(name ?? generatedName);
20
+ let {
21
+ value = $bindable(),
22
+ defaultValue,
23
+ options = [],
24
+ name,
25
+ size,
26
+ class: klass,
27
+ ...rest
28
+ }: Props = $props();
29
+ // SSR-safe: $props.id() is stable across server and client renders.
30
+ const generatedName = $props.id();
31
+ const groupName = $derived(name ?? `hl-seg-${generatedName}`);
32
+ const current = $derived(value ?? defaultValue);
20
33
  </script>
21
34
 
22
35
  <div {...rest} role="radiogroup" class={['hl-segmented', klass]} data-hl-size={size}>
@@ -26,7 +39,7 @@
26
39
  type="radio"
27
40
  name={groupName}
28
41
  value={option.value}
29
- checked={value === option.value}
42
+ checked={current === option.value}
30
43
  disabled={option.disabled}
31
44
  onchange={() => (value = option.value)}
32
45
  />
@@ -5,7 +5,10 @@ interface SegmentedOption {
5
5
  disabled?: boolean;
6
6
  }
7
7
  interface Props extends HTMLAttributes<HTMLDivElement> {
8
+ /** Selected value; two-way bindable (`bind:value`). */
8
9
  value?: string;
10
+ /** Initially selected value for uncontrolled usage. */
11
+ defaultValue?: string;
9
12
  options?: SegmentedOption[];
10
13
  name?: string;
11
14
  size?: 'sm' | 'md' | 'lg';
@@ -13,8 +13,9 @@
13
13
  }
14
14
 
15
15
  let { label, tip, id, children }: Props = $props();
16
- const generatedId = `hl-tip-${Math.random().toString(36).slice(2, 8)}`;
17
- const tipId = $derived(id ?? generatedId);
16
+ // SSR-safe: $props.id() is stable across server and client renders.
17
+ const generatedId = $props.id();
18
+ const tipId = $derived(id ?? `hl-tip-${generatedId}`);
18
19
  let host = $state<HTMLSpanElement>();
19
20
 
20
21
  $effect(() => {
package/dist/index.d.ts CHANGED
@@ -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 { AccordionApi, ComboboxApi, Disposer, DrawerApi, DropdownApi, EnhancerHandle, ModalApi, PopoverApi, TabsApi, ToastApi, TooltipApi, } from '@hydrateless/enhancers';
75
+ export type { AccordionApi, CommandApi, ComboboxApi, DisclosureApi, Disposer, DrawerApi, DropdownApi, EnhancerHandle, MenuApi, ModalApi, PopoverApi, TabsApi, ToastApi, TocApi, TooltipApi, } from '@hydrateless/enhancers';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hydrateless/svelte",
3
- "version": "0.7.0",
3
+ "version": "0.8.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.7.0"
35
+ "@hydrateless/enhancers": "^0.8.0"
36
36
  },
37
37
  "peerDependencies": {
38
38
  "svelte": ">=5"