@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.
- package/LICENSE +19 -0
- package/README.md +32 -0
- package/dist/core.d.ts +52 -0
- package/dist/core.js +29 -0
- package/dist/formisch.d.ts +5 -0
- package/dist/formisch.js +22 -0
- package/dist/forms.d.ts +34 -0
- package/dist/forms.js +18 -0
- package/dist/index.d.ts +144 -0
- package/dist/index.js +75 -0
- package/dist/layout/Layout.d.ts +28 -0
- package/dist/layout/Layout.js +121 -0
- package/dist/metadata.d.ts +46 -0
- package/dist/metadata.js +47 -0
- package/dist/navigation.d.ts +30 -0
- package/dist/navigation.js +16 -0
- package/dist/overlays.d.ts +28 -0
- package/dist/overlays.js +15 -0
- package/dist/primitives/Accordion.d.ts +16 -0
- package/dist/primitives/Accordion.js +223 -0
- package/dist/primitives/ActivityTimeline.d.ts +20 -0
- package/dist/primitives/ActivityTimeline.js +104 -0
- package/dist/primitives/AlertDialog.d.ts +7 -0
- package/dist/primitives/AlertDialog.js +8 -0
- package/dist/primitives/Avatar.d.ts +22 -0
- package/dist/primitives/Avatar.js +115 -0
- package/dist/primitives/Breadcrumb.d.ts +11 -0
- package/dist/primitives/Breadcrumb.js +66 -0
- package/dist/primitives/Button.d.ts +11 -0
- package/dist/primitives/Button.js +59 -0
- package/dist/primitives/ButtonGroup.d.ts +7 -0
- package/dist/primitives/ButtonGroup.js +33 -0
- package/dist/primitives/Checkbox.d.ts +22 -0
- package/dist/primitives/Checkbox.js +93 -0
- package/dist/primitives/CheckboxGroup.d.ts +18 -0
- package/dist/primitives/CheckboxGroup.js +122 -0
- package/dist/primitives/Collapsible.d.ts +9 -0
- package/dist/primitives/Collapsible.js +72 -0
- package/dist/primitives/Combobox.d.ts +41 -0
- package/dist/primitives/Combobox.js +684 -0
- package/dist/primitives/CommandPalette.d.ts +42 -0
- package/dist/primitives/CommandPalette.js +319 -0
- package/dist/primitives/ConfirmDialog.d.ts +11 -0
- package/dist/primitives/ConfirmDialog.js +42 -0
- package/dist/primitives/ContextMenu.d.ts +16 -0
- package/dist/primitives/ContextMenu.js +132 -0
- package/dist/primitives/DescriptionList.d.ts +11 -0
- package/dist/primitives/DescriptionList.js +44 -0
- package/dist/primitives/Dialog.d.ts +21 -0
- package/dist/primitives/Dialog.js +141 -0
- package/dist/primitives/Drawer.d.ts +9 -0
- package/dist/primitives/Drawer.js +21 -0
- package/dist/primitives/DropdownMenu.d.ts +59 -0
- package/dist/primitives/DropdownMenu.js +441 -0
- package/dist/primitives/EditableTextField.d.ts +11 -0
- package/dist/primitives/EditableTextField.js +126 -0
- package/dist/primitives/EmptyState.d.ts +7 -0
- package/dist/primitives/EmptyState.js +45 -0
- package/dist/primitives/Field.d.ts +18 -0
- package/dist/primitives/Field.js +72 -0
- package/dist/primitives/FileDropzone.d.ts +40 -0
- package/dist/primitives/FileDropzone.js +261 -0
- package/dist/primitives/IconButton.d.ts +8 -0
- package/dist/primitives/IconButton.js +34 -0
- package/dist/primitives/Input.d.ts +7 -0
- package/dist/primitives/Input.js +55 -0
- package/dist/primitives/InputGroup.d.ts +7 -0
- package/dist/primitives/InputGroup.js +73 -0
- package/dist/primitives/Link.d.ts +7 -0
- package/dist/primitives/Link.js +35 -0
- package/dist/primitives/LinkTooltip.d.ts +11 -0
- package/dist/primitives/LinkTooltip.js +88 -0
- package/dist/primitives/NavList.d.ts +17 -0
- package/dist/primitives/NavList.js +143 -0
- package/dist/primitives/Notice.d.ts +8 -0
- package/dist/primitives/Notice.js +64 -0
- package/dist/primitives/NumberField.d.ts +22 -0
- package/dist/primitives/NumberField.js +177 -0
- package/dist/primitives/Pagination.d.ts +12 -0
- package/dist/primitives/Pagination.js +127 -0
- package/dist/primitives/Popover.d.ts +12 -0
- package/dist/primitives/Popover.js +103 -0
- package/dist/primitives/Progress.d.ts +17 -0
- package/dist/primitives/Progress.js +98 -0
- package/dist/primitives/RadioGroup.d.ts +19 -0
- package/dist/primitives/RadioGroup.js +173 -0
- package/dist/primitives/Resizable.d.ts +36 -0
- package/dist/primitives/Resizable.js +196 -0
- package/dist/primitives/ScrollArea.d.ts +6 -0
- package/dist/primitives/ScrollArea.js +178 -0
- package/dist/primitives/SearchInput.d.ts +12 -0
- package/dist/primitives/SearchInput.js +89 -0
- package/dist/primitives/SegmentedControl.d.ts +25 -0
- package/dist/primitives/SegmentedControl.js +188 -0
- package/dist/primitives/Select.d.ts +24 -0
- package/dist/primitives/Select.js +320 -0
- package/dist/primitives/Separator.d.ts +6 -0
- package/dist/primitives/Separator.js +31 -0
- package/dist/primitives/ShortcutPending.d.ts +6 -0
- package/dist/primitives/ShortcutPending.js +40 -0
- package/dist/primitives/ShortcutProvider.d.ts +32 -0
- package/dist/primitives/ShortcutProvider.js +172 -0
- package/dist/primitives/ShortcutScope.d.ts +6 -0
- package/dist/primitives/ShortcutScope.js +29 -0
- package/dist/primitives/ShortcutSheet.d.ts +5 -0
- package/dist/primitives/ShortcutSheet.js +87 -0
- package/dist/primitives/Skeleton.d.ts +5 -0
- package/dist/primitives/Skeleton.js +23 -0
- package/dist/primitives/Slider.d.ts +35 -0
- package/dist/primitives/Slider.js +210 -0
- package/dist/primitives/Spinner.d.ts +7 -0
- package/dist/primitives/Spinner.js +44 -0
- package/dist/primitives/Status.d.ts +15 -0
- package/dist/primitives/Status.js +78 -0
- package/dist/primitives/Stepper.d.ts +32 -0
- package/dist/primitives/Stepper.js +147 -0
- package/dist/primitives/Surface.d.ts +11 -0
- package/dist/primitives/Surface.js +58 -0
- package/dist/primitives/Switch.d.ts +4 -0
- package/dist/primitives/Switch.js +7 -0
- package/dist/primitives/Table.d.ts +23 -0
- package/dist/primitives/Table.js +101 -0
- package/dist/primitives/Tabs.d.ts +17 -0
- package/dist/primitives/Tabs.js +157 -0
- package/dist/primitives/TagInput.d.ts +33 -0
- package/dist/primitives/TagInput.js +288 -0
- package/dist/primitives/Textarea.d.ts +8 -0
- package/dist/primitives/Textarea.js +106 -0
- package/dist/primitives/Toast.d.ts +10 -0
- package/dist/primitives/Toast.js +78 -0
- package/dist/primitives/Toaster.d.ts +9 -0
- package/dist/primitives/Toaster.js +183 -0
- package/dist/primitives/Tooltip.d.ts +13 -0
- package/dist/primitives/Tooltip.js +84 -0
- package/dist/primitives/Tree.d.ts +23 -0
- package/dist/primitives/Tree.js +275 -0
- package/dist/primitives/Typography.d.ts +16 -0
- package/dist/primitives/Typography.js +82 -0
- package/dist/primitives/boolean-input-state.d.ts +14 -0
- package/dist/primitives/boolean-input-state.js +39 -0
- package/dist/primitives/button-variants.d.ts +9 -0
- package/dist/primitives/button-variants.js +7 -0
- package/dist/primitives/choice-options.d.ts +7 -0
- package/dist/primitives/choice-options.js +35 -0
- package/dist/primitives/create-confirm.d.ts +15 -0
- package/dist/primitives/create-confirm.js +49 -0
- package/dist/primitives/create-toaster.d.ts +51 -0
- package/dist/primitives/create-toaster.js +87 -0
- package/dist/primitives/link-focus.d.ts +2 -0
- package/dist/primitives/link-focus.js +38 -0
- package/dist/primitives/menu-focus.d.ts +3 -0
- package/dist/primitives/menu-focus.js +32 -0
- package/dist/primitives/tooltip-layer.d.ts +14 -0
- package/dist/primitives/tooltip-layer.js +84 -0
- package/dist/theme/ThemeProvider.d.ts +59 -0
- package/dist/theme/ThemeProvider.js +239 -0
- package/dist/theme/intl.d.ts +6 -0
- package/dist/theme/intl.js +13 -0
- package/dist/theme/keyboard-script.d.ts +2 -0
- package/dist/theme/keyboard-script.js +6 -0
- package/dist/theme/layers.d.ts +7 -0
- package/dist/theme/layers.js +23 -0
- package/dist/theme/messages.d.ts +235 -0
- package/dist/theme/messages.js +115 -0
- package/dist/theme/script.d.ts +9 -0
- package/dist/theme/script.js +49 -0
- package/dist/theme/state.d.ts +27 -0
- package/dist/theme/state.js +55 -0
- package/dist/utils/browser-status.d.ts +5 -0
- package/dist/utils/browser-status.js +22 -0
- package/dist/utils/cn.d.ts +3 -0
- package/dist/utils/cn.js +7 -0
- package/dist/utils/optimistic.d.ts +6 -0
- package/dist/utils/optimistic.js +16 -0
- package/dist/utils/polymorphic.d.ts +1 -0
- package/dist/utils/shortcut-platform.d.ts +2 -0
- package/dist/utils/shortcut-platform.js +10 -0
- package/dist/utils/shortcut-registry.d.ts +61 -0
- package/dist/utils/shortcut-registry.js +186 -0
- package/package.json +100 -0
- package/src/core.ts +55 -0
- package/src/formisch.ts +28 -0
- package/src/forms.ts +34 -0
- package/src/index.ts +144 -0
- package/src/layout/Layout.tsx +43 -0
- package/src/metadata.d.ts +46 -0
- package/src/metadata.ts +66 -0
- package/src/navigation.ts +30 -0
- package/src/overlays.ts +28 -0
- package/src/primitives/Accordion.tsx +145 -0
- package/src/primitives/ActivityTimeline.tsx +72 -0
- package/src/primitives/AlertDialog.tsx +8 -0
- package/src/primitives/Avatar.tsx +85 -0
- package/src/primitives/Breadcrumb.tsx +41 -0
- package/src/primitives/Button.tsx +27 -0
- package/src/primitives/ButtonGroup.tsx +18 -0
- package/src/primitives/Checkbox.tsx +51 -0
- package/src/primitives/CheckboxGroup.tsx +73 -0
- package/src/primitives/Collapsible.tsx +43 -0
- package/src/primitives/Combobox.tsx +402 -0
- package/src/primitives/CommandPalette.tsx +234 -0
- package/src/primitives/ConfirmDialog.tsx +25 -0
- package/src/primitives/ContextMenu.tsx +100 -0
- package/src/primitives/DescriptionList.tsx +26 -0
- package/src/primitives/Dialog.tsx +81 -0
- package/src/primitives/Drawer.tsx +26 -0
- package/src/primitives/DropdownMenu.tsx +221 -0
- package/src/primitives/EditableTextField.tsx +91 -0
- package/src/primitives/EmptyState.tsx +21 -0
- package/src/primitives/Field.tsx +38 -0
- package/src/primitives/FileDropzone.tsx +189 -0
- package/src/primitives/IconButton.tsx +22 -0
- package/src/primitives/Input.tsx +17 -0
- package/src/primitives/InputGroup.tsx +23 -0
- package/src/primitives/Link.tsx +19 -0
- package/src/primitives/LinkTooltip.tsx +31 -0
- package/src/primitives/NavList.tsx +58 -0
- package/src/primitives/Notice.tsx +25 -0
- package/src/primitives/NumberField.tsx +168 -0
- package/src/primitives/Pagination.tsx +64 -0
- package/src/primitives/Popover.tsx +44 -0
- package/src/primitives/Progress.tsx +62 -0
- package/src/primitives/RadioGroup.tsx +104 -0
- package/src/primitives/Resizable.tsx +170 -0
- package/src/primitives/ScrollArea.tsx +115 -0
- package/src/primitives/SearchInput.tsx +98 -0
- package/src/primitives/SegmentedControl.tsx +140 -0
- package/src/primitives/Select.tsx +171 -0
- package/src/primitives/Separator.tsx +13 -0
- package/src/primitives/ShortcutPending.tsx +21 -0
- package/src/primitives/ShortcutProvider.tsx +179 -0
- package/src/primitives/ShortcutScope.tsx +17 -0
- package/src/primitives/ShortcutSheet.tsx +35 -0
- package/src/primitives/Skeleton.tsx +12 -0
- package/src/primitives/Slider.tsx +153 -0
- package/src/primitives/Spinner.tsx +19 -0
- package/src/primitives/Status.tsx +33 -0
- package/src/primitives/Stepper.tsx +106 -0
- package/src/primitives/Surface.tsx +21 -0
- package/src/primitives/Switch.tsx +9 -0
- package/src/primitives/Table.tsx +57 -0
- package/src/primitives/Tabs.tsx +96 -0
- package/src/primitives/TagInput.tsx +282 -0
- package/src/primitives/Textarea.tsx +77 -0
- package/src/primitives/Toast.tsx +36 -0
- package/src/primitives/Toaster.tsx +175 -0
- package/src/primitives/Tooltip.tsx +41 -0
- package/src/primitives/Tree.tsx +256 -0
- package/src/primitives/Typography.tsx +39 -0
- package/src/primitives/boolean-input-state.ts +65 -0
- package/src/primitives/button-variants.ts +20 -0
- package/src/primitives/choice-options.ts +39 -0
- package/src/primitives/create-confirm.tsx +56 -0
- package/src/primitives/create-toaster.ts +128 -0
- package/src/primitives/link-focus.ts +38 -0
- package/src/primitives/menu-focus.ts +43 -0
- package/src/primitives/tooltip-layer.tsx +53 -0
- package/src/styles.css +655 -0
- package/src/theme/ThemeProvider.tsx +215 -0
- package/src/theme/intl.ts +14 -0
- package/src/theme/keyboard-script.ts +4 -0
- package/src/theme/layers.ts +16 -0
- package/src/theme/messages.ts +157 -0
- package/src/theme/script.ts +70 -0
- package/src/theme/state.ts +48 -0
- package/src/utils/browser-status.ts +36 -0
- package/src/utils/cn.ts +5 -0
- package/src/utils/optimistic.ts +22 -0
- package/src/utils/polymorphic.ts +1 -0
- package/src/utils/shortcut-platform.ts +4 -0
- package/src/utils/shortcut-registry.ts +216 -0
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import * as Primitive from "@kobalte/core/select";
|
|
2
|
+
import { For, Show, createEffect, createMemo, createSignal, createUniqueId, onCleanup, onMount, splitProps, untrack } from "solid-js";
|
|
3
|
+
import type { JSX } from "solid-js";
|
|
4
|
+
import { useTheme } from "../theme/ThemeProvider.tsx";
|
|
5
|
+
import { cn } from "../utils/cn.ts";
|
|
6
|
+
import { useShortcutKeyboardOwner } from "./ShortcutProvider.tsx";
|
|
7
|
+
|
|
8
|
+
export interface SelectOption {
|
|
9
|
+
readonly value: string;
|
|
10
|
+
readonly label: string;
|
|
11
|
+
readonly description?: string;
|
|
12
|
+
readonly disabled?: boolean;
|
|
13
|
+
}
|
|
14
|
+
export interface SelectProps extends Omit<JSX.HTMLAttributes<HTMLDivElement>, "children" | "onChange"> {
|
|
15
|
+
label: string;
|
|
16
|
+
options: readonly SelectOption[];
|
|
17
|
+
value?: string | null;
|
|
18
|
+
defaultValue?: string | null;
|
|
19
|
+
onValueChange?: (value: string | null) => void;
|
|
20
|
+
name?: string;
|
|
21
|
+
form?: string;
|
|
22
|
+
placeholder?: string;
|
|
23
|
+
description?: string;
|
|
24
|
+
error?: string;
|
|
25
|
+
disabled?: boolean;
|
|
26
|
+
readOnly?: boolean;
|
|
27
|
+
required?: boolean;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Single selection with app-owned values and a contextual listbox. */
|
|
31
|
+
export function Select(props: SelectProps): JSX.Element {
|
|
32
|
+
const [local, others] = splitProps(props, ["id", "class", "label", "options", "value", "defaultValue", "onValueChange", "name", "form", "placeholder", "description", "error", "disabled", "readOnly", "required"]);
|
|
33
|
+
const theme = useTheme();
|
|
34
|
+
const id = createUniqueId();
|
|
35
|
+
const initial = local.defaultValue ?? null;
|
|
36
|
+
const [draft, setDraft] = createSignal(initial);
|
|
37
|
+
const [open, setOpen] = createSignal(false);
|
|
38
|
+
const value = () => local.value === undefined ? draft() : local.value;
|
|
39
|
+
const [trigger, setTrigger] = createSignal<HTMLButtonElement>();
|
|
40
|
+
const [content, setContent] = createSignal<HTMLElement>();
|
|
41
|
+
useShortcutKeyboardOwner(trigger, () => true);
|
|
42
|
+
useShortcutKeyboardOwner(content, open);
|
|
43
|
+
let native: HTMLSelectElement | undefined;
|
|
44
|
+
let listbox: HTMLUListElement | undefined;
|
|
45
|
+
let disposed = false;
|
|
46
|
+
let refreshingOptions = false;
|
|
47
|
+
onCleanup(() => { disposed = true; });
|
|
48
|
+
const options = createMemo(() => {
|
|
49
|
+
const result = new Map<string, SelectOption>();
|
|
50
|
+
for (const option of local.options) {
|
|
51
|
+
if (!option.value) throw new Error("Select: option values must be nonempty");
|
|
52
|
+
if (result.has(option.value)) throw new Error(`Select: duplicate option value ${JSON.stringify(option.value)}`);
|
|
53
|
+
result.set(option.value, option);
|
|
54
|
+
}
|
|
55
|
+
if (listbox) {
|
|
56
|
+
refreshingOptions = true;
|
|
57
|
+
listbox.ownerDocument.defaultView?.queueMicrotask(() => { refreshingOptions = false; });
|
|
58
|
+
}
|
|
59
|
+
return result;
|
|
60
|
+
});
|
|
61
|
+
let previousKeys: string[] = [];
|
|
62
|
+
const keys = createMemo(() => {
|
|
63
|
+
const next = [...options().keys()];
|
|
64
|
+
const element = listbox;
|
|
65
|
+
const focused = element?.querySelector<HTMLElement>("[role=option]:focus");
|
|
66
|
+
if (element && focused) {
|
|
67
|
+
const key = focused.dataset.key;
|
|
68
|
+
const previousIndex = previousKeys.indexOf(key ?? "");
|
|
69
|
+
element.ownerDocument.defaultView?.queueMicrotask(() => {
|
|
70
|
+
if (disposed || !element.isConnected) return;
|
|
71
|
+
const active = element.ownerDocument.activeElement;
|
|
72
|
+
if (active !== element.ownerDocument.body && active !== focused && active !== element) return;
|
|
73
|
+
const items = [...element.querySelectorAll<HTMLElement>("[role=option]")];
|
|
74
|
+
const eligible = (item: HTMLElement) => item.getAttribute("aria-disabled") !== "true";
|
|
75
|
+
const retained = items.find(item => item.dataset.key === key && eligible(item));
|
|
76
|
+
const index = Math.max(0, previousIndex);
|
|
77
|
+
const nearest = items.slice(index).find(eligible) ?? items.slice(0, index).reverse().find(eligible);
|
|
78
|
+
(retained ?? nearest ?? element).focus({ preventScroll: true });
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
previousKeys = next;
|
|
82
|
+
return next;
|
|
83
|
+
});
|
|
84
|
+
const selected = () => options().get(value() ?? "");
|
|
85
|
+
const describedBy = () => [local.description ? `${id}-description` : "", local.error ? `${id}-error` : ""].filter(Boolean).join(" ") || undefined;
|
|
86
|
+
const change = (next: string | null) => {
|
|
87
|
+
if (next === value()) return;
|
|
88
|
+
if (local.value === undefined) setDraft(next);
|
|
89
|
+
local.onValueChange?.(next);
|
|
90
|
+
};
|
|
91
|
+
const requestChange = (next: string | null) => {
|
|
92
|
+
if (refreshingOptions || !next) return;
|
|
93
|
+
if (local.disabled || local.readOnly || (next && options().get(next)?.disabled)) return;
|
|
94
|
+
setOpen(false);
|
|
95
|
+
change(next || null);
|
|
96
|
+
};
|
|
97
|
+
const synchronize = () => { if (native) native.value = selected()?.value ?? ""; };
|
|
98
|
+
createEffect(() => { options(); value(); synchronize(); });
|
|
99
|
+
createEffect(() => { if (open()) onCleanup(theme.layers.register(id)); });
|
|
100
|
+
createEffect(() => { if (local.disabled) setOpen(false); });
|
|
101
|
+
onMount(() => {
|
|
102
|
+
const document = native?.ownerDocument;
|
|
103
|
+
const window = document?.defaultView;
|
|
104
|
+
if (!document || !window) return;
|
|
105
|
+
const timers = new Set<number>();
|
|
106
|
+
const reset = (event: Event) => {
|
|
107
|
+
if (event.target !== native?.form) return;
|
|
108
|
+
const timer = window.setTimeout(() => {
|
|
109
|
+
timers.delete(timer);
|
|
110
|
+
if (event.defaultPrevented) return;
|
|
111
|
+
if (local.value === undefined) change(initial);
|
|
112
|
+
synchronize();
|
|
113
|
+
}, 0);
|
|
114
|
+
timers.add(timer);
|
|
115
|
+
};
|
|
116
|
+
document.addEventListener("reset", reset, true);
|
|
117
|
+
onCleanup(() => { document.removeEventListener("reset", reset, true); for (const timer of timers) window.clearTimeout(timer); });
|
|
118
|
+
});
|
|
119
|
+
function TriggerButton(buttonProps: JSX.ButtonHTMLAttributes<HTMLButtonElement>): JSX.Element {
|
|
120
|
+
return <button {...buttonProps} aria-labelledby={`${id}-label ${id}-value`} aria-describedby={describedBy()} aria-invalid={local.error ? true : undefined} />;
|
|
121
|
+
}
|
|
122
|
+
return <Primitive.Root<string> {...others} id={local.id ?? `${id}-root`} multiple={false} class={cn("sheen-field sheen-select", local.class)}
|
|
123
|
+
options={keys()} optionValue={key => key} optionTextValue={key => options().get(key)?.label ?? key}
|
|
124
|
+
optionDisabled={key => options().get(key)?.disabled ?? false}
|
|
125
|
+
value={value()} defaultValue={value() ?? ""} onChange={requestChange} closeOnSelection={false} disallowEmptySelection
|
|
126
|
+
open={open()} onOpenChange={next => setOpen(next && !local.disabled)}
|
|
127
|
+
disabled={local.disabled ?? false} readOnly={local.readOnly ?? false} required={local.required ?? false}
|
|
128
|
+
validationState={local.error ? "invalid" : "valid"} placement="bottom-start" sameWidth gutter={4} virtualized>
|
|
129
|
+
<label id={`${id}-label`} for={`${id}-trigger`} class="sheen-field-label">{local.label}<Show when={local.required}><span aria-hidden="true"> *</span></Show></label>
|
|
130
|
+
<Primitive.Trigger as={TriggerButton} data-sheen-select-trigger="" id={`${id}-trigger`} ref={setTrigger} form={local.form} class="sheen-input sheen-select-trigger"
|
|
131
|
+
onClick={event => { if (!local.disabled && (!event.currentTarget.dataset.pointerType || event.detail === 0)) setOpen(current => !current); }}>
|
|
132
|
+
<span id={`${id}-value`} data-placeholder={!selected() || undefined}>{selected()?.label ?? local.placeholder ?? theme.messages().selectOption}</span>
|
|
133
|
+
<span aria-hidden="true">▾</span>
|
|
134
|
+
</Primitive.Trigger>
|
|
135
|
+
<select id={`${id}-native`} ref={native} class="sheen-select-native" aria-hidden="true" tabIndex={-1} name={local.name} form={local.form}
|
|
136
|
+
required={local.required} disabled={local.disabled} aria-label={local.label}
|
|
137
|
+
onInvalid={event => { event.preventDefault(); trigger()?.focus(); }} onChange={event => { requestChange(event.currentTarget.value || null); synchronize(); }}>
|
|
138
|
+
<option value="" selected={!selected()} />
|
|
139
|
+
<For each={keys()}>{key => <option value={key} disabled={options().get(key)?.disabled} selected={value() === key}>{options().get(key)?.label}</option>}</For>
|
|
140
|
+
</select>
|
|
141
|
+
<Show when={local.description}><span id={`${id}-description`} class="sheen-field-description">{local.description}</span></Show>
|
|
142
|
+
<Show when={local.error}><span id={`${id}-error`} class="sheen-field-error">{local.error}</span></Show>
|
|
143
|
+
<Show when={theme.portal()}>{target => <Primitive.Portal mount={target()}>
|
|
144
|
+
<Primitive.Content ref={setContent} data-kb-top-layer="true" aria-hidden={!open() || undefined} inert={!open()} class="sheen-select-content" style={{ "z-index": theme.layers.zIndex(id) }}>
|
|
145
|
+
<Primitive.Listbox<string> ref={element => { listbox = element; }} class="sheen-select-listbox" aria-labelledby={`${id}-label`}
|
|
146
|
+
onFocusIn={event => {
|
|
147
|
+
if (event.target !== event.currentTarget) return;
|
|
148
|
+
event.currentTarget.querySelector<HTMLElement>('[role=option][data-highlighted]:not([data-disabled])')?.focus({ preventScroll: true });
|
|
149
|
+
}}
|
|
150
|
+
scrollToItem={key => {
|
|
151
|
+
const item = [...listbox?.querySelectorAll<HTMLElement>("[role=option]") ?? []].find(element => element.dataset.key === key);
|
|
152
|
+
item?.focus({ preventScroll: true });
|
|
153
|
+
item?.scrollIntoView({ block: "nearest", inline: "nearest" });
|
|
154
|
+
}}>
|
|
155
|
+
{collection => <For each={keys()}>{key => {
|
|
156
|
+
// Kobalte's custom-children path is named virtualized. Render every option,
|
|
157
|
+
// keyed by value, so replacement collection records do not remount items.
|
|
158
|
+
const initialItem = untrack(() => collection().getItem(key));
|
|
159
|
+
if (!initialItem) throw new Error(`Select: missing collection item ${JSON.stringify(key)}`);
|
|
160
|
+
return <Primitive.Item item={collection().getItem(key) ?? initialItem} class="sheen-select-option">
|
|
161
|
+
<Primitive.ItemLabel>{options().get(key)?.label}</Primitive.ItemLabel>
|
|
162
|
+
<Show when={options().get(key)?.description}>{description => <Primitive.ItemDescription class="sheen-select-option-description">{description()}</Primitive.ItemDescription>}</Show>
|
|
163
|
+
<Primitive.ItemIndicator class="sheen-select-indicator" aria-hidden="true">✓</Primitive.ItemIndicator>
|
|
164
|
+
</Primitive.Item>;
|
|
165
|
+
}}</For>}
|
|
166
|
+
</Primitive.Listbox>
|
|
167
|
+
<Show when={!keys().length}><p class="sheen-select-empty">{theme.messages().noResults}</p></Show>
|
|
168
|
+
</Primitive.Content>
|
|
169
|
+
</Primitive.Portal>}</Show>
|
|
170
|
+
</Primitive.Root>;
|
|
171
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { splitProps } from "solid-js";
|
|
2
|
+
import type { JSX } from "solid-js";
|
|
3
|
+
import { cn } from "../utils/cn.ts";
|
|
4
|
+
|
|
5
|
+
export interface SeparatorProps extends JSX.HTMLAttributes<HTMLHRElement> {
|
|
6
|
+
orientation?: "horizontal" | "vertical";
|
|
7
|
+
decorative?: boolean;
|
|
8
|
+
}
|
|
9
|
+
export function Separator(props: SeparatorProps): JSX.Element {
|
|
10
|
+
const [local, others] = splitProps(props, ["class", "orientation", "decorative"]);
|
|
11
|
+
const orientation = () => local.orientation ?? "horizontal";
|
|
12
|
+
return <hr {...others} class={cn("sheen-separator", local.class)} data-orientation={orientation()} aria-orientation={orientation()} aria-hidden={local.decorative || undefined} />;
|
|
13
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { For, splitProps } from "solid-js";
|
|
2
|
+
import type { JSX } from "solid-js";
|
|
3
|
+
import { usePendingShortcut } from "./ShortcutProvider.tsx";
|
|
4
|
+
import { Kbd } from "./Typography.tsx";
|
|
5
|
+
import { useTheme } from "../theme/ThemeProvider.tsx";
|
|
6
|
+
import { cn } from "../utils/cn.ts";
|
|
7
|
+
|
|
8
|
+
export interface ShortcutPendingProps extends Omit<JSX.HTMLAttributes<HTMLDivElement>, "children"> {
|
|
9
|
+
label?: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** A persistent, single-line status slot; sequence updates never take focus. */
|
|
13
|
+
export function ShortcutPending(props: ShortcutPendingProps): JSX.Element {
|
|
14
|
+
const [local, others] = splitProps(props, ["label", "class"]);
|
|
15
|
+
const pending = usePendingShortcut();
|
|
16
|
+
const theme = useTheme();
|
|
17
|
+
return <div {...others} class={cn("sheen-shortcut-pending", local.class)} role="status" aria-live="polite" aria-atomic="true"
|
|
18
|
+
aria-label={local.label ?? theme.messages().pendingShortcut} data-pending={pending().length > 0 || undefined}>
|
|
19
|
+
<For each={pending()}>{(candidate, index) => <span><Kbd>{candidate.displayKeys}</Kbd>{": "}{candidate.label}{index() < pending().length - 1 ? "; " : ""}</span>}</For>
|
|
20
|
+
</div>;
|
|
21
|
+
}
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import { createContext, createEffect, createMemo, createSignal, getOwner, onCleanup, onMount, useContext } from "solid-js";
|
|
2
|
+
import type { Accessor, JSX } from "solid-js";
|
|
3
|
+
import { createShortcutRegistry } from "../utils/shortcut-registry.ts";
|
|
4
|
+
import { resolveShortcutPlatform } from "../utils/shortcut-platform.ts";
|
|
5
|
+
import type { PendingShortcut, ShortcutAction, ShortcutBinding, ShortcutDefinition } from "../utils/shortcut-registry.ts";
|
|
6
|
+
|
|
7
|
+
export interface ShortcutProviderProps {
|
|
8
|
+
children?: JSX.Element;
|
|
9
|
+
platform?: "mac" | "other";
|
|
10
|
+
development: boolean;
|
|
11
|
+
activeScopes?: readonly string[];
|
|
12
|
+
characterShortcuts?: boolean;
|
|
13
|
+
onError?: (error: unknown) => void;
|
|
14
|
+
}
|
|
15
|
+
const ShortcutContext = createContext<{ registry: Accessor<ReturnType<typeof createShortcutRegistry> | undefined>; pending: Accessor<readonly PendingShortcut[]>; bindings: Accessor<readonly ShortcutBinding[]>; characterShortcuts: Accessor<boolean>; acquireModal: (scope: string) => () => void; acquireFocusScope: (element: HTMLElement, scope: string) => () => void; acquireKeyboardOwner: (element: HTMLElement) => () => void }>();
|
|
16
|
+
|
|
17
|
+
/** One provider per document. Dialogs override application-owned base scopes while open. */
|
|
18
|
+
export function ShortcutProvider(props: ShortcutProviderProps): JSX.Element {
|
|
19
|
+
if (useContext(ShortcutContext)) throw new Error("ShortcutProvider cannot be nested; use shared scope IDs");
|
|
20
|
+
const report = (error: unknown) => { if (props.onError) props.onError(error); else queueMicrotask(() => { throw error; }); };
|
|
21
|
+
const [pending, setPending] = createSignal<readonly PendingShortcut[]>([]);
|
|
22
|
+
const [bindings, setBindings] = createSignal<readonly ShortcutBinding[]>([]);
|
|
23
|
+
const baseScopes = createMemo(() => props.activeScopes ?? ["global"]);
|
|
24
|
+
const [modals, setModals] = createSignal<readonly { id: number; scope: string }[]>([]);
|
|
25
|
+
let nextModal = 0;
|
|
26
|
+
const activeScopes = createMemo(() => { const modal = modals().at(-1); return modal ? [modal.scope] : baseScopes(); });
|
|
27
|
+
const acquireModal = (scope: string) => {
|
|
28
|
+
if (!scope.trim()) throw new Error("Dialog shortcut scopes require a nonempty ID");
|
|
29
|
+
const id = ++nextModal;
|
|
30
|
+
setModals(current => {
|
|
31
|
+
if (current.some(modal => modal.scope === scope)) throw new Error(`Dialog shortcut scope already active: ${scope}`);
|
|
32
|
+
return [...current, { id, scope }];
|
|
33
|
+
});
|
|
34
|
+
let removed = false;
|
|
35
|
+
return () => { if (removed) return; removed = true; setModals(current => current.filter(modal => modal.id !== id)); };
|
|
36
|
+
};
|
|
37
|
+
const characterShortcuts = createMemo(() => props.characterShortcuts !== false);
|
|
38
|
+
const development = props.development;
|
|
39
|
+
const makeRegistry = (platform: "mac" | "other") => createShortcutRegistry({ platform, development, onError: report, onPending: value => setPending(value), onChange: value => setBindings(value) });
|
|
40
|
+
const [registry, setRegistry] = createSignal(props.platform === undefined ? undefined : makeRegistry(props.platform));
|
|
41
|
+
const focusScopes = new WeakMap<EventTarget, string>();
|
|
42
|
+
const scopeElements = new Map<string, HTMLElement>();
|
|
43
|
+
const keyboardOwners = new WeakSet<EventTarget>();
|
|
44
|
+
const acquireKeyboardOwner = (element: HTMLElement) => {
|
|
45
|
+
keyboardOwners.add(element);
|
|
46
|
+
registry()?.cancelPending();
|
|
47
|
+
return () => { keyboardOwners.delete(element); registry()?.cancelPending(); };
|
|
48
|
+
};
|
|
49
|
+
const acquireFocusScope = (element: HTMLElement, scope: string) => {
|
|
50
|
+
if (!scope.trim() || scope === "global") throw new Error("ShortcutScope requires a nonempty, non-global scope ID");
|
|
51
|
+
if (scopeElements.has(scope)) throw new Error(`ShortcutScope already mounted: ${scope}`);
|
|
52
|
+
scopeElements.set(scope, element);
|
|
53
|
+
focusScopes.set(element, scope);
|
|
54
|
+
registry()?.cancelPending();
|
|
55
|
+
let removed = false;
|
|
56
|
+
return () => {
|
|
57
|
+
if (removed) return;
|
|
58
|
+
removed = true;
|
|
59
|
+
scopeElements.delete(scope);
|
|
60
|
+
focusScopes.delete(element);
|
|
61
|
+
registry()?.cancelPending();
|
|
62
|
+
};
|
|
63
|
+
};
|
|
64
|
+
createEffect(() => { activeScopes(); characterShortcuts(); registry()?.cancelPending(); });
|
|
65
|
+
onCleanup(() => registry()?.dispose());
|
|
66
|
+
onMount(() => {
|
|
67
|
+
if (!registry()) setRegistry(makeRegistry(resolveShortcutPlatform(navigator.platform)));
|
|
68
|
+
const cancelPending = () => registry()?.cancelPending();
|
|
69
|
+
const handle = (event: KeyboardEvent) => {
|
|
70
|
+
if (event.defaultPrevented || event.getModifierState("AltGraph")) { cancelPending(); return; }
|
|
71
|
+
const path = event.composedPath();
|
|
72
|
+
if (!event.ctrlKey && !event.metaKey && path.some(target => keyboardOwners.has(target))) { cancelPending(); return; }
|
|
73
|
+
const focusedScopes = path.flatMap(target => { const scope = focusScopes.get(target); return scope ? [scope] : []; }).reverse();
|
|
74
|
+
const scopes = modals().length ? activeScopes() : [...new Set([...activeScopes().filter(scope => !focusedScopes.includes(scope)), ...focusedScopes])];
|
|
75
|
+
const editing = path.some(target => target instanceof HTMLElement &&
|
|
76
|
+
(target.matches("input, textarea, select, [role=textbox]") || target.isContentEditable));
|
|
77
|
+
try {
|
|
78
|
+
registry()?.dispatch({ key: event.key, ctrlKey: event.ctrlKey, metaKey: event.metaKey, altKey: event.altKey, shiftKey: event.shiftKey,
|
|
79
|
+
editing, characterShortcuts: characterShortcuts(), composing: event.isComposing || event.key === "Process", repeat: event.repeat }, scopes, () => event.preventDefault());
|
|
80
|
+
} catch (error) {
|
|
81
|
+
report(error);
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
document.addEventListener("keydown", handle);
|
|
85
|
+
document.addEventListener("focusin", cancelPending);
|
|
86
|
+
document.addEventListener("pointerdown", cancelPending, true);
|
|
87
|
+
document.addEventListener("compositionstart", cancelPending);
|
|
88
|
+
window.addEventListener("blur", cancelPending);
|
|
89
|
+
onCleanup(() => {
|
|
90
|
+
document.removeEventListener("keydown", handle);
|
|
91
|
+
document.removeEventListener("focusin", cancelPending);
|
|
92
|
+
document.removeEventListener("pointerdown", cancelPending, true);
|
|
93
|
+
document.removeEventListener("compositionstart", cancelPending);
|
|
94
|
+
window.removeEventListener("blur", cancelPending);
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
return <ShortcutContext.Provider value={{ registry, pending, bindings, characterShortcuts, acquireModal, acquireFocusScope, acquireKeyboardOwner }}>{props.children}</ShortcutContext.Provider>;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** Register in the nearest provider and release the binding with its Solid owner. */
|
|
101
|
+
export function useShortcut(definition: ShortcutDefinition): void {
|
|
102
|
+
const context = useContext(ShortcutContext);
|
|
103
|
+
if (!context || !getOwner()) throw new Error("useShortcut requires an owned ShortcutProvider context");
|
|
104
|
+
createEffect(() => { const registry = context.registry(); if (registry) onCleanup(registry.register({ ...definition })); });
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** Internal control binding. Outside a provider the control remains usable without a shortcut. */
|
|
108
|
+
export function useShortcutAction(action: Accessor<ShortcutAction | undefined>, run: () => void): Accessor<ShortcutBinding | undefined> {
|
|
109
|
+
if (!getOwner()) throw new Error("shortcut actions require an owned component context");
|
|
110
|
+
const context = useContext(ShortcutContext);
|
|
111
|
+
if (!context) return () => undefined;
|
|
112
|
+
createEffect(() => {
|
|
113
|
+
const registry = context.registry();
|
|
114
|
+
const value = action();
|
|
115
|
+
if (!registry || !value) return;
|
|
116
|
+
onCleanup(registry.register({
|
|
117
|
+
keys: value.keys,
|
|
118
|
+
label: value.label,
|
|
119
|
+
group: value.group,
|
|
120
|
+
scope: value.scope ?? "global",
|
|
121
|
+
...(value.developmentOnly === undefined ? {} : { developmentOnly: value.developmentOnly }),
|
|
122
|
+
run,
|
|
123
|
+
}));
|
|
124
|
+
});
|
|
125
|
+
return createMemo(() => {
|
|
126
|
+
const value = action();
|
|
127
|
+
if (!value) return undefined;
|
|
128
|
+
const scope = value.scope ?? "global";
|
|
129
|
+
return context.bindings().findLast(binding => binding.keys === value.keys && binding.scope === scope && binding.label === value.label && binding.group === value.group);
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** Read immutable pending candidates to render contextual, localized sequence feedback. */
|
|
134
|
+
export function usePendingShortcut(): Accessor<readonly PendingShortcut[]> {
|
|
135
|
+
const context = useContext(ShortcutContext);
|
|
136
|
+
if (!context || !getOwner()) throw new Error("usePendingShortcut requires an owned ShortcutProvider context");
|
|
137
|
+
return context.pending;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** Observe all registered bindings, including inactive scopes and same-scope shadows. */
|
|
141
|
+
export function useShortcutBindings(): Accessor<readonly ShortcutBinding[]> {
|
|
142
|
+
const context = useContext(ShortcutContext);
|
|
143
|
+
if (!context || !getOwner()) throw new Error("useShortcutBindings requires an owned ShortcutProvider context");
|
|
144
|
+
return context.bindings;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** Execute the current winning registration by its introspected binding ID. */
|
|
148
|
+
export function useShortcutCommands(): { readonly bindings: Accessor<readonly ShortcutBinding[]>; readonly run: (id: number) => boolean } {
|
|
149
|
+
const context = useContext(ShortcutContext);
|
|
150
|
+
if (!context || !getOwner()) throw new Error("useShortcutCommands requires an owned ShortcutProvider context");
|
|
151
|
+
return { bindings: context.bindings, run: id => context.registry()?.run(id) ?? false };
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export function useCharacterShortcuts(): Accessor<boolean> {
|
|
155
|
+
const context = useContext(ShortcutContext);
|
|
156
|
+
if (!context || !getOwner()) throw new Error("useCharacterShortcuts requires an owned ShortcutProvider context");
|
|
157
|
+
return context.characterShortcuts;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** Optional integration for modal primitives; standalone dialogs need no shortcut provider. */
|
|
161
|
+
export function useModalShortcutScope(scope: Accessor<string>, active: Accessor<boolean>): void {
|
|
162
|
+
const context = useContext(ShortcutContext);
|
|
163
|
+
if (!context) return;
|
|
164
|
+
createEffect(() => { if (active()) onCleanup(context.acquireModal(scope())); });
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** Internal DOM-bound scope registration. Debug attributes alone do not activate scopes. */
|
|
168
|
+
export function useFocusShortcutScope(scope: Accessor<string>, element: Accessor<HTMLElement | undefined>): void {
|
|
169
|
+
const context = useContext(ShortcutContext);
|
|
170
|
+
if (!context || !getOwner()) throw new Error("ShortcutScope requires an owned ShortcutProvider context");
|
|
171
|
+
createEffect(() => { const node = element(); if (node) onCleanup(context.acquireFocusScope(node, scope())); });
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** Optional integration for widgets whose own typeahead/navigation consumes unmodified keys. */
|
|
175
|
+
export function useShortcutKeyboardOwner(element: Accessor<HTMLElement | undefined>, active: Accessor<boolean>): void {
|
|
176
|
+
const context = useContext(ShortcutContext);
|
|
177
|
+
if (!context) return;
|
|
178
|
+
createEffect(() => { const node = element(); if (active() && node) onCleanup(context.acquireKeyboardOwner(node)); });
|
|
179
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { createSignal, splitProps } from "solid-js";
|
|
2
|
+
import type { JSX } from "solid-js";
|
|
3
|
+
import { useFocusShortcutScope } from "./ShortcutProvider.tsx";
|
|
4
|
+
import { cn } from "../utils/cn.ts";
|
|
5
|
+
|
|
6
|
+
export interface ShortcutScopeProps extends JSX.HTMLAttributes<HTMLDivElement> {
|
|
7
|
+
scope: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/** Non-focusable by default. Descendant keyboard events activate this view/pane scope. */
|
|
11
|
+
export function ShortcutScope(props: ShortcutScopeProps): JSX.Element {
|
|
12
|
+
const [local, others] = splitProps(props, ["scope", "ref", "class", "children"]);
|
|
13
|
+
const [element, setElement] = createSignal<HTMLDivElement>();
|
|
14
|
+
useFocusShortcutScope(() => local.scope, element);
|
|
15
|
+
return <div {...others} ref={node => { setElement(node); if (typeof local.ref === "function") local.ref(node); }}
|
|
16
|
+
class={cn("sheen-shortcut-scope", local.class)} data-sheen-shortcut-scope={local.scope}>{local.children}</div>;
|
|
17
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { For, Show, createMemo } from "solid-js";
|
|
2
|
+
import type { JSX } from "solid-js";
|
|
3
|
+
import { Dialog } from "./Dialog.tsx";
|
|
4
|
+
import type { DialogProps } from "./Dialog.tsx";
|
|
5
|
+
import { Heading, Kbd, Text } from "./Typography.tsx";
|
|
6
|
+
import { useCharacterShortcuts, useShortcutBindings } from "./ShortcutProvider.tsx";
|
|
7
|
+
import { useTheme } from "../theme/ThemeProvider.tsx";
|
|
8
|
+
|
|
9
|
+
export type ShortcutSheetProps = Omit<DialogProps, "children">;
|
|
10
|
+
|
|
11
|
+
/** Generated inventory. The application owns opening; Dialog owns modal scope activation. */
|
|
12
|
+
export function ShortcutSheet(props: ShortcutSheetProps): JSX.Element {
|
|
13
|
+
const bindings = useShortcutBindings();
|
|
14
|
+
const characters = useCharacterShortcuts();
|
|
15
|
+
const theme = useTheme();
|
|
16
|
+
const records = createMemo(() => new Map(bindings().map(binding => [binding.id, binding])));
|
|
17
|
+
const groups = createMemo(() => [...new Set(bindings().map(binding => binding.group))]);
|
|
18
|
+
return <Dialog {...props}>
|
|
19
|
+
<Text>{theme.messages().shortcutInventory}</Text>
|
|
20
|
+
<Show when={bindings().length} fallback={<Text>{theme.messages().noResults}</Text>}>
|
|
21
|
+
<For each={groups()}>{group => <section>
|
|
22
|
+
<Heading level={3}>{group}</Heading>
|
|
23
|
+
<dl class="sheen-shortcut-bindings">
|
|
24
|
+
<For each={bindings().filter(binding => binding.group === group).map(binding => binding.id)}>{id => <Show when={records().get(id)}>{binding => <div>
|
|
25
|
+
<dt><Text>{binding().label}</Text><Text tone="muted">{binding().scope}</Text></dt>
|
|
26
|
+
<dd><Kbd>{binding().displayKeys}</Kbd>
|
|
27
|
+
<Show when={binding().shadowed}><Text tone="muted">{theme.messages().shortcutShadowed}</Text></Show>
|
|
28
|
+
<Show when={binding().characterOnly && !characters()}><Text tone="muted">{theme.messages().shortcutDisabled}</Text></Show>
|
|
29
|
+
</dd>
|
|
30
|
+
</div>}</Show>}</For>
|
|
31
|
+
</dl>
|
|
32
|
+
</section>}</For>
|
|
33
|
+
</Show>
|
|
34
|
+
</Dialog>;
|
|
35
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { splitProps } from "solid-js";
|
|
2
|
+
import type { JSX } from "solid-js";
|
|
3
|
+
import { cn } from "../utils/cn.ts";
|
|
4
|
+
|
|
5
|
+
export interface SkeletonProps extends Omit<JSX.HTMLAttributes<HTMLSpanElement>, "children"> {
|
|
6
|
+
shape?: "text" | "rectangle" | "circle";
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function Skeleton(props: SkeletonProps): JSX.Element {
|
|
10
|
+
const [local, others] = splitProps(props, ["class", "shape"]);
|
|
11
|
+
return <span {...others} class={cn("sheen-skeleton", local.class)} data-shape={local.shape ?? "text"} aria-hidden="true" inert />;
|
|
12
|
+
}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { createSignal, createUniqueId, splitProps } from "solid-js";
|
|
2
|
+
import type { JSX } from "solid-js";
|
|
3
|
+
import { useTheme } from "../theme/ThemeProvider.tsx";
|
|
4
|
+
import { cn } from "../utils/cn.ts";
|
|
5
|
+
|
|
6
|
+
interface SharedSliderProps extends Omit<JSX.HTMLAttributes<HTMLDivElement>, "children" | "onChange"> {
|
|
7
|
+
label: string;
|
|
8
|
+
min?: number;
|
|
9
|
+
max?: number;
|
|
10
|
+
step?: number;
|
|
11
|
+
formatOptions?: Intl.NumberFormatOptions;
|
|
12
|
+
name?: string;
|
|
13
|
+
form?: string;
|
|
14
|
+
description?: string;
|
|
15
|
+
error?: string;
|
|
16
|
+
disabled?: boolean;
|
|
17
|
+
readOnly?: boolean;
|
|
18
|
+
required?: boolean;
|
|
19
|
+
orientation?: "horizontal" | "vertical";
|
|
20
|
+
inverted?: boolean;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface SliderProps extends SharedSliderProps {
|
|
24
|
+
value?: number;
|
|
25
|
+
defaultValue?: number;
|
|
26
|
+
onValueChange?: (value: number) => void;
|
|
27
|
+
onValueChangeEnd?: (value: number) => void;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface RangeSliderProps extends SharedSliderProps {
|
|
31
|
+
value?: readonly [number, number];
|
|
32
|
+
defaultValue?: readonly [number, number];
|
|
33
|
+
onValueChange?: (value: readonly [number, number]) => void;
|
|
34
|
+
onValueChangeEnd?: (value: readonly [number, number]) => void;
|
|
35
|
+
minStepsBetweenThumbs?: number;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
interface SliderFrameProps extends SharedSliderProps {
|
|
39
|
+
readonly values: readonly number[] | undefined;
|
|
40
|
+
readonly defaultValues: readonly number[];
|
|
41
|
+
readonly onChange: (values: readonly number[]) => void;
|
|
42
|
+
readonly onChangeEnd: (values: readonly number[]) => void;
|
|
43
|
+
readonly minStepsBetweenThumbs?: number;
|
|
44
|
+
readonly thumbLabels: readonly string[];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function validateRange(label: string, min: number, max: number, step: number, values: readonly number[], minimumSteps: number): void {
|
|
48
|
+
if (!label.trim()) throw new Error("Slider label must be nonempty");
|
|
49
|
+
if (!Number.isFinite(min) || !Number.isFinite(max) || min >= max) throw new Error("Slider min and max must be finite and increasing");
|
|
50
|
+
if (!Number.isFinite(step) || step <= 0) throw new Error("Slider step must be positive and finite");
|
|
51
|
+
if (!Number.isInteger(minimumSteps) || minimumSteps < 0) throw new Error("RangeSlider minStepsBetweenThumbs must be a nonnegative integer");
|
|
52
|
+
for (const value of values) if (!Number.isFinite(value) || value < min || value > max) throw new Error("Slider values must be finite and within range");
|
|
53
|
+
if (values.length === 2 && values[0] !== undefined && values[1] !== undefined && values[0] > values[1]) throw new Error("RangeSlider values must be increasing");
|
|
54
|
+
if (values.length === 2 && values[0] !== undefined && values[1] !== undefined && values[1] - values[0] < minimumSteps * step) throw new Error("RangeSlider values must satisfy minStepsBetweenThumbs");
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function pair(values: readonly number[]): readonly [number, number] {
|
|
58
|
+
const first = values[0], second = values[1];
|
|
59
|
+
if (values.length !== 2 || first === undefined || second === undefined) throw new Error("RangeSlider requires exactly two values");
|
|
60
|
+
return [first, second];
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function message(template: string, label: string): string {
|
|
64
|
+
return template.replace("{label}", label);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function SliderFrame(props: SliderFrameProps): JSX.Element {
|
|
68
|
+
const [local, others] = splitProps(props, ["id", "class", "label", "min", "max", "step", "formatOptions", "name", "form", "description", "error", "disabled", "readOnly", "required", "orientation", "inverted", "values", "defaultValues", "onChange", "onChangeEnd", "minStepsBetweenThumbs", "thumbLabels"]);
|
|
69
|
+
const theme = useTheme();
|
|
70
|
+
const min = () => local.min ?? 0;
|
|
71
|
+
const max = () => local.max ?? 100;
|
|
72
|
+
const step = () => local.step ?? 1;
|
|
73
|
+
const minimumSteps = () => local.minStepsBetweenThumbs ?? 0;
|
|
74
|
+
const orientation = () => local.orientation ?? "horizontal";
|
|
75
|
+
const formatter = () => new Intl.NumberFormat(theme.state().locale, local.formatOptions);
|
|
76
|
+
const [uncontrolled, setUncontrolled] = createSignal([...local.defaultValues]);
|
|
77
|
+
const current = (): readonly number[] => local.values ?? uncontrolled();
|
|
78
|
+
validateRange(local.label, min(), max(), step(), current(), minimumSteps());
|
|
79
|
+
const generatedId = createUniqueId();
|
|
80
|
+
const id = () => local.id ?? generatedId;
|
|
81
|
+
const labelId = () => `${id()}-label`;
|
|
82
|
+
const descriptionId = () => `${id()}-description`;
|
|
83
|
+
const errorId = () => `${id()}-error`;
|
|
84
|
+
const describedBy = (): string | undefined => [local.description ? descriptionId() : undefined, local.error ? errorId() : undefined].filter(value => value !== undefined).join(" ") || undefined;
|
|
85
|
+
const position = (value: number): number => {
|
|
86
|
+
const ratio = (value - min()) / (max() - min());
|
|
87
|
+
return (local.inverted ? 1 - ratio : ratio) * 100;
|
|
88
|
+
};
|
|
89
|
+
const bounds = (index: number): readonly [number, number] => {
|
|
90
|
+
const gap = minimumSteps() * step();
|
|
91
|
+
const values = current();
|
|
92
|
+
if (values.length !== 2) return [min(), max()];
|
|
93
|
+
if (index === 0) return [min(), (values[1] ?? max()) - gap];
|
|
94
|
+
return [(values[0] ?? min()) + gap, max()];
|
|
95
|
+
};
|
|
96
|
+
const propose = (index: number, raw: number): readonly number[] => {
|
|
97
|
+
if (local.disabled || local.readOnly || !Number.isFinite(raw)) return current();
|
|
98
|
+
const [low, high] = bounds(index);
|
|
99
|
+
const next = [...current()];
|
|
100
|
+
next[index] = Math.min(high, Math.max(low, raw));
|
|
101
|
+
if (local.values === undefined) setUncontrolled(next);
|
|
102
|
+
local.onChange(next);
|
|
103
|
+
return current();
|
|
104
|
+
};
|
|
105
|
+
const fillStyle = (): string => {
|
|
106
|
+
const positions = current().map(position);
|
|
107
|
+
const start = Math.min(...positions), end = Math.max(...positions);
|
|
108
|
+
if (orientation() === "vertical") return `inset-block-end:${current().length === 1 ? 0 : start}%;block-size:${current().length === 1 ? end : end - start}%`;
|
|
109
|
+
return `inset-inline-start:${current().length === 1 ? 0 : start}%;inline-size:${current().length === 1 ? end : end - start}%`;
|
|
110
|
+
};
|
|
111
|
+
return <div {...others} id={id()} class={cn("sheen-slider", local.class)} role="group" aria-labelledby={labelId()} aria-describedby={describedBy()} aria-invalid={local.error ? "true" : undefined}
|
|
112
|
+
data-orientation={orientation()} data-inverted={local.inverted ? "" : undefined} data-range={current().length === 2 ? "" : undefined}
|
|
113
|
+
data-disabled={local.disabled ? "" : undefined} data-invalid={local.error ? "" : undefined}>
|
|
114
|
+
<div class="sheen-slider-heading">
|
|
115
|
+
<span id={labelId()} class="sheen-slider-label">{local.label}{local.required ? <span aria-hidden="true"> *</span> : null}</span>
|
|
116
|
+
<span class="sheen-slider-value">{current().map(value => formatter().format(value)).join(" – ")}</span>
|
|
117
|
+
</div>
|
|
118
|
+
<div class="sheen-slider-track">
|
|
119
|
+
<span class="sheen-slider-fill" style={fillStyle()} />
|
|
120
|
+
{local.thumbLabels.map((label, index) => <input class="sheen-slider-input" type="range" role="slider" min={min()} max={max()} step={step()} value={current()[index]}
|
|
121
|
+
name={local.name} form={local.form} required={local.required} disabled={local.disabled} aria-label={label} aria-describedby={describedBy()}
|
|
122
|
+
aria-valuemin={bounds(index)[0]} aria-valuemax={bounds(index)[1]} aria-valuenow={current()[index]} aria-valuetext={formatter().format(current()[index] ?? min())}
|
|
123
|
+
aria-orientation={orientation()} aria-readonly={local.readOnly ? "true" : undefined} onInput={event => {
|
|
124
|
+
const accepted = propose(index, event.currentTarget.valueAsNumber);
|
|
125
|
+
event.currentTarget.value = String(accepted[index] ?? min());
|
|
126
|
+
}} onChange={() => local.onChangeEnd(current())} />)}
|
|
127
|
+
</div>
|
|
128
|
+
{local.description ? <p id={descriptionId()} class="sheen-slider-description">{local.description}</p> : null}
|
|
129
|
+
{local.error ? <p id={errorId()} class="sheen-slider-error">{local.error}</p> : null}
|
|
130
|
+
</div>;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** A single-value, locale-formatted slider. */
|
|
134
|
+
export function Slider(props: SliderProps): JSX.Element {
|
|
135
|
+
const [local, others] = splitProps(props, ["value", "defaultValue", "onValueChange", "onValueChangeEnd", "label"]);
|
|
136
|
+
const defaultValue = () => local.defaultValue ?? props.min ?? 0;
|
|
137
|
+
const values = (): readonly number[] | undefined => local.value === undefined ? undefined : [local.value];
|
|
138
|
+
return <SliderFrame {...others} label={local.label} values={values()} defaultValues={[defaultValue()]}
|
|
139
|
+
onChange={values => { const value = values[0]; if (value === undefined) throw new Error("Slider requires one value"); local.onValueChange?.(value); }}
|
|
140
|
+
onChangeEnd={values => { const value = values[0]; if (value === undefined) throw new Error("Slider requires one value"); local.onValueChangeEnd?.(value); }}
|
|
141
|
+
thumbLabels={[local.label]} />;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** A two-thumb slider whose ordered tuple cannot silently grow extra values. */
|
|
145
|
+
export function RangeSlider(props: RangeSliderProps): JSX.Element {
|
|
146
|
+
const [local, others] = splitProps(props, ["value", "defaultValue", "onValueChange", "onValueChangeEnd", "minStepsBetweenThumbs", "label"]);
|
|
147
|
+
const theme = useTheme();
|
|
148
|
+
const defaultValue = (): readonly [number, number] => local.defaultValue ?? [props.min ?? 0, props.max ?? 100];
|
|
149
|
+
return <SliderFrame {...others} label={local.label} values={local.value} defaultValues={defaultValue()}
|
|
150
|
+
onChange={values => local.onValueChange?.(pair(values))} onChangeEnd={values => local.onValueChangeEnd?.(pair(values))}
|
|
151
|
+
minStepsBetweenThumbs={local.minStepsBetweenThumbs ?? 0}
|
|
152
|
+
thumbLabels={[message(theme.messages().minimumValue, local.label), message(theme.messages().maximumValue, local.label)]} />;
|
|
153
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Show, splitProps } from "solid-js";
|
|
2
|
+
import type { JSX } from "solid-js";
|
|
3
|
+
import { useTheme } from "../theme/ThemeProvider.tsx";
|
|
4
|
+
import { cn } from "../utils/cn.ts";
|
|
5
|
+
|
|
6
|
+
export interface SpinnerProps extends Omit<JSX.HTMLAttributes<HTMLSpanElement>, "children"> {
|
|
7
|
+
size?: "sm" | "md" | "lg";
|
|
8
|
+
label?: string;
|
|
9
|
+
decorative?: boolean;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function Spinner(props: SpinnerProps): JSX.Element {
|
|
13
|
+
const theme = useTheme();
|
|
14
|
+
const [local, others] = splitProps(props, ["class", "size", "label", "decorative"]);
|
|
15
|
+
return <span {...others} class={cn("sheen-spinner", local.class)} data-size={local.size ?? "md"} role={local.decorative ? undefined : "status"} aria-hidden={local.decorative || undefined} aria-atomic={local.decorative ? undefined : true}>
|
|
16
|
+
<span class="sheen-spinner-mark" aria-hidden="true" />
|
|
17
|
+
<Show when={!local.decorative}><span class="sheen-spinner-label">{local.label ?? theme.messages().loading}</span></Show>
|
|
18
|
+
</span>;
|
|
19
|
+
}
|