@diegopluna/narvi 0.4.1 → 0.5.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.
Files changed (177) hide show
  1. package/README.md +3 -1
  2. package/dist/base.css +3 -0
  3. package/dist/components/button/Button.svelte +29 -17
  4. package/dist/components/checkbox/Checkbox.svelte +106 -54
  5. package/dist/components/checkbox/Checkbox.svelte.d.ts +22 -8
  6. package/dist/components/choice-cards/ChoiceCards.svelte +68 -39
  7. package/dist/components/choice-cards/ChoiceCards.svelte.d.ts +12 -8
  8. package/dist/components/collapsible/CollapsibleContent.svelte +71 -17
  9. package/dist/components/collapsible/CollapsibleContent.svelte.d.ts +14 -10
  10. package/dist/components/collapsible/CollapsibleRoot.svelte +13 -9
  11. package/dist/components/collapsible/CollapsibleRoot.svelte.d.ts +8 -9
  12. package/dist/components/collapsible/CollapsibleTrigger.svelte +24 -15
  13. package/dist/components/collapsible/CollapsibleTrigger.svelte.d.ts +10 -14
  14. package/dist/components/color-field/ColorField.svelte +13 -7
  15. package/dist/components/command/CommandDialog.svelte +30 -20
  16. package/dist/components/command/CommandDialog.svelte.d.ts +4 -3
  17. package/dist/components/command/CommandEmpty.svelte +11 -9
  18. package/dist/components/command/CommandEmpty.svelte.d.ts +6 -17
  19. package/dist/components/command/CommandGroup.svelte +32 -11
  20. package/dist/components/command/CommandGroup.svelte.d.ts +7 -11
  21. package/dist/components/command/CommandInput.svelte +25 -10
  22. package/dist/components/command/CommandInput.svelte.d.ts +8 -12
  23. package/dist/components/command/CommandItem.svelte +67 -29
  24. package/dist/components/command/CommandItem.svelte.d.ts +6 -32
  25. package/dist/components/command/CommandList.svelte +8 -10
  26. package/dist/components/command/CommandList.svelte.d.ts +6 -8
  27. package/dist/components/command/CommandRoot.svelte +247 -57
  28. package/dist/components/command/CommandRoot.svelte.d.ts +14 -28
  29. package/dist/components/command/CommandSeparator.svelte +18 -0
  30. package/dist/components/command/CommandSeparator.svelte.d.ts +7 -0
  31. package/dist/components/command/context.d.ts +27 -0
  32. package/dist/components/command/context.js +16 -0
  33. package/dist/components/command/index.d.ts +6 -4
  34. package/dist/components/command/index.js +5 -4
  35. package/dist/components/command/score.d.ts +27 -0
  36. package/dist/components/command/score.js +77 -0
  37. package/dist/components/command/styles.d.ts +32 -9
  38. package/dist/components/command/styles.js +42 -14
  39. package/dist/components/composer/ComposerModelPicker.svelte +350 -346
  40. package/dist/components/composer/ComposerModelPicker.svelte.d.ts +6 -3
  41. package/dist/components/composer/ComposerRoot.svelte.d.ts +1 -1
  42. package/dist/components/dialog/ConfirmDialog.svelte +73 -53
  43. package/dist/components/dialog/DialogClose.svelte +17 -0
  44. package/dist/components/dialog/DialogClose.svelte.d.ts +10 -0
  45. package/dist/components/dialog/DialogContent.svelte +35 -33
  46. package/dist/components/dialog/DialogContent.svelte.d.ts +6 -20
  47. package/dist/components/dialog/DialogDescription.svelte +9 -12
  48. package/dist/components/dialog/DialogDescription.svelte.d.ts +6 -8
  49. package/dist/components/dialog/DialogRoot.svelte +20 -0
  50. package/dist/components/dialog/DialogRoot.svelte.d.ts +7 -0
  51. package/dist/components/dialog/DialogTitle.svelte +12 -8
  52. package/dist/components/dialog/DialogTitle.svelte.d.ts +8 -8
  53. package/dist/components/dialog/DialogTrigger.svelte +17 -0
  54. package/dist/components/dialog/DialogTrigger.svelte.d.ts +10 -0
  55. package/dist/components/dialog/index.d.ts +6 -5
  56. package/dist/components/dialog/index.js +3 -6
  57. package/dist/components/dialog/styles.d.ts +1 -2
  58. package/dist/components/dialog/styles.js +11 -8
  59. package/dist/components/hover-card/HoverCardContent.svelte +35 -29
  60. package/dist/components/hover-card/HoverCardContent.svelte.d.ts +8 -12
  61. package/dist/components/hover-card/HoverCardRoot.svelte +7 -7
  62. package/dist/components/hover-card/HoverCardRoot.svelte.d.ts +5 -4
  63. package/dist/components/hover-card/HoverCardTrigger.svelte +18 -0
  64. package/dist/components/hover-card/HoverCardTrigger.svelte.d.ts +10 -0
  65. package/dist/components/hover-card/index.d.ts +2 -3
  66. package/dist/components/hover-card/index.js +1 -5
  67. package/dist/components/input/Input.svelte.d.ts +1 -1
  68. package/dist/components/markdown/MarkdownBlock.svelte +6 -3
  69. package/dist/components/markdown/MarkdownTable.svelte +15 -7
  70. package/dist/components/menu/ContextMenuContent.svelte +15 -15
  71. package/dist/components/menu/ContextMenuContent.svelte.d.ts +6 -9
  72. package/dist/components/menu/ContextMenuTrigger.svelte +14 -16
  73. package/dist/components/menu/ContextMenuTrigger.svelte.d.ts +8 -14
  74. package/dist/components/menu/MenuCheckboxItem.svelte +22 -11
  75. package/dist/components/menu/MenuCheckboxItem.svelte.d.ts +8 -22
  76. package/dist/components/menu/MenuContent.svelte +16 -18
  77. package/dist/components/menu/MenuContent.svelte.d.ts +6 -18
  78. package/dist/components/menu/MenuGroup.svelte +4 -4
  79. package/dist/components/menu/MenuGroup.svelte.d.ts +5 -4
  80. package/dist/components/menu/MenuItem.svelte +25 -12
  81. package/dist/components/menu/MenuItem.svelte.d.ts +6 -21
  82. package/dist/components/menu/MenuLabel.svelte +7 -11
  83. package/dist/components/menu/MenuLabel.svelte.d.ts +6 -8
  84. package/dist/components/menu/MenuRadioGroup.svelte +4 -4
  85. package/dist/components/menu/MenuRadioGroup.svelte.d.ts +5 -4
  86. package/dist/components/menu/MenuRadioItem.svelte +16 -14
  87. package/dist/components/menu/MenuRadioItem.svelte.d.ts +6 -19
  88. package/dist/components/menu/MenuSeparator.svelte +6 -10
  89. package/dist/components/menu/MenuSeparator.svelte.d.ts +6 -7
  90. package/dist/components/menu/MenuSubContent.svelte +22 -19
  91. package/dist/components/menu/MenuSubContent.svelte.d.ts +8 -15
  92. package/dist/components/menu/MenuSubTrigger.svelte +7 -11
  93. package/dist/components/menu/MenuSubTrigger.svelte.d.ts +6 -15
  94. package/dist/components/menu/MenuTrigger.svelte +17 -0
  95. package/dist/components/menu/MenuTrigger.svelte.d.ts +10 -0
  96. package/dist/components/menu/context-menu.d.ts +2 -2
  97. package/dist/components/menu/context-menu.js +3 -2
  98. package/dist/components/menu/group.js +1 -1
  99. package/dist/components/menu/index.d.ts +5 -4
  100. package/dist/components/menu/index.js +5 -5
  101. package/dist/components/menu/row.d.ts +20 -0
  102. package/dist/components/menu/row.js +12 -0
  103. package/dist/components/popover/PopoverClose.svelte +17 -0
  104. package/dist/components/popover/PopoverClose.svelte.d.ts +10 -0
  105. package/dist/components/popover/PopoverContent.svelte +30 -29
  106. package/dist/components/popover/PopoverContent.svelte.d.ts +6 -17
  107. package/dist/components/popover/PopoverTrigger.svelte +17 -0
  108. package/dist/components/popover/PopoverTrigger.svelte.d.ts +10 -0
  109. package/dist/components/popover/index.d.ts +6 -4
  110. package/dist/components/popover/index.js +6 -5
  111. package/dist/components/progress/Progress.svelte +16 -22
  112. package/dist/components/progress/Progress.svelte.d.ts +6 -5
  113. package/dist/components/reasoning/ReasoningRoot.svelte.d.ts +1 -1
  114. package/dist/components/segmented/SegmentedControl.svelte +100 -91
  115. package/dist/components/segmented/SegmentedControl.svelte.d.ts +6 -4
  116. package/dist/components/select/Select.svelte +97 -59
  117. package/dist/components/select/Select.svelte.d.ts +3 -2
  118. package/dist/components/sheet/SheetContent.svelte +31 -38
  119. package/dist/components/sheet/SheetContent.svelte.d.ts +7 -22
  120. package/dist/components/sheet/index.d.ts +3 -5
  121. package/dist/components/sheet/index.js +3 -5
  122. package/dist/components/sheet/styles.js +26 -5
  123. package/dist/components/sidebar/SidebarItem.svelte +2 -3
  124. package/dist/components/slider/Slider.svelte +86 -65
  125. package/dist/components/slider/Slider.svelte.d.ts +9 -8
  126. package/dist/components/split-button/SplitButton.svelte +20 -3
  127. package/dist/components/split-button/SplitButton.svelte.d.ts +1 -1
  128. package/dist/components/switch/Switch.svelte +77 -49
  129. package/dist/components/switch/Switch.svelte.d.ts +15 -8
  130. package/dist/components/tab-strip/TabStrip.svelte +87 -68
  131. package/dist/components/tabs/Tabs.svelte +129 -99
  132. package/dist/components/tabs/Tabs.svelte.d.ts +6 -4
  133. package/dist/components/textarea/Textarea.svelte.d.ts +1 -1
  134. package/dist/components/toast/Toaster.svelte +132 -115
  135. package/dist/components/toast/Toaster.svelte.d.ts +5 -0
  136. package/dist/components/toast/toasts.svelte.d.ts +10 -23
  137. package/dist/components/toast/toasts.svelte.js +62 -40
  138. package/dist/components/toggle/Toggle.svelte +15 -9
  139. package/dist/components/toggle/Toggle.svelte.d.ts +8 -17
  140. package/dist/components/toggle-group/ToggleGroupItem.svelte +20 -9
  141. package/dist/components/toggle-group/ToggleGroupItem.svelte.d.ts +10 -10
  142. package/dist/components/toggle-group/ToggleGroupRoot.svelte +33 -18
  143. package/dist/components/toggle-group/ToggleGroupRoot.svelte.d.ts +4 -4
  144. package/dist/components/tool/ToolRoot.svelte.d.ts +1 -1
  145. package/dist/components/tooltip/TooltipContent.svelte +38 -28
  146. package/dist/components/tooltip/TooltipContent.svelte.d.ts +6 -15
  147. package/dist/components/tooltip/TooltipProvider.svelte +34 -10
  148. package/dist/components/tooltip/TooltipProvider.svelte.d.ts +18 -4
  149. package/dist/components/tooltip/TooltipRoot.svelte +52 -23
  150. package/dist/components/tooltip/TooltipRoot.svelte.d.ts +10 -10
  151. package/dist/components/tooltip/TooltipTrigger.svelte +14 -17
  152. package/dist/components/tooltip/TooltipTrigger.svelte.d.ts +10 -4
  153. package/dist/components/tooltip/index.d.ts +1 -1
  154. package/dist/components/tooltip/index.js +1 -1
  155. package/dist/components/work-fold/WorkFold.svelte +14 -5
  156. package/dist/index.d.ts +5 -5
  157. package/dist/index.js +1 -1
  158. package/dist/internal/TooltipScope.svelte +4 -4
  159. package/dist/internal/layer.d.ts +11 -9
  160. package/dist/internal/layer.js +21 -15
  161. package/dist/internal/loose.d.ts +30 -5
  162. package/dist/internal/loose.js +1 -1
  163. package/dist/internal/markers.stylex.d.ts +0 -4
  164. package/dist/internal/markers.stylex.js +0 -2
  165. package/dist/internal/toggle.d.ts +3 -0
  166. package/dist/internal/toggle.js +6 -2
  167. package/dist/internal/tooltip-trigger.d.ts +7 -6
  168. package/dist/internal/tooltip-trigger.js +9 -12
  169. package/dist/vite.d.ts +7 -1
  170. package/dist/vite.js +20 -5
  171. package/package.json +3 -2
  172. package/skills/building-ui/SKILL.md +20 -20
  173. package/skills/building-ui/references/components.md +11 -4
  174. package/skills/markdown/SKILL.md +1 -1
  175. package/skills/setup/SKILL.md +4 -8
  176. package/skills/styling/SKILL.md +1 -1
  177. package/skills/theming/SKILL.md +3 -7
package/README.md CHANGED
@@ -3,7 +3,9 @@
3
3
  A component library for Svelte 5, styled with [StyleX](https://stylexjs.com). Beside buttons and
4
4
  dialogs it has the parts an app for AI agents is made of: a window's shell, a conversation, a
5
5
  composer, and a timeline of what an agent did. Styles are compiled to atomic CSS at build time, so
6
- no styling code runs in the browser.
6
+ no styling code runs in the browser. Menus, dialogs, tooltips, selects and the other controls that
7
+ take keys and focus are built on [ui-sdk](https://ui-sdk.dev), which handles their behaviour and
8
+ accessibility.
7
9
 
8
10
  Docs, live examples and every component: **[narvi.dpeter.dev](https://narvi.dpeter.dev)**.
9
11
 
package/dist/base.css CHANGED
@@ -10,6 +10,9 @@
10
10
  * A colour followed by a comment naming a token is copied from that token when the library
11
11
  * is built (`scripts/base-css.js`), so the page and the components never drift apart.
12
12
  */
13
+ /* ui-sdk's runtime rules sit below Narvi's (see `UI_SDK_LAYER` in the Vite plugin) */
14
+ @layer ui-sdk, narvi.base;
15
+
13
16
  @layer narvi.base {
14
17
  *,
15
18
  *::before,
@@ -9,9 +9,11 @@
9
9
  import Spinner from '../spinner/Spinner.svelte';
10
10
  import * as Tooltip from '../tooltip/index.js';
11
11
  import TooltipScope from '../../internal/TooltipScope.svelte';
12
- import { mergeProps } from 'bits-ui';
13
12
  import '../../internal/icons.css';
14
13
 
14
+ // Last in its slot, not counting a tooltip, which ui-sdk renders in place after its trigger
15
+ const LAST = ':nth-last-child(1 of :not([data-slot="tooltip-content"]))';
16
+
15
17
  /**
16
18
  * `primary` is the one solid; `secondary` an edge; `soft` a fill of the accent. `ghost` has
17
19
  * a fill only under the pointer, `bare` never has one, and `link` is words that underline.
@@ -83,7 +85,7 @@
83
85
 
84
86
  // One key for every hover, so the catalog can show it without a pointer
85
87
  const HOVER = ':is(:hover, [data-preview~="hover"])';
86
- // The trigger of a menu, a popover or a select while what it opened is showing. bits-ui
88
+ // The trigger of a menu, a popover or a select while what it opened is showing. ui-sdk
87
89
  // marks it with `aria-expanded`, which a disclosure's trigger carries too: only a popup's
88
90
  // (`aria-haspopup`) is held in its open look.
89
91
  // A toggle button that is pressed holds the same look.
@@ -204,7 +206,7 @@
204
206
  paddingInline: 0,
205
207
  marginInlineEnd: {
206
208
  default: null,
207
- ':last-child': `calc(var(--narvi-icon-flush, 0) * (0.75rem - ${d('1.25rem')}) / 2)`
209
+ [LAST]: `calc(var(--narvi-icon-flush, 0) * (0.75rem - ${d('1.25rem')}) / 2)`
208
210
  }
209
211
  },
210
212
  iconXs: {
@@ -212,7 +214,7 @@
212
214
  paddingInline: 0,
213
215
  marginInlineEnd: {
214
216
  default: null,
215
- ':last-child': `calc(var(--narvi-icon-flush, 0) * (0.875rem - ${d('1.5rem')}) / 2)`
217
+ [LAST]: `calc(var(--narvi-icon-flush, 0) * (0.875rem - ${d('1.5rem')}) / 2)`
216
218
  }
217
219
  },
218
220
  iconSm: {
@@ -220,7 +222,7 @@
220
222
  paddingInline: 0,
221
223
  marginInlineEnd: {
222
224
  default: null,
223
- ':last-child': `calc(var(--narvi-icon-flush, 0) * (0.875rem - ${d('1.75rem')}) / 2)`
225
+ [LAST]: `calc(var(--narvi-icon-flush, 0) * (0.875rem - ${d('1.75rem')}) / 2)`
224
226
  }
225
227
  },
226
228
  iconMd: {
@@ -228,7 +230,7 @@
228
230
  paddingInline: 0,
229
231
  marginInlineEnd: {
230
232
  default: null,
231
- ':last-child': `calc(var(--narvi-icon-flush, 0) * (1rem - ${d('2rem')}) / 2)`
233
+ [LAST]: `calc(var(--narvi-icon-flush, 0) * (1rem - ${d('2rem')}) / 2)`
232
234
  }
233
235
  },
234
236
  iconLg: {
@@ -236,7 +238,7 @@
236
238
  paddingInline: 0,
237
239
  marginInlineEnd: {
238
240
  default: null,
239
- ':last-child': `calc(var(--narvi-icon-flush, 0) * (1.125rem - ${d('2.5rem')}) / 2)`
241
+ [LAST]: `calc(var(--narvi-icon-flush, 0) * (1.125rem - ${d('2.5rem')}) / 2)`
240
242
  }
241
243
  },
242
244
 
@@ -512,10 +514,14 @@
512
514
  class: className,
513
515
  ref = $bindable(null),
514
516
  children,
517
+ onclick,
515
518
  ...rest
516
519
  }: ButtonProps = $props();
517
520
 
518
- const inert = $derived(disabled || loading);
521
+ // ui-sdk's triggers mark theirs disabled with an empty attribute, which reaches a Button
522
+ // rendered as one (`child`) as `disabled=""`
523
+ const off = $derived(!!disabled || (disabled as unknown) === '');
524
+ const inert = $derived(off || loading);
519
525
 
520
526
  const rootAttrs = $derived(
521
527
  attrs(
@@ -527,7 +533,7 @@
527
533
  (shape === 'pill' || variant === 'floating') && styles.pill,
528
534
  justify === 'start' && styles.start,
529
535
  variantStyles[variant],
530
- disabled && styles.disabled,
536
+ off && styles.disabled,
531
537
  loading && styles.loading,
532
538
  sx
533
539
  ],
@@ -554,9 +560,13 @@
554
560
  {/if}
555
561
  {/snippet}
556
562
 
557
- <!-- The element, with a tooltip trigger's props when it has a tooltip -->
558
- {#snippet control(trigger: Record<string, unknown>)}
559
- {@const own = Object.keys(trigger).length ? mergeProps(rest, trigger) : rest}
563
+ <!--
564
+ The element, with its own props, or a tooltip trigger's: the Tooltip.Trigger takes the
565
+ button's props and hands them back merged with its own, which is ui-sdk's way to merge them.
566
+ Its `type="button"` gives way to the button's own type, and its click goes through here, so a
567
+ button that is loading ignores it.
568
+ -->
569
+ {#snippet control(own: Record<string, unknown>)}
560
570
  {#if href !== undefined}
561
571
  <a
562
572
  bind:this={ref}
@@ -566,7 +576,9 @@
566
576
  aria-disabled={inert ? true : undefined}
567
577
  aria-busy={loading ? true : undefined}
568
578
  {...own}
579
+ type={undefined}
569
580
  {...rootAttrs}
581
+ onclick={onclick as HTMLAnchorAttributes['onclick']}
570
582
  >
571
583
  {@render content()}
572
584
  </a>
@@ -574,19 +586,19 @@
574
586
  <button
575
587
  bind:this={ref}
576
588
  data-icons
577
- {type}
578
- disabled={disabled || undefined}
589
+ disabled={off || undefined}
579
590
  aria-pressed={pressed}
580
591
  aria-disabled={loading ? true : undefined}
581
592
  aria-busy={loading ? true : undefined}
582
593
  {...own}
594
+ {type}
583
595
  {...rootAttrs}
584
596
  onclick={loading
585
597
  ? (event: MouseEvent) => {
586
598
  event.preventDefault();
587
599
  event.stopPropagation();
588
600
  }
589
- : (own.onclick as HTMLButtonAttributes['onclick'])}
601
+ : (onclick as HTMLButtonAttributes['onclick'])}
590
602
  >
591
603
  {@render content()}
592
604
  </button>
@@ -596,7 +608,7 @@
596
608
  {#if tooltip}
597
609
  <TooltipScope>
598
610
  <Tooltip.Root>
599
- <Tooltip.Trigger>
611
+ <Tooltip.Trigger {...rest}>
600
612
  {#snippet child({ props })}{@render control(props)}{/snippet}
601
613
  </Tooltip.Trigger>
602
614
  <Tooltip.Content side={tooltipSide} shortcut={tooltipShortcut}>
@@ -605,5 +617,5 @@
605
617
  </Tooltip.Root>
606
618
  </TooltipScope>
607
619
  {:else}
608
- {@render control({})}
620
+ {@render control(rest)}
609
621
  {/if}
@@ -1,26 +1,58 @@
1
1
  <script lang="ts" module>
2
+ import type { ChangeDetails } from '@ui-sdk/core';
3
+ import type { CheckboxCheckedChangeReason } from '@ui-sdk/core/checkbox';
4
+ import { Checkbox as UiCheckbox } from '@ui-sdk/svelte/checkbox';
2
5
  import * as stylex from '@stylexjs/stylex';
3
6
  import type { Snippet } from 'svelte';
4
- import type { ChangeEventHandler, HTMLInputAttributes } from 'svelte/elements';
7
+ import type { ChangeEventHandler } from 'svelte/elements';
8
+ import type { Loose, PartProps } from '../../internal/loose.js';
5
9
  import { attrs, classes, type Sx } from '../../internal/sx.js';
6
10
  import { colors, font, motion, radius } from '../../tokens.stylex.js';
7
11
  import { media } from '../../media.stylex.js';
12
+ import { getFieldContext } from '../field/context.js';
8
13
 
9
- export type CheckboxProps = Omit<HTMLInputAttributes, 'type' | 'children'> & {
10
- checked?: boolean | undefined;
11
- /** Called with the new value when the person turns it on or off. */
12
- onCheckedChange?: ((checked: boolean) => void) | undefined;
13
- /** Shows a dash for a partially selected group. Cleared when the user toggles the box. */
14
- indeterminate?: boolean | undefined;
15
- disabled?: boolean | undefined;
16
- /** A second, quieter line under the label. */
17
- description?: string | undefined;
18
- /** Applied to the outermost element: the label when there is one, else the input. */
19
- sx?: Sx | undefined;
20
- ref?: HTMLInputElement | null | undefined;
21
- /** Label content. Without it, give the checkbox an `aria-label`. */
22
- children?: Snippet | undefined;
23
- };
14
+ export interface CheckboxProps extends Loose<
15
+ Omit<
16
+ PartProps<typeof UiCheckbox>,
17
+ | 'children'
18
+ | 'render'
19
+ | 'checked'
20
+ | 'defaultChecked'
21
+ | 'onCheckedChange'
22
+ | 'onchange'
23
+ | 'parent'
24
+ > & {
25
+ /** Whether it is ticked. Bindable. */
26
+ checked?: boolean | undefined;
27
+ /**
28
+ * Called with the new value when the person ticks or clears it, and the change's details
29
+ * after: `details.reason`, and `details.cancel()` to keep it as it was.
30
+ */
31
+ onCheckedChange?:
32
+ | ((checked: boolean, details: ChangeDetails<CheckboxCheckedChangeReason>) => void)
33
+ | undefined;
34
+ /**
35
+ * The native change event, from the form input underneath: `event.currentTarget.checked`
36
+ * is the new value. It follows `onCheckedChange`.
37
+ */
38
+ onchange?: ChangeEventHandler<HTMLInputElement> | undefined;
39
+ /** Shows a dash for a partially selected group. Cleared when the user toggles the box. */
40
+ indeterminate?: boolean | undefined;
41
+ /** A second, quieter line under the label. */
42
+ description?: string | undefined;
43
+ /** Applied to the outermost element: the label when there is one, else the checkbox. */
44
+ sx?: Sx | undefined;
45
+ /** The checkbox's button. Bindable. */
46
+ ref?: HTMLButtonElement | null | undefined;
47
+ /** Label content. Without it, give the checkbox an `aria-label`. */
48
+ children?: Snippet | undefined;
49
+ }
50
+ > {}
51
+
52
+ // The same look serves ui-sdk's checkbox, a button with state attributes, and a native
53
+ // checkbox, which Markdown's task lists write as a string (`checkboxClass`)
54
+ const ON = ':is(:checked, [data-checked])';
55
+ const MIXED = ':is(:indeterminate, [data-indeterminate])';
24
56
 
25
57
  const CHECK = `url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 12' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M2.5 6.5 5 9l4.5-6'/%3E%3C/svg%3E")`;
26
58
  const DASH = `url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 12' fill='none' stroke='black' stroke-width='2' stroke-linecap='round'%3E%3Cpath d='M2.5 6h7'/%3E%3C/svg%3E")`;
@@ -37,6 +69,8 @@
37
69
  cursor: 'pointer',
38
70
  WebkitTapHighlightColor: 'transparent'
39
71
  },
72
+ // A label with no break in it, such as a URL, wraps rather than running out of the box
73
+ text: { minWidth: 0, overflowWrap: 'anywhere' },
40
74
  rootDisabled: {
41
75
  color: colors.textSubtle,
42
76
  cursor: 'not-allowed'
@@ -56,6 +90,7 @@
56
90
  width: '1rem',
57
91
  height: '1rem',
58
92
  margin: 0,
93
+ padding: 0,
59
94
  // Centre the 16px box on the label's 20px first line
60
95
  marginBlock: '2px',
61
96
  borderWidth: '1px',
@@ -64,20 +99,20 @@
64
99
  borderColor: {
65
100
  default: colors.borderControl,
66
101
  ':hover': { default: null, [media.hover]: colors.borderControlHover },
67
- ':checked': 'transparent',
68
- ':indeterminate': 'transparent',
102
+ [ON]: 'transparent',
103
+ [MIXED]: 'transparent',
69
104
  [media.forcedColors]: 'ButtonText'
70
105
  },
71
106
  backgroundColor: { default: 'transparent', [media.forcedColors]: 'Canvas' },
72
107
  backgroundOrigin: 'border-box',
73
108
  backgroundImage: {
74
109
  default: 'none',
75
- ':checked': `linear-gradient(${colors.accent}, ${colors.accent})`,
76
- ':indeterminate': `linear-gradient(${colors.accent}, ${colors.accent})`,
110
+ [ON]: `linear-gradient(${colors.accent}, ${colors.accent})`,
111
+ [MIXED]: `linear-gradient(${colors.accent}, ${colors.accent})`,
77
112
  [media.forcedColors]: {
78
113
  default: 'none',
79
- ':checked': 'linear-gradient(Highlight, Highlight)',
80
- ':indeterminate': 'linear-gradient(Highlight, Highlight)'
114
+ [ON]: 'linear-gradient(Highlight, Highlight)',
115
+ [MIXED]: 'linear-gradient(Highlight, Highlight)'
81
116
  }
82
117
  },
83
118
  forcedColorAdjust: 'none',
@@ -96,24 +131,24 @@
96
131
  width: '0.625rem',
97
132
  height: '0.625rem',
98
133
  backgroundColor: { default: colors.onAccent, [media.forcedColors]: 'HighlightText' },
99
- maskImage: { default: CHECK, ':indeterminate': DASH },
134
+ maskImage: { default: CHECK, [MIXED]: DASH },
100
135
  maskSize: 'contain',
101
136
  maskRepeat: 'no-repeat',
102
137
  maskPosition: 'center',
103
- opacity: { default: 0, ':checked': 1, ':indeterminate': 1 },
138
+ opacity: { default: 0, [ON]: 1, [MIXED]: 1 },
104
139
  // The mark settles in from just under full size, with a touch of blur to
105
140
  // soften the swap. It never grows from nothing, and it is quick: boxes
106
141
  // get ticked many times a day.
107
142
  scale: {
108
143
  default: null,
109
- [media.motion]: { default: 0.9, ':checked': 1, ':indeterminate': 1 }
144
+ [media.motion]: { default: 0.9, [ON]: 1, [MIXED]: 1 }
110
145
  },
111
146
  filter: {
112
147
  default: null,
113
148
  [media.motion]: {
114
149
  default: 'blur(2px)',
115
- ':checked': 'blur(0px)',
116
- ':indeterminate': 'blur(0px)'
150
+ [ON]: 'blur(0px)',
151
+ [MIXED]: 'blur(0px)'
117
152
  }
118
153
  },
119
154
  transitionProperty: { default: 'opacity', [media.motion]: 'opacity, scale, filter' },
@@ -147,7 +182,11 @@
147
182
  onCheckedChange,
148
183
  onchange,
149
184
  indeterminate = $bindable(false),
150
- disabled = false,
185
+ disabled,
186
+ required,
187
+ id,
188
+ 'aria-describedby': describedBy,
189
+ 'aria-invalid': ariaInvalid,
151
190
  description,
152
191
  sx,
153
192
  class: className,
@@ -156,25 +195,47 @@
156
195
  ...rest
157
196
  }: CheckboxProps = $props();
158
197
 
159
- const changed: ChangeEventHandler<HTMLInputElement> = (event) => {
160
- onchange?.(event);
161
- onCheckedChange?.(event.currentTarget.checked);
162
- };
198
+ const field = getFieldContext();
199
+ const isDisabled = $derived(disabled ?? field?.disabled ?? false);
200
+
201
+ // ui-sdk's checkbox is a button, and the native change event fires on the form input it
202
+ // keeps beside it, so `onchange` is heard there, where `currentTarget.checked` is the new value.
203
+ $effect(() => {
204
+ const input = ref?.nextElementSibling;
205
+ if (!onchange || !(input instanceof HTMLInputElement)) return;
206
+ const listener = onchange as unknown as EventListener;
207
+ input.addEventListener('change', listener);
208
+ return () => input.removeEventListener('change', listener);
209
+ });
163
210
  </script>
164
211
 
212
+ {#snippet control(own: (Sx | false)[], className?: CheckboxProps['class'])}
213
+ <UiCheckbox
214
+ checked={indeterminate ? 'indeterminate' : checked}
215
+ onCheckedChange={(next, details) => {
216
+ onCheckedChange?.(next === true, details);
217
+ if (details.canceled) return;
218
+ checked = next === true;
219
+ indeterminate = next === 'indeterminate';
220
+ }}
221
+ id={id ?? field?.id}
222
+ disabled={isDisabled}
223
+ required={required ?? field?.required}
224
+ aria-invalid={ariaInvalid ?? (field?.invalid || undefined)}
225
+ aria-describedby={describedBy ?? field?.describedBy}
226
+ {...rest}
227
+ {...attrs([styles.input, ...own], className)}
228
+ {@attach (element: HTMLButtonElement) => {
229
+ ref = element;
230
+ return () => (ref = null);
231
+ }}
232
+ />
233
+ {/snippet}
234
+
165
235
  {#if children}
166
- <label {...attrs([styles.root, disabled && styles.rootDisabled, sx], className)}>
167
- <input
168
- bind:this={ref}
169
- bind:checked
170
- bind:indeterminate
171
- type="checkbox"
172
- {disabled}
173
- {...rest}
174
- onchange={changed}
175
- {...stylex.attrs(styles.input)}
176
- />
177
- <span>
236
+ <label {...attrs([styles.root, isDisabled && styles.rootDisabled, sx], className)}>
237
+ {@render control([])}
238
+ <span {...stylex.attrs(styles.text)}>
178
239
  {@render children()}
179
240
  {#if description}
180
241
  <span {...stylex.attrs(styles.description)}>{description}</span>
@@ -182,14 +243,5 @@
182
243
  </span>
183
244
  </label>
184
245
  {:else}
185
- <input
186
- bind:this={ref}
187
- bind:checked
188
- bind:indeterminate
189
- type="checkbox"
190
- {disabled}
191
- {...rest}
192
- onchange={changed}
193
- {...attrs([styles.input, styles.standalone, sx], className)}
194
- />
246
+ {@render control([styles.standalone, sx], className)}
195
247
  {/if}
@@ -1,21 +1,35 @@
1
+ import type { ChangeDetails } from '@ui-sdk/core';
2
+ import type { CheckboxCheckedChangeReason } from '@ui-sdk/core/checkbox';
3
+ import { Checkbox as UiCheckbox } from '@ui-sdk/svelte/checkbox';
1
4
  import type { Snippet } from 'svelte';
2
- import type { HTMLInputAttributes } from 'svelte/elements';
5
+ import type { ChangeEventHandler } from 'svelte/elements';
6
+ import type { Loose, PartProps } from '../../internal/loose.js';
3
7
  import { type Sx } from '../../internal/sx.js';
4
- export type CheckboxProps = Omit<HTMLInputAttributes, 'type' | 'children'> & {
8
+ export interface CheckboxProps extends Loose<Omit<PartProps<typeof UiCheckbox>, 'children' | 'render' | 'checked' | 'defaultChecked' | 'onCheckedChange' | 'onchange' | 'parent'> & {
9
+ /** Whether it is ticked. Bindable. */
5
10
  checked?: boolean | undefined;
6
- /** Called with the new value when the person turns it on or off. */
7
- onCheckedChange?: ((checked: boolean) => void) | undefined;
11
+ /**
12
+ * Called with the new value when the person ticks or clears it, and the change's details
13
+ * after: `details.reason`, and `details.cancel()` to keep it as it was.
14
+ */
15
+ onCheckedChange?: ((checked: boolean, details: ChangeDetails<CheckboxCheckedChangeReason>) => void) | undefined;
16
+ /**
17
+ * The native change event, from the form input underneath: `event.currentTarget.checked`
18
+ * is the new value. It follows `onCheckedChange`.
19
+ */
20
+ onchange?: ChangeEventHandler<HTMLInputElement> | undefined;
8
21
  /** Shows a dash for a partially selected group. Cleared when the user toggles the box. */
9
22
  indeterminate?: boolean | undefined;
10
- disabled?: boolean | undefined;
11
23
  /** A second, quieter line under the label. */
12
24
  description?: string | undefined;
13
- /** Applied to the outermost element: the label when there is one, else the input. */
25
+ /** Applied to the outermost element: the label when there is one, else the checkbox. */
14
26
  sx?: Sx | undefined;
15
- ref?: HTMLInputElement | null | undefined;
27
+ /** The checkbox's button. Bindable. */
28
+ ref?: HTMLButtonElement | null | undefined;
16
29
  /** Label content. Without it, give the checkbox an `aria-label`. */
17
30
  children?: Snippet | undefined;
18
- };
31
+ }> {
32
+ }
19
33
  /**
20
34
  * The class of a checkbox that stands alone, as the component gives it one: for the parts
21
35
  * of Markdown that are written as a string.
@@ -1,18 +1,21 @@
1
1
  <!--
2
2
  One choice among a few, each a card with a title and a line of detail: a plan, a way to sign
3
- in, where a thread runs. Native radios underneath, so it is one tab stop, the arrow keys move
4
- the choice, and it submits with a form. For short labels alone, use a SegmentedControl.
3
+ in, where a thread runs. Built on ui-sdk's radio group: each card is a radio, the group is one
4
+ tab stop, the arrow keys move the choice, and it submits with a form. For short labels alone,
5
+ use a SegmentedControl.
5
6
  -->
6
7
  <script lang="ts" module>
8
+ import type { ChangeDetails } from '@ui-sdk/core';
9
+ import type { RadioGroupValueChangeReason } from '@ui-sdk/core/radio';
10
+ import { Radio, RadioGroup } from '@ui-sdk/svelte/radio';
7
11
  import type { IconComponent } from '../../internal/icon.js';
8
12
  import * as stylex from '@stylexjs/stylex';
9
- import type { HTMLAttributes } from 'svelte/elements';
10
13
  import '../../internal/icons.css';
14
+ import type { Loose, PartProps } from '../../internal/loose.js';
11
15
  import { attrs, type Sx } from '../../internal/sx.js';
12
16
  import { colors, density, motion, radius, space } from '../../tokens.stylex.js';
13
17
  import { media } from '../../media.stylex.js';
14
18
  import { typography } from '../../typography.js';
15
- import { visuallyHidden } from '../../visually-hidden.js';
16
19
 
17
20
  export type ChoiceCardOption = {
18
21
  value: string;
@@ -24,23 +27,31 @@
24
27
  disabled?: boolean | undefined;
25
28
  };
26
29
 
27
- export type ChoiceCardsProps = Omit<HTMLAttributes<HTMLDivElement>, 'children'> & {
28
- options: ChoiceCardOption[];
29
- /** The chosen option's value. Bindable. */
30
- value?: string | undefined;
31
- /** Called with the new value when the person chooses. */
32
- onValueChange?: ((value: string) => void) | undefined;
33
- /** Form field name. Generated when omitted. */
34
- name?: string | undefined;
35
- /** How many cards to a row. Without it, as many as fit at 12rem or more each. */
36
- columns?: 1 | 2 | 3 | 4 | undefined;
37
- disabled?: boolean | undefined;
38
- sx?: Sx | undefined;
39
- };
30
+ export interface ChoiceCardsProps extends Loose<
31
+ Omit<
32
+ PartProps<typeof RadioGroup>,
33
+ 'children' | 'render' | 'value' | 'defaultValue' | 'onValueChange'
34
+ > & {
35
+ options: ChoiceCardOption[];
36
+ /** The chosen option's value. Bindable. */
37
+ value?: string | undefined;
38
+ /**
39
+ * Called with the new value when the person chooses, and the change's details after:
40
+ * `details.reason`, and `details.cancel()` to keep the choice as it was.
41
+ */
42
+ onValueChange?:
43
+ ((value: string, details: ChangeDetails<RadioGroupValueChangeReason>) => void) | undefined;
44
+ /** How many cards to a row. Without it, as many as fit at 12rem or more each. */
45
+ columns?: 1 | 2 | 3 | 4 | undefined;
46
+ sx?: Sx | undefined;
47
+ }
48
+ > {}
40
49
 
41
- const CHECKED = ':has(:checked)';
42
- const FOCUSED = ':has(:focus-visible)';
43
- const OFF = ':has(:disabled)';
50
+ // Each card is ui-sdk's radio, a button with state attributes
51
+ const CHECKED = ':is([data-checked])';
52
+ const FOCUSED = ':focus-visible';
53
+ // One option disabled is a disabled button; the whole group disabled marks each `data-disabled`
54
+ const OFF = ':is(:disabled, [data-disabled])';
44
55
 
45
56
  /** A length at the window's density (`density` in the tokens). */
46
57
  const d = (rem: string) => `calc(${rem} * ${density.factor})`;
@@ -58,9 +69,20 @@
58
69
  alignItems: 'flex-start',
59
70
  gap: space.x3,
60
71
  minWidth: 0,
72
+ margin: 0,
61
73
  paddingBlock: space.x3,
62
74
  paddingInline: d('0.875rem'),
75
+ borderWidth: 0,
63
76
  borderRadius: radius.panel,
77
+ appearance: 'none',
78
+ // A button's own type and alignment give way to the text's
79
+ color: 'inherit',
80
+ fontFamily: 'inherit',
81
+ fontSize: 'inherit',
82
+ fontWeight: 'inherit',
83
+ lineHeight: 'inherit',
84
+ letterSpacing: 'inherit',
85
+ textAlign: 'start',
64
86
  backgroundColor: colors.bgRaised,
65
87
  // The edge says which is chosen: the accent, twice as thick
66
88
  boxShadow: {
@@ -84,7 +106,14 @@
84
106
  WebkitTapHighlightColor: 'transparent'
85
107
  },
86
108
  icon: { display: 'grid', placeItems: 'center', height: '1.25rem', color: colors.textMuted },
87
- text: { display: 'flex', flexDirection: 'column', gap: '0.125rem', minWidth: 0 },
109
+ // A word too long for the card, such as a URL, wraps rather than running out of it
110
+ text: {
111
+ display: 'flex',
112
+ flexDirection: 'column',
113
+ gap: '0.125rem',
114
+ minWidth: 0,
115
+ overflowWrap: 'break-word'
116
+ },
88
117
  label: { color: colors.text },
89
118
  detail: { color: colors.textMuted }
90
119
  });
@@ -102,36 +131,36 @@
102
131
  options,
103
132
  value = $bindable(),
104
133
  onValueChange,
105
- name,
106
134
  columns,
107
- disabled = false,
108
135
  sx,
109
136
  class: className,
110
137
  ...rest
111
138
  }: ChoiceCardsProps = $props();
112
139
 
113
140
  const uid = $props.id();
141
+
142
+ function change(next: string | null, details: ChangeDetails<RadioGroupValueChangeReason>) {
143
+ // Nothing is chosen only after a form reset with no default, which has nothing to report
144
+ if (next !== null) onValueChange?.(next, details);
145
+ if (!details.canceled) value = next ?? undefined;
146
+ }
114
147
  </script>
115
148
 
116
- <div
117
- role="radiogroup"
118
- data-slot="choice-cards"
149
+ <RadioGroup
150
+ value={value ?? null}
151
+ onValueChange={change}
119
152
  {...rest}
120
153
  {...attrs([styles.root, columns && columnStyles[columns], sx], className)}
121
154
  >
122
155
  {#each options as option, at (option.value)}
123
156
  {@const detailId = option.detail ? `${uid}-detail-${at}` : undefined}
124
- <label data-icons {...stylex.attrs(styles.card)}>
125
- <input
126
- type="radio"
127
- name={name ?? uid}
128
- value={option.value}
129
- bind:group={value}
130
- disabled={disabled || option.disabled}
131
- aria-describedby={detailId}
132
- onchange={() => onValueChange?.(option.value)}
133
- {...stylex.attrs(visuallyHidden)}
134
- />
157
+ <Radio
158
+ value={option.value}
159
+ disabled={option.disabled}
160
+ aria-describedby={detailId}
161
+ data-icons
162
+ {...stylex.attrs(styles.card)}
163
+ >
135
164
  {#if option.icon}
136
165
  <span {...stylex.attrs(styles.icon)}><option.icon aria-hidden="true" /></span>
137
166
  {/if}
@@ -143,6 +172,6 @@
143
172
  </span>
144
173
  {/if}
145
174
  </span>
146
- </label>
175
+ </Radio>
147
176
  {/each}
148
- </div>
177
+ </RadioGroup>