@glasshome/ui 1.42.0 → 1.43.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/dist/index.js +2 -2
- package/dist/lib/anchor-placement.d.ts +4 -0
- package/dist/lib/balance-rows.d.ts +10 -4
- package/dist/lib/behavior/anchored.d.ts +16 -2
- package/dist/lib/behavior/button-root.d.ts +7 -0
- package/dist/lib/behavior/collection.d.ts +8 -0
- package/dist/lib/behavior/dismiss.d.ts +6 -4
- package/dist/lib/behavior/focus-scope.d.ts +7 -1
- package/dist/lib/behavior/hide-outside.d.ts +1 -1
- package/dist/lib/behavior/layer-stack.d.ts +3 -2
- package/dist/lib/behavior/menu.d.ts +59 -0
- package/dist/lib/behavior/modal-root.d.ts +2 -0
- package/dist/lib/behavior/range.d.ts +44 -0
- package/dist/lib/color/color.d.ts +41 -0
- package/dist/lib/color/index.d.ts +2 -0
- package/dist/lib/color/intl.d.ts +40 -0
- package/dist/lib/dock-slot-classes.d.ts +2 -0
- package/dist/lib/hold.d.ts +1 -1
- package/dist/lib/locale.d.ts +3 -0
- package/dist/lib/menu-classes.d.ts +2 -0
- package/dist/lib/motion-classes.d.ts +1 -1
- package/dist/lib/overlay-classes.d.ts +1 -0
- package/dist/lib/scroll-track.d.ts +3 -2
- package/dist/lib/section-tokens.d.ts +1 -1
- package/dist/lib/segment-classes.d.ts +2 -0
- package/dist/lib/shadow-layer.d.ts +0 -45
- package/dist/lib/toggle-set.d.ts +44 -0
- package/dist/lib/visually-hidden.d.ts +3 -0
- package/dist/solid/accordion.d.ts +35 -11
- package/dist/solid/avatar.d.ts +8 -4
- package/dist/solid/bottom-sheet/bottom-sheet.d.ts +1 -1
- package/dist/solid/button.d.ts +5 -4
- package/dist/solid/charts.d.ts +1 -1
- package/dist/solid/checkbox.d.ts +14 -4
- package/dist/solid/collapsible.d.ts +33 -5
- package/dist/solid/color-disc.d.ts +1 -1
- package/dist/solid/color-slider.d.ts +1 -1
- package/dist/solid/color-wheel.d.ts +1 -1
- package/dist/solid/context-menu.d.ts +58 -24
- package/dist/solid/dialog-parts.d.ts +8 -8
- package/dist/solid/dropdown-menu.d.ts +55 -24
- package/dist/solid/empty.d.ts +1 -1
- package/dist/solid/form.d.ts +5 -6
- package/dist/solid/hover-card.d.ts +65 -9
- package/dist/solid/index.d.ts +1 -1
- package/dist/solid/index.js +7725 -13458
- package/dist/solid/input-group.d.ts +6 -6
- package/dist/solid/item.d.ts +1 -1
- package/dist/solid/menu-parts.d.ts +175 -24
- package/dist/solid/option-card.d.ts +1 -4
- package/dist/solid/picker-row.d.ts +2 -0
- package/dist/solid/popover.d.ts +77 -12
- package/dist/solid/progress.d.ts +11 -3
- package/dist/solid/radio-group.d.ts +31 -6
- package/dist/solid/responsive-dialog.d.ts +1 -1
- package/dist/solid/scroll-area.d.ts +1 -1
- package/dist/solid/section-card.d.ts +2 -3
- package/dist/solid/select.d.ts +107 -10
- package/dist/solid/selection.d.ts +10 -1
- package/dist/solid/sheet.d.ts +1 -1
- package/dist/solid/sliding-indicator.d.ts +1 -1
- package/dist/solid/sonner.d.ts +1 -1
- package/dist/solid/toggle.d.ts +11 -3
- package/dist/solid/tooltip.d.ts +22 -22
- package/dist/tokens/index.js +54 -51
- package/dist/{utils-CxAii-e9.js → utils-3Codx-NZ.js} +527 -489
- package/package.json +3 -6
- package/src/lib/anchor-placement.ts +10 -3
- package/src/lib/balance-rows.ts +115 -40
- package/src/lib/behavior/anchored.ts +26 -8
- package/src/lib/behavior/button-root.tsx +103 -0
- package/src/lib/behavior/collection.ts +66 -29
- package/src/lib/behavior/dismiss.ts +61 -21
- package/src/lib/behavior/focus-scope.ts +31 -6
- package/src/lib/behavior/hide-outside.ts +2 -2
- package/src/lib/behavior/layer-stack.ts +33 -8
- package/src/lib/behavior/menu.ts +470 -0
- package/src/lib/behavior/modal-root.tsx +24 -18
- package/src/lib/behavior/polymorphic.tsx +1 -1
- package/src/lib/behavior/presence.ts +1 -1
- package/src/lib/behavior/range.ts +212 -0
- package/src/lib/clipboard.ts +2 -3
- package/src/lib/color/color.ts +746 -0
- package/src/lib/color/index.ts +2 -0
- package/src/lib/color/intl.ts +60 -0
- package/src/lib/dock-slot-classes.ts +3 -0
- package/src/lib/hold.ts +1 -1
- package/src/lib/locale.ts +62 -0
- package/src/lib/menu-classes.ts +3 -0
- package/src/lib/motion-classes.ts +7 -7
- package/src/lib/overlay-classes.ts +1 -3
- package/src/lib/scroll-track.ts +11 -6
- package/src/lib/section-tokens.ts +1 -1
- package/src/lib/segment-classes.ts +3 -0
- package/src/lib/shadow-layer.ts +0 -118
- package/src/lib/toggle-set.ts +133 -0
- package/src/lib/visually-hidden.ts +15 -0
- package/src/solid/accordion.tsx +294 -20
- package/src/solid/area-picker.tsx +3 -2
- package/src/solid/avatar.tsx +98 -24
- package/src/solid/bottom-sheet/bottom-sheet.tsx +1 -1
- package/src/solid/button.tsx +12 -6
- package/src/solid/charts.tsx +1 -1
- package/src/solid/checkbox.tsx +167 -44
- package/src/solid/collapsible.tsx +260 -12
- package/src/solid/color-disc.tsx +11 -12
- package/src/solid/color-slider.tsx +114 -47
- package/src/solid/color-wheel.tsx +137 -39
- package/src/solid/context-menu.tsx +285 -94
- package/src/solid/dialog-parts.tsx +9 -9
- package/src/solid/dock.tsx +3 -2
- package/src/solid/dropdown-menu.tsx +252 -115
- package/src/solid/empty.tsx +1 -1
- package/src/solid/entity-selector.tsx +3 -2
- package/src/solid/face-toggle.tsx +43 -18
- package/src/solid/form.tsx +5 -6
- package/src/solid/glyph-tile.tsx +48 -21
- package/src/solid/hover-card.tsx +287 -25
- package/src/solid/image-picker.tsx +2 -2
- package/src/solid/index.ts +1 -3
- package/src/solid/input-group.tsx +2 -2
- package/src/solid/item.tsx +1 -1
- package/src/solid/menu-parts.tsx +619 -145
- package/src/solid/option-card.tsx +6 -10
- package/src/solid/picker-row.tsx +5 -1
- package/src/solid/popover.tsx +424 -79
- package/src/solid/preview-tile.tsx +1 -1
- package/src/solid/progress.tsx +84 -15
- package/src/solid/radio-group.tsx +271 -34
- package/src/solid/responsive-dialog.tsx +1 -1
- package/src/solid/schema-form.tsx +1 -6
- package/src/solid/scroll-area.tsx +1 -1
- package/src/solid/section-card.tsx +3 -4
- package/src/solid/select.tsx +950 -95
- package/src/solid/selection.tsx +142 -66
- package/src/solid/sheet.tsx +1 -1
- package/src/solid/slider.tsx +115 -65
- package/src/solid/sliding-indicator.tsx +1 -1
- package/src/solid/sonner.tsx +1 -1
- package/src/solid/tabs.tsx +12 -6
- package/src/solid/term.tsx +3 -4
- package/src/solid/toggle-group.tsx +29 -99
- package/src/solid/toggle.tsx +43 -9
- package/src/solid/tooltip.tsx +24 -24
- package/src/styles/globals.css +61 -7
- package/src/styles/theme.css +7 -4
- package/src/tokens/material.ts +11 -2
|
@@ -0,0 +1,470 @@
|
|
|
1
|
+
import { type Accessor, createSignal, onCleanup, untrack } from "solid-js";
|
|
2
|
+
import type { Align, Side } from "../anchor-placement.js";
|
|
3
|
+
import { type AnchorLike, createAnchored } from "./anchored.js";
|
|
4
|
+
import { createCollection } from "./collection.js";
|
|
5
|
+
import { createDismiss } from "./dismiss.js";
|
|
6
|
+
import { focusWithoutTracking } from "./focus-scope.js";
|
|
7
|
+
import { registerLayer } from "./layer-stack.js";
|
|
8
|
+
import { effect } from "./solid.js";
|
|
9
|
+
|
|
10
|
+
const MENU_ITEMS = '[role="menuitem"], [role="menuitemcheckbox"], [role="menuitemradio"]';
|
|
11
|
+
export const SUBMENU_OPEN_DELAY_MS = 100;
|
|
12
|
+
export const POINTER_GRACE_MS = 300;
|
|
13
|
+
|
|
14
|
+
/** Where focus goes as a menu opens: an item, the panel itself, or nowhere (a submenu opened by hover). */
|
|
15
|
+
export type MenuFocus = "first" | "last" | "content" | "none";
|
|
16
|
+
|
|
17
|
+
type Dir = "ltr" | "rtl";
|
|
18
|
+
type Point = { x: number; y: number };
|
|
19
|
+
|
|
20
|
+
export interface MenuItemEntry {
|
|
21
|
+
disabled: Accessor<boolean>;
|
|
22
|
+
closeOnSelect: Accessor<boolean>;
|
|
23
|
+
onSelect: () => void;
|
|
24
|
+
submenu?: Menu;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface MenuOptions {
|
|
28
|
+
open: Accessor<boolean>;
|
|
29
|
+
onOpenChange: (open: boolean) => void;
|
|
30
|
+
parent?: Menu;
|
|
31
|
+
/** What the menu sits beside, given its trigger; unset, the trigger itself, as a submenu always does. */
|
|
32
|
+
anchor?: (trigger: HTMLElement | undefined) => AnchorLike | undefined;
|
|
33
|
+
/** The panel covers its anchor from the anchor's top edge, as createAnchored's coverAnchor. */
|
|
34
|
+
coverAnchor?: boolean;
|
|
35
|
+
side?: Accessor<Side>;
|
|
36
|
+
align?: Accessor<Align>;
|
|
37
|
+
offset?: number;
|
|
38
|
+
dir?: Accessor<Dir>;
|
|
39
|
+
/** A press on the trigger counts as outside: a context menu's trigger is the area it opens over. */
|
|
40
|
+
triggerIsArea?: boolean;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface Menu {
|
|
44
|
+
parent: Menu | undefined;
|
|
45
|
+
open: Accessor<boolean>;
|
|
46
|
+
dir: Accessor<Dir>;
|
|
47
|
+
content: Accessor<HTMLElement | undefined>;
|
|
48
|
+
setContent: (el: HTMLElement | undefined) => void;
|
|
49
|
+
trigger: Accessor<HTMLElement | undefined>;
|
|
50
|
+
setTrigger: (el: HTMLElement | undefined) => void;
|
|
51
|
+
highlighted: Accessor<string | undefined>;
|
|
52
|
+
tabIndex: (key: string) => 0 | -1;
|
|
53
|
+
show: (focus: MenuFocus) => void;
|
|
54
|
+
/** Closes this menu and its submenus; with `returnFocus`, focus held inside goes back to the trigger. */
|
|
55
|
+
hide: (returnFocus: boolean) => void;
|
|
56
|
+
/** Closes every menu from the root down, as choosing an item does. */
|
|
57
|
+
closeTree: () => void;
|
|
58
|
+
register: (key: string, entry: MenuItemEntry) => void;
|
|
59
|
+
holdsFocus: () => boolean;
|
|
60
|
+
unhighlight: () => void;
|
|
61
|
+
/** A submenu joins its parent, so focus inside it counts as inside the tree. */
|
|
62
|
+
adopt: (child: Menu) => void;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export interface MenuButton {
|
|
66
|
+
onPointerDown: (event: PointerEvent) => void;
|
|
67
|
+
onClick: () => void;
|
|
68
|
+
onKeyDown: (event: KeyboardEvent) => void;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const isDisabled = (el: Element) =>
|
|
72
|
+
el.getAttribute("aria-disabled") === "true" || el.hasAttribute("data-disabled");
|
|
73
|
+
|
|
74
|
+
const activeIn = (el: Element): Element | null =>
|
|
75
|
+
(el.getRootNode() as Document | ShadowRoot).activeElement;
|
|
76
|
+
|
|
77
|
+
function inside(point: Point, polygon: Point[]): boolean {
|
|
78
|
+
let hit = false;
|
|
79
|
+
for (let i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
|
|
80
|
+
const a = polygon[i];
|
|
81
|
+
const b = polygon[j];
|
|
82
|
+
if (!a || !b) continue;
|
|
83
|
+
const crosses =
|
|
84
|
+
a.y > point.y !== b.y > point.y &&
|
|
85
|
+
point.x < ((b.x - a.x) * (point.y - a.y)) / (b.y - a.y) + a.x;
|
|
86
|
+
if (crosses) hit = !hit;
|
|
87
|
+
}
|
|
88
|
+
return hit;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** The pointer's exit point fanned out to the submenu's whole panel. */
|
|
92
|
+
function graceArea(exit: Point, panel: DOMRect, side: "left" | "right"): Point[] {
|
|
93
|
+
const toRight = side === "right";
|
|
94
|
+
const near = toRight ? panel.left : panel.right;
|
|
95
|
+
const far = toRight ? panel.right : panel.left;
|
|
96
|
+
return [
|
|
97
|
+
{ x: exit.x + (toRight ? -5 : 5), y: exit.y },
|
|
98
|
+
{ x: near, y: panel.top },
|
|
99
|
+
{ x: far, y: panel.top },
|
|
100
|
+
{ x: far, y: panel.bottom },
|
|
101
|
+
{ x: near, y: panel.bottom },
|
|
102
|
+
];
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export function createMenu(options: MenuOptions): Menu {
|
|
106
|
+
const parent = options.parent;
|
|
107
|
+
const [content, setContent] = createSignal<HTMLElement>();
|
|
108
|
+
const [trigger, setTrigger] = createSignal<HTMLElement>();
|
|
109
|
+
const [active, setActive] = createSignal<string>();
|
|
110
|
+
const entries = new Map<string, MenuItemEntry>();
|
|
111
|
+
const children = new Set<Menu>();
|
|
112
|
+
const dir = (): Dir =>
|
|
113
|
+
options.dir?.() ??
|
|
114
|
+
parent?.dir() ??
|
|
115
|
+
(trigger()?.closest("[dir]")?.getAttribute("dir") === "rtl" ? "rtl" : "ltr");
|
|
116
|
+
const open = () => options.open() && (parent?.open() ?? true);
|
|
117
|
+
const restingFocus: MenuFocus = parent ? "none" : "content";
|
|
118
|
+
let pending: MenuFocus = restingFocus;
|
|
119
|
+
|
|
120
|
+
const collection = createCollection({
|
|
121
|
+
root: content,
|
|
122
|
+
itemSelector: MENU_ITEMS,
|
|
123
|
+
orientation: () => "vertical",
|
|
124
|
+
focus: "roving",
|
|
125
|
+
loop: () => true,
|
|
126
|
+
dir,
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
const enabledItems = (el: HTMLElement) =>
|
|
130
|
+
[...el.querySelectorAll<HTMLElement>(MENU_ITEMS)].filter((item) => !isDisabled(item));
|
|
131
|
+
|
|
132
|
+
const itemOf = (target: EventTarget | null): HTMLElement | undefined => {
|
|
133
|
+
const el = content();
|
|
134
|
+
if (!el || !(target instanceof Element)) return undefined;
|
|
135
|
+
const item = target.closest<HTMLElement>(MENU_ITEMS);
|
|
136
|
+
return item && el.contains(item) && entries.has(item.dataset.key ?? "") ? item : undefined;
|
|
137
|
+
};
|
|
138
|
+
const entryOf = (item: HTMLElement) => entries.get(item.dataset.key ?? "");
|
|
139
|
+
|
|
140
|
+
function highlight(key: string) {
|
|
141
|
+
setActive(key);
|
|
142
|
+
collection.highlight(key, "pointer");
|
|
143
|
+
for (const [other, entry] of entries) {
|
|
144
|
+
if (other !== key && entry.submenu && untrack(entry.submenu.open)) entry.submenu.hide(false);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function focusItem(item: HTMLElement) {
|
|
149
|
+
focusWithoutTracking(item);
|
|
150
|
+
const key = item.dataset.key;
|
|
151
|
+
if (key) highlight(key);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function applyFocus(el: HTMLElement, focus: MenuFocus) {
|
|
155
|
+
if (focus === "none") return;
|
|
156
|
+
const items = enabledItems(el);
|
|
157
|
+
const target = focus === "first" ? items[0] : focus === "last" ? items.at(-1) : undefined;
|
|
158
|
+
if (target) focusItem(target);
|
|
159
|
+
else focusWithoutTracking(el);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const holdsFocus = (): boolean => {
|
|
163
|
+
const el = content();
|
|
164
|
+
const focused = el ? activeIn(el) : null;
|
|
165
|
+
if (el && focused && el.contains(focused)) return true;
|
|
166
|
+
return [...children].some((child) => child.holdsFocus());
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
let openTimer: ReturnType<typeof setTimeout> | undefined;
|
|
170
|
+
const cancelOpenTimer = () => {
|
|
171
|
+
clearTimeout(openTimer);
|
|
172
|
+
openTimer = undefined;
|
|
173
|
+
};
|
|
174
|
+
let grace: { area: Point[]; timer: ReturnType<typeof setTimeout> } | undefined;
|
|
175
|
+
const clearGrace = () => {
|
|
176
|
+
if (grace) clearTimeout(grace.timer);
|
|
177
|
+
grace = undefined;
|
|
178
|
+
};
|
|
179
|
+
const inGrace = (event: PointerEvent) =>
|
|
180
|
+
grace !== undefined && inside({ x: event.clientX, y: event.clientY }, grace.area);
|
|
181
|
+
onCleanup(() => {
|
|
182
|
+
cancelOpenTimer();
|
|
183
|
+
clearGrace();
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
function show(focus: MenuFocus) {
|
|
187
|
+
const el = content();
|
|
188
|
+
if (untrack(open) && el) return applyFocus(el, focus);
|
|
189
|
+
pending = focus;
|
|
190
|
+
if (!untrack(options.open)) options.onOpenChange(true);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function hide(returnFocus: boolean) {
|
|
194
|
+
const held = holdsFocus();
|
|
195
|
+
cancelOpenTimer();
|
|
196
|
+
clearGrace();
|
|
197
|
+
if (untrack(options.open)) options.onOpenChange(false);
|
|
198
|
+
if (returnFocus && held) focusWithoutTracking(trigger());
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
const menu: Menu = {
|
|
202
|
+
parent,
|
|
203
|
+
open,
|
|
204
|
+
dir,
|
|
205
|
+
content,
|
|
206
|
+
setContent,
|
|
207
|
+
trigger,
|
|
208
|
+
setTrigger,
|
|
209
|
+
highlighted: active,
|
|
210
|
+
tabIndex: collection.tabIndex,
|
|
211
|
+
show,
|
|
212
|
+
hide,
|
|
213
|
+
closeTree: () => (parent ? parent.closeTree() : hide(true)),
|
|
214
|
+
register(key, entry) {
|
|
215
|
+
entries.set(key, entry);
|
|
216
|
+
collection.refresh();
|
|
217
|
+
onCleanup(() => {
|
|
218
|
+
if (entries.get(key) === entry) entries.delete(key);
|
|
219
|
+
if (untrack(active) === key) setActive(undefined);
|
|
220
|
+
collection.refresh();
|
|
221
|
+
});
|
|
222
|
+
},
|
|
223
|
+
holdsFocus,
|
|
224
|
+
unhighlight: () => {
|
|
225
|
+
clearGrace();
|
|
226
|
+
setActive(undefined);
|
|
227
|
+
},
|
|
228
|
+
adopt(child) {
|
|
229
|
+
children.add(child);
|
|
230
|
+
onCleanup(() => children.delete(child));
|
|
231
|
+
},
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
if (parent) {
|
|
235
|
+
parent.adopt(menu);
|
|
236
|
+
effect(parent.open, (parentOpen) => {
|
|
237
|
+
if (!parentOpen && untrack(options.open)) options.onOpenChange(false);
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
effect(
|
|
242
|
+
() => [open(), content()] as const,
|
|
243
|
+
([isOpen, el]) => {
|
|
244
|
+
if (!isOpen || !el) return;
|
|
245
|
+
const focus = pending;
|
|
246
|
+
pending = restingFocus;
|
|
247
|
+
applyFocus(el, focus);
|
|
248
|
+
},
|
|
249
|
+
);
|
|
250
|
+
|
|
251
|
+
const layer = registerLayer({
|
|
252
|
+
element: content,
|
|
253
|
+
open,
|
|
254
|
+
kind: "menu",
|
|
255
|
+
escape: () => true,
|
|
256
|
+
trigger: () => (options.triggerIsArea ? undefined : trigger()),
|
|
257
|
+
dismiss: () => hide(true),
|
|
258
|
+
});
|
|
259
|
+
createDismiss({ layer, onDismiss: (reason) => hide(reason === "escape") });
|
|
260
|
+
|
|
261
|
+
createAnchored({
|
|
262
|
+
anchor: () => (options.anchor ? options.anchor(trigger()) : trigger()),
|
|
263
|
+
floating: content,
|
|
264
|
+
open,
|
|
265
|
+
side: () => options.side?.() ?? (dir() === "rtl" ? "left" : "right"),
|
|
266
|
+
align: () => options.align?.() ?? "start",
|
|
267
|
+
offset: options.offset ?? 0,
|
|
268
|
+
coverAnchor: options.coverAnchor,
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
function activate(item: HTMLElement, by: "pointer" | "keyboard") {
|
|
272
|
+
const entry = entryOf(item);
|
|
273
|
+
if (!entry || entry.disabled() || !untrack(open)) return;
|
|
274
|
+
if (entry.submenu) return entry.submenu.show(by === "keyboard" ? "first" : "none");
|
|
275
|
+
entry.onSelect();
|
|
276
|
+
if (entry.closeOnSelect()) menu.closeTree();
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
function leaveItem(el: HTMLElement) {
|
|
280
|
+
setActive(undefined);
|
|
281
|
+
const focused = activeIn(el);
|
|
282
|
+
if (focused && el.contains(focused)) focusWithoutTracking(el);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
const openKeys = () => (dir() === "rtl" ? "ArrowLeft" : "ArrowRight");
|
|
286
|
+
const closeKeys = () => (dir() === "rtl" ? "ArrowRight" : "ArrowLeft");
|
|
287
|
+
|
|
288
|
+
effect(content, (el) => {
|
|
289
|
+
if (!el) return;
|
|
290
|
+
let pressedInside = false;
|
|
291
|
+
let pointerPressed = false;
|
|
292
|
+
const release = () => {
|
|
293
|
+
pressedInside = false;
|
|
294
|
+
};
|
|
295
|
+
const onDown = () => {
|
|
296
|
+
pressedInside = true;
|
|
297
|
+
pointerPressed = true;
|
|
298
|
+
window.addEventListener("pointerup", release, { once: true });
|
|
299
|
+
};
|
|
300
|
+
// Only a press that began inside the panel chooses: the press that opened it ends here too.
|
|
301
|
+
const onUp = (event: PointerEvent) => {
|
|
302
|
+
const pressed = pressedInside;
|
|
303
|
+
pressedInside = false;
|
|
304
|
+
const item = itemOf(event.target);
|
|
305
|
+
if (pressed && item && event.button === 0) activate(item, "pointer");
|
|
306
|
+
};
|
|
307
|
+
const onMove = (event: PointerEvent) => {
|
|
308
|
+
if (event.pointerType !== "mouse") return;
|
|
309
|
+
if (inGrace(event)) return;
|
|
310
|
+
clearGrace();
|
|
311
|
+
const item = itemOf(event.target);
|
|
312
|
+
const entry = item && entryOf(item);
|
|
313
|
+
if (!item || !entry) return;
|
|
314
|
+
if (entry.disabled()) return leaveItem(el);
|
|
315
|
+
if (untrack(active) !== item.dataset.key || activeIn(item) !== item) focusItem(item);
|
|
316
|
+
const sub = entry.submenu;
|
|
317
|
+
if (sub && !untrack(sub.open) && openTimer === undefined) {
|
|
318
|
+
openTimer = setTimeout(() => {
|
|
319
|
+
openTimer = undefined;
|
|
320
|
+
sub.show("none");
|
|
321
|
+
}, SUBMENU_OPEN_DELAY_MS);
|
|
322
|
+
}
|
|
323
|
+
};
|
|
324
|
+
const onOut = (event: PointerEvent) => {
|
|
325
|
+
if (event.pointerType !== "mouse") return;
|
|
326
|
+
const item = itemOf(event.target);
|
|
327
|
+
if (!item || (event.relatedTarget instanceof Node && item.contains(event.relatedTarget)))
|
|
328
|
+
return;
|
|
329
|
+
cancelOpenTimer();
|
|
330
|
+
const sub = entryOf(item)?.submenu;
|
|
331
|
+
const panel = sub && untrack(sub.open) ? sub.content() : undefined;
|
|
332
|
+
if (panel) {
|
|
333
|
+
clearGrace();
|
|
334
|
+
const side = panel.getAttribute("data-side") === "left" ? "left" : "right";
|
|
335
|
+
grace = {
|
|
336
|
+
area: graceArea(
|
|
337
|
+
{ x: event.clientX, y: event.clientY },
|
|
338
|
+
panel.getBoundingClientRect(),
|
|
339
|
+
side,
|
|
340
|
+
),
|
|
341
|
+
timer: setTimeout(clearGrace, POINTER_GRACE_MS),
|
|
342
|
+
};
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
if (!inGrace(event)) leaveItem(el);
|
|
346
|
+
};
|
|
347
|
+
const onEnter = () => parent?.unhighlight();
|
|
348
|
+
// Enter on a link row also clicks it; that click is the same activation.
|
|
349
|
+
let linkKeyed = false;
|
|
350
|
+
// A click with no pointer press before it comes from assistive technology.
|
|
351
|
+
const onClick = (event: MouseEvent) => {
|
|
352
|
+
const skip = linkKeyed || pointerPressed;
|
|
353
|
+
linkKeyed = false;
|
|
354
|
+
pointerPressed = false;
|
|
355
|
+
if (skip) return;
|
|
356
|
+
const item = itemOf(event.target);
|
|
357
|
+
if (item) activate(item, "keyboard");
|
|
358
|
+
};
|
|
359
|
+
const onFocusIn = (event: FocusEvent) => {
|
|
360
|
+
const key = itemOf(event.target)?.dataset.key;
|
|
361
|
+
if (key && untrack(active) !== key) highlight(key);
|
|
362
|
+
};
|
|
363
|
+
const onKey = (event: KeyboardEvent) => {
|
|
364
|
+
if (!(event.target instanceof Node) || !el.contains(event.target)) return;
|
|
365
|
+
if (event.key === "Tab") return event.preventDefault();
|
|
366
|
+
const item = itemOf(event.target);
|
|
367
|
+
const entry = item && entryOf(item);
|
|
368
|
+
if (item && entry && !entry.disabled()) {
|
|
369
|
+
if ((event.key === "Enter" || event.key === " ") && !event.repeat) {
|
|
370
|
+
if (event.key === " " || !item.matches("a[href]")) event.preventDefault();
|
|
371
|
+
else linkKeyed = true;
|
|
372
|
+
return activate(item, "keyboard");
|
|
373
|
+
}
|
|
374
|
+
if (event.key === openKeys() && entry.submenu) {
|
|
375
|
+
event.preventDefault();
|
|
376
|
+
return entry.submenu.show("first");
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
if (parent && event.key === closeKeys()) {
|
|
380
|
+
event.preventDefault();
|
|
381
|
+
event.stopPropagation();
|
|
382
|
+
return hide(true);
|
|
383
|
+
}
|
|
384
|
+
if (event.target === el) {
|
|
385
|
+
const items = enabledItems(el);
|
|
386
|
+
const ends: Record<string, HTMLElement | undefined> = {
|
|
387
|
+
ArrowDown: items[0],
|
|
388
|
+
Home: items[0],
|
|
389
|
+
ArrowUp: items.at(-1),
|
|
390
|
+
End: items.at(-1),
|
|
391
|
+
};
|
|
392
|
+
const target = ends[event.key];
|
|
393
|
+
if (target) {
|
|
394
|
+
event.preventDefault();
|
|
395
|
+
return focusItem(target);
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
collection.onKeyDown(event);
|
|
399
|
+
};
|
|
400
|
+
el.addEventListener("pointerdown", onDown, true);
|
|
401
|
+
el.addEventListener("pointerup", onUp);
|
|
402
|
+
el.addEventListener("pointermove", onMove);
|
|
403
|
+
el.addEventListener("pointerout", onOut);
|
|
404
|
+
el.addEventListener("pointerenter", onEnter);
|
|
405
|
+
el.addEventListener("focusin", onFocusIn);
|
|
406
|
+
el.addEventListener("keydown", onKey);
|
|
407
|
+
el.addEventListener("click", onClick);
|
|
408
|
+
return () => {
|
|
409
|
+
el.removeEventListener("pointerdown", onDown, true);
|
|
410
|
+
el.removeEventListener("pointerup", onUp);
|
|
411
|
+
el.removeEventListener("pointermove", onMove);
|
|
412
|
+
el.removeEventListener("pointerout", onOut);
|
|
413
|
+
el.removeEventListener("pointerenter", onEnter);
|
|
414
|
+
el.removeEventListener("focusin", onFocusIn);
|
|
415
|
+
el.removeEventListener("keydown", onKey);
|
|
416
|
+
el.removeEventListener("click", onClick);
|
|
417
|
+
window.removeEventListener("pointerup", release);
|
|
418
|
+
setActive(undefined);
|
|
419
|
+
// An outside press on nothing focusable leaves focus nowhere; it goes back to the trigger.
|
|
420
|
+
if (!focusLost(el) || (parent && !untrack(parent.open))) return;
|
|
421
|
+
focusWithoutTracking(trigger());
|
|
422
|
+
};
|
|
423
|
+
});
|
|
424
|
+
|
|
425
|
+
return menu;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
function focusLost(el: HTMLElement): boolean {
|
|
429
|
+
const doc = el.ownerDocument;
|
|
430
|
+
const focused = doc.activeElement;
|
|
431
|
+
if (!focused || focused === doc.body) return true;
|
|
432
|
+
const inner = activeIn(el);
|
|
433
|
+
return inner !== null && el.contains(inner);
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
/** The APG menu button: a mouse press toggles, a finger toggles on its click, Enter, Space and ArrowDown open on the first item, ArrowUp on the last. */
|
|
437
|
+
export function createMenuButton(menu: Menu, disabled: Accessor<boolean>): MenuButton {
|
|
438
|
+
let pointerType = "";
|
|
439
|
+
const keepFocus = (event: Event) => event.preventDefault();
|
|
440
|
+
const toggle = () => (untrack(menu.open) ? menu.hide(true) : menu.show("content"));
|
|
441
|
+
const keys: Record<string, MenuFocus> = {
|
|
442
|
+
Enter: "first",
|
|
443
|
+
" ": "first",
|
|
444
|
+
ArrowDown: "first",
|
|
445
|
+
ArrowUp: "last",
|
|
446
|
+
};
|
|
447
|
+
return {
|
|
448
|
+
onPointerDown(event) {
|
|
449
|
+
pointerType = event.pointerType;
|
|
450
|
+
if (disabled() || event.pointerType === "touch" || event.button !== 0) return;
|
|
451
|
+
// The press's mousedown would focus the trigger after the menu took focus.
|
|
452
|
+
if (!untrack(menu.open))
|
|
453
|
+
event.currentTarget?.addEventListener("mousedown", keepFocus, { once: true });
|
|
454
|
+
toggle();
|
|
455
|
+
},
|
|
456
|
+
// A click with no pointer press before it comes from assistive technology.
|
|
457
|
+
onClick() {
|
|
458
|
+
if (!disabled() && (pointerType === "touch" || pointerType === "")) toggle();
|
|
459
|
+
pointerType = "";
|
|
460
|
+
},
|
|
461
|
+
onKeyDown(event) {
|
|
462
|
+
pointerType = "";
|
|
463
|
+
const focus = keys[event.key];
|
|
464
|
+
if (disabled() || !focus) return;
|
|
465
|
+
event.preventDefault();
|
|
466
|
+
event.stopPropagation();
|
|
467
|
+
menu.show(focus);
|
|
468
|
+
},
|
|
469
|
+
};
|
|
470
|
+
}
|
|
@@ -14,15 +14,15 @@ import {
|
|
|
14
14
|
type ValidComponent,
|
|
15
15
|
} from "solid-js";
|
|
16
16
|
import { Portal } from "solid-js/web";
|
|
17
|
-
import { LAYER_ROOT } from "../overlay-classes";
|
|
18
|
-
import { createControllableSignal } from "./controllable";
|
|
19
|
-
import { createDismiss } from "./dismiss";
|
|
20
|
-
import { createFocusScope, focusWithoutTracking } from "./focus-scope";
|
|
21
|
-
import { createHideOutside } from "./hide-outside";
|
|
22
|
-
import { layerStack, registerLayer } from "./layer-stack";
|
|
23
|
-
import { Polymorphic } from "./polymorphic";
|
|
24
|
-
import { createPresence } from "./presence";
|
|
25
|
-
import { callEventHandler, effect, merge } from "./solid";
|
|
17
|
+
import { LAYER_ROOT } from "../overlay-classes.js";
|
|
18
|
+
import { createControllableSignal } from "./controllable.js";
|
|
19
|
+
import { createDismiss } from "./dismiss.js";
|
|
20
|
+
import { activeOf, createFocusScope, focusWithoutTracking } from "./focus-scope.js";
|
|
21
|
+
import { createHideOutside } from "./hide-outside.js";
|
|
22
|
+
import { layerStack, registerLayer } from "./layer-stack.js";
|
|
23
|
+
import { Polymorphic } from "./polymorphic.js";
|
|
24
|
+
import { createPresence } from "./presence.js";
|
|
25
|
+
import { callEventHandler, effect, merge } from "./solid.js";
|
|
26
26
|
|
|
27
27
|
export type ModalRole = "dialog" | "alertdialog";
|
|
28
28
|
|
|
@@ -228,7 +228,10 @@ export type PointerDownOutsideEvent = CustomEvent<OutsideDetail<PointerEvent>>;
|
|
|
228
228
|
export type FocusOutsideEvent = CustomEvent<OutsideDetail<FocusEvent>>;
|
|
229
229
|
export type InteractOutsideEvent = PointerDownOutsideEvent | FocusOutsideEvent;
|
|
230
230
|
|
|
231
|
-
function vetoed<E extends Event>(
|
|
231
|
+
export function vetoed<E extends Event>(
|
|
232
|
+
handler: ((event: E) => void) | undefined,
|
|
233
|
+
event: E,
|
|
234
|
+
): boolean {
|
|
232
235
|
handler?.(event);
|
|
233
236
|
return event.defaultPrevented;
|
|
234
237
|
}
|
|
@@ -242,13 +245,16 @@ function isContextMenu(event: PointerEvent): boolean {
|
|
|
242
245
|
|
|
243
246
|
const OUTSIDE_EVENT = "interactoutside";
|
|
244
247
|
|
|
245
|
-
function
|
|
246
|
-
original:
|
|
247
|
-
handle: (event:
|
|
248
|
-
):
|
|
249
|
-
const event
|
|
248
|
+
export function interactOutside<T extends PointerEvent | FocusEvent>(
|
|
249
|
+
original: T,
|
|
250
|
+
handle: (event: CustomEvent<OutsideDetail<T>>) => void,
|
|
251
|
+
): CustomEvent<OutsideDetail<T>> {
|
|
252
|
+
const event = new CustomEvent<OutsideDetail<T>>(OUTSIDE_EVENT, {
|
|
250
253
|
cancelable: true,
|
|
251
|
-
detail: {
|
|
254
|
+
detail: {
|
|
255
|
+
originalEvent: original,
|
|
256
|
+
isContextMenu: original.type === "pointerdown" && isContextMenu(original as PointerEvent),
|
|
257
|
+
},
|
|
252
258
|
});
|
|
253
259
|
const target = original.target;
|
|
254
260
|
// Dispatched on the pressed node so handlers read its target, as Kobalte's did.
|
|
@@ -332,7 +338,7 @@ export function ModalFrame(props: ModalFrameProps): JSX.Element {
|
|
|
332
338
|
if (reason === "escape") {
|
|
333
339
|
if (vetoed(local.onEscapeKeyDown, original as KeyboardEvent)) return;
|
|
334
340
|
} else {
|
|
335
|
-
const event =
|
|
341
|
+
const event = interactOutside(original as PointerEvent, (e) => {
|
|
336
342
|
local.onPointerDownOutside?.(e);
|
|
337
343
|
if (ctx.modal() && e.detail.isContextMenu) e.preventDefault();
|
|
338
344
|
local.onInteractOutside?.(e);
|
|
@@ -356,7 +362,7 @@ export function ModalFrame(props: ModalFrameProps): JSX.Element {
|
|
|
356
362
|
initialFocus: (container) => {
|
|
357
363
|
if (!vetoed(local.onOpenAutoFocus, new Event("openautofocus", { cancelable: true })))
|
|
358
364
|
return null;
|
|
359
|
-
const active = (container
|
|
365
|
+
const active = activeOf(container);
|
|
360
366
|
return active instanceof HTMLElement && container.contains(active) ? active : container;
|
|
361
367
|
},
|
|
362
368
|
});
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type ComponentProps, type JSX, type ValidComponent } from "solid-js";
|
|
2
2
|
import { Dynamic } from "solid-js/web";
|
|
3
|
-
import { merge, omit } from "./solid";
|
|
3
|
+
import { merge, omit } from "./solid.js";
|
|
4
4
|
|
|
5
5
|
export type PolymorphicProps<D extends ValidComponent> = {
|
|
6
6
|
as?: ValidComponent;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type Accessor, createMemo, createSignal } from "solid-js";
|
|
2
|
-
import { effect } from "./solid";
|
|
2
|
+
import { effect } from "./solid.js";
|
|
3
3
|
|
|
4
4
|
export type PresencePhase = "opening" | "open" | "closing" | "closed";
|
|
5
5
|
export type MotionEvent = AnimationEvent | TransitionEvent;
|