@lab206/ui 0.1.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 +114 -0
- package/README.md +6 -0
- package/dist/components/Accordion.d.ts +25 -0
- package/dist/components/Accordion.d.ts.map +1 -0
- package/dist/components/Accordion.js +123 -0
- package/dist/components/Accordion.js.map +1 -0
- package/dist/components/Alert.d.ts +17 -0
- package/dist/components/Alert.d.ts.map +1 -0
- package/dist/components/Alert.js +59 -0
- package/dist/components/Alert.js.map +1 -0
- package/dist/components/AspectRatio.d.ts +13 -0
- package/dist/components/AspectRatio.d.ts.map +1 -0
- package/dist/components/AspectRatio.js +38 -0
- package/dist/components/AspectRatio.js.map +1 -0
- package/dist/components/Avatar.d.ts +17 -0
- package/dist/components/Avatar.d.ts.map +1 -0
- package/dist/components/Avatar.js +57 -0
- package/dist/components/Avatar.js.map +1 -0
- package/dist/components/Badge.d.ts +12 -0
- package/dist/components/Badge.d.ts.map +1 -0
- package/dist/components/Badge.js +62 -0
- package/dist/components/Badge.js.map +1 -0
- package/dist/components/Breadcrumb.d.ts +16 -0
- package/dist/components/Breadcrumb.d.ts.map +1 -0
- package/dist/components/Breadcrumb.js +76 -0
- package/dist/components/Breadcrumb.js.map +1 -0
- package/dist/components/Button.d.ts +28 -0
- package/dist/components/Button.d.ts.map +1 -0
- package/dist/components/Button.js +110 -0
- package/dist/components/Button.js.map +1 -0
- package/dist/components/Card.d.ts +18 -0
- package/dist/components/Card.d.ts.map +1 -0
- package/dist/components/Card.js +57 -0
- package/dist/components/Card.js.map +1 -0
- package/dist/components/Checkbox.d.ts +23 -0
- package/dist/components/Checkbox.d.ts.map +1 -0
- package/dist/components/Checkbox.js +107 -0
- package/dist/components/Checkbox.js.map +1 -0
- package/dist/components/Chip.d.ts +18 -0
- package/dist/components/Chip.d.ts.map +1 -0
- package/dist/components/Chip.js +103 -0
- package/dist/components/Chip.js.map +1 -0
- package/dist/components/Code.d.ts +12 -0
- package/dist/components/Code.d.ts.map +1 -0
- package/dist/components/Code.js +57 -0
- package/dist/components/Code.js.map +1 -0
- package/dist/components/Collapse.d.ts +15 -0
- package/dist/components/Collapse.d.ts.map +1 -0
- package/dist/components/Collapse.js +49 -0
- package/dist/components/Collapse.js.map +1 -0
- package/dist/components/Combobox.d.ts +47 -0
- package/dist/components/Combobox.d.ts.map +1 -0
- package/dist/components/Combobox.js +325 -0
- package/dist/components/Combobox.js.map +1 -0
- package/dist/components/Command.d.ts +42 -0
- package/dist/components/Command.d.ts.map +1 -0
- package/dist/components/Command.js +263 -0
- package/dist/components/Command.js.map +1 -0
- package/dist/components/Container.d.ts +11 -0
- package/dist/components/Container.d.ts.map +1 -0
- package/dist/components/Container.js +38 -0
- package/dist/components/Container.js.map +1 -0
- package/dist/components/Dialog.d.ts +26 -0
- package/dist/components/Dialog.d.ts.map +1 -0
- package/dist/components/Dialog.js +159 -0
- package/dist/components/Dialog.js.map +1 -0
- package/dist/components/Divider.d.ts +10 -0
- package/dist/components/Divider.d.ts.map +1 -0
- package/dist/components/Divider.js +52 -0
- package/dist/components/Divider.js.map +1 -0
- package/dist/components/Drawer.d.ts +19 -0
- package/dist/components/Drawer.d.ts.map +1 -0
- package/dist/components/Drawer.js +131 -0
- package/dist/components/Drawer.js.map +1 -0
- package/dist/components/Empty.d.ts +17 -0
- package/dist/components/Empty.d.ts.map +1 -0
- package/dist/components/Empty.js +51 -0
- package/dist/components/Empty.js.map +1 -0
- package/dist/components/Field.d.ts +27 -0
- package/dist/components/Field.d.ts.map +1 -0
- package/dist/components/Field.js +153 -0
- package/dist/components/Field.js.map +1 -0
- package/dist/components/Grid.d.ts +13 -0
- package/dist/components/Grid.d.ts.map +1 -0
- package/dist/components/Grid.js +44 -0
- package/dist/components/Grid.js.map +1 -0
- package/dist/components/Input.d.ts +55 -0
- package/dist/components/Input.d.ts.map +1 -0
- package/dist/components/Input.js +113 -0
- package/dist/components/Input.js.map +1 -0
- package/dist/components/Kbd.d.ts +10 -0
- package/dist/components/Kbd.d.ts.map +1 -0
- package/dist/components/Kbd.js +41 -0
- package/dist/components/Kbd.js.map +1 -0
- package/dist/components/Label.d.ts +13 -0
- package/dist/components/Label.d.ts.map +1 -0
- package/dist/components/Label.js +34 -0
- package/dist/components/Label.js.map +1 -0
- package/dist/components/Link.d.ts +18 -0
- package/dist/components/Link.d.ts.map +1 -0
- package/dist/components/Link.js +41 -0
- package/dist/components/Link.js.map +1 -0
- package/dist/components/List.d.ts +22 -0
- package/dist/components/List.d.ts.map +1 -0
- package/dist/components/List.js +157 -0
- package/dist/components/List.js.map +1 -0
- package/dist/components/Menu.d.ts +28 -0
- package/dist/components/Menu.d.ts.map +1 -0
- package/dist/components/Menu.js +126 -0
- package/dist/components/Menu.js.map +1 -0
- package/dist/components/NumberInput.d.ts +27 -0
- package/dist/components/NumberInput.d.ts.map +1 -0
- package/dist/components/NumberInput.js +92 -0
- package/dist/components/NumberInput.js.map +1 -0
- package/dist/components/Pagination.d.ts +13 -0
- package/dist/components/Pagination.d.ts.map +1 -0
- package/dist/components/Pagination.js +104 -0
- package/dist/components/Pagination.js.map +1 -0
- package/dist/components/Popover.d.ts +26 -0
- package/dist/components/Popover.d.ts.map +1 -0
- package/dist/components/Popover.js +165 -0
- package/dist/components/Popover.js.map +1 -0
- package/dist/components/Progress.d.ts +15 -0
- package/dist/components/Progress.d.ts.map +1 -0
- package/dist/components/Progress.js +63 -0
- package/dist/components/Progress.js.map +1 -0
- package/dist/components/Radio.d.ts +25 -0
- package/dist/components/Radio.d.ts.map +1 -0
- package/dist/components/Radio.js +95 -0
- package/dist/components/Radio.js.map +1 -0
- package/dist/components/ScrollArea.d.ts +13 -0
- package/dist/components/ScrollArea.d.ts.map +1 -0
- package/dist/components/ScrollArea.js +33 -0
- package/dist/components/ScrollArea.js.map +1 -0
- package/dist/components/Select.d.ts +34 -0
- package/dist/components/Select.d.ts.map +1 -0
- package/dist/components/Select.js +108 -0
- package/dist/components/Select.js.map +1 -0
- package/dist/components/Skeleton.d.ts +18 -0
- package/dist/components/Skeleton.d.ts.map +1 -0
- package/dist/components/Skeleton.js +86 -0
- package/dist/components/Skeleton.js.map +1 -0
- package/dist/components/Slider.d.ts +26 -0
- package/dist/components/Slider.d.ts.map +1 -0
- package/dist/components/Slider.js +113 -0
- package/dist/components/Slider.js.map +1 -0
- package/dist/components/Spinner.d.ts +12 -0
- package/dist/components/Spinner.d.ts.map +1 -0
- package/dist/components/Spinner.js +44 -0
- package/dist/components/Spinner.js.map +1 -0
- package/dist/components/Stack.d.ts +21 -0
- package/dist/components/Stack.d.ts.map +1 -0
- package/dist/components/Stack.js +48 -0
- package/dist/components/Stack.js.map +1 -0
- package/dist/components/Stat.d.ts +23 -0
- package/dist/components/Stat.d.ts.map +1 -0
- package/dist/components/Stat.js +115 -0
- package/dist/components/Stat.js.map +1 -0
- package/dist/components/Steps.d.ts +20 -0
- package/dist/components/Steps.d.ts.map +1 -0
- package/dist/components/Steps.js +126 -0
- package/dist/components/Steps.js.map +1 -0
- package/dist/components/Switch.d.ts +23 -0
- package/dist/components/Switch.d.ts.map +1 -0
- package/dist/components/Switch.js +108 -0
- package/dist/components/Switch.js.map +1 -0
- package/dist/components/Table.d.ts +27 -0
- package/dist/components/Table.d.ts.map +1 -0
- package/dist/components/Table.js +112 -0
- package/dist/components/Table.js.map +1 -0
- package/dist/components/Tabs.d.ts +27 -0
- package/dist/components/Tabs.d.ts.map +1 -0
- package/dist/components/Tabs.js +171 -0
- package/dist/components/Tabs.js.map +1 -0
- package/dist/components/Text.d.ts +17 -0
- package/dist/components/Text.d.ts.map +1 -0
- package/dist/components/Text.js +48 -0
- package/dist/components/Text.js.map +1 -0
- package/dist/components/Textarea.d.ts +32 -0
- package/dist/components/Textarea.d.ts.map +1 -0
- package/dist/components/Textarea.js +91 -0
- package/dist/components/Textarea.js.map +1 -0
- package/dist/components/Timeline.d.ts +17 -0
- package/dist/components/Timeline.d.ts.map +1 -0
- package/dist/components/Timeline.js +95 -0
- package/dist/components/Timeline.js.map +1 -0
- package/dist/components/Toast.d.ts +36 -0
- package/dist/components/Toast.d.ts.map +1 -0
- package/dist/components/Toast.js +143 -0
- package/dist/components/Toast.js.map +1 -0
- package/dist/components/ToggleGroup.d.ts +25 -0
- package/dist/components/ToggleGroup.d.ts.map +1 -0
- package/dist/components/ToggleGroup.js +103 -0
- package/dist/components/ToggleGroup.js.map +1 -0
- package/dist/components/Tooltip.d.ts +19 -0
- package/dist/components/Tooltip.d.ts.map +1 -0
- package/dist/components/Tooltip.js +141 -0
- package/dist/components/Tooltip.js.map +1 -0
- package/dist/components/Transition.d.ts +22 -0
- package/dist/components/Transition.d.ts.map +1 -0
- package/dist/components/Transition.js +139 -0
- package/dist/components/Transition.js.map +1 -0
- package/dist/density.d.ts +21 -0
- package/dist/density.d.ts.map +1 -0
- package/dist/density.js +31 -0
- package/dist/density.js.map +1 -0
- package/dist/dev.d.ts +27 -0
- package/dist/dev.d.ts.map +1 -0
- package/dist/dev.js +77 -0
- package/dist/dev.js.map +1 -0
- package/dist/focusTrap.d.ts +6 -0
- package/dist/focusTrap.d.ts.map +1 -0
- package/dist/focusTrap.js +55 -0
- package/dist/focusTrap.js.map +1 -0
- package/dist/form.d.ts +125 -0
- package/dist/form.d.ts.map +1 -0
- package/dist/form.js +192 -0
- package/dist/form.js.map +1 -0
- package/dist/index.d.ts +134 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +85 -0
- package/dist/index.js.map +1 -0
- package/dist/motion.d.ts +29 -0
- package/dist/motion.d.ts.map +1 -0
- package/dist/motion.js +43 -0
- package/dist/motion.js.map +1 -0
- package/dist/overlay.d.ts +63 -0
- package/dist/overlay.d.ts.map +1 -0
- package/dist/overlay.js +172 -0
- package/dist/overlay.js.map +1 -0
- package/dist/palette.d.ts +20 -0
- package/dist/palette.d.ts.map +1 -0
- package/dist/palette.js +52 -0
- package/dist/palette.js.map +1 -0
- package/dist/reactive.d.ts +25 -0
- package/dist/reactive.d.ts.map +1 -0
- package/dist/reactive.js +43 -0
- package/dist/reactive.js.map +1 -0
- package/dist/rovingFocus.d.ts +36 -0
- package/dist/rovingFocus.d.ts.map +1 -0
- package/dist/rovingFocus.js +127 -0
- package/dist/rovingFocus.js.map +1 -0
- package/dist/size-entries/form-kit.d.ts +12 -0
- package/dist/size-entries/form-kit.d.ts.map +1 -0
- package/dist/size-entries/form-kit.js +12 -0
- package/dist/size-entries/form-kit.js.map +1 -0
- package/dist/styles.d.ts +29 -0
- package/dist/styles.d.ts.map +1 -0
- package/dist/styles.js +53 -0
- package/dist/styles.js.map +1 -0
- package/dist/theme.d.ts +22 -0
- package/dist/theme.d.ts.map +1 -0
- package/dist/theme.js +32 -0
- package/dist/theme.js.map +1 -0
- package/dist/utils.d.ts +5 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +24 -0
- package/dist/utils.js.map +1 -0
- package/package.json +71 -0
- package/src/components/Accordion.tsx +194 -0
- package/src/components/Alert.tsx +88 -0
- package/src/components/AspectRatio.tsx +64 -0
- package/src/components/Avatar.tsx +89 -0
- package/src/components/Badge.tsx +84 -0
- package/src/components/Breadcrumb.tsx +116 -0
- package/src/components/Button.tsx +148 -0
- package/src/components/Card.tsx +92 -0
- package/src/components/Checkbox.tsx +143 -0
- package/src/components/Chip.tsx +153 -0
- package/src/components/Code.tsx +71 -0
- package/src/components/Collapse.tsx +76 -0
- package/src/components/Combobox.tsx +438 -0
- package/src/components/Command.tsx +380 -0
- package/src/components/Container.tsx +58 -0
- package/src/components/Dialog.tsx +226 -0
- package/src/components/Divider.tsx +72 -0
- package/src/components/Drawer.tsx +191 -0
- package/src/components/Empty.tsx +86 -0
- package/src/components/Field.tsx +184 -0
- package/src/components/Grid.tsx +67 -0
- package/src/components/Input.tsx +169 -0
- package/src/components/Kbd.tsx +57 -0
- package/src/components/Label.tsx +54 -0
- package/src/components/Link.tsx +87 -0
- package/src/components/List.tsx +236 -0
- package/src/components/Menu.tsx +195 -0
- package/src/components/NumberInput.tsx +158 -0
- package/src/components/Pagination.tsx +153 -0
- package/src/components/Popover.tsx +219 -0
- package/src/components/Progress.tsx +108 -0
- package/src/components/Radio.tsx +161 -0
- package/src/components/ScrollArea.tsx +58 -0
- package/src/components/Select.tsx +128 -0
- package/src/components/Skeleton.tsx +117 -0
- package/src/components/Slider.tsx +175 -0
- package/src/components/Spinner.tsx +70 -0
- package/src/components/Stack.tsx +79 -0
- package/src/components/Stat.tsx +169 -0
- package/src/components/Steps.tsx +191 -0
- package/src/components/Switch.tsx +146 -0
- package/src/components/Table.tsx +168 -0
- package/src/components/Tabs.tsx +246 -0
- package/src/components/Text.tsx +69 -0
- package/src/components/Textarea.tsx +128 -0
- package/src/components/Timeline.tsx +150 -0
- package/src/components/Toast.tsx +221 -0
- package/src/components/ToggleGroup.tsx +161 -0
- package/src/components/Tooltip.tsx +187 -0
- package/src/components/Transition.tsx +155 -0
- package/src/density.ts +46 -0
- package/src/dev.ts +87 -0
- package/src/focusTrap.ts +61 -0
- package/src/form.ts +269 -0
- package/src/index.ts +243 -0
- package/src/motion.ts +62 -0
- package/src/overlay.ts +232 -0
- package/src/palette.ts +70 -0
- package/src/reactive.ts +54 -0
- package/src/rovingFocus.ts +171 -0
- package/src/size-entries/form-kit.ts +11 -0
- package/src/styles/base.css +70 -0
- package/src/styles/theme.css +14 -0
- package/src/styles/tokens.css +377 -0
- package/src/styles/utilities.css +216 -0
- package/src/styles.ts +52 -0
- package/src/theme.ts +48 -0
- package/src/utils.ts +25 -0
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import { signal, effect } from "@lab206/core";
|
|
2
|
+
import { For, component, mergeProps, type ComponentProps } from "@lab206/dom";
|
|
3
|
+
import { cx } from "../utils.js";
|
|
4
|
+
import { Popover } from "./Popover.js";
|
|
5
|
+
import {
|
|
6
|
+
focusRovingItem,
|
|
7
|
+
handleRovingKeydown,
|
|
8
|
+
initRovingFocus,
|
|
9
|
+
listRovingItems,
|
|
10
|
+
} from "../rovingFocus.js";
|
|
11
|
+
|
|
12
|
+
export type MenuItem = {
|
|
13
|
+
id: string;
|
|
14
|
+
label: string;
|
|
15
|
+
disabled?: boolean;
|
|
16
|
+
danger?: boolean;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export type MenuProps = {
|
|
20
|
+
/** Items shown in the menu */
|
|
21
|
+
items: MenuItem[] | (() => MenuItem[]);
|
|
22
|
+
/** Trigger node (Button recommended) */
|
|
23
|
+
trigger: unknown;
|
|
24
|
+
/** Called when an enabled item is chosen */
|
|
25
|
+
onSelect?: (id: string) => void;
|
|
26
|
+
align?: "start" | "center" | "end";
|
|
27
|
+
class?: string | (() => string);
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const ITEM_SEL = '[role="menuitem"]:not([disabled])';
|
|
31
|
+
|
|
32
|
+
const styles = `
|
|
33
|
+
.pu-menu {
|
|
34
|
+
display: flex;
|
|
35
|
+
flex-direction: column;
|
|
36
|
+
gap: 2px;
|
|
37
|
+
min-width: 10.5rem;
|
|
38
|
+
margin: 0;
|
|
39
|
+
padding: 0;
|
|
40
|
+
}
|
|
41
|
+
.pu-menu__item {
|
|
42
|
+
appearance: none;
|
|
43
|
+
border: 0;
|
|
44
|
+
background: transparent;
|
|
45
|
+
text-align: left;
|
|
46
|
+
width: 100%;
|
|
47
|
+
font: inherit;
|
|
48
|
+
font-size: var(--pu-text-sm);
|
|
49
|
+
font-weight: var(--pu-font-medium);
|
|
50
|
+
letter-spacing: -0.01em;
|
|
51
|
+
color: var(--pu-color-text);
|
|
52
|
+
padding: 0.5rem 0.65rem;
|
|
53
|
+
border-radius: var(--pu-radius-sm);
|
|
54
|
+
cursor: pointer;
|
|
55
|
+
transition:
|
|
56
|
+
background var(--pu-duration-fast) var(--pu-ease-out),
|
|
57
|
+
transform var(--pu-duration-fast) var(--pu-ease-out),
|
|
58
|
+
color var(--pu-duration-fast) var(--pu-ease-out);
|
|
59
|
+
}
|
|
60
|
+
.pu-menu__item:hover:not(:disabled),
|
|
61
|
+
.pu-menu__item:focus-visible:not(:disabled) {
|
|
62
|
+
background: var(--pu-color-surface-2);
|
|
63
|
+
transform: translateX(2px);
|
|
64
|
+
outline: none;
|
|
65
|
+
}
|
|
66
|
+
.pu-menu__item:focus-visible:not(:disabled) {
|
|
67
|
+
box-shadow: 0 0 0 2px color-mix(in srgb, var(--pu-color-focus) 45%, transparent);
|
|
68
|
+
}
|
|
69
|
+
.pu-menu__item:disabled {
|
|
70
|
+
opacity: 0.45;
|
|
71
|
+
cursor: not-allowed;
|
|
72
|
+
}
|
|
73
|
+
.pu-menu__item--danger {
|
|
74
|
+
color: var(--pu-color-danger);
|
|
75
|
+
}
|
|
76
|
+
.pu-menu__item--danger:hover:not(:disabled),
|
|
77
|
+
.pu-menu__item--danger:focus-visible:not(:disabled) {
|
|
78
|
+
background: color-mix(in srgb, var(--pu-color-danger) 12%, transparent);
|
|
79
|
+
}
|
|
80
|
+
@media (prefers-reduced-motion: reduce) {
|
|
81
|
+
.pu-menu__item { transition: none; }
|
|
82
|
+
.pu-menu__item:hover:not(:disabled),
|
|
83
|
+
.pu-menu__item:focus-visible:not(:disabled) { transform: none; }
|
|
84
|
+
}
|
|
85
|
+
`;
|
|
86
|
+
|
|
87
|
+
function ensureStyles(doc: Document = document) {
|
|
88
|
+
if (typeof doc === "undefined") return;
|
|
89
|
+
if (doc.querySelector('style[data-pu-ui="menu"]')) return;
|
|
90
|
+
const el = doc.createElement("style");
|
|
91
|
+
el.setAttribute("data-pu-ui", "menu");
|
|
92
|
+
el.textContent = styles;
|
|
93
|
+
doc.head.appendChild(el);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Action menu built on Popover.
|
|
98
|
+
* Arrow / Home / End roving focus · Enter / Space select · Esc closes (Popover).
|
|
99
|
+
*/
|
|
100
|
+
export const Menu = component((raw: MenuProps) => {
|
|
101
|
+
ensureStyles();
|
|
102
|
+
const props = mergeProps({ align: "start" as const }, raw) as ComponentProps<
|
|
103
|
+
MenuProps & { align: "start" | "center" | "end" }
|
|
104
|
+
>;
|
|
105
|
+
|
|
106
|
+
const open = signal(false);
|
|
107
|
+
let menuEl: HTMLElement | null = null;
|
|
108
|
+
|
|
109
|
+
const getItems = (): MenuItem[] => {
|
|
110
|
+
const i = props.items;
|
|
111
|
+
return typeof i === "function" ? (i as () => MenuItem[])() : (i ?? []);
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
const pick = (id: string) => {
|
|
115
|
+
const item = getItems().find((x) => x.id === id);
|
|
116
|
+
if (!item || item.disabled) return;
|
|
117
|
+
props.onSelect?.(id);
|
|
118
|
+
open.set(false);
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
// Focus first enabled item when menu opens
|
|
122
|
+
effect(() => {
|
|
123
|
+
if (!open()) return;
|
|
124
|
+
const t = window.setTimeout(() => {
|
|
125
|
+
if (!menuEl) return;
|
|
126
|
+
initRovingFocus(menuEl, ITEM_SEL, 0);
|
|
127
|
+
const items = listRovingItems(menuEl, ITEM_SEL);
|
|
128
|
+
focusRovingItem(items, 0);
|
|
129
|
+
}, 0);
|
|
130
|
+
return () => window.clearTimeout(t);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
return (
|
|
134
|
+
<div
|
|
135
|
+
class={() =>
|
|
136
|
+
cx(
|
|
137
|
+
"pu-menu-root",
|
|
138
|
+
typeof props.class === "function" ? props.class() : props.class,
|
|
139
|
+
)
|
|
140
|
+
}
|
|
141
|
+
ref={(el) => ensureStyles(el.ownerDocument)}
|
|
142
|
+
>
|
|
143
|
+
<Popover
|
|
144
|
+
open={open}
|
|
145
|
+
onOpenChange={(v) => open.set(v)}
|
|
146
|
+
align={props.align}
|
|
147
|
+
trigger={props.trigger}
|
|
148
|
+
>
|
|
149
|
+
<div
|
|
150
|
+
class="pu-menu"
|
|
151
|
+
role="menu"
|
|
152
|
+
ref={(el) => {
|
|
153
|
+
menuEl = el;
|
|
154
|
+
}}
|
|
155
|
+
onKeyDown={(e: KeyboardEvent) => {
|
|
156
|
+
if (!menuEl) return;
|
|
157
|
+
handleRovingKeydown(e, menuEl, ITEM_SEL, {
|
|
158
|
+
orientation: "vertical",
|
|
159
|
+
loop: true,
|
|
160
|
+
onActivate: (el) => {
|
|
161
|
+
const id = el.dataset.menuId;
|
|
162
|
+
if (id) pick(id);
|
|
163
|
+
},
|
|
164
|
+
});
|
|
165
|
+
}}
|
|
166
|
+
>
|
|
167
|
+
<For each={getItems}>
|
|
168
|
+
{(item) => (
|
|
169
|
+
<button
|
|
170
|
+
type="button"
|
|
171
|
+
role="menuitem"
|
|
172
|
+
tabindex={-1}
|
|
173
|
+
data-menu-id={() => item().id}
|
|
174
|
+
class={() =>
|
|
175
|
+
cx(
|
|
176
|
+
"pu-menu__item",
|
|
177
|
+
item().danger && "pu-menu__item--danger",
|
|
178
|
+
)
|
|
179
|
+
}
|
|
180
|
+
disabled={() => !!item().disabled}
|
|
181
|
+
onClick={(e: MouseEvent) => {
|
|
182
|
+
e.stopPropagation();
|
|
183
|
+
if (item().disabled) return;
|
|
184
|
+
pick(item().id);
|
|
185
|
+
}}
|
|
186
|
+
>
|
|
187
|
+
{() => item().label}
|
|
188
|
+
</button>
|
|
189
|
+
)}
|
|
190
|
+
</For>
|
|
191
|
+
</div>
|
|
192
|
+
</Popover>
|
|
193
|
+
</div>
|
|
194
|
+
);
|
|
195
|
+
});
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { component, mergeProps, type ComponentProps } from "@lab206/dom";
|
|
2
|
+
import { cx } from "../utils.js";
|
|
3
|
+
import { createStyleSheet } from "../styles.js";
|
|
4
|
+
import { readBool, readNum, type MaybeReactive } from "../reactive.js";
|
|
5
|
+
|
|
6
|
+
export type NumberInputProps = {
|
|
7
|
+
value?: MaybeReactive<number>;
|
|
8
|
+
min?: number;
|
|
9
|
+
max?: number;
|
|
10
|
+
step?: number;
|
|
11
|
+
disabled?: MaybeReactive<boolean>;
|
|
12
|
+
onChange?: (value: number) => void;
|
|
13
|
+
placeholder?: string;
|
|
14
|
+
class?: MaybeReactive<string>;
|
|
15
|
+
id?: string;
|
|
16
|
+
"aria-label"?: string;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const ensure = createStyleSheet(
|
|
20
|
+
"number-input",
|
|
21
|
+
`
|
|
22
|
+
.pu-number {
|
|
23
|
+
display: inline-flex;
|
|
24
|
+
align-items: stretch;
|
|
25
|
+
border: 1px solid var(--pu-color-control-border, var(--pu-color-border));
|
|
26
|
+
border-radius: var(--pu-radius-md);
|
|
27
|
+
background: var(--pu-color-surface);
|
|
28
|
+
overflow: hidden;
|
|
29
|
+
height: var(--pu-control-h-md);
|
|
30
|
+
}
|
|
31
|
+
.pu-number:focus-within {
|
|
32
|
+
border-color: var(--pu-color-accent);
|
|
33
|
+
box-shadow: 0 0 0 3px color-mix(in srgb, var(--pu-color-accent) 18%, transparent);
|
|
34
|
+
}
|
|
35
|
+
.pu-number[data-disabled="true"] {
|
|
36
|
+
opacity: 0.55;
|
|
37
|
+
pointer-events: none;
|
|
38
|
+
}
|
|
39
|
+
.pu-number__btn {
|
|
40
|
+
appearance: none;
|
|
41
|
+
border: 0;
|
|
42
|
+
background: var(--pu-color-surface-2);
|
|
43
|
+
color: var(--pu-color-text);
|
|
44
|
+
width: 2rem;
|
|
45
|
+
cursor: pointer;
|
|
46
|
+
font-size: 1rem;
|
|
47
|
+
line-height: 1;
|
|
48
|
+
flex-shrink: 0;
|
|
49
|
+
}
|
|
50
|
+
.pu-number__btn:hover:not(:disabled) {
|
|
51
|
+
background: color-mix(in srgb, var(--pu-color-accent) 12%, var(--pu-color-surface-2));
|
|
52
|
+
}
|
|
53
|
+
.pu-number__btn:focus-visible {
|
|
54
|
+
outline: none;
|
|
55
|
+
z-index: 1;
|
|
56
|
+
box-shadow:
|
|
57
|
+
inset 0 0 0 2px color-mix(in srgb, var(--pu-color-focus) 45%, transparent);
|
|
58
|
+
}
|
|
59
|
+
.pu-number__btn:disabled { opacity: 0.4; cursor: not-allowed; }
|
|
60
|
+
.pu-number__input {
|
|
61
|
+
appearance: textfield;
|
|
62
|
+
-moz-appearance: textfield;
|
|
63
|
+
border: 0;
|
|
64
|
+
background: transparent;
|
|
65
|
+
color: var(--pu-color-text);
|
|
66
|
+
font: inherit;
|
|
67
|
+
font-size: var(--pu-text-sm);
|
|
68
|
+
font-variant-numeric: tabular-nums;
|
|
69
|
+
text-align: center;
|
|
70
|
+
width: 4rem;
|
|
71
|
+
min-width: 0;
|
|
72
|
+
padding: 0 0.25rem;
|
|
73
|
+
outline: none;
|
|
74
|
+
}
|
|
75
|
+
.pu-number__input::-webkit-outer-spin-button,
|
|
76
|
+
.pu-number__input::-webkit-inner-spin-button {
|
|
77
|
+
-webkit-appearance: none;
|
|
78
|
+
margin: 0;
|
|
79
|
+
}
|
|
80
|
+
`,
|
|
81
|
+
);
|
|
82
|
+
|
|
83
|
+
function clamp(n: number, min?: number, max?: number) {
|
|
84
|
+
let v = n;
|
|
85
|
+
if (min != null && !Number.isNaN(min)) v = Math.max(min, v);
|
|
86
|
+
if (max != null && !Number.isNaN(max)) v = Math.min(max, v);
|
|
87
|
+
return v;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** Numeric stepper with − / + controls. */
|
|
91
|
+
export const NumberInput = component((raw: NumberInputProps) => {
|
|
92
|
+
ensure();
|
|
93
|
+
const props = mergeProps({ step: 1 }, raw) as ComponentProps<
|
|
94
|
+
NumberInputProps & { step: number }
|
|
95
|
+
>;
|
|
96
|
+
const id = props.id ?? `pu-num-${Math.random().toString(36).slice(2, 9)}`;
|
|
97
|
+
|
|
98
|
+
const val = () => readNum(props.value as MaybeReactive<number>, 0);
|
|
99
|
+
const isDisabled = () =>
|
|
100
|
+
readBool(props.disabled as MaybeReactive<boolean>);
|
|
101
|
+
|
|
102
|
+
const set = (n: number) => {
|
|
103
|
+
const next = clamp(n, props.min, props.max);
|
|
104
|
+
if (!Number.isNaN(next)) props.onChange?.(next);
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
return (
|
|
108
|
+
<div
|
|
109
|
+
class={() =>
|
|
110
|
+
cx(
|
|
111
|
+
"pu-number",
|
|
112
|
+
typeof props.class === "function" ? props.class() : props.class,
|
|
113
|
+
)
|
|
114
|
+
}
|
|
115
|
+
data-disabled={() => (isDisabled() ? "true" : "false")}
|
|
116
|
+
ref={(el) => ensure(el.ownerDocument)}
|
|
117
|
+
>
|
|
118
|
+
<button
|
|
119
|
+
type="button"
|
|
120
|
+
class="pu-number__btn"
|
|
121
|
+
aria-label="Decrease"
|
|
122
|
+
disabled={() =>
|
|
123
|
+
isDisabled() || (props.min != null && val() <= props.min)
|
|
124
|
+
}
|
|
125
|
+
onClick={() => set(val() - props.step)}
|
|
126
|
+
>
|
|
127
|
+
−
|
|
128
|
+
</button>
|
|
129
|
+
<input
|
|
130
|
+
id={id}
|
|
131
|
+
class="pu-number__input"
|
|
132
|
+
type="number"
|
|
133
|
+
value={val}
|
|
134
|
+
min={props.min}
|
|
135
|
+
max={props.max}
|
|
136
|
+
step={props.step}
|
|
137
|
+
disabled={isDisabled}
|
|
138
|
+
placeholder={props.placeholder}
|
|
139
|
+
aria-label={props["aria-label"]}
|
|
140
|
+
onInput={(e: Event) => {
|
|
141
|
+
const n = Number((e.target as HTMLInputElement).value);
|
|
142
|
+
if (!Number.isNaN(n)) set(n);
|
|
143
|
+
}}
|
|
144
|
+
/>
|
|
145
|
+
<button
|
|
146
|
+
type="button"
|
|
147
|
+
class="pu-number__btn"
|
|
148
|
+
aria-label="Increase"
|
|
149
|
+
disabled={() =>
|
|
150
|
+
isDisabled() || (props.max != null && val() >= props.max)
|
|
151
|
+
}
|
|
152
|
+
onClick={() => set(val() + props.step)}
|
|
153
|
+
>
|
|
154
|
+
+
|
|
155
|
+
</button>
|
|
156
|
+
</div>
|
|
157
|
+
);
|
|
158
|
+
});
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { component, type ComponentProps } from "@lab206/dom";
|
|
2
|
+
import { cx } from "../utils.js";
|
|
3
|
+
import { createStyleSheet } from "../styles.js";
|
|
4
|
+
|
|
5
|
+
export type PaginationProps = {
|
|
6
|
+
page: number | (() => number);
|
|
7
|
+
pageCount: number | (() => number);
|
|
8
|
+
onChange?: (page: number) => void;
|
|
9
|
+
class?: string | (() => string);
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
const ensure = createStyleSheet(
|
|
13
|
+
"pagination",
|
|
14
|
+
`
|
|
15
|
+
.pu-pagination {
|
|
16
|
+
display: inline-flex;
|
|
17
|
+
align-items: center;
|
|
18
|
+
gap: 0.35rem;
|
|
19
|
+
flex-wrap: wrap;
|
|
20
|
+
}
|
|
21
|
+
.pu-pagination__btn {
|
|
22
|
+
appearance: none;
|
|
23
|
+
border: 1px solid var(--pu-color-border);
|
|
24
|
+
background: var(--pu-color-surface);
|
|
25
|
+
color: var(--pu-color-text);
|
|
26
|
+
font: inherit;
|
|
27
|
+
font-size: var(--pu-text-sm);
|
|
28
|
+
font-weight: var(--pu-font-medium);
|
|
29
|
+
min-width: 2rem;
|
|
30
|
+
height: 2rem;
|
|
31
|
+
padding: 0 0.5rem;
|
|
32
|
+
border-radius: var(--pu-radius-md);
|
|
33
|
+
cursor: pointer;
|
|
34
|
+
}
|
|
35
|
+
.pu-pagination__btn:hover:not(:disabled) {
|
|
36
|
+
background: var(--pu-color-surface-2);
|
|
37
|
+
}
|
|
38
|
+
.pu-pagination__btn:focus-visible {
|
|
39
|
+
outline: none;
|
|
40
|
+
box-shadow:
|
|
41
|
+
0 0 0 2px var(--pu-color-surface),
|
|
42
|
+
0 0 0 4px color-mix(in srgb, var(--pu-color-focus) 55%, transparent);
|
|
43
|
+
}
|
|
44
|
+
.pu-pagination__btn:disabled {
|
|
45
|
+
opacity: 0.45;
|
|
46
|
+
cursor: not-allowed;
|
|
47
|
+
}
|
|
48
|
+
.pu-pagination__btn.is-active {
|
|
49
|
+
background: var(--pu-color-accent);
|
|
50
|
+
color: var(--pu-color-accent-fg);
|
|
51
|
+
border-color: transparent;
|
|
52
|
+
}
|
|
53
|
+
.pu-pagination__btn {
|
|
54
|
+
transition:
|
|
55
|
+
background var(--pu-duration-fast) var(--pu-ease),
|
|
56
|
+
color var(--pu-duration-fast) var(--pu-ease),
|
|
57
|
+
border-color var(--pu-duration-fast) var(--pu-ease),
|
|
58
|
+
box-shadow var(--pu-duration-fast) var(--pu-ease);
|
|
59
|
+
}
|
|
60
|
+
.pu-pagination__meta {
|
|
61
|
+
font-size: var(--pu-text-xs);
|
|
62
|
+
color: var(--pu-color-text-muted);
|
|
63
|
+
margin-left: 0.35rem;
|
|
64
|
+
}
|
|
65
|
+
`,
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
export const Pagination = component((raw: PaginationProps) => {
|
|
69
|
+
ensure();
|
|
70
|
+
const props = raw as ComponentProps<PaginationProps>;
|
|
71
|
+
const page = () =>
|
|
72
|
+
typeof props.page === "function"
|
|
73
|
+
? (props.page as () => number)()
|
|
74
|
+
: props.page;
|
|
75
|
+
const count = () =>
|
|
76
|
+
typeof props.pageCount === "function"
|
|
77
|
+
? (props.pageCount as () => number)()
|
|
78
|
+
: props.pageCount;
|
|
79
|
+
|
|
80
|
+
const go = (p: number) => {
|
|
81
|
+
const c = Math.max(1, count());
|
|
82
|
+
const next = Math.min(c, Math.max(1, p));
|
|
83
|
+
props.onChange?.(next);
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
// Build a compact page list: 1 … n around current
|
|
87
|
+
const pages = () => {
|
|
88
|
+
const c = Math.max(1, count());
|
|
89
|
+
const p = page();
|
|
90
|
+
const set = new Set<number>([1, c, p - 1, p, p + 1]);
|
|
91
|
+
return [...set].filter((n) => n >= 1 && n <= c).sort((a, b) => a - b);
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
return (
|
|
95
|
+
<div
|
|
96
|
+
class={() =>
|
|
97
|
+
cx(
|
|
98
|
+
"pu-pagination",
|
|
99
|
+
typeof props.class === "function" ? props.class() : props.class,
|
|
100
|
+
)
|
|
101
|
+
}
|
|
102
|
+
role="navigation"
|
|
103
|
+
aria-label="Pagination"
|
|
104
|
+
ref={(el) => ensure(el.ownerDocument)}
|
|
105
|
+
>
|
|
106
|
+
<button
|
|
107
|
+
type="button"
|
|
108
|
+
class="pu-pagination__btn"
|
|
109
|
+
disabled={() => page() <= 1}
|
|
110
|
+
onClick={() => go(page() - 1)}
|
|
111
|
+
aria-label="Previous page"
|
|
112
|
+
>
|
|
113
|
+
‹
|
|
114
|
+
</button>
|
|
115
|
+
{() => {
|
|
116
|
+
const list = pages();
|
|
117
|
+
const frag = document.createDocumentFragment();
|
|
118
|
+
let prev = 0;
|
|
119
|
+
for (const n of list) {
|
|
120
|
+
if (prev && n - prev > 1) {
|
|
121
|
+
const dots = document.createElement("span");
|
|
122
|
+
dots.className = "pu-pagination__meta";
|
|
123
|
+
dots.textContent = "…";
|
|
124
|
+
frag.appendChild(dots);
|
|
125
|
+
}
|
|
126
|
+
const btn = document.createElement("button");
|
|
127
|
+
btn.type = "button";
|
|
128
|
+
btn.className =
|
|
129
|
+
"pu-pagination__btn" + (n === page() ? " is-active" : "");
|
|
130
|
+
btn.textContent = String(n);
|
|
131
|
+
btn.setAttribute("aria-label", `Page ${n}`);
|
|
132
|
+
if (n === page()) btn.setAttribute("aria-current", "page");
|
|
133
|
+
btn.onclick = () => go(n);
|
|
134
|
+
frag.appendChild(btn);
|
|
135
|
+
prev = n;
|
|
136
|
+
}
|
|
137
|
+
return frag;
|
|
138
|
+
}}
|
|
139
|
+
<button
|
|
140
|
+
type="button"
|
|
141
|
+
class="pu-pagination__btn"
|
|
142
|
+
disabled={() => page() >= count()}
|
|
143
|
+
onClick={() => go(page() + 1)}
|
|
144
|
+
aria-label="Next page"
|
|
145
|
+
>
|
|
146
|
+
›
|
|
147
|
+
</button>
|
|
148
|
+
<span class="pu-pagination__meta">
|
|
149
|
+
{() => `Page ${page()} / ${count()}`}
|
|
150
|
+
</span>
|
|
151
|
+
</div>
|
|
152
|
+
);
|
|
153
|
+
});
|