@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,170 @@
|
|
|
1
|
+
import CorvuResizable from "@corvu/resizable";
|
|
2
|
+
import { createContext, createMemo, createSignal, splitProps, useContext, type JSX } from "solid-js";
|
|
3
|
+
import { cn } from "../utils/cn.ts";
|
|
4
|
+
|
|
5
|
+
export interface ResizablePersistence {
|
|
6
|
+
readonly initialSizes: readonly number[];
|
|
7
|
+
readonly save: (sizes: readonly number[]) => void | Promise<void>;
|
|
8
|
+
readonly onError: (error: unknown) => void;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface ResizableProps extends Omit<JSX.HTMLAttributes<HTMLDivElement>, "children" | "onChange"> {
|
|
12
|
+
readonly children: JSX.Element;
|
|
13
|
+
readonly orientation?: "horizontal" | "vertical";
|
|
14
|
+
readonly sizes?: readonly number[];
|
|
15
|
+
readonly defaultSizes?: readonly number[];
|
|
16
|
+
readonly onSizesChange?: (sizes: readonly number[]) => void;
|
|
17
|
+
readonly keyboardStep?: number;
|
|
18
|
+
readonly persistence?: ResizablePersistence;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface ResizablePanelProps extends Omit<JSX.HTMLAttributes<HTMLDivElement>, "children" | "onResize"> {
|
|
22
|
+
readonly children: JSX.Element;
|
|
23
|
+
readonly index: number;
|
|
24
|
+
readonly panelId?: string;
|
|
25
|
+
readonly initialSize?: number;
|
|
26
|
+
readonly minSize?: number;
|
|
27
|
+
readonly maxSize?: number;
|
|
28
|
+
readonly collapsible?: boolean;
|
|
29
|
+
readonly collapsedSize?: number;
|
|
30
|
+
readonly collapseThreshold?: number;
|
|
31
|
+
readonly onResize?: (size: number) => void;
|
|
32
|
+
readonly onCollapse?: (size: number) => void;
|
|
33
|
+
readonly onExpand?: (size: number) => void;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface ResizableHandleProps extends Omit<JSX.ButtonHTMLAttributes<HTMLButtonElement>, "children"> {
|
|
37
|
+
readonly label: string;
|
|
38
|
+
readonly index: number;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const ResizableInitialSizes = createContext<{ readonly at: (index: number) => number; readonly sizes: () => readonly number[] }>();
|
|
42
|
+
|
|
43
|
+
function validatedSizes(value: readonly number[], name: string): number[] {
|
|
44
|
+
if (value.length < 2) throw new Error(`${name} requires at least two panel sizes`);
|
|
45
|
+
let total = 0;
|
|
46
|
+
const result = value.map((size, index) => {
|
|
47
|
+
if (!Number.isFinite(size) || size < 0 || size > 1) throw new Error(`${name} panel ${index + 1} must be between zero and one`);
|
|
48
|
+
total += size;
|
|
49
|
+
return size;
|
|
50
|
+
});
|
|
51
|
+
if (Math.abs(total - 1) > 0.001) throw new Error(`${name} must sum to one`);
|
|
52
|
+
return result;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function optionalSize(value: number | undefined, name: string): number | undefined {
|
|
56
|
+
if (value === undefined) return undefined;
|
|
57
|
+
if (!Number.isFinite(value) || value < 0 || value > 1) throw new Error(`${name} must be between zero and one`);
|
|
58
|
+
return value;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function sameSizes(left: readonly number[], right: readonly number[]): boolean {
|
|
62
|
+
return left.length === right.length && left.every((size, index) => Math.abs(size - (right[index] ?? Number.NaN)) < 0.000001);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function Resizable(props: ResizableProps): JSX.Element {
|
|
66
|
+
const [local, rest] = splitProps(props, ["class", "style", "children", "orientation", "sizes", "defaultSizes", "onSizesChange", "keyboardStep", "persistence", "ref"]);
|
|
67
|
+
const [saving, setSaving] = createSignal(false);
|
|
68
|
+
let pendingSaves = 0;
|
|
69
|
+
const initialSizes = () => validatedSizes(local.defaultSizes ?? local.persistence?.initialSizes ?? [0.5, 0.5], "Resizable initial sizes");
|
|
70
|
+
const [acceptedSizes, setAcceptedSizes] = createSignal<readonly number[]>(initialSizes());
|
|
71
|
+
const effectiveSizes = createMemo(() => validatedSizes(local.sizes ?? acceptedSizes(), "Resizable sizes"));
|
|
72
|
+
const keyboardDelta = () => {
|
|
73
|
+
const value = local.keyboardStep ?? 0.05;
|
|
74
|
+
if (!Number.isFinite(value) || value <= 0 || value > 1) throw new Error("Resizable keyboardStep must be greater than zero and at most one");
|
|
75
|
+
return value;
|
|
76
|
+
};
|
|
77
|
+
const changed = (sizes: number[]) => {
|
|
78
|
+
if (sizes.length !== initialSizes().length) return;
|
|
79
|
+
const accepted = Object.freeze(validatedSizes(sizes, "Resizable result"));
|
|
80
|
+
if (sameSizes(accepted, effectiveSizes())) return;
|
|
81
|
+
if (local.sizes === undefined) setAcceptedSizes(accepted);
|
|
82
|
+
local.onSizesChange?.(accepted);
|
|
83
|
+
const persistence = local.persistence;
|
|
84
|
+
if (!persistence) return;
|
|
85
|
+
try {
|
|
86
|
+
const result = persistence.save(accepted);
|
|
87
|
+
if (result instanceof Promise) {
|
|
88
|
+
pendingSaves += 1;
|
|
89
|
+
setSaving(true);
|
|
90
|
+
void result.catch(persistence.onError).finally(() => {
|
|
91
|
+
pendingSaves -= 1;
|
|
92
|
+
if (pendingSaves === 0) setSaving(false);
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
} catch (error) {
|
|
96
|
+
persistence.onError(error);
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
const shared = () => ({
|
|
100
|
+
...(local.ref === undefined ? {} : { ref: local.ref }),
|
|
101
|
+
...(local.style === undefined ? {} : { style: local.style }),
|
|
102
|
+
});
|
|
103
|
+
const initialContext = { at: (index: number) => {
|
|
104
|
+
if (!Number.isSafeInteger(index) || index < 0 || index >= initialSizes().length) throw new Error("ResizablePanel index must address an initial size");
|
|
105
|
+
const size = initialSizes()[index];
|
|
106
|
+
if (size === undefined) throw new Error("ResizablePanel index must address an initial size");
|
|
107
|
+
return size;
|
|
108
|
+
}, sizes: effectiveSizes };
|
|
109
|
+
return <CorvuResizable {...rest} {...shared()} sizes={effectiveSizes()} class={cn("sheen-resizable", local.class)} orientation={local.orientation ?? "horizontal"}
|
|
110
|
+
initialSizes={initialSizes()} keyboardDelta={keyboardDelta()} onSizesChange={changed} data-saving={saving() || undefined}>
|
|
111
|
+
<ResizableInitialSizes.Provider value={initialContext}>{local.children}</ResizableInitialSizes.Provider>
|
|
112
|
+
</CorvuResizable>;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export function ResizablePanel(props: ResizablePanelProps): JSX.Element {
|
|
116
|
+
const initial = useContext(ResizableInitialSizes);
|
|
117
|
+
const [local, rest] = splitProps(props, ["class", "style", "children", "index", "panelId", "initialSize", "minSize", "maxSize", "collapsible", "collapsedSize", "collapseThreshold", "onResize", "onCollapse", "onExpand", "ref"]);
|
|
118
|
+
const options = () => {
|
|
119
|
+
const initialSize = optionalSize(local.initialSize ?? initial?.at(local.index), "ResizablePanel initialSize");
|
|
120
|
+
const minSize = optionalSize(local.minSize, "ResizablePanel minSize");
|
|
121
|
+
const maxSize = optionalSize(local.maxSize, "ResizablePanel maxSize");
|
|
122
|
+
const collapsedSize = optionalSize(local.collapsedSize, "ResizablePanel collapsedSize");
|
|
123
|
+
const collapseThreshold = optionalSize(local.collapseThreshold, "ResizablePanel collapseThreshold");
|
|
124
|
+
return {
|
|
125
|
+
...(local.ref === undefined ? {} : { ref: local.ref }),
|
|
126
|
+
...(local.style === undefined ? {} : { style: local.style }),
|
|
127
|
+
...(local.panelId === undefined ? {} : { panelId: local.panelId }),
|
|
128
|
+
...(initialSize === undefined ? {} : { initialSize }),
|
|
129
|
+
...(minSize === undefined ? {} : { minSize }),
|
|
130
|
+
...(maxSize === undefined ? {} : { maxSize }),
|
|
131
|
+
...(local.collapsible === undefined ? {} : { collapsible: local.collapsible }),
|
|
132
|
+
...(collapsedSize === undefined ? {} : { collapsedSize }),
|
|
133
|
+
...(collapseThreshold === undefined ? {} : { collapseThreshold }),
|
|
134
|
+
...(local.onResize === undefined ? {} : { onResize: local.onResize }),
|
|
135
|
+
...(local.onCollapse === undefined ? {} : { onCollapse: local.onCollapse }),
|
|
136
|
+
...(local.onExpand === undefined ? {} : { onExpand: local.onExpand }),
|
|
137
|
+
};
|
|
138
|
+
};
|
|
139
|
+
return <CorvuResizable.Panel {...rest} {...options()} class={cn("sheen-resizable-panel", local.class)}>
|
|
140
|
+
{local.children}
|
|
141
|
+
</CorvuResizable.Panel>;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export function ResizableHandle(props: ResizableHandleProps): JSX.Element {
|
|
145
|
+
const initial = useContext(ResizableInitialSizes);
|
|
146
|
+
if (!initial) throw new Error("ResizableHandle requires a Resizable parent");
|
|
147
|
+
const [local, rest] = splitProps(props, ["class", "style", "label", "index", "ref", "disabled", "onBlur", "onFocus", "onKeyDown", "onKeyUp", "onMouseEnter", "onMouseLeave", "onPointerDown"]);
|
|
148
|
+
if (!local.label.trim()) throw new Error("ResizableHandle requires a nonempty label");
|
|
149
|
+
const percentage = () => {
|
|
150
|
+
const sizes = initial.sizes();
|
|
151
|
+
if (!Number.isSafeInteger(local.index) || local.index < 0 || local.index >= sizes.length - 1) throw new Error("ResizableHandle index must address the preceding panel");
|
|
152
|
+
return Math.round(sizes.slice(0, local.index + 1).reduce((total, size) => total + size, 0) * 100);
|
|
153
|
+
};
|
|
154
|
+
const shared = () => ({
|
|
155
|
+
...(local.ref === undefined ? {} : { ref: local.ref }),
|
|
156
|
+
...(local.style === undefined ? {} : { style: local.style }),
|
|
157
|
+
...(local.disabled === undefined ? {} : { disabled: local.disabled }),
|
|
158
|
+
...(local.onBlur === undefined ? {} : { onBlur: local.onBlur }),
|
|
159
|
+
...(local.onFocus === undefined ? {} : { onFocus: local.onFocus }),
|
|
160
|
+
...(local.onKeyDown === undefined ? {} : { onKeyDown: local.onKeyDown }),
|
|
161
|
+
...(local.onKeyUp === undefined ? {} : { onKeyUp: local.onKeyUp }),
|
|
162
|
+
...(local.onMouseEnter === undefined ? {} : { onMouseEnter: local.onMouseEnter }),
|
|
163
|
+
...(local.onMouseLeave === undefined ? {} : { onMouseLeave: local.onMouseLeave }),
|
|
164
|
+
...(local.onPointerDown === undefined ? {} : { onPointerDown: local.onPointerDown }),
|
|
165
|
+
});
|
|
166
|
+
return <CorvuResizable.Handle {...rest} {...shared()} class={cn("sheen-resizable-handle", local.class)} aria-label={local.label}
|
|
167
|
+
aria-valuemin={0} aria-valuemax={100} aria-valuenow={percentage()} aria-valuetext={`${percentage()}%`}>
|
|
168
|
+
<span aria-hidden="true" />
|
|
169
|
+
</CorvuResizable.Handle>;
|
|
170
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { Show, createEffect, createMemo, createSignal, onCleanup, onMount, splitProps } from "solid-js";
|
|
2
|
+
import type { JSX } from "solid-js";
|
|
3
|
+
import { cn } from "../utils/cn.ts";
|
|
4
|
+
|
|
5
|
+
export interface ScrollAreaProps extends JSX.HTMLAttributes<HTMLDivElement> {
|
|
6
|
+
label: string;
|
|
7
|
+
orientation?: "vertical" | "horizontal" | "both";
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function ScrollArea(props: ScrollAreaProps): JSX.Element {
|
|
11
|
+
const [local, others] = splitProps(props, ["label", "orientation", "class", "onScroll", "ref", "children"]);
|
|
12
|
+
let viewport: HTMLDivElement | undefined;
|
|
13
|
+
let content: HTMLDivElement | undefined;
|
|
14
|
+
let frame: number | undefined;
|
|
15
|
+
const [metrics, setMetrics] = createSignal({ x: 0, y: 0, width: 0, height: 0, scrollWidth: 0, scrollHeight: 0, left: 0, top: 0, rtl: false });
|
|
16
|
+
const [dragging, setDragging] = createSignal(false);
|
|
17
|
+
const measure = () => {
|
|
18
|
+
frame = undefined;
|
|
19
|
+
if (!viewport) return;
|
|
20
|
+
setMetrics({ x: viewport.offsetLeft + viewport.clientLeft, y: viewport.offsetTop + viewport.clientTop, width: viewport.clientWidth, height: viewport.clientHeight, scrollWidth: viewport.scrollWidth, scrollHeight: viewport.scrollHeight, left: Math.abs(viewport.scrollLeft), top: viewport.scrollTop, rtl: viewport.matches(":dir(rtl)") });
|
|
21
|
+
};
|
|
22
|
+
const schedule = () => { if (frame === undefined) frame = viewport?.ownerDocument.defaultView?.requestAnimationFrame(measure); };
|
|
23
|
+
const vertical = () => local.orientation !== "horizontal" && metrics().scrollHeight > metrics().height;
|
|
24
|
+
const horizontal = () => (local.orientation === "horizontal" || local.orientation === "both") && metrics().scrollWidth > metrics().width;
|
|
25
|
+
const geometry = (axis: "x" | "y") => {
|
|
26
|
+
const data = metrics();
|
|
27
|
+
const visible = axis === "y" ? data.height : data.width;
|
|
28
|
+
const total = axis === "y" ? data.scrollHeight : data.scrollWidth;
|
|
29
|
+
const length = Math.max(0, visible - ((axis === "y" ? horizontal() : vertical()) ? 8 : 0));
|
|
30
|
+
const size = Math.min(length, Math.max(20, total > 0 ? visible / total * length : length));
|
|
31
|
+
const maximum = Math.max(0, total - visible);
|
|
32
|
+
const position = axis === "y" ? data.top : data.left;
|
|
33
|
+
const fraction = maximum > 0 ? Math.min(1, Math.max(0, position / maximum)) : 0;
|
|
34
|
+
return { length, size, maximum, travel: length - size, offset: (axis === "x" && data.rtl ? 1 - fraction : fraction) * (length - size) };
|
|
35
|
+
};
|
|
36
|
+
const coordinate = (event: PointerEvent, track: HTMLDivElement, axis: "x" | "y") => {
|
|
37
|
+
const bounds = track.getBoundingClientRect();
|
|
38
|
+
const rendered = axis === "y" ? bounds.height : bounds.width;
|
|
39
|
+
const length = axis === "y" ? track.clientHeight : track.clientWidth;
|
|
40
|
+
const delta = axis === "y" ? event.clientY - bounds.top : event.clientX - bounds.left;
|
|
41
|
+
return rendered > 0 ? delta * length / rendered : 0;
|
|
42
|
+
};
|
|
43
|
+
let drag: { pointer: number; axis: "x" | "y"; grab: number; track: HTMLDivElement } | undefined;
|
|
44
|
+
const move = (event: PointerEvent, track: HTMLDivElement) => {
|
|
45
|
+
if (!drag || !viewport || event.pointerId !== drag.pointer) return;
|
|
46
|
+
const data = geometry(drag.axis);
|
|
47
|
+
const point = coordinate(event, track, drag.axis);
|
|
48
|
+
let fraction = data.travel > 0 ? Math.min(1, Math.max(0, (point - drag.grab) / data.travel)) : 0;
|
|
49
|
+
if (drag.axis === "x" && metrics().rtl) fraction = 1 - fraction;
|
|
50
|
+
if (drag.axis === "y") viewport.scrollTop = fraction * data.maximum;
|
|
51
|
+
else viewport.scrollLeft = fraction * data.maximum * (metrics().rtl ? -1 : 1);
|
|
52
|
+
schedule();
|
|
53
|
+
};
|
|
54
|
+
const start = (axis: "x" | "y", event: PointerEvent & { currentTarget: HTMLDivElement }) => {
|
|
55
|
+
if (event.button !== 0 || !viewport || drag) return;
|
|
56
|
+
event.preventDefault();
|
|
57
|
+
const data = geometry(axis);
|
|
58
|
+
const point = coordinate(event, event.currentTarget, axis);
|
|
59
|
+
drag = { pointer: event.pointerId, axis, grab: event.target === event.currentTarget ? data.size / 2 : point - data.offset, track: event.currentTarget };
|
|
60
|
+
event.currentTarget.setPointerCapture(event.pointerId);
|
|
61
|
+
setDragging(true);
|
|
62
|
+
move(event, event.currentTarget);
|
|
63
|
+
};
|
|
64
|
+
const stop = () => {
|
|
65
|
+
const previous = drag;
|
|
66
|
+
drag = undefined;
|
|
67
|
+
setDragging(false);
|
|
68
|
+
if (previous?.track.hasPointerCapture(previous.pointer)) previous.track.releasePointerCapture(previous.pointer);
|
|
69
|
+
};
|
|
70
|
+
const finish = (event: PointerEvent) => { if (event.pointerId === drag?.pointer) stop(); };
|
|
71
|
+
createEffect(() => {
|
|
72
|
+
if (dragging() && drag && !(drag.axis === "y" ? vertical() : horizontal())) stop();
|
|
73
|
+
});
|
|
74
|
+
onMount(() => {
|
|
75
|
+
measure();
|
|
76
|
+
const observer = new ResizeObserver(schedule);
|
|
77
|
+
if (viewport) observer.observe(viewport);
|
|
78
|
+
if (viewport?.parentElement) observer.observe(viewport.parentElement);
|
|
79
|
+
if (content) observer.observe(content);
|
|
80
|
+
const attributes = new MutationObserver(schedule);
|
|
81
|
+
let ancestor: HTMLElement | null | undefined = viewport;
|
|
82
|
+
while (ancestor) {
|
|
83
|
+
attributes.observe(ancestor, { attributes: true, attributeFilter: ["dir", "data-sheen-direction", "data-orientation", "class", "style"] });
|
|
84
|
+
ancestor = ancestor.parentElement;
|
|
85
|
+
}
|
|
86
|
+
onCleanup(() => { observer.disconnect(); attributes.disconnect(); });
|
|
87
|
+
});
|
|
88
|
+
const [scrolling, setScrolling] = createSignal(false);
|
|
89
|
+
let idle: ReturnType<typeof setTimeout> | undefined;
|
|
90
|
+
onCleanup(() => { stop(); if (idle !== undefined) clearTimeout(idle); if (frame !== undefined) viewport?.ownerDocument.defaultView?.cancelAnimationFrame(frame); });
|
|
91
|
+
const label = createMemo(() => {
|
|
92
|
+
if (!local.label.trim()) throw new Error("ScrollArea requires a nonempty accessible label");
|
|
93
|
+
return local.label;
|
|
94
|
+
});
|
|
95
|
+
const onScroll: JSX.EventHandler<HTMLDivElement, Event> = event => {
|
|
96
|
+
schedule();
|
|
97
|
+
setScrolling(true);
|
|
98
|
+
if (idle !== undefined) clearTimeout(idle);
|
|
99
|
+
idle = setTimeout(() => { idle = undefined; setScrolling(false); }, 800);
|
|
100
|
+
const handler = local.onScroll;
|
|
101
|
+
if (typeof handler === "function") handler(event);
|
|
102
|
+
else if (handler) handler[0](handler[1], event);
|
|
103
|
+
};
|
|
104
|
+
return <div class="sheen-scroll-frame" dir={others.dir} data-scrolling={scrolling() || dragging() || undefined} data-both={vertical() && horizontal() || undefined}>
|
|
105
|
+
<div role="region" {...others} ref={element => { viewport = element; if (typeof local.ref === "function") local.ref(element); }} aria-label={label()} tabIndex={others.tabIndex ?? 0} class={cn("sheen-scroll-area", local.class)} data-orientation={local.orientation ?? "vertical"} data-scrolling={scrolling() || undefined} onScroll={onScroll}>
|
|
106
|
+
<div ref={content} class="sheen-scroll-content">{local.children}</div>
|
|
107
|
+
</div>
|
|
108
|
+
<Show when={vertical()}><div aria-hidden="true" class="sheen-scroll-track" data-axis="y" style={{ left: `${metrics().x + (metrics().rtl ? 0 : metrics().width - 8)}px`, top: `${metrics().y}px`, height: `${geometry("y").length}px` }} onPointerDown={event => start("y", event)} onPointerMove={event => move(event, event.currentTarget)} onPointerUp={finish} onPointerCancel={finish} onLostPointerCapture={finish}>
|
|
109
|
+
<div class="sheen-scroll-thumb" style={{ height: `${geometry("y").size}px`, transform: `translateY(${geometry("y").offset}px)` }} />
|
|
110
|
+
</div></Show>
|
|
111
|
+
<Show when={horizontal()}><div aria-hidden="true" class="sheen-scroll-track" data-axis="x" style={{ left: `${metrics().x + (metrics().rtl && vertical() ? 8 : 0)}px`, top: `${metrics().y + metrics().height - 8}px`, width: `${geometry("x").length}px` }} onPointerDown={event => start("x", event)} onPointerMove={event => move(event, event.currentTarget)} onPointerUp={finish} onPointerCancel={finish} onLostPointerCapture={finish}>
|
|
112
|
+
<div class="sheen-scroll-thumb" style={{ width: `${geometry("x").size}px`, transform: `translateX(${geometry("x").offset}px)` }} />
|
|
113
|
+
</div></Show>
|
|
114
|
+
</div>;
|
|
115
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { createSignal, onCleanup, onMount, splitProps } from "solid-js";
|
|
2
|
+
import type { JSX } from "solid-js";
|
|
3
|
+
import { Button } from "./Button.tsx";
|
|
4
|
+
import { InputGroup } from "./InputGroup.tsx";
|
|
5
|
+
import type { InputProps } from "./Input.tsx";
|
|
6
|
+
import { useTheme } from "../theme/ThemeProvider.tsx";
|
|
7
|
+
import { cn } from "../utils/cn.ts";
|
|
8
|
+
import { useShortcutAction } from "./ShortcutProvider.tsx";
|
|
9
|
+
import type { ShortcutAction } from "../utils/shortcut-registry.ts";
|
|
10
|
+
|
|
11
|
+
export interface SearchInputProps extends Omit<InputProps, "type" | "value"> {
|
|
12
|
+
value?: string;
|
|
13
|
+
defaultValue?: string;
|
|
14
|
+
onValueChange?: (value: string) => void;
|
|
15
|
+
clearLabel?: string;
|
|
16
|
+
/** Optional registry-backed shortcut that focuses the search input. */
|
|
17
|
+
shortcut?: ShortcutAction | undefined;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function SearchInput(props: SearchInputProps): JSX.Element {
|
|
21
|
+
const theme = useTheme();
|
|
22
|
+
const [local, others] = splitProps(props, ["value", "defaultValue", "onValueChange", "clearLabel", "shortcut", "onInput", "onCompositionStart", "onCompositionEnd", "ref", "class"]);
|
|
23
|
+
const initialValue = local.defaultValue ?? "";
|
|
24
|
+
const [draft, setDraft] = createSignal(initialValue);
|
|
25
|
+
const value = () => local.value ?? draft();
|
|
26
|
+
const [composition, setComposition] = createSignal<string | undefined>();
|
|
27
|
+
const displayedValue = () => composition() ?? value();
|
|
28
|
+
let input: HTMLInputElement | undefined;
|
|
29
|
+
const update = (next: string) => {
|
|
30
|
+
if (local.value === undefined) setDraft(next);
|
|
31
|
+
local.onValueChange?.(next);
|
|
32
|
+
};
|
|
33
|
+
const binding = useShortcutAction(() => local.shortcut, () => input?.focus({ preventScroll: true }));
|
|
34
|
+
onMount(() => {
|
|
35
|
+
const element = input;
|
|
36
|
+
if (!element) return;
|
|
37
|
+
if (element.value !== value()) {
|
|
38
|
+
update(element.value);
|
|
39
|
+
if (element.value !== value()) element.value = value();
|
|
40
|
+
}
|
|
41
|
+
const view = element.ownerDocument.defaultView;
|
|
42
|
+
if (!view) return;
|
|
43
|
+
let disposed = false;
|
|
44
|
+
const pending = new Set<number>();
|
|
45
|
+
const reset = (event: Event) => {
|
|
46
|
+
if (event.target !== element.form) return;
|
|
47
|
+
// A microtask can run before the native reset default action finishes.
|
|
48
|
+
const task = view.setTimeout(() => {
|
|
49
|
+
pending.delete(task);
|
|
50
|
+
if (disposed || event.defaultPrevented) return;
|
|
51
|
+
setComposition(undefined);
|
|
52
|
+
if (local.value === undefined && draft() !== initialValue) update(initialValue);
|
|
53
|
+
if (element.value !== value()) element.value = value();
|
|
54
|
+
}, 0);
|
|
55
|
+
pending.add(task);
|
|
56
|
+
};
|
|
57
|
+
element.ownerDocument.addEventListener("reset", reset, true);
|
|
58
|
+
onCleanup(() => {
|
|
59
|
+
disposed = true;
|
|
60
|
+
for (const task of pending) view.clearTimeout(task);
|
|
61
|
+
pending.clear();
|
|
62
|
+
element.ownerDocument.removeEventListener("reset", reset, true);
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
const onInput: JSX.InputEventHandler<HTMLInputElement, InputEvent> = event => {
|
|
66
|
+
if (event.isComposing || composition() !== undefined) setComposition(event.currentTarget.value);
|
|
67
|
+
update(event.currentTarget.value);
|
|
68
|
+
const handler = local.onInput;
|
|
69
|
+
if (typeof handler === "function") handler(event);
|
|
70
|
+
else if (handler) handler[0](handler[1], event);
|
|
71
|
+
if (composition() === undefined && !event.isComposing && event.currentTarget.value !== value()) event.currentTarget.value = value();
|
|
72
|
+
};
|
|
73
|
+
const onCompositionStart: JSX.EventHandler<HTMLInputElement, CompositionEvent> = event => {
|
|
74
|
+
setComposition(event.currentTarget.value);
|
|
75
|
+
const handler = local.onCompositionStart;
|
|
76
|
+
if (typeof handler === "function") handler(event);
|
|
77
|
+
else if (handler) handler[0](handler[1], event);
|
|
78
|
+
};
|
|
79
|
+
const onCompositionEnd: JSX.EventHandler<HTMLInputElement, CompositionEvent> = event => {
|
|
80
|
+
const handler = local.onCompositionEnd;
|
|
81
|
+
if (typeof handler === "function") handler(event);
|
|
82
|
+
else if (handler) handler[0](handler[1], event);
|
|
83
|
+
setComposition(undefined);
|
|
84
|
+
if (event.currentTarget.value !== value()) event.currentTarget.value = value();
|
|
85
|
+
};
|
|
86
|
+
return <InputGroup {...others} type="search" class={cn("sheen-search-input", local.class)} data-sheen-shortcut={binding()?.displayKeys} value={displayedValue()} onInput={onInput} onCompositionStart={onCompositionStart} onCompositionEnd={onCompositionEnd} ref={element => {
|
|
87
|
+
input = element;
|
|
88
|
+
if (typeof local.ref === "function") local.ref(element);
|
|
89
|
+
}} endContent={<>
|
|
90
|
+
<Button class="sheen-search-input-clear" type="button" disabled={others.disabled || others.readOnly || others.readonly || composition() !== undefined || !value()} onClick={() => {
|
|
91
|
+
if (others.disabled || others.readOnly || others.readonly || composition() !== undefined || !value()) return;
|
|
92
|
+
update("");
|
|
93
|
+
if (input && input.value !== value()) input.value = value();
|
|
94
|
+
input?.focus();
|
|
95
|
+
}}>{local.clearLabel ?? theme.messages().clearSearch}</Button>
|
|
96
|
+
<span class="sheen-search-input-indicator" aria-hidden="true" />
|
|
97
|
+
</>} />;
|
|
98
|
+
}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { For, Show, createMemo, createSignal, createUniqueId, onCleanup, onMount, splitProps, untrack } from "solid-js";
|
|
2
|
+
import type { JSX } from "solid-js";
|
|
3
|
+
import { cn } from "../utils/cn.ts";
|
|
4
|
+
import { createChoiceOptions } from "./choice-options.ts";
|
|
5
|
+
|
|
6
|
+
export interface SegmentedControlOption {
|
|
7
|
+
readonly value: string;
|
|
8
|
+
readonly label: string;
|
|
9
|
+
readonly disabled?: boolean;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface SegmentedControlProps extends Omit<JSX.FieldsetHTMLAttributes<HTMLFieldSetElement>, "children" | "onChange" | "id" | "ref" | "name"> {
|
|
13
|
+
id?: string;
|
|
14
|
+
ref?: HTMLFieldSetElement | ((element: HTMLFieldSetElement) => void);
|
|
15
|
+
name: string;
|
|
16
|
+
label: string;
|
|
17
|
+
options: readonly SegmentedControlOption[];
|
|
18
|
+
value?: string | null;
|
|
19
|
+
defaultValue?: string | null;
|
|
20
|
+
onValueChange?: (value: string | null) => void;
|
|
21
|
+
description?: string;
|
|
22
|
+
error?: string;
|
|
23
|
+
readOnly?: boolean;
|
|
24
|
+
required?: boolean;
|
|
25
|
+
orientation?: "horizontal" | "vertical";
|
|
26
|
+
size?: "sm" | "md";
|
|
27
|
+
overflowBehavior?: "scroll" | "wrap";
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** A compact, native-radio single-selection control with roving focus. */
|
|
31
|
+
export function SegmentedControl(props: SegmentedControlProps): JSX.Element {
|
|
32
|
+
const generatedId = createUniqueId();
|
|
33
|
+
const [local, others] = splitProps(props, ["id", "ref", "class", "name", "label", "options", "value", "defaultValue", "onValueChange", "description", "error", "disabled", "readOnly", "required", "orientation", "size", "overflowBehavior", "form", "aria-label", "aria-labelledby", "aria-describedby"]);
|
|
34
|
+
const initialValue = local.defaultValue ?? null;
|
|
35
|
+
const [uncontrolled, setUncontrolled] = createSignal<string | null>(initialValue);
|
|
36
|
+
const value = () => local.value === undefined ? uncontrolled() : local.value;
|
|
37
|
+
let fieldset: HTMLFieldSetElement | undefined;
|
|
38
|
+
let disposed = false;
|
|
39
|
+
onCleanup(() => { disposed = true; });
|
|
40
|
+
const { byValue, keys } = createChoiceOptions(() => {
|
|
41
|
+
if (!local.name.trim()) throw new Error("SegmentedControl: name must be nonempty for native form submission");
|
|
42
|
+
for (const option of local.options) {
|
|
43
|
+
if (!option.value.trim()) throw new Error("SegmentedControl: option values must be nonempty; use null for no selection");
|
|
44
|
+
if (!option.label.trim()) throw new Error(`SegmentedControl: option ${JSON.stringify(option.value)} must have a nonempty label`);
|
|
45
|
+
}
|
|
46
|
+
return local.options;
|
|
47
|
+
}, () => fieldset, "SegmentedControl");
|
|
48
|
+
const tabStop = createMemo(() => {
|
|
49
|
+
const selected = value();
|
|
50
|
+
if (selected !== null && !byValue().get(selected)?.disabled) return selected;
|
|
51
|
+
return keys().find(key => !byValue().get(key)?.disabled);
|
|
52
|
+
});
|
|
53
|
+
const change = (next: string | null): void => {
|
|
54
|
+
if (next === value()) return;
|
|
55
|
+
if (local.value === undefined) setUncontrolled(next);
|
|
56
|
+
local.onValueChange?.(next);
|
|
57
|
+
};
|
|
58
|
+
const synchronize = (): void => {
|
|
59
|
+
if (disposed) return;
|
|
60
|
+
for (const input of fieldset?.querySelectorAll<HTMLInputElement>('input[type="radio"]') ?? []) input.checked = input.value === value();
|
|
61
|
+
};
|
|
62
|
+
const requestChange = (next: string): void => {
|
|
63
|
+
if (local.disabled || local.readOnly || byValue().get(next)?.disabled) {
|
|
64
|
+
synchronize();
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
change(next);
|
|
68
|
+
synchronize();
|
|
69
|
+
};
|
|
70
|
+
const moveFocus = (event: KeyboardEvent & { currentTarget: HTMLInputElement }, destination: "first" | "last" | 1 | -1): void => {
|
|
71
|
+
const root = fieldset;
|
|
72
|
+
if (!root || local.disabled) return;
|
|
73
|
+
const inputs = [...root.querySelectorAll<HTMLInputElement>('input[type="radio"]')].filter(input => !input.disabled);
|
|
74
|
+
if (inputs.length === 0) return;
|
|
75
|
+
const current = Math.max(0, inputs.indexOf(event.currentTarget));
|
|
76
|
+
const target = destination === "first" ? inputs[0] : destination === "last" ? inputs.at(-1) : inputs[(current + destination + inputs.length) % inputs.length];
|
|
77
|
+
if (!target) return;
|
|
78
|
+
event.preventDefault();
|
|
79
|
+
target.focus({ preventScroll: true });
|
|
80
|
+
target.scrollIntoView({ block: "nearest", inline: "nearest" });
|
|
81
|
+
if (!local.readOnly) target.click();
|
|
82
|
+
};
|
|
83
|
+
const onKeyDown = (event: KeyboardEvent & { currentTarget: HTMLInputElement }): void => {
|
|
84
|
+
if (event.key === "Home") moveFocus(event, "first");
|
|
85
|
+
else if (event.key === "End") moveFocus(event, "last");
|
|
86
|
+
else if (event.key === "ArrowUp") moveFocus(event, -1);
|
|
87
|
+
else if (event.key === "ArrowDown") moveFocus(event, 1);
|
|
88
|
+
else if (event.key === "ArrowLeft") moveFocus(event, event.currentTarget.closest('[dir="rtl"]') ? 1 : -1);
|
|
89
|
+
else if (event.key === "ArrowRight") moveFocus(event, event.currentTarget.closest('[dir="rtl"]') ? -1 : 1);
|
|
90
|
+
};
|
|
91
|
+
onMount(() => {
|
|
92
|
+
const element = fieldset;
|
|
93
|
+
const view = element?.ownerDocument.defaultView;
|
|
94
|
+
if (!element || !view) return;
|
|
95
|
+
const selected = element.querySelector<HTMLInputElement>('input[type="radio"]:checked');
|
|
96
|
+
if (!local.disabled && !local.readOnly && selected && !selected.disabled) change(selected.value);
|
|
97
|
+
synchronize();
|
|
98
|
+
const pending = new Set<number>();
|
|
99
|
+
const reset = (event: Event): void => {
|
|
100
|
+
if (event.target !== element.form) return;
|
|
101
|
+
const task = view.setTimeout(() => {
|
|
102
|
+
pending.delete(task);
|
|
103
|
+
if (event.defaultPrevented) return;
|
|
104
|
+
if (local.value === undefined) change(initialValue);
|
|
105
|
+
synchronize();
|
|
106
|
+
}, 0);
|
|
107
|
+
pending.add(task);
|
|
108
|
+
};
|
|
109
|
+
element.ownerDocument.addEventListener("reset", reset, true);
|
|
110
|
+
onCleanup(() => {
|
|
111
|
+
element.ownerDocument.removeEventListener("reset", reset, true);
|
|
112
|
+
for (const task of pending) view.clearTimeout(task);
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
const describedBy = () => [local.description ? `${generatedId}-description` : "", local.error ? `${generatedId}-error` : "", local["aria-describedby"]].filter(Boolean).join(" ") || undefined;
|
|
116
|
+
return <fieldset role="radiogroup" {...others} {...(local["aria-label"] === undefined ? {} : { "aria-label": local["aria-label"] })}
|
|
117
|
+
id={local.id ?? generatedId} ref={element => { fieldset = element; if (typeof local.ref === "function") local.ref(element); }} form={local.form}
|
|
118
|
+
class={cn("sheen-segmented-control", local.class)} aria-labelledby={local["aria-labelledby"] ?? `${generatedId}-label`} aria-describedby={describedBy()}
|
|
119
|
+
aria-invalid={local.error ? "true" : undefined} aria-readonly={local.readOnly || undefined} aria-orientation={local.orientation ?? "horizontal"}
|
|
120
|
+
data-invalid={local.error ? "" : undefined} data-readonly={local.readOnly || undefined} data-size={local.size ?? "sm"}
|
|
121
|
+
data-overflow={local.overflowBehavior ?? "scroll"} disabled={local.disabled ?? false}>
|
|
122
|
+
<legend id={`${generatedId}-label`} class="sheen-segmented-label">{local.label}<Show when={local.required}><span aria-hidden="true"> *</span></Show></legend>
|
|
123
|
+
<Show when={local.description}><p id={`${generatedId}-description`} class="sheen-segmented-description">{local.description}</p></Show>
|
|
124
|
+
<div class="sheen-segmented-options"><For each={keys()}>{key => {
|
|
125
|
+
const initialOption = untrack(() => byValue().get(key));
|
|
126
|
+
if (!initialOption) throw new Error("SegmentedControl: missing option during creation");
|
|
127
|
+
const option = () => byValue().get(key) ?? initialOption;
|
|
128
|
+
const itemId = createUniqueId();
|
|
129
|
+
return <label class="sheen-segmented-item" data-disabled={local.disabled || option().disabled || undefined}>
|
|
130
|
+
<input type="radio" class="sheen-segmented-input" id={`${itemId}-input`} name={local.name} form={local.form} value={key}
|
|
131
|
+
checked={value() === key} required={local.required} disabled={local.disabled || option().disabled} readOnly={local.readOnly}
|
|
132
|
+
aria-labelledby={`${itemId}-label`} aria-describedby={describedBy()}
|
|
133
|
+
tabIndex={!local.disabled && tabStop() === key ? 0 : -1} onKeyDown={onKeyDown} onClick={event => { if (local.readOnly) event.preventDefault(); }}
|
|
134
|
+
onChange={() => requestChange(key)} />
|
|
135
|
+
<span id={`${itemId}-label`} class="sheen-segmented-item-label">{option().label}</span>
|
|
136
|
+
</label>;
|
|
137
|
+
}}</For></div>
|
|
138
|
+
<Show when={local.error}><p id={`${generatedId}-error`} class="sheen-segmented-error">{local.error}</p></Show>
|
|
139
|
+
</fieldset>;
|
|
140
|
+
}
|