@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,31 @@
|
|
|
1
|
+
import * as Primitive from "@kobalte/core/tooltip";
|
|
2
|
+
import { splitProps } from "solid-js";
|
|
3
|
+
import type { JSX } from "solid-js";
|
|
4
|
+
import { createTooltipLayer, TooltipLayer } from "./tooltip-layer.tsx";
|
|
5
|
+
import { cn } from "../utils/cn.ts";
|
|
6
|
+
|
|
7
|
+
export interface LinkTooltipProps extends JSX.AnchorHTMLAttributes<HTMLAnchorElement> {
|
|
8
|
+
href: string;
|
|
9
|
+
content: string;
|
|
10
|
+
tooltipDisabled?: boolean;
|
|
11
|
+
placement?: "top" | "bottom" | "left" | "right";
|
|
12
|
+
openDelay?: number;
|
|
13
|
+
closeDelay?: number;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** Native navigation with contextual help; disabling the tooltip never disables the link. */
|
|
17
|
+
export function LinkTooltip(props: LinkTooltipProps): JSX.Element {
|
|
18
|
+
const [local, link] = splitProps(props, ["content", "tooltipDisabled", "placement", "openDelay", "closeDelay", "children", "ref", "class", "aria-describedby", "type"]);
|
|
19
|
+
const Anchor = (attributes: JSX.AnchorHTMLAttributes<HTMLAnchorElement>) => <a {...attributes} type={local.type} />;
|
|
20
|
+
let trigger: HTMLAnchorElement | undefined;
|
|
21
|
+
const state = createTooltipLayer(() => trigger, () => local.tooltipDisabled ?? false);
|
|
22
|
+
return <Primitive.Root open={state.open()} onOpenChange={state.change} disabled={local.tooltipDisabled ?? false}
|
|
23
|
+
placement={local.placement ?? "top"} openDelay={local.openDelay ?? 500} closeDelay={local.closeDelay ?? 100} gutter={6}>
|
|
24
|
+
<Primitive.Trigger as={Anchor} {...link} class={cn("sheen-link-tooltip", local.class)}
|
|
25
|
+
aria-describedby={[local["aria-describedby"], state.open() ? `${state.id}-content` : undefined].filter(Boolean).join(" ") || undefined}
|
|
26
|
+
onClick={link.onClick ?? (() => {})} onFocus={link.onFocus ?? (() => {})} onBlur={link.onBlur ?? (() => {})}
|
|
27
|
+
onPointerDown={link.onPointerDown ?? (() => {})} onPointerEnter={link.onPointerEnter ?? (() => {})} onPointerLeave={link.onPointerLeave ?? (() => {})}
|
|
28
|
+
ref={(element: HTMLAnchorElement) => { trigger = element; if (typeof local.ref === "function") local.ref(element); }}>{local.children}</Primitive.Trigger>
|
|
29
|
+
<TooltipLayer state={state} content={local.content} />
|
|
30
|
+
</Primitive.Root>;
|
|
31
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { Show, children, createContext, createMemo, splitProps, useContext } from "solid-js";
|
|
2
|
+
import type { JSX } from "solid-js";
|
|
3
|
+
import { cn } from "../utils/cn.ts";
|
|
4
|
+
import { createLinkFocusRecovery } from "./link-focus.ts";
|
|
5
|
+
import { LinkTooltip } from "./LinkTooltip.tsx";
|
|
6
|
+
|
|
7
|
+
export interface NavListProps extends JSX.HTMLAttributes<HTMLElement> {
|
|
8
|
+
label: string;
|
|
9
|
+
}
|
|
10
|
+
export interface NavItemProps extends Omit<JSX.AnchorHTMLAttributes<HTMLAnchorElement>, "children" | "aria-current"> {
|
|
11
|
+
href: string;
|
|
12
|
+
label: string;
|
|
13
|
+
current?: boolean;
|
|
14
|
+
icon?: JSX.Element;
|
|
15
|
+
badge?: JSX.Element;
|
|
16
|
+
tooltip?: string;
|
|
17
|
+
tooltipDisabled?: boolean;
|
|
18
|
+
/** Optional sibling controls. Interactive content is never nested inside the destination link. */
|
|
19
|
+
actions?: JSX.Element;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const NavOwner = createContext(false);
|
|
23
|
+
|
|
24
|
+
export function NavList(props: NavListProps): JSX.Element {
|
|
25
|
+
const [local, others] = splitProps(props, ["label", "class", "children", "ref"]);
|
|
26
|
+
let root: HTMLElement | undefined;
|
|
27
|
+
createLinkFocusRecovery(() => root);
|
|
28
|
+
const label = createMemo(() => {
|
|
29
|
+
if (!local.label.trim()) throw new Error("NavList requires a nonempty accessible label");
|
|
30
|
+
return local.label;
|
|
31
|
+
});
|
|
32
|
+
return <nav {...others} ref={element => { root = element; if (typeof local.ref === "function") local.ref(element); }} tabIndex={others.tabIndex ?? -1} aria-label={label()} class={cn("sheen-nav-list", local.class)}>
|
|
33
|
+
<ul><NavOwner.Provider value={true}>{local.children}</NavOwner.Provider></ul>
|
|
34
|
+
</nav>;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function NavItem(props: NavItemProps): JSX.Element {
|
|
38
|
+
if (!useContext(NavOwner)) throw new Error("NavItem requires a NavList owner");
|
|
39
|
+
const [local, others] = splitProps(props, ["href", "label", "current", "class", "icon", "badge", "tooltip", "tooltipDisabled", "actions"]);
|
|
40
|
+
const icon = children(() => local.icon);
|
|
41
|
+
const badge = children(() => local.badge);
|
|
42
|
+
const actions = children(() => local.actions);
|
|
43
|
+
const href = createMemo(() => {
|
|
44
|
+
if (!local.href.trim() || !local.label.trim()) throw new Error("NavItem requires nonempty href and label");
|
|
45
|
+
return local.href;
|
|
46
|
+
});
|
|
47
|
+
const content = children(() => <>
|
|
48
|
+
<Show when={icon()}><span class="sheen-nav-item-icon" aria-hidden="true">{icon()}</span></Show>
|
|
49
|
+
<span class="sheen-nav-item-label">{local.label}</span>
|
|
50
|
+
<Show when={badge()}><span class="sheen-nav-item-badge">{badge()}</span></Show>
|
|
51
|
+
</>);
|
|
52
|
+
return <li class="sheen-nav-item-row" data-current={local.current || undefined} data-has-actions={actions() != null || undefined}>
|
|
53
|
+
<Show when={local.tooltip !== undefined} fallback={<a {...others} href={href()} aria-current={local.current ? "page" : undefined} class={cn("sheen-nav-item", local.class)}>{content()}</a>}>
|
|
54
|
+
<LinkTooltip {...others} href={href()} content={local.tooltip ?? ""} tooltipDisabled={local.tooltipDisabled ?? false} aria-current={local.current ? "page" : undefined} class={cn("sheen-nav-item", local.class)}>{content()}</LinkTooltip>
|
|
55
|
+
</Show>
|
|
56
|
+
<Show when={actions() != null}><span class="sheen-nav-item-actions">{actions()}</span></Show>
|
|
57
|
+
</li>;
|
|
58
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Show, splitProps } from "solid-js";
|
|
2
|
+
import type { JSX } from "solid-js";
|
|
3
|
+
import { cn } from "../utils/cn.ts";
|
|
4
|
+
|
|
5
|
+
export interface CalloutProps extends JSX.HTMLAttributes<HTMLDivElement> {
|
|
6
|
+
tone?: "info" | "success" | "warning" | "danger";
|
|
7
|
+
heading?: string;
|
|
8
|
+
}
|
|
9
|
+
export type AlertProps = CalloutProps;
|
|
10
|
+
|
|
11
|
+
export function Callout(props: CalloutProps): JSX.Element {
|
|
12
|
+
const [local, others] = splitProps(props, ["class", "tone", "heading", "children"]);
|
|
13
|
+
return <div {...others} class={cn("sheen-notice", local.class)} data-tone={local.tone ?? "info"} role="note">
|
|
14
|
+
<Show when={local.heading}><strong class="sheen-notice-heading">{local.heading}</strong></Show>
|
|
15
|
+
<div class="sheen-notice-content">{local.children}</div>
|
|
16
|
+
</div>;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function Alert(props: AlertProps): JSX.Element {
|
|
20
|
+
const [local, others] = splitProps(props, ["class", "tone", "heading", "children"]);
|
|
21
|
+
return <div {...others} class={cn("sheen-notice", local.class)} data-tone={local.tone ?? "danger"} role="alert" aria-atomic="true">
|
|
22
|
+
<Show when={local.heading}><strong class="sheen-notice-heading">{local.heading}</strong></Show>
|
|
23
|
+
<div class="sheen-notice-content">{local.children}</div>
|
|
24
|
+
</div>;
|
|
25
|
+
}
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { createSignal, createUniqueId, onMount, 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 NumberFieldProps extends Omit<JSX.HTMLAttributes<HTMLDivElement>, "children" | "onChange"> {
|
|
7
|
+
label: string;
|
|
8
|
+
value?: number | null;
|
|
9
|
+
defaultValue?: number | null;
|
|
10
|
+
onValueChange?: (value: number | null) => void;
|
|
11
|
+
min?: number;
|
|
12
|
+
max?: number;
|
|
13
|
+
step?: number;
|
|
14
|
+
largeStep?: number;
|
|
15
|
+
formatOptions?: Intl.NumberFormatOptions;
|
|
16
|
+
name?: string;
|
|
17
|
+
form?: string;
|
|
18
|
+
description?: string;
|
|
19
|
+
error?: string;
|
|
20
|
+
disabled?: boolean;
|
|
21
|
+
readOnly?: boolean;
|
|
22
|
+
required?: boolean;
|
|
23
|
+
inputRef?: HTMLInputElement | ((element: HTMLInputElement) => void);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function validateRange(min: number, max: number, step: number, largeStep: number | undefined): void {
|
|
27
|
+
if (!Number.isFinite(min) || !Number.isFinite(max) || min >= max) throw new Error("NumberField min and max must be finite and increasing");
|
|
28
|
+
if (!Number.isFinite(step) || step <= 0) throw new Error("NumberField step must be positive and finite");
|
|
29
|
+
if (largeStep !== undefined && (!Number.isFinite(largeStep) || largeStep <= 0)) throw new Error("NumberField largeStep must be positive and finite");
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function message(template: string, label: string): string {
|
|
33
|
+
return template.replace("{label}", label);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function decimalPlaces(value: number): number {
|
|
37
|
+
const match = value.toString().match(/(?:\.(\d+))?(?:e-([0-9]+))?$/iu);
|
|
38
|
+
return Math.min(12, Math.max(match?.[1]?.length ?? 0, Number(match?.[2] ?? 0)));
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function stepValue(value: number, offset: number, min: number, max: number, step: number): number {
|
|
42
|
+
const scale = 10 ** Math.max(decimalPlaces(step), decimalPlaces(min));
|
|
43
|
+
const stepped = Math.round((value + offset) * scale) / scale;
|
|
44
|
+
return Math.min(max, Math.max(min, stepped));
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
type ParsedNumber = number | null | undefined;
|
|
48
|
+
|
|
49
|
+
function createParser(locale: string, options: Intl.NumberFormatOptions | undefined): (value: string) => ParsedNumber {
|
|
50
|
+
const formatter = new Intl.NumberFormat(locale, options);
|
|
51
|
+
const digitFormatter = new Intl.NumberFormat(locale, { useGrouping: false });
|
|
52
|
+
const digits = Array.from({ length: 10 }, (_, digit) => digitFormatter.format(digit));
|
|
53
|
+
const removable: string[] = [];
|
|
54
|
+
let decimal = ".", minus = "-", plus = "+";
|
|
55
|
+
for (const part of formatter.formatToParts(-12345.6)) {
|
|
56
|
+
if (part.type === "decimal") decimal = part.value;
|
|
57
|
+
else if (part.type === "minusSign") minus = part.value;
|
|
58
|
+
else if (part.type === "plusSign") plus = part.value;
|
|
59
|
+
else if (part.type !== "integer" && part.type !== "fraction") removable.push(part.value);
|
|
60
|
+
}
|
|
61
|
+
const percent = options?.style === "percent";
|
|
62
|
+
return value => {
|
|
63
|
+
let normalized = value.trim();
|
|
64
|
+
if (!normalized) return null;
|
|
65
|
+
digits.forEach((digit, index) => { normalized = normalized.replaceAll(digit, String(index)); });
|
|
66
|
+
removable.forEach(part => { normalized = normalized.replaceAll(part, ""); });
|
|
67
|
+
normalized = normalized.replaceAll(decimal, ".").replaceAll(minus, "-").replaceAll("−", "-").replaceAll(plus, "+").replaceAll(" ", "");
|
|
68
|
+
if (!normalized || normalized === "-" || normalized === "+" || normalized === "." || normalized === "-." || normalized === "+.") return undefined;
|
|
69
|
+
const parsed = Number(normalized);
|
|
70
|
+
return Number.isFinite(parsed) ? parsed / (percent ? 100 : 1) : undefined;
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** A locale-aware spinbutton with numeric app state and raw native form submission. */
|
|
75
|
+
export function NumberField(props: NumberFieldProps): JSX.Element {
|
|
76
|
+
const [local, others] = splitProps(props, ["id", "class", "label", "value", "defaultValue", "onValueChange", "min", "max", "step", "largeStep", "formatOptions", "name", "form", "description", "error", "disabled", "readOnly", "required", "inputRef"]);
|
|
77
|
+
const theme = useTheme();
|
|
78
|
+
const generatedId = createUniqueId();
|
|
79
|
+
const id = () => local.id ?? generatedId;
|
|
80
|
+
const inputId = () => `${id()}-input`;
|
|
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 minimum = () => local.min ?? Number.MIN_SAFE_INTEGER;
|
|
86
|
+
const maximum = () => local.max ?? Number.MAX_SAFE_INTEGER;
|
|
87
|
+
const increment = () => local.step ?? 1;
|
|
88
|
+
const formatter = () => new Intl.NumberFormat(theme.state().locale, local.formatOptions);
|
|
89
|
+
const parser = () => createParser(theme.state().locale, local.formatOptions);
|
|
90
|
+
const [uncontrolled, setUncontrolled] = createSignal<number | null>(local.defaultValue ?? null);
|
|
91
|
+
const current = () => local.value === undefined ? uncontrolled() : local.value;
|
|
92
|
+
const formatted = (value: number | null) => value === null ? "" : formatter().format(value);
|
|
93
|
+
const [draft, setDraft] = createSignal(formatted(current()));
|
|
94
|
+
const [focused, setFocused] = createSignal(false);
|
|
95
|
+
let input: HTMLInputElement | undefined;
|
|
96
|
+
let focusedParser: ((value: string) => ParsedNumber) | undefined;
|
|
97
|
+
const validate = (): void => {
|
|
98
|
+
if (!local.label.trim()) throw new Error("NumberField label must be nonempty");
|
|
99
|
+
validateRange(minimum(), maximum(), increment(), local.largeStep);
|
|
100
|
+
for (const [name, value] of [["value", local.value], ["defaultValue", local.defaultValue]] satisfies readonly (readonly [string, number | null | undefined])[]) {
|
|
101
|
+
if (value !== undefined && value !== null && (!Number.isFinite(value) || value < minimum() || value > maximum())) throw new Error(`NumberField ${name} must be finite and within range`);
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
validate();
|
|
105
|
+
const displayed = () => focused() ? draft() : formatted(current());
|
|
106
|
+
const publish = (value: number | null, formatDraft: boolean): void => {
|
|
107
|
+
if (local.value === undefined) setUncontrolled(value);
|
|
108
|
+
local.onValueChange?.(value);
|
|
109
|
+
if (formatDraft) setDraft(formatted(current()));
|
|
110
|
+
};
|
|
111
|
+
const accepted = (value: ParsedNumber): value is number | null => value === null ? !local.required : value !== undefined && value >= minimum() && value <= maximum();
|
|
112
|
+
const commitDraft = (): void => {
|
|
113
|
+
const parsed = (focusedParser ?? parser())(draft());
|
|
114
|
+
if (accepted(parsed)) publish(parsed, true);
|
|
115
|
+
else setDraft(formatted(current()));
|
|
116
|
+
};
|
|
117
|
+
const vary = (offset: number): void => {
|
|
118
|
+
if (local.disabled || local.readOnly) return;
|
|
119
|
+
const base = current() ?? Math.min(maximum(), Math.max(minimum(), 0));
|
|
120
|
+
publish(stepValue(base, offset, minimum(), maximum(), increment()), true);
|
|
121
|
+
};
|
|
122
|
+
const assignInput = (element: HTMLInputElement): void => {
|
|
123
|
+
input = element;
|
|
124
|
+
if (typeof local.inputRef === "function") local.inputRef(element);
|
|
125
|
+
};
|
|
126
|
+
onMount(() => {
|
|
127
|
+
if (!input) return;
|
|
128
|
+
const active = input.ownerDocument.activeElement === input;
|
|
129
|
+
setFocused(active);
|
|
130
|
+
if (active) focusedParser = parser();
|
|
131
|
+
if (input.value !== formatted(current())) {
|
|
132
|
+
setDraft(input.value);
|
|
133
|
+
const parsed = parser()(input.value);
|
|
134
|
+
if (accepted(parsed)) publish(parsed, false);
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
return <div {...others} id={id()} class={cn("sheen-field sheen-number-field", local.class)} role="group" data-invalid={local.error ? "" : undefined} data-disabled={local.disabled ? "" : undefined}>
|
|
138
|
+
<label id={labelId()} class="sheen-field-label" for={inputId()}>{local.label}{local.required ? <span aria-hidden="true"> *</span> : null}</label>
|
|
139
|
+
<div class="sheen-number-field-control">
|
|
140
|
+
<input ref={assignInput} id={inputId()} class="sheen-input sheen-number-field-input" type="text" role="spinbutton" inputMode="decimal" value={displayed()}
|
|
141
|
+
aria-labelledby={labelId()} aria-describedby={describedBy()} aria-valuemin={minimum()} aria-valuemax={maximum()} aria-valuenow={current() ?? undefined}
|
|
142
|
+
aria-valuetext={current() === null ? undefined : formatted(current())} aria-invalid={local.error ? "true" : undefined} required={local.required}
|
|
143
|
+
disabled={local.disabled} readOnly={local.readOnly} form={local.form}
|
|
144
|
+
onFocus={() => { focusedParser = parser(); setFocused(true); }} onInput={event => {
|
|
145
|
+
setDraft(event.currentTarget.value);
|
|
146
|
+
const parsed = (focusedParser ?? parser())(event.currentTarget.value);
|
|
147
|
+
if (accepted(parsed)) publish(parsed, false);
|
|
148
|
+
}} onBlur={() => { setFocused(false); commitDraft(); focusedParser = undefined; }} onKeyDown={event => {
|
|
149
|
+
if (event.key === "Enter") { event.preventDefault(); commitDraft(); }
|
|
150
|
+
else if (event.key === "Escape") { event.preventDefault(); setDraft(formatted(current())); event.currentTarget.select(); }
|
|
151
|
+
else if (event.key === "ArrowUp" || event.key === "ArrowDown" || event.key === "PageUp" || event.key === "PageDown") {
|
|
152
|
+
if (local.readOnly) return;
|
|
153
|
+
event.preventDefault();
|
|
154
|
+
const amount = event.key.startsWith("Page") ? (local.largeStep ?? increment() * 10) : increment();
|
|
155
|
+
vary(event.key === "ArrowDown" || event.key === "PageDown" ? -amount : amount);
|
|
156
|
+
} else if (event.key === "Home" && !local.readOnly) { event.preventDefault(); publish(minimum(), true); }
|
|
157
|
+
else if (event.key === "End" && !local.readOnly) { event.preventDefault(); publish(maximum(), true); }
|
|
158
|
+
}} />
|
|
159
|
+
<div class="sheen-number-field-steps">
|
|
160
|
+
<button type="button" class="sheen-number-field-step" aria-label={message(theme.messages().decreaseValue, local.label)} disabled={local.disabled || local.readOnly} onClick={() => vary(-increment())}><span aria-hidden="true">−</span></button>
|
|
161
|
+
<button type="button" class="sheen-number-field-step" aria-label={message(theme.messages().increaseValue, local.label)} disabled={local.disabled || local.readOnly} onClick={() => vary(increment())}><span aria-hidden="true">+</span></button>
|
|
162
|
+
</div>
|
|
163
|
+
</div>
|
|
164
|
+
{local.name ? <input type="hidden" name={local.name} form={local.form} value={current() ?? ""} /> : null}
|
|
165
|
+
{local.description ? <p id={descriptionId()} class="sheen-field-description">{local.description}</p> : null}
|
|
166
|
+
{local.error ? <p id={errorId()} class="sheen-field-error">{local.error}</p> : null}
|
|
167
|
+
</div>;
|
|
168
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { For, createMemo, onCleanup, onMount, splitProps } from "solid-js";
|
|
2
|
+
import type { JSX } from "solid-js";
|
|
3
|
+
import { Button } from "./Button.tsx";
|
|
4
|
+
import { useTheme } from "../theme/ThemeProvider.tsx";
|
|
5
|
+
import { useNumberFormatter } from "../theme/intl.ts";
|
|
6
|
+
import { cn } from "../utils/cn.ts";
|
|
7
|
+
|
|
8
|
+
export interface PaginationProps extends Omit<JSX.HTMLAttributes<HTMLElement>, "children" | "onChange"> {
|
|
9
|
+
pageIndex: number;
|
|
10
|
+
pageCount: number;
|
|
11
|
+
onPageChange: (pageIndex: number) => void;
|
|
12
|
+
pending?: boolean;
|
|
13
|
+
/** Disable page requests without claiming that a request is still in progress. */
|
|
14
|
+
disabled?: boolean;
|
|
15
|
+
label?: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Controlled accepted-page state; requesting a page does not optimistically announce it. */
|
|
19
|
+
export function Pagination(props: PaginationProps): JSX.Element {
|
|
20
|
+
const [local, others] = splitProps(props, ["pageIndex", "pageCount", "onPageChange", "pending", "disabled", "label", "class", "ref"]);
|
|
21
|
+
let root: HTMLElement | undefined;
|
|
22
|
+
onMount(() => {
|
|
23
|
+
const element = root;
|
|
24
|
+
if (!element) return;
|
|
25
|
+
const document = element.ownerDocument;
|
|
26
|
+
let focused: Element | undefined;
|
|
27
|
+
const record = () => { focused = element.contains(document.activeElement) ? document.activeElement ?? undefined : undefined; };
|
|
28
|
+
record();
|
|
29
|
+
document.addEventListener("focusin", record);
|
|
30
|
+
const observer = new MutationObserver(() => {
|
|
31
|
+
if (!focused || element.contains(focused) || document.activeElement !== document.body || !element.isConnected) return;
|
|
32
|
+
const target = element.querySelector<HTMLElement>('[aria-current="page"]') ?? element.querySelector<HTMLButtonElement>("button");
|
|
33
|
+
if (target && !target.closest("[inert], [hidden]") && target.getClientRects().length) target.focus({ preventScroll: true });
|
|
34
|
+
});
|
|
35
|
+
observer.observe(element, { childList: true, subtree: true });
|
|
36
|
+
onCleanup(() => { observer.disconnect(); document.removeEventListener("focusin", record); });
|
|
37
|
+
});
|
|
38
|
+
const theme = useTheme();
|
|
39
|
+
const format = useNumberFormatter();
|
|
40
|
+
const state = createMemo(() => {
|
|
41
|
+
if (!Number.isSafeInteger(local.pageCount) || local.pageCount < 0) throw new Error("Pagination pageCount must be a nonnegative safe integer");
|
|
42
|
+
if (!Number.isSafeInteger(local.pageIndex) || local.pageIndex < 0 || local.pageIndex >= Math.max(1, local.pageCount)) throw new Error("Pagination pageIndex must identify an accepted page, or zero for empty results");
|
|
43
|
+
return { index: local.pageIndex, count: local.pageCount };
|
|
44
|
+
});
|
|
45
|
+
const pages = createMemo(() => {
|
|
46
|
+
const { index, count } = state();
|
|
47
|
+
const start = Math.max(0, Math.min(index - 2, count - 5));
|
|
48
|
+
return Array.from({ length: Math.min(5, count) }, (_, offset) => start + offset);
|
|
49
|
+
});
|
|
50
|
+
const unavailable = (index: number) => local.pending || local.disabled || index < 0 || index >= state().count || index === state().index;
|
|
51
|
+
const request = (index: number) => { if (!unavailable(index)) local.onPageChange(index); };
|
|
52
|
+
const pageLabel = (index: number) => theme.messages().pageLabel.replaceAll("{page}", format().format(index + 1));
|
|
53
|
+
const summary = () => theme.messages().pageStatus
|
|
54
|
+
.replaceAll("{page}", format().format(state().count ? state().index + 1 : 0))
|
|
55
|
+
.replaceAll("{pages}", format().format(state().count));
|
|
56
|
+
return <nav {...others} ref={element => { root = element; if (typeof local.ref === "function") local.ref(element); }} class={cn("sheen-pagination", local.class)} aria-label={local.label ?? theme.messages().pagination} aria-busy={local.pending || undefined} aria-disabled={local.disabled || undefined} data-pending={local.pending ? "" : undefined}>
|
|
57
|
+
<Button aria-disabled={unavailable(0) || undefined} onClick={() => request(0)}>{theme.messages().firstPage}</Button>
|
|
58
|
+
<Button aria-disabled={unavailable(state().index - 1) || undefined} onClick={() => request(state().index - 1)}>{theme.messages().previousPage}</Button>
|
|
59
|
+
<For each={pages()}>{index => <Button aria-label={pageLabel(index)} aria-current={index === state().index ? "page" : undefined} aria-disabled={unavailable(index) || undefined} onClick={() => request(index)}>{format().format(index + 1)}</Button>}</For>
|
|
60
|
+
<Button aria-disabled={unavailable(state().index + 1) || undefined} onClick={() => request(state().index + 1)}>{theme.messages().nextPage}</Button>
|
|
61
|
+
<Button aria-disabled={unavailable(state().count - 1) || undefined} onClick={() => request(state().count - 1)}>{theme.messages().lastPage}</Button>
|
|
62
|
+
<output aria-live="polite" aria-atomic="true">{summary()}</output>
|
|
63
|
+
</nav>;
|
|
64
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import * as Primitive from "@kobalte/core/popover";
|
|
2
|
+
import { Show, createEffect, createMemo, createSignal, createUniqueId, onCleanup } from "solid-js";
|
|
3
|
+
import type { JSX, ParentProps } from "solid-js";
|
|
4
|
+
import { Button } from "./Button.tsx";
|
|
5
|
+
import { useTheme } from "../theme/ThemeProvider.tsx";
|
|
6
|
+
import { cn } from "../utils/cn.ts";
|
|
7
|
+
|
|
8
|
+
export interface PopoverProps extends ParentProps {
|
|
9
|
+
trigger: JSX.Element;
|
|
10
|
+
title: string;
|
|
11
|
+
description?: string;
|
|
12
|
+
open?: boolean;
|
|
13
|
+
defaultOpen?: boolean;
|
|
14
|
+
onOpenChange?: (open: boolean) => void;
|
|
15
|
+
placement?: "top" | "top-start" | "top-end" | "bottom" | "bottom-start" | "bottom-end" | "left" | "right";
|
|
16
|
+
class?: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function Popover(props: PopoverProps): JSX.Element {
|
|
20
|
+
const theme = useTheme();
|
|
21
|
+
const id = createUniqueId();
|
|
22
|
+
const [draft, setDraft] = createSignal(props.defaultOpen ?? false);
|
|
23
|
+
const open = () => props.open === undefined ? draft() : props.open;
|
|
24
|
+
const change = (next: boolean) => {
|
|
25
|
+
if (next === open()) return;
|
|
26
|
+
if (props.open === undefined) setDraft(next);
|
|
27
|
+
props.onOpenChange?.(next);
|
|
28
|
+
};
|
|
29
|
+
createEffect(() => { if (open()) onCleanup(theme.layers.register(id)); });
|
|
30
|
+
const layer = createMemo<number>(previous => open() ? theme.layers.zIndex(id) : previous ?? 100);
|
|
31
|
+
return <Primitive.Root open={open()} onOpenChange={change} placement={props.placement ?? "bottom"} gutter={6}>
|
|
32
|
+
<Primitive.Trigger as={Button} variant="outline" data-sheen-popover-trigger="">{props.trigger}</Primitive.Trigger>
|
|
33
|
+
<Show when={theme.portal()}>{target => <Primitive.Portal mount={target()}>
|
|
34
|
+
<Primitive.Content data-kb-top-layer="true" class={cn("sheen-popover", props.class)} aria-hidden={!open() || undefined} inert={!open()} style={{ "z-index": layer() }}
|
|
35
|
+
onEscapeKeyDown={event => { if (!theme.layers.isTop(id)) event.preventDefault(); }}>
|
|
36
|
+
<div class="sheen-dialog-header"><Primitive.Title>{props.title}</Primitive.Title>
|
|
37
|
+
<Show when={props.description}><Primitive.Description>{props.description}</Primitive.Description></Show>
|
|
38
|
+
</div>
|
|
39
|
+
<div class="sheen-dialog-body">{props.children}</div>
|
|
40
|
+
<div class="sheen-dialog-footer"><Primitive.CloseButton as={Button} aria-label={theme.messages().close}>{theme.messages().close}</Primitive.CloseButton></div>
|
|
41
|
+
</Primitive.Content>
|
|
42
|
+
</Primitive.Portal>}</Show>
|
|
43
|
+
</Primitive.Root>;
|
|
44
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { createMemo, splitProps } from "solid-js";
|
|
2
|
+
import type { JSX } from "solid-js";
|
|
3
|
+
import { cn } from "../utils/cn.ts";
|
|
4
|
+
|
|
5
|
+
export interface ProgressProps extends Omit<JSX.ProgressHTMLAttributes<HTMLProgressElement>, "children" | "max" | "value"> {
|
|
6
|
+
label: string;
|
|
7
|
+
max?: number;
|
|
8
|
+
value?: number;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function Progress(props: ProgressProps): JSX.Element {
|
|
12
|
+
const [local, forwarded] = splitProps(props, ["label", "max", "value", "class"]);
|
|
13
|
+
if (!local.label?.trim()) throw new Error("Progress requires a nonempty label");
|
|
14
|
+
const value = (): number | undefined => {
|
|
15
|
+
const max = local.max ?? 1;
|
|
16
|
+
if (!Number.isFinite(max) || max <= 0) throw new Error("Progress max is invalid");
|
|
17
|
+
if (local.value !== undefined && (!Number.isFinite(local.value) || local.value < 0 || local.value > max)) throw new Error("Progress value is invalid");
|
|
18
|
+
return local.value;
|
|
19
|
+
};
|
|
20
|
+
return <progress {...forwarded} max={local.max} value={value()} class={cn("sheen-progress", local.class)} aria-label={local.label} />;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface MeterProps extends Omit<JSX.MeterHTMLAttributes<HTMLMeterElement>, "children" | "high" | "low" | "max" | "min" | "optimum" | "value"> {
|
|
24
|
+
label: string;
|
|
25
|
+
value: number;
|
|
26
|
+
min?: number;
|
|
27
|
+
max?: number;
|
|
28
|
+
low?: number;
|
|
29
|
+
high?: number;
|
|
30
|
+
optimum?: number;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
type MeterState = "optimum" | "suboptimum" | "critical";
|
|
34
|
+
|
|
35
|
+
function validateThreshold(name: string, value: unknown, min: number, max: number): void {
|
|
36
|
+
if (value !== undefined && (typeof value !== "number" || !Number.isFinite(value) || value < min || value > max)) throw new Error(`Meter ${name} is invalid`);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function Meter(props: MeterProps): JSX.Element {
|
|
40
|
+
const [local, forwarded] = splitProps(props, ["label", "value", "min", "max", "low", "high", "optimum", "class"]);
|
|
41
|
+
if (!local.label?.trim()) throw new Error("Meter requires a nonempty label");
|
|
42
|
+
const measurement = createMemo(() => {
|
|
43
|
+
const min = local.min ?? 0;
|
|
44
|
+
const max = local.max ?? 1;
|
|
45
|
+
if (!Number.isFinite(min) || !Number.isFinite(max) || min >= max) throw new Error("Meter range is invalid");
|
|
46
|
+
if (!Number.isFinite(local.value) || local.value < min || local.value > max) throw new Error("Meter value must be finite and within range");
|
|
47
|
+
validateThreshold("low", local.low, min, max);
|
|
48
|
+
validateThreshold("high", local.high, min, max);
|
|
49
|
+
validateThreshold("optimum", local.optimum, min, max);
|
|
50
|
+
if (local.low !== undefined && local.high !== undefined && local.low > local.high) throw new Error("Meter low exceeds high");
|
|
51
|
+
const low = local.low ?? min;
|
|
52
|
+
const high = local.high ?? max;
|
|
53
|
+
const optimum = local.optimum ?? (min + max) / 2;
|
|
54
|
+
let state: MeterState;
|
|
55
|
+
if (optimum < low) state = local.value <= low ? "optimum" : local.value <= high ? "suboptimum" : "critical";
|
|
56
|
+
else if (optimum > high) state = local.value >= high ? "optimum" : local.value >= low ? "suboptimum" : "critical";
|
|
57
|
+
else state = local.value >= low && local.value <= high ? "optimum" : "suboptimum";
|
|
58
|
+
return { value: local.value, state };
|
|
59
|
+
});
|
|
60
|
+
return <meter {...forwarded} value={measurement().value} min={local.min} max={local.max} low={local.low} high={local.high} optimum={local.optimum}
|
|
61
|
+
data-meter-state={measurement().state} class={cn("sheen-meter", local.class)} aria-label={local.label} />;
|
|
62
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { For, Show, createMemo, createSignal, createUniqueId, onCleanup, onMount, splitProps, untrack } from "solid-js";
|
|
2
|
+
import type { JSX } from "solid-js";
|
|
3
|
+
import type { CheckboxOption } from "./CheckboxGroup.tsx";
|
|
4
|
+
import { createChoiceOptions } from "./choice-options.ts";
|
|
5
|
+
import { cn } from "../utils/cn.ts";
|
|
6
|
+
|
|
7
|
+
export type RadioOption = CheckboxOption;
|
|
8
|
+
export interface RadioGroupProps extends Omit<JSX.FieldsetHTMLAttributes<HTMLFieldSetElement>, "children" | "onChange" | "id" | "ref" | "name"> {
|
|
9
|
+
id?: string;
|
|
10
|
+
ref?: HTMLFieldSetElement | ((element: HTMLFieldSetElement) => void);
|
|
11
|
+
name: string;
|
|
12
|
+
label: string;
|
|
13
|
+
options: readonly RadioOption[];
|
|
14
|
+
value?: string | null;
|
|
15
|
+
defaultValue?: string | null;
|
|
16
|
+
onValueChange?: (value: string | null) => void;
|
|
17
|
+
description?: string;
|
|
18
|
+
error?: string;
|
|
19
|
+
readOnly?: boolean;
|
|
20
|
+
required?: boolean;
|
|
21
|
+
orientation?: "horizontal" | "vertical";
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function RadioGroup(props: RadioGroupProps): JSX.Element {
|
|
25
|
+
const generatedId = createUniqueId();
|
|
26
|
+
const [local, others] = splitProps(props, ["id", "ref", "class", "name", "label", "options", "value", "defaultValue", "onValueChange", "description", "error", "disabled", "readOnly", "required", "orientation", "aria-label", "aria-labelledby", "aria-describedby"]);
|
|
27
|
+
const initialValue = local.defaultValue ?? null;
|
|
28
|
+
const [draft, setDraft] = createSignal<string | null>(initialValue);
|
|
29
|
+
const value = () => local.value === undefined ? draft() : local.value;
|
|
30
|
+
let fieldset: HTMLFieldSetElement | undefined;
|
|
31
|
+
let disposed = false;
|
|
32
|
+
onCleanup(() => { disposed = true; });
|
|
33
|
+
const { byValue, keys } = createChoiceOptions(() => {
|
|
34
|
+
if (!local.name.trim()) throw new Error("RadioGroup: name must be nonempty for native grouping");
|
|
35
|
+
if (local.options.some(option => !option.value)) throw new Error("RadioGroup: option values must be nonempty; use null for no selection");
|
|
36
|
+
return local.options;
|
|
37
|
+
}, () => fieldset, "RadioGroup");
|
|
38
|
+
const tabStop = createMemo(() => {
|
|
39
|
+
const selected = value();
|
|
40
|
+
return selected !== null && byValue().has(selected) && !byValue().get(selected)?.disabled ? selected : keys().find(key => !byValue().get(key)?.disabled);
|
|
41
|
+
});
|
|
42
|
+
const change = (next: string | null) => {
|
|
43
|
+
if (next === value()) return;
|
|
44
|
+
if (local.value === undefined) setDraft(next);
|
|
45
|
+
local.onValueChange?.(next);
|
|
46
|
+
};
|
|
47
|
+
const synchronize = () => {
|
|
48
|
+
if (disposed) return;
|
|
49
|
+
for (const input of fieldset?.querySelectorAll<HTMLInputElement>("input[type=radio]") ?? []) input.checked = input.value === value();
|
|
50
|
+
};
|
|
51
|
+
const requestChange = (next: string) => {
|
|
52
|
+
if (local.disabled || local.readOnly || byValue().get(next)?.disabled) {
|
|
53
|
+
synchronize();
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
change(next || null);
|
|
57
|
+
synchronize();
|
|
58
|
+
};
|
|
59
|
+
onMount(() => {
|
|
60
|
+
const element = fieldset;
|
|
61
|
+
const view = element?.ownerDocument.defaultView;
|
|
62
|
+
if (!element || !view) return;
|
|
63
|
+
const selected = element.querySelector<HTMLInputElement>("input[type=radio]:checked");
|
|
64
|
+
if (!local.disabled && !local.readOnly && selected && !selected.disabled) change(selected.value);
|
|
65
|
+
synchronize();
|
|
66
|
+
const pending = new Set<number>();
|
|
67
|
+
const reset = (event: Event) => {
|
|
68
|
+
if (event.target !== element.form) return;
|
|
69
|
+
const task = view.setTimeout(() => {
|
|
70
|
+
pending.delete(task);
|
|
71
|
+
if (event.defaultPrevented) return;
|
|
72
|
+
if (local.value === undefined) change(initialValue);
|
|
73
|
+
synchronize();
|
|
74
|
+
}, 0);
|
|
75
|
+
pending.add(task);
|
|
76
|
+
};
|
|
77
|
+
element.ownerDocument.addEventListener("reset", reset, true);
|
|
78
|
+
onCleanup(() => {
|
|
79
|
+
element.ownerDocument.removeEventListener("reset", reset, true);
|
|
80
|
+
for (const task of pending) view.clearTimeout(task);
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
const describedBy = () => [local.description ? `${generatedId}-description` : "", local.error ? `${generatedId}-error` : "", local["aria-describedby"]].filter(Boolean).join(" ") || undefined;
|
|
84
|
+
return <fieldset role="radiogroup" {...others} {...(local["aria-label"] === undefined ? {} : { "aria-label": local["aria-label"] })} aria-labelledby={local["aria-labelledby"] ?? `${generatedId}-label`} aria-describedby={describedBy()} aria-invalid={local.error ? "true" : undefined} aria-readonly={local.readOnly || undefined} aria-orientation={local.orientation ?? "vertical"} data-invalid={local.error ? "" : undefined} data-readonly={local.readOnly || undefined} id={local.id ?? generatedId} ref={element => { fieldset = element; if (typeof local.ref === "function") local.ref(element); }} class={cn("sheen-radio-group", local.class)} disabled={local.disabled ?? false} tabIndex={others.tabIndex ?? -1}>
|
|
85
|
+
<legend id={`${generatedId}-label`} class="sheen-radio-group-label">{local.label}<Show when={local.required}><span aria-hidden="true"> *</span></Show></legend>
|
|
86
|
+
<Show when={local.description}><p id={`${generatedId}-description`} class="sheen-radio-group-description">{local.description}</p></Show>
|
|
87
|
+
<div class="sheen-radio-group-options"><For each={keys()}>{key => {
|
|
88
|
+
const initialOption = untrack(() => byValue().get(key));
|
|
89
|
+
if (!initialOption) throw new Error("RadioGroup: missing option during creation");
|
|
90
|
+
const option = () => byValue().get(key) ?? initialOption;
|
|
91
|
+
const itemId = createUniqueId();
|
|
92
|
+
let input: HTMLInputElement | undefined;
|
|
93
|
+
return <div data-disabled={local.disabled || option().disabled || undefined} class="sheen-radio-item">
|
|
94
|
+
<div class="sheen-radio-row">
|
|
95
|
+
<input ref={input} type="radio" id={`${itemId}-input`} name={local.name} value={key} checked={value() === key} required={local.required} disabled={local.disabled || option().disabled} readOnly={local.readOnly} aria-labelledby={`${itemId}-label`} aria-describedby={[option().description ? `${itemId}-description` : "", describedBy()].filter(Boolean).join(" ") || undefined} form={others.form} class="sheen-radio-input" data-checked={value() === key ? "" : undefined} tabIndex={!local.disabled && tabStop() === key ? 0 : -1} onChange={() => requestChange(key)} />
|
|
96
|
+
<span class="sheen-radio-control" aria-hidden="true" onClick={() => input?.click()}><span class="sheen-radio-indicator" /></span>
|
|
97
|
+
<label id={`${itemId}-label`} for={`${itemId}-input`} class="sheen-radio-label">{option().label}</label>
|
|
98
|
+
</div>
|
|
99
|
+
<Show when={option().description}><p id={`${itemId}-description`} class="sheen-radio-description">{option().description}</p></Show>
|
|
100
|
+
</div>;
|
|
101
|
+
}}</For></div>
|
|
102
|
+
<Show when={local.error}><p id={`${generatedId}-error`} class="sheen-radio-group-error">{local.error}</p></Show>
|
|
103
|
+
</fieldset>;
|
|
104
|
+
}
|