@gemologic/sheen 0.1.0-rc.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 (271) hide show
  1. package/LICENSE +19 -0
  2. package/README.md +32 -0
  3. package/dist/core.d.ts +52 -0
  4. package/dist/core.js +29 -0
  5. package/dist/formisch.d.ts +5 -0
  6. package/dist/formisch.js +22 -0
  7. package/dist/forms.d.ts +34 -0
  8. package/dist/forms.js +18 -0
  9. package/dist/index.d.ts +144 -0
  10. package/dist/index.js +75 -0
  11. package/dist/layout/Layout.d.ts +28 -0
  12. package/dist/layout/Layout.js +121 -0
  13. package/dist/metadata.d.ts +46 -0
  14. package/dist/metadata.js +47 -0
  15. package/dist/navigation.d.ts +30 -0
  16. package/dist/navigation.js +16 -0
  17. package/dist/overlays.d.ts +28 -0
  18. package/dist/overlays.js +15 -0
  19. package/dist/primitives/Accordion.d.ts +16 -0
  20. package/dist/primitives/Accordion.js +223 -0
  21. package/dist/primitives/ActivityTimeline.d.ts +20 -0
  22. package/dist/primitives/ActivityTimeline.js +104 -0
  23. package/dist/primitives/AlertDialog.d.ts +7 -0
  24. package/dist/primitives/AlertDialog.js +8 -0
  25. package/dist/primitives/Avatar.d.ts +22 -0
  26. package/dist/primitives/Avatar.js +115 -0
  27. package/dist/primitives/Breadcrumb.d.ts +11 -0
  28. package/dist/primitives/Breadcrumb.js +66 -0
  29. package/dist/primitives/Button.d.ts +11 -0
  30. package/dist/primitives/Button.js +59 -0
  31. package/dist/primitives/ButtonGroup.d.ts +7 -0
  32. package/dist/primitives/ButtonGroup.js +33 -0
  33. package/dist/primitives/Checkbox.d.ts +22 -0
  34. package/dist/primitives/Checkbox.js +93 -0
  35. package/dist/primitives/CheckboxGroup.d.ts +18 -0
  36. package/dist/primitives/CheckboxGroup.js +122 -0
  37. package/dist/primitives/Collapsible.d.ts +9 -0
  38. package/dist/primitives/Collapsible.js +72 -0
  39. package/dist/primitives/Combobox.d.ts +41 -0
  40. package/dist/primitives/Combobox.js +684 -0
  41. package/dist/primitives/CommandPalette.d.ts +42 -0
  42. package/dist/primitives/CommandPalette.js +319 -0
  43. package/dist/primitives/ConfirmDialog.d.ts +11 -0
  44. package/dist/primitives/ConfirmDialog.js +42 -0
  45. package/dist/primitives/ContextMenu.d.ts +16 -0
  46. package/dist/primitives/ContextMenu.js +132 -0
  47. package/dist/primitives/DescriptionList.d.ts +11 -0
  48. package/dist/primitives/DescriptionList.js +44 -0
  49. package/dist/primitives/Dialog.d.ts +21 -0
  50. package/dist/primitives/Dialog.js +141 -0
  51. package/dist/primitives/Drawer.d.ts +9 -0
  52. package/dist/primitives/Drawer.js +21 -0
  53. package/dist/primitives/DropdownMenu.d.ts +59 -0
  54. package/dist/primitives/DropdownMenu.js +441 -0
  55. package/dist/primitives/EditableTextField.d.ts +11 -0
  56. package/dist/primitives/EditableTextField.js +126 -0
  57. package/dist/primitives/EmptyState.d.ts +7 -0
  58. package/dist/primitives/EmptyState.js +45 -0
  59. package/dist/primitives/Field.d.ts +18 -0
  60. package/dist/primitives/Field.js +72 -0
  61. package/dist/primitives/FileDropzone.d.ts +40 -0
  62. package/dist/primitives/FileDropzone.js +261 -0
  63. package/dist/primitives/IconButton.d.ts +8 -0
  64. package/dist/primitives/IconButton.js +34 -0
  65. package/dist/primitives/Input.d.ts +7 -0
  66. package/dist/primitives/Input.js +55 -0
  67. package/dist/primitives/InputGroup.d.ts +7 -0
  68. package/dist/primitives/InputGroup.js +73 -0
  69. package/dist/primitives/Link.d.ts +7 -0
  70. package/dist/primitives/Link.js +35 -0
  71. package/dist/primitives/LinkTooltip.d.ts +11 -0
  72. package/dist/primitives/LinkTooltip.js +88 -0
  73. package/dist/primitives/NavList.d.ts +17 -0
  74. package/dist/primitives/NavList.js +143 -0
  75. package/dist/primitives/Notice.d.ts +8 -0
  76. package/dist/primitives/Notice.js +64 -0
  77. package/dist/primitives/NumberField.d.ts +22 -0
  78. package/dist/primitives/NumberField.js +177 -0
  79. package/dist/primitives/Pagination.d.ts +12 -0
  80. package/dist/primitives/Pagination.js +127 -0
  81. package/dist/primitives/Popover.d.ts +12 -0
  82. package/dist/primitives/Popover.js +103 -0
  83. package/dist/primitives/Progress.d.ts +17 -0
  84. package/dist/primitives/Progress.js +98 -0
  85. package/dist/primitives/RadioGroup.d.ts +19 -0
  86. package/dist/primitives/RadioGroup.js +173 -0
  87. package/dist/primitives/Resizable.d.ts +36 -0
  88. package/dist/primitives/Resizable.js +196 -0
  89. package/dist/primitives/ScrollArea.d.ts +6 -0
  90. package/dist/primitives/ScrollArea.js +178 -0
  91. package/dist/primitives/SearchInput.d.ts +12 -0
  92. package/dist/primitives/SearchInput.js +89 -0
  93. package/dist/primitives/SegmentedControl.d.ts +25 -0
  94. package/dist/primitives/SegmentedControl.js +188 -0
  95. package/dist/primitives/Select.d.ts +24 -0
  96. package/dist/primitives/Select.js +320 -0
  97. package/dist/primitives/Separator.d.ts +6 -0
  98. package/dist/primitives/Separator.js +31 -0
  99. package/dist/primitives/ShortcutPending.d.ts +6 -0
  100. package/dist/primitives/ShortcutPending.js +40 -0
  101. package/dist/primitives/ShortcutProvider.d.ts +32 -0
  102. package/dist/primitives/ShortcutProvider.js +172 -0
  103. package/dist/primitives/ShortcutScope.d.ts +6 -0
  104. package/dist/primitives/ShortcutScope.js +29 -0
  105. package/dist/primitives/ShortcutSheet.d.ts +5 -0
  106. package/dist/primitives/ShortcutSheet.js +87 -0
  107. package/dist/primitives/Skeleton.d.ts +5 -0
  108. package/dist/primitives/Skeleton.js +23 -0
  109. package/dist/primitives/Slider.d.ts +35 -0
  110. package/dist/primitives/Slider.js +210 -0
  111. package/dist/primitives/Spinner.d.ts +7 -0
  112. package/dist/primitives/Spinner.js +44 -0
  113. package/dist/primitives/Status.d.ts +15 -0
  114. package/dist/primitives/Status.js +78 -0
  115. package/dist/primitives/Stepper.d.ts +32 -0
  116. package/dist/primitives/Stepper.js +147 -0
  117. package/dist/primitives/Surface.d.ts +11 -0
  118. package/dist/primitives/Surface.js +58 -0
  119. package/dist/primitives/Switch.d.ts +4 -0
  120. package/dist/primitives/Switch.js +7 -0
  121. package/dist/primitives/Table.d.ts +23 -0
  122. package/dist/primitives/Table.js +101 -0
  123. package/dist/primitives/Tabs.d.ts +17 -0
  124. package/dist/primitives/Tabs.js +157 -0
  125. package/dist/primitives/TagInput.d.ts +33 -0
  126. package/dist/primitives/TagInput.js +288 -0
  127. package/dist/primitives/Textarea.d.ts +8 -0
  128. package/dist/primitives/Textarea.js +106 -0
  129. package/dist/primitives/Toast.d.ts +10 -0
  130. package/dist/primitives/Toast.js +78 -0
  131. package/dist/primitives/Toaster.d.ts +9 -0
  132. package/dist/primitives/Toaster.js +183 -0
  133. package/dist/primitives/Tooltip.d.ts +13 -0
  134. package/dist/primitives/Tooltip.js +84 -0
  135. package/dist/primitives/Tree.d.ts +23 -0
  136. package/dist/primitives/Tree.js +275 -0
  137. package/dist/primitives/Typography.d.ts +16 -0
  138. package/dist/primitives/Typography.js +82 -0
  139. package/dist/primitives/boolean-input-state.d.ts +14 -0
  140. package/dist/primitives/boolean-input-state.js +39 -0
  141. package/dist/primitives/button-variants.d.ts +9 -0
  142. package/dist/primitives/button-variants.js +7 -0
  143. package/dist/primitives/choice-options.d.ts +7 -0
  144. package/dist/primitives/choice-options.js +35 -0
  145. package/dist/primitives/create-confirm.d.ts +15 -0
  146. package/dist/primitives/create-confirm.js +49 -0
  147. package/dist/primitives/create-toaster.d.ts +51 -0
  148. package/dist/primitives/create-toaster.js +87 -0
  149. package/dist/primitives/link-focus.d.ts +2 -0
  150. package/dist/primitives/link-focus.js +38 -0
  151. package/dist/primitives/menu-focus.d.ts +3 -0
  152. package/dist/primitives/menu-focus.js +32 -0
  153. package/dist/primitives/tooltip-layer.d.ts +14 -0
  154. package/dist/primitives/tooltip-layer.js +84 -0
  155. package/dist/theme/ThemeProvider.d.ts +59 -0
  156. package/dist/theme/ThemeProvider.js +239 -0
  157. package/dist/theme/intl.d.ts +6 -0
  158. package/dist/theme/intl.js +13 -0
  159. package/dist/theme/keyboard-script.d.ts +2 -0
  160. package/dist/theme/keyboard-script.js +6 -0
  161. package/dist/theme/layers.d.ts +7 -0
  162. package/dist/theme/layers.js +23 -0
  163. package/dist/theme/messages.d.ts +235 -0
  164. package/dist/theme/messages.js +115 -0
  165. package/dist/theme/script.d.ts +9 -0
  166. package/dist/theme/script.js +49 -0
  167. package/dist/theme/state.d.ts +27 -0
  168. package/dist/theme/state.js +55 -0
  169. package/dist/utils/browser-status.d.ts +5 -0
  170. package/dist/utils/browser-status.js +22 -0
  171. package/dist/utils/cn.d.ts +3 -0
  172. package/dist/utils/cn.js +7 -0
  173. package/dist/utils/optimistic.d.ts +6 -0
  174. package/dist/utils/optimistic.js +16 -0
  175. package/dist/utils/polymorphic.d.ts +1 -0
  176. package/dist/utils/shortcut-platform.d.ts +2 -0
  177. package/dist/utils/shortcut-platform.js +10 -0
  178. package/dist/utils/shortcut-registry.d.ts +61 -0
  179. package/dist/utils/shortcut-registry.js +186 -0
  180. package/package.json +100 -0
  181. package/src/core.ts +55 -0
  182. package/src/formisch.ts +28 -0
  183. package/src/forms.ts +34 -0
  184. package/src/index.ts +144 -0
  185. package/src/layout/Layout.tsx +43 -0
  186. package/src/metadata.d.ts +46 -0
  187. package/src/metadata.ts +66 -0
  188. package/src/navigation.ts +30 -0
  189. package/src/overlays.ts +28 -0
  190. package/src/primitives/Accordion.tsx +145 -0
  191. package/src/primitives/ActivityTimeline.tsx +72 -0
  192. package/src/primitives/AlertDialog.tsx +8 -0
  193. package/src/primitives/Avatar.tsx +85 -0
  194. package/src/primitives/Breadcrumb.tsx +41 -0
  195. package/src/primitives/Button.tsx +27 -0
  196. package/src/primitives/ButtonGroup.tsx +18 -0
  197. package/src/primitives/Checkbox.tsx +51 -0
  198. package/src/primitives/CheckboxGroup.tsx +73 -0
  199. package/src/primitives/Collapsible.tsx +43 -0
  200. package/src/primitives/Combobox.tsx +402 -0
  201. package/src/primitives/CommandPalette.tsx +234 -0
  202. package/src/primitives/ConfirmDialog.tsx +25 -0
  203. package/src/primitives/ContextMenu.tsx +100 -0
  204. package/src/primitives/DescriptionList.tsx +26 -0
  205. package/src/primitives/Dialog.tsx +81 -0
  206. package/src/primitives/Drawer.tsx +26 -0
  207. package/src/primitives/DropdownMenu.tsx +221 -0
  208. package/src/primitives/EditableTextField.tsx +91 -0
  209. package/src/primitives/EmptyState.tsx +21 -0
  210. package/src/primitives/Field.tsx +38 -0
  211. package/src/primitives/FileDropzone.tsx +189 -0
  212. package/src/primitives/IconButton.tsx +22 -0
  213. package/src/primitives/Input.tsx +17 -0
  214. package/src/primitives/InputGroup.tsx +23 -0
  215. package/src/primitives/Link.tsx +19 -0
  216. package/src/primitives/LinkTooltip.tsx +31 -0
  217. package/src/primitives/NavList.tsx +58 -0
  218. package/src/primitives/Notice.tsx +25 -0
  219. package/src/primitives/NumberField.tsx +168 -0
  220. package/src/primitives/Pagination.tsx +64 -0
  221. package/src/primitives/Popover.tsx +44 -0
  222. package/src/primitives/Progress.tsx +62 -0
  223. package/src/primitives/RadioGroup.tsx +104 -0
  224. package/src/primitives/Resizable.tsx +170 -0
  225. package/src/primitives/ScrollArea.tsx +115 -0
  226. package/src/primitives/SearchInput.tsx +98 -0
  227. package/src/primitives/SegmentedControl.tsx +140 -0
  228. package/src/primitives/Select.tsx +171 -0
  229. package/src/primitives/Separator.tsx +13 -0
  230. package/src/primitives/ShortcutPending.tsx +21 -0
  231. package/src/primitives/ShortcutProvider.tsx +179 -0
  232. package/src/primitives/ShortcutScope.tsx +17 -0
  233. package/src/primitives/ShortcutSheet.tsx +35 -0
  234. package/src/primitives/Skeleton.tsx +12 -0
  235. package/src/primitives/Slider.tsx +153 -0
  236. package/src/primitives/Spinner.tsx +19 -0
  237. package/src/primitives/Status.tsx +33 -0
  238. package/src/primitives/Stepper.tsx +106 -0
  239. package/src/primitives/Surface.tsx +21 -0
  240. package/src/primitives/Switch.tsx +9 -0
  241. package/src/primitives/Table.tsx +57 -0
  242. package/src/primitives/Tabs.tsx +96 -0
  243. package/src/primitives/TagInput.tsx +282 -0
  244. package/src/primitives/Textarea.tsx +77 -0
  245. package/src/primitives/Toast.tsx +36 -0
  246. package/src/primitives/Toaster.tsx +175 -0
  247. package/src/primitives/Tooltip.tsx +41 -0
  248. package/src/primitives/Tree.tsx +256 -0
  249. package/src/primitives/Typography.tsx +39 -0
  250. package/src/primitives/boolean-input-state.ts +65 -0
  251. package/src/primitives/button-variants.ts +20 -0
  252. package/src/primitives/choice-options.ts +39 -0
  253. package/src/primitives/create-confirm.tsx +56 -0
  254. package/src/primitives/create-toaster.ts +128 -0
  255. package/src/primitives/link-focus.ts +38 -0
  256. package/src/primitives/menu-focus.ts +43 -0
  257. package/src/primitives/tooltip-layer.tsx +53 -0
  258. package/src/styles.css +655 -0
  259. package/src/theme/ThemeProvider.tsx +215 -0
  260. package/src/theme/intl.ts +14 -0
  261. package/src/theme/keyboard-script.ts +4 -0
  262. package/src/theme/layers.ts +16 -0
  263. package/src/theme/messages.ts +157 -0
  264. package/src/theme/script.ts +70 -0
  265. package/src/theme/state.ts +48 -0
  266. package/src/utils/browser-status.ts +36 -0
  267. package/src/utils/cn.ts +5 -0
  268. package/src/utils/optimistic.ts +22 -0
  269. package/src/utils/polymorphic.ts +1 -0
  270. package/src/utils/shortcut-platform.ts +4 -0
  271. package/src/utils/shortcut-registry.ts +216 -0
@@ -0,0 +1,73 @@
1
+ import { For, Show, createSignal, createUniqueId, onCleanup, onMount, splitProps, untrack } from "solid-js";
2
+ import type { JSX } from "solid-js";
3
+ import { Checkbox } from "./Checkbox.tsx";
4
+ import { cn } from "../utils/cn.ts";
5
+ import { createChoiceOptions } from "./choice-options.ts";
6
+
7
+ export interface CheckboxOption {
8
+ readonly value: string;
9
+ readonly label: string;
10
+ readonly description?: string;
11
+ readonly disabled?: boolean;
12
+ }
13
+
14
+ export interface CheckboxGroupProps extends Omit<JSX.FieldsetHTMLAttributes<HTMLFieldSetElement>, "children" | "onChange"> {
15
+ label: string;
16
+ options: readonly CheckboxOption[];
17
+ value?: readonly string[];
18
+ defaultValue?: readonly string[];
19
+ onValueChange?: (value: string[]) => void;
20
+ description?: string;
21
+ error?: string;
22
+ readOnly?: boolean;
23
+ }
24
+
25
+ export function CheckboxGroup(props: CheckboxGroupProps): JSX.Element {
26
+ const id = createUniqueId();
27
+ const [local, others] = splitProps(props, ["label", "options", "value", "defaultValue", "onValueChange", "description", "error", "readOnly", "disabled", "name", "class", "ref", "aria-describedby"]);
28
+ const initialValue = [...new Set(local.defaultValue ?? [])];
29
+ const [draft, setDraft] = createSignal(initialValue);
30
+ const value = () => local.value ?? draft();
31
+ let fieldset: HTMLFieldSetElement | undefined;
32
+ const { byValue, keys } = createChoiceOptions(() => local.options, () => fieldset, "CheckboxGroup");
33
+ const change = (next: readonly string[]) => {
34
+ const unique = [...new Set(next)];
35
+ if (unique.length === value().length && unique.every((item, index) => item === value()[index])) return;
36
+ if (local.value === undefined) setDraft(unique);
37
+ local.onValueChange?.([...unique]);
38
+ };
39
+ onMount(() => {
40
+ const element = fieldset;
41
+ const view = element?.ownerDocument.defaultView;
42
+ if (!element || !view) return;
43
+ const pending = new Set<number>();
44
+ const reset = (event: Event) => {
45
+ if (event.target !== element.form) return;
46
+ const task = view.setTimeout(() => {
47
+ pending.delete(task);
48
+ if (!event.defaultPrevented && local.value === undefined) change(initialValue);
49
+ }, 0);
50
+ pending.add(task);
51
+ };
52
+ element.ownerDocument.addEventListener("reset", reset, true);
53
+ onCleanup(() => {
54
+ element.ownerDocument.removeEventListener("reset", reset, true);
55
+ for (const task of pending) view.clearTimeout(task);
56
+ });
57
+ });
58
+ const describedBy = () => [local.description ? `${id}-description` : "", local.error ? `${id}-error` : "", local["aria-describedby"]].filter(Boolean).join(" ") || undefined;
59
+ return <fieldset {...others} tabIndex={others.tabIndex ?? -1} ref={element => { fieldset = element; if (typeof local.ref === "function") local.ref(element); }} class={cn("sheen-checkbox-group", local.class)} disabled={local.disabled} aria-describedby={describedBy()} aria-invalid={local.error ? true : undefined} data-invalid={local.error ? "true" : undefined} data-readonly={local.readOnly || undefined}>
60
+ <legend class="sheen-checkbox-group-label">{local.label}</legend>
61
+ <Show when={local.description}><p id={`${id}-description`} class="sheen-checkbox-group-description">{local.description}</p></Show>
62
+ <div class="sheen-checkbox-group-options"><For each={keys()}>{key => {
63
+ const initialOption = untrack(() => byValue().get(key));
64
+ if (!initialOption) throw new Error("CheckboxGroup: missing option during creation");
65
+ const option = () => byValue().get(key) ?? initialOption;
66
+ return <Checkbox {...option()} name={local.name ?? ""} form={others.form} checked={value().includes(key)} disabled={!!local.disabled || !!option().disabled} readOnly={local.readOnly ?? false} onCheckedChange={checked => {
67
+ if (local.disabled || local.readOnly || option().disabled) return;
68
+ change(checked ? [...value(), key] : value().filter(item => item !== key));
69
+ }} />;
70
+ }}</For></div>
71
+ <Show when={local.error}><p id={`${id}-error`} class="sheen-checkbox-group-error">{local.error}</p></Show>
72
+ </fieldset>;
73
+ }
@@ -0,0 +1,43 @@
1
+ import * as Primitive from "@kobalte/core/collapsible";
2
+ import { createEffect, createSignal, createUniqueId, splitProps } from "solid-js";
3
+ import type { JSX } from "solid-js";
4
+ import { Button } from "./Button.tsx";
5
+ import { cn } from "../utils/cn.ts";
6
+
7
+ export interface CollapsibleProps extends JSX.HTMLAttributes<HTMLDivElement> {
8
+ label: string;
9
+ open?: boolean;
10
+ defaultOpen?: boolean;
11
+ onOpenChange?: (open: boolean) => void;
12
+ disabled?: boolean;
13
+ }
14
+
15
+ export function Collapsible(props: CollapsibleProps): JSX.Element {
16
+ const [local, others] = splitProps(props, ["label", "open", "defaultOpen", "onOpenChange", "disabled", "class", "children", "id"]);
17
+ const generatedId = createUniqueId();
18
+ const id = () => local.id ?? `sheen-collapsible-${generatedId}`;
19
+ const [draft, setDraft] = createSignal(local.defaultOpen ?? false);
20
+ const open = () => local.open === undefined ? draft() : local.open;
21
+ let trigger: HTMLButtonElement | undefined;
22
+ let content: HTMLDivElement | undefined;
23
+ const restoreFocus = () => {
24
+ if (content?.contains(content.ownerDocument.activeElement)) trigger?.focus({ preventScroll: true });
25
+ };
26
+ const change = (next: boolean) => {
27
+ if (local.disabled || next === open()) return;
28
+ if (local.open === undefined) {
29
+ if (!next) restoreFocus();
30
+ setDraft(next);
31
+ }
32
+ local.onOpenChange?.(next);
33
+ };
34
+ createEffect(() => { if (!open()) restoreFocus(); });
35
+ return <Primitive.Root {...others} id={id()} class={cn("sheen-collapsible", local.class)} open={open()} onOpenChange={change} disabled={local.disabled ?? false}>
36
+ <Primitive.Trigger as={Button} ref={trigger} id={`${id()}-trigger`} aria-controls={`${id()}-content`} class="sheen-collapsible-trigger">
37
+ <span class="sheen-collapsible-indicator" aria-hidden="true" />{local.label}
38
+ </Primitive.Trigger>
39
+ <div ref={content} id={`${id()}-content`} class="sheen-collapsible-content" data-open={open()} aria-hidden={!open() || undefined} inert={!open()}>
40
+ <div class="sheen-collapsible-clip"><div class="sheen-collapsible-body">{local.children}</div></div>
41
+ </div>
42
+ </Primitive.Root>;
43
+ }
@@ -0,0 +1,402 @@
1
+ import * as Primitive from "@kobalte/core/combobox";
2
+ import { For, Show, createEffect, createMemo, createSignal, createUniqueId, onCleanup, onMount, splitProps } from "solid-js";
3
+ import type { Accessor, JSX } from "solid-js";
4
+ import { useTheme } from "../theme/ThemeProvider.tsx";
5
+ import { cn } from "../utils/cn.ts";
6
+ import { Button } from "./Button.tsx";
7
+ import { useShortcutKeyboardOwner } from "./ShortcutProvider.tsx";
8
+ import { Tag } from "./Status.tsx";
9
+
10
+ export interface ComboboxOption {
11
+ readonly value: string;
12
+ readonly label: string;
13
+ readonly description?: string;
14
+ readonly disabled?: boolean;
15
+ }
16
+
17
+ export type ComboboxFilter = "contains" | "startsWith" | "endsWith" | false;
18
+
19
+ interface ComboboxCommonProps extends Omit<JSX.HTMLAttributes<HTMLDivElement>, "children" | "onChange"> {
20
+ label: string;
21
+ options: readonly ComboboxOption[];
22
+ name?: string;
23
+ form?: string;
24
+ placeholder?: string;
25
+ description?: string;
26
+ error?: string;
27
+ disabled?: boolean;
28
+ readOnly?: boolean;
29
+ required?: boolean;
30
+ filter?: ComboboxFilter;
31
+ onInputChange?: (value: string) => void;
32
+ pending?: boolean;
33
+ resultsError?: string;
34
+ onRetry?: () => void;
35
+ inputRef?: (element: HTMLInputElement) => void;
36
+ }
37
+
38
+ export interface ComboboxProps extends ComboboxCommonProps {
39
+ value?: string | null;
40
+ defaultValue?: string | null;
41
+ onValueChange?: (value: string | null) => void;
42
+ }
43
+
44
+ export interface MultiComboboxProps extends ComboboxCommonProps {
45
+ value?: readonly string[];
46
+ defaultValue?: readonly string[];
47
+ onValueChange?: (value: readonly string[]) => void;
48
+ }
49
+
50
+ interface OptionSnapshot {
51
+ readonly list: ComboboxOption[];
52
+ readonly map: ReadonlyMap<string, ComboboxOption>;
53
+ }
54
+
55
+ interface AcceptedOptionState {
56
+ readonly snapshot: OptionSnapshot;
57
+ readonly retained: boolean;
58
+ }
59
+
60
+ interface OptionModel {
61
+ readonly state: Accessor<AcceptedOptionState>;
62
+ readonly rootOptions: (selectedValues: readonly string[]) => ComboboxOption[];
63
+ readonly resolve: (value: string, owner: string) => ComboboxOption;
64
+ readonly includesAccepted: (value: string) => boolean;
65
+ }
66
+
67
+ function validateOptions(options: readonly ComboboxOption[], owner: string): OptionSnapshot {
68
+ const map = new Map<string, ComboboxOption>();
69
+ for (const option of options) {
70
+ if (!option.value) throw new Error(`${owner}: option values must be nonempty`);
71
+ if (!option.label.trim()) throw new Error(`${owner}: option labels must be nonempty`);
72
+ if (map.has(option.value)) throw new Error(`${owner}: duplicate option value ${JSON.stringify(option.value)}`);
73
+ map.set(option.value, option);
74
+ }
75
+ return { list: [...map.values()], map };
76
+ }
77
+
78
+ function createOptionModel(options: Accessor<readonly ComboboxOption[]>, pending: Accessor<boolean>, owner: string): OptionModel {
79
+ let accepted: OptionSnapshot | undefined;
80
+ const seen = new Map<string, ComboboxOption>();
81
+ const state = createMemo<AcceptedOptionState>(() => {
82
+ const candidate = validateOptions(options(), owner);
83
+ if (pending() && accepted) return { snapshot: accepted, retained: true };
84
+ accepted = candidate;
85
+ for (const option of candidate.list) seen.set(option.value, option);
86
+ return { snapshot: candidate, retained: false };
87
+ });
88
+ // A pending transition changes status, not the accepted collection. Preserve
89
+ // the snapshot reference so item renderers do not churn merely to show busy UI.
90
+ const snapshot = createMemo(() => state().snapshot);
91
+ const resolve = (value: string, valueOwner: string): ComboboxOption => {
92
+ snapshot();
93
+ const option = seen.get(value);
94
+ if (!option) throw new Error(`${valueOwner}: selected value ${JSON.stringify(value)} has not appeared in options`);
95
+ return option;
96
+ };
97
+ return {
98
+ state,
99
+ resolve,
100
+ includesAccepted: value => snapshot().map.has(value),
101
+ rootOptions(selectedValues) {
102
+ const acceptedSnapshot = snapshot();
103
+ const result = [...acceptedSnapshot.list];
104
+ for (const value of selectedValues) {
105
+ if (!acceptedSnapshot.map.has(value)) result.push(resolve(value, owner));
106
+ }
107
+ return result;
108
+ },
109
+ };
110
+ }
111
+
112
+ function validateValues(values: readonly string[], owner: string): void {
113
+ const unique = new Set<string>();
114
+ for (const value of values) {
115
+ if (!value) throw new Error(`${owner}: selected values must be nonempty`);
116
+ if (unique.has(value)) throw new Error(`${owner}: duplicate selected value ${JSON.stringify(value)}`);
117
+ unique.add(value);
118
+ }
119
+ }
120
+
121
+ function normalized(value: string, locale: string): string {
122
+ return value.normalize("NFKD").replaceAll(/\p{M}/gu, "").toLocaleLowerCase(locale);
123
+ }
124
+
125
+ function optionFilter(model: OptionModel, mode: Accessor<ComboboxFilter>, locale: Accessor<string>): (option: ComboboxOption, input: string) => boolean {
126
+ return (option, input) => {
127
+ if (!model.includesAccepted(option.value)) return false;
128
+ if (mode() === false) return true;
129
+ const candidate = normalized(option.label, locale());
130
+ const query = normalized(input, locale());
131
+ if (mode() === "startsWith") return candidate.startsWith(query);
132
+ if (mode() === "endsWith") return candidate.endsWith(query);
133
+ return candidate.includes(query);
134
+ };
135
+ }
136
+
137
+ function OptionItem(props: Primitive.ComboboxRootItemComponentProps<ComboboxOption>): JSX.Element {
138
+ return <Primitive.Item item={props.item} class="sheen-select-option sheen-combobox-option" onPointerDown={event => {
139
+ // Keep the editable input focused until Kobalte selects on pointer-up. A
140
+ // mouse blur would reset the query and reorder a filtered list under the
141
+ // pointer before the selection event lands.
142
+ if (event.pointerType === "mouse") event.preventDefault();
143
+ }}>
144
+ <Primitive.ItemLabel>{props.item.rawValue.label}</Primitive.ItemLabel>
145
+ <Show when={props.item.rawValue.description}>{description => <Primitive.ItemDescription class="sheen-select-option-description">{description()}</Primitive.ItemDescription>}</Show>
146
+ <Primitive.ItemIndicator class="sheen-select-indicator" aria-hidden="true">✓</Primitive.ItemIndicator>
147
+ </Primitive.Item>;
148
+ }
149
+
150
+ interface ContentProps {
151
+ readonly id: string;
152
+ readonly label: string;
153
+ readonly open: Accessor<boolean>;
154
+ readonly state: Accessor<AcceptedOptionState>;
155
+ readonly pending: Accessor<boolean>;
156
+ readonly resultsError: Accessor<string | undefined>;
157
+ readonly onRetry: Accessor<(() => void) | undefined>;
158
+ readonly setContent: (element: HTMLElement) => void;
159
+ }
160
+
161
+ function OptionsContent(props: ContentProps): JSX.Element {
162
+ const theme = useTheme();
163
+ return <Show when={theme.portal()}>{target => <Primitive.Portal mount={target()}>
164
+ <Primitive.Content ref={props.setContent} data-kb-top-layer="true" aria-hidden={!props.open() || undefined} inert={!props.open()}
165
+ class="sheen-select-content sheen-combobox-content" style={{ "z-index": theme.layers.zIndex(props.id) }}>
166
+ <Primitive.Listbox<ComboboxOption> class="sheen-select-listbox sheen-combobox-listbox" aria-label={`${props.label} Suggestions`} inert={props.pending()} aria-disabled={props.pending() || undefined} />
167
+ <Show when={props.pending()}>
168
+ <p class="sheen-combobox-status" role="status" aria-live="polite">{props.state().retained ? (theme.messages().refreshing ?? theme.messages().loading) : theme.messages().loading}</p>
169
+ </Show>
170
+ <Show when={!props.pending() && !props.state().snapshot.list.length && !props.resultsError()}>
171
+ <p class="sheen-select-empty">{theme.messages().noResults}</p>
172
+ </Show>
173
+ <Show when={props.resultsError()}>{message => <div class="sheen-combobox-results-error" role="alert">
174
+ <span>{message()}</span>
175
+ <Show when={props.onRetry()}>{retry => <Button size="sm" onClick={() => retry()()}>{theme.messages().retry}</Button>}</Show>
176
+ </div>}</Show>
177
+ </Primitive.Content>
178
+ </Primitive.Portal>}</Show>;
179
+ }
180
+
181
+ interface QueryContinuityProps {
182
+ readonly snapshot: Accessor<OptionSnapshot>;
183
+ readonly active: Accessor<boolean>;
184
+ readonly inputValue: Accessor<string>;
185
+ }
186
+
187
+ function QueryContinuity(props: QueryContinuityProps): null {
188
+ const context = Primitive.useComboboxContext();
189
+ let initialized = false;
190
+ createEffect(() => {
191
+ props.snapshot();
192
+ if (!initialized) {
193
+ initialized = true;
194
+ return;
195
+ }
196
+ if (!props.active()) return;
197
+ const value = props.inputValue();
198
+ queueMicrotask(() => { if (props.active()) context.setInputValue(value); });
199
+ });
200
+ return null;
201
+ }
202
+
203
+ interface FieldTextProps {
204
+ readonly id: string;
205
+ readonly description: string | undefined;
206
+ readonly error: string | undefined;
207
+ }
208
+
209
+ function FieldText(props: FieldTextProps): JSX.Element {
210
+ return <>
211
+ <Show when={props.description}><Primitive.Description id={`${props.id}-description`} class="sheen-field-description">{props.description}</Primitive.Description></Show>
212
+ <Show when={props.error}><Primitive.ErrorMessage id={`${props.id}-error`} class="sheen-field-error">{props.error}</Primitive.ErrorMessage></Show>
213
+ </>;
214
+ }
215
+
216
+ function describedBy(id: string, description: string | undefined, error: string | undefined): string | undefined {
217
+ return [description ? `${id}-description` : "", error ? `${id}-error` : ""].filter(Boolean).join(" ") || undefined;
218
+ }
219
+
220
+ /** Editable single selection with app-owned async results and accepted-result continuity. */
221
+ export function Combobox(props: ComboboxProps): JSX.Element {
222
+ const [local, others] = splitProps(props, ["id", "class", "label", "options", "value", "defaultValue", "onValueChange", "name", "form", "placeholder", "description", "error", "disabled", "readOnly", "required", "filter", "onInputChange", "pending", "resultsError", "onRetry", "inputRef"]);
223
+ const theme = useTheme();
224
+ const id = createUniqueId();
225
+ const initial = local.defaultValue ?? null;
226
+ const [draft, setDraft] = createSignal<string | null>(initial);
227
+ const [open, setOpen] = createSignal(false);
228
+ const [input, setInput] = createSignal<HTMLInputElement>();
229
+ const [content, setContent] = createSignal<HTMLElement>();
230
+ const [inputValue, setInputValue] = createSignal("");
231
+ const [inputDirty, setInputDirty] = createSignal(false);
232
+ let native: HTMLSelectElement | undefined;
233
+ const currentValue = () => local.value === undefined ? draft() : local.value;
234
+ const model = createOptionModel(() => local.options, () => local.pending ?? false, "Combobox");
235
+ const selectedValues = () => {
236
+ const value = currentValue();
237
+ if (value === "") throw new Error("Combobox: selected values must be nonempty");
238
+ return value === null ? [] : [value];
239
+ };
240
+ const selectedOption = () => {
241
+ const value = currentValue();
242
+ if (value === "") throw new Error("Combobox: selected values must be nonempty");
243
+ return value === null ? null : model.resolve(value, "Combobox");
244
+ };
245
+ const rootOptions = createMemo(() => model.rootOptions(selectedValues()));
246
+ const filter = optionFilter(model, () => local.filter ?? "contains", () => theme.state().locale);
247
+ useShortcutKeyboardOwner(input, () => true);
248
+ useShortcutKeyboardOwner(content, open);
249
+ const change = (option: ComboboxOption | null) => {
250
+ if (local.disabled || local.readOnly || local.pending) return;
251
+ const next = option?.value ?? null;
252
+ if (next === currentValue()) return;
253
+ setInputDirty(false);
254
+ if (local.value === undefined) setDraft(next);
255
+ local.onValueChange?.(next);
256
+ };
257
+ const synchronizeNative = () => {
258
+ if (!native) return;
259
+ native.value = currentValue() ?? "";
260
+ };
261
+ createEffect(() => { currentValue(); rootOptions(); synchronizeNative(); });
262
+ onMount(() => {
263
+ const document = native?.ownerDocument;
264
+ const window = document?.defaultView;
265
+ if (!document || !window) return;
266
+ const timers = new Set<number>();
267
+ const reset = (event: Event) => {
268
+ if (event.target !== native?.form) return;
269
+ const timer = window.setTimeout(() => {
270
+ timers.delete(timer);
271
+ if (event.defaultPrevented) return;
272
+ if (local.value === undefined) change(initial === null ? null : model.resolve(initial, "Combobox defaultValue"));
273
+ synchronizeNative();
274
+ }, 0);
275
+ timers.add(timer);
276
+ };
277
+ document.addEventListener("reset", reset, true);
278
+ onCleanup(() => { document.removeEventListener("reset", reset, true); for (const timer of timers) window.clearTimeout(timer); });
279
+ });
280
+ createEffect(() => { if (open()) onCleanup(theme.layers.register(id)); });
281
+ return <Primitive.Root<ComboboxOption> {...others} id={local.id ?? `${id}-root`} aria-labelledby={`${id}-label`} multiple={false}
282
+ class={cn("sheen-field sheen-combobox", local.class)} data-previous-results={local.pending && model.state().retained ? "" : undefined}
283
+ options={rootOptions()} optionValue={option => option.value} optionTextValue={option => option.label} optionLabel={option => option.label}
284
+ optionDisabled={option => Boolean(option.disabled)} itemComponent={OptionItem}
285
+ value={selectedOption()} {...(initial ? { defaultValue: model.resolve(initial, "Combobox defaultValue") } : {})} onChange={change}
286
+ defaultFilter={filter} allowsEmptyCollection closeOnSelection disallowEmptySelection={false}
287
+ open={open()} onOpenChange={next => { setOpen(next && !local.disabled); if (!next) setInputDirty(false); }} disabled={local.disabled ?? false} readOnly={local.readOnly ?? false}
288
+ required={local.required ?? false} validationState={local.error ? "invalid" : "valid"} placement="bottom-start" sameWidth gutter={4}>
289
+ <label id={`${id}-label`} for={`${id}-input`} class="sheen-field-label">{local.label}<Show when={local.required}><span aria-hidden="true"> *</span></Show></label>
290
+ <Primitive.Control<ComboboxOption> class="sheen-combobox-control">
291
+ <Primitive.Input id={`${id}-input`} aria-labelledby={`${id}-label`} aria-describedby={describedBy(id, local.description, local.error)} ref={element => { setInput(element); local.inputRef?.(element); }} class="sheen-input sheen-combobox-input" placeholder={local.placeholder}
292
+ aria-busy={local.pending || undefined} onInput={() => {
293
+ const value = input()?.value ?? "";
294
+ setInputValue(value);
295
+ setInputDirty(true);
296
+ local.onInputChange?.(value);
297
+ }} />
298
+ </Primitive.Control>
299
+ <select ref={native} id={`${id}-native`} class="sheen-select-native" aria-hidden="true" tabIndex={-1} name={local.name} form={local.form}
300
+ required={local.required} disabled={Boolean(local.disabled || (currentValue() === null && !local.required))} aria-label={local.label}
301
+ onInvalid={event => { event.preventDefault(); input()?.focus(); }} onChange={event => {
302
+ const value = event.currentTarget.value;
303
+ change(value === "" ? null : model.resolve(value, "Combobox native value"));
304
+ synchronizeNative();
305
+ }}>
306
+ <option value=""></option>
307
+ <For each={rootOptions()}>{option => <option value={option.value} disabled={option.disabled} selected={currentValue() === option.value}>{option.label}</option>}</For>
308
+ </select>
309
+ <QueryContinuity snapshot={() => model.state().snapshot} active={() => open() && inputDirty() && input()?.ownerDocument.activeElement === input()} inputValue={inputValue} />
310
+ <FieldText id={id} description={local.description} error={local.error} />
311
+ <OptionsContent id={id} label={local.label} open={open} state={model.state} pending={() => local.pending ?? false} resultsError={() => local.resultsError} onRetry={() => local.onRetry} setContent={setContent} />
312
+ </Primitive.Root>;
313
+ }
314
+
315
+ /** Editable multiple selection with removable tags and app-owned async results. */
316
+ export function MultiCombobox(props: MultiComboboxProps): JSX.Element {
317
+ const [local, others] = splitProps(props, ["id", "class", "label", "options", "value", "defaultValue", "onValueChange", "name", "form", "placeholder", "description", "error", "disabled", "readOnly", "required", "filter", "onInputChange", "pending", "resultsError", "onRetry", "inputRef"]);
318
+ const theme = useTheme();
319
+ const id = createUniqueId();
320
+ const initial = [...(local.defaultValue ?? [])];
321
+ validateValues(initial, "MultiCombobox defaultValue");
322
+ const [draft, setDraft] = createSignal<readonly string[]>(initial);
323
+ const [open, setOpen] = createSignal(false);
324
+ const [input, setInput] = createSignal<HTMLInputElement>();
325
+ const [content, setContent] = createSignal<HTMLElement>();
326
+ let native: HTMLSelectElement | undefined;
327
+ const currentValues = () => {
328
+ const values = local.value === undefined ? draft() : local.value;
329
+ validateValues(values, "MultiCombobox value");
330
+ return values;
331
+ };
332
+ const model = createOptionModel(() => local.options, () => local.pending ?? false, "MultiCombobox");
333
+ const selectedOptions = createMemo(() => currentValues().map(value => model.resolve(value, "MultiCombobox")));
334
+ const rootOptions = createMemo(() => model.rootOptions(currentValues()));
335
+ const filter = optionFilter(model, () => local.filter ?? "contains", () => theme.state().locale);
336
+ useShortcutKeyboardOwner(input, () => true);
337
+ useShortcutKeyboardOwner(content, open);
338
+ const changeValues = (next: readonly string[]) => {
339
+ if (local.disabled || local.readOnly) return;
340
+ validateValues(next, "MultiCombobox value");
341
+ const current = currentValues();
342
+ if (current.length === next.length && current.every((value, index) => value === next[index])) return;
343
+ if (local.value === undefined) setDraft([...next]);
344
+ local.onValueChange?.([...next]);
345
+ };
346
+ const change = (options: ComboboxOption[]) => {
347
+ if (local.pending) return;
348
+ changeValues(options.map(option => option.value));
349
+ };
350
+ const remove = (value: string) => changeValues(currentValues().filter(candidate => candidate !== value));
351
+ const synchronizeNative = () => {
352
+ if (!native) return;
353
+ const selected = new Set(currentValues());
354
+ for (const option of native.options) option.selected = selected.has(option.value);
355
+ };
356
+ createEffect(() => { currentValues(); rootOptions(); synchronizeNative(); });
357
+ onMount(() => {
358
+ const document = native?.ownerDocument;
359
+ const window = document?.defaultView;
360
+ if (!document || !window) return;
361
+ const timers = new Set<number>();
362
+ const reset = (event: Event) => {
363
+ if (event.target !== native?.form) return;
364
+ const timer = window.setTimeout(() => {
365
+ timers.delete(timer);
366
+ if (event.defaultPrevented) return;
367
+ if (local.value === undefined) changeValues(initial);
368
+ synchronizeNative();
369
+ }, 0);
370
+ timers.add(timer);
371
+ };
372
+ document.addEventListener("reset", reset, true);
373
+ onCleanup(() => { document.removeEventListener("reset", reset, true); for (const timer of timers) window.clearTimeout(timer); });
374
+ });
375
+ createEffect(() => { if (open()) onCleanup(theme.layers.register(id)); });
376
+ return <Primitive.Root<ComboboxOption> {...others} id={local.id ?? `${id}-root`} aria-labelledby={`${id}-label`} multiple
377
+ class={cn("sheen-field sheen-combobox sheen-multi-combobox", local.class)} data-previous-results={local.pending && model.state().retained ? "" : undefined}
378
+ options={rootOptions()} optionValue={option => option.value} optionTextValue={option => option.label} optionLabel={option => option.label}
379
+ optionDisabled={option => Boolean(local.pending || option.disabled)} itemComponent={OptionItem}
380
+ value={selectedOptions()} defaultValue={initial.map(value => model.resolve(value, "MultiCombobox defaultValue"))} onChange={change}
381
+ defaultFilter={filter} allowsEmptyCollection closeOnSelection={false} removeOnBackspace selectionBehavior="toggle"
382
+ open={open()} onOpenChange={next => setOpen(next && !local.disabled)} disabled={local.disabled ?? false} readOnly={local.readOnly ?? false}
383
+ required={local.required ?? false} validationState={local.error ? "invalid" : "valid"} placement="bottom-start" sameWidth gutter={4}>
384
+ <label id={`${id}-label`} for={`${id}-input`} class="sheen-field-label">{local.label}<Show when={local.required}><span aria-hidden="true"> *</span></Show></label>
385
+ <Primitive.Control<ComboboxOption> class="sheen-input sheen-combobox-control sheen-multi-combobox-control">
386
+ <For each={selectedOptions()}>{option => <Tag label={option.label} disabled={Boolean(local.disabled || local.readOnly)} onRemove={() => remove(option.value)} />}</For>
387
+ <Primitive.Input id={`${id}-input`} aria-labelledby={`${id}-label`} aria-describedby={describedBy(id, local.description, local.error)} aria-required={local.required || undefined} required={false}
388
+ ref={element => { setInput(element); local.inputRef?.(element); }} class="sheen-combobox-input sheen-multi-combobox-input" placeholder={!selectedOptions().length ? local.placeholder : undefined}
389
+ aria-busy={local.pending || undefined} onInput={() => local.onInputChange?.(input()?.value ?? "")} />
390
+ </Primitive.Control>
391
+ <select ref={native} class="sheen-select-native" aria-hidden="true" tabIndex={-1} multiple name={local.name} form={local.form}
392
+ required={local.required} disabled={local.disabled} aria-label={local.label}
393
+ onInvalid={event => { event.preventDefault(); input()?.focus(); }} onChange={event => {
394
+ changeValues([...event.currentTarget.selectedOptions].map(option => option.value));
395
+ synchronizeNative();
396
+ }}>
397
+ <For each={rootOptions()}>{option => <option value={option.value} disabled={option.disabled} selected={currentValues().includes(option.value)}>{option.label}</option>}</For>
398
+ </select>
399
+ <FieldText id={id} description={local.description} error={local.error} />
400
+ <OptionsContent id={id} label={local.label} open={open} state={model.state} pending={() => local.pending ?? false} resultsError={() => local.resultsError} onRetry={() => local.onRetry} setContent={setContent} />
401
+ </Primitive.Root>;
402
+ }