loseta 0.17.0 → 0.19.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/README.md +37 -8
- package/dist/commands/add.d.ts +8 -0
- package/dist/commands/add.js +60 -15
- package/dist/commands/add.js.map +1 -1
- package/dist/commands/init.js +2 -1
- package/dist/commands/init.js.map +1 -1
- package/dist/config.d.ts +7 -0
- package/dist/config.js +13 -0
- package/dist/config.js.map +1 -1
- package/dist/imports.d.ts +4 -0
- package/dist/imports.js +30 -2
- package/dist/imports.js.map +1 -1
- package/dist/install.d.ts +23 -3
- package/dist/install.js +84 -5
- package/dist/install.js.map +1 -1
- package/dist/style-switch.js +2 -1
- package/dist/style-switch.js.map +1 -1
- package/dist/types.d.ts +11 -0
- package/dist/ui.js +4 -1
- package/dist/ui.js.map +1 -1
- package/package.json +4 -3
- package/registry/behaviors/calendar.js +855 -0
- package/registry/behaviors/carousel.js +280 -0
- package/registry/behaviors/code-block.js +106 -0
- package/registry/behaviors/combobox.js +237 -0
- package/registry/behaviors/command.js +105 -0
- package/registry/behaviors/core.js +302 -0
- package/registry/behaviors/dialog.js +97 -0
- package/registry/behaviors/hover-overlay.js +180 -0
- package/registry/behaviors/listbox.js +135 -0
- package/registry/behaviors/menu.js +586 -0
- package/registry/behaviors/message-scroller.js +147 -0
- package/registry/behaviors/popover.js +127 -0
- package/registry/behaviors/questionnaire.js +91 -0
- package/registry/behaviors/resizable.js +463 -0
- package/registry/behaviors/select.js +196 -0
- package/registry/behaviors/tabs.js +95 -0
- package/registry/behaviors/toast.js +137 -0
- package/registry/components/carousel.css +1 -0
- package/registry/components/code-block.css +19 -0
- package/registry/components/hover-card.css +2 -2
- package/registry/components/resizable.css +49 -0
- package/registry/components/tooltip.css +4 -1
- package/registry/examples/alert-dialog.html +21 -21
- package/registry/examples/button-group.html +3 -3
- package/registry/examples/calendar.html +387 -326
- package/registry/examples/carousel.html +80 -2
- package/registry/examples/code-block.html +10 -4
- package/registry/examples/combobox.html +2 -0
- package/registry/examples/command.html +17 -17
- package/registry/examples/context-menu.html +18 -18
- package/registry/examples/dialog.html +15 -15
- package/registry/examples/drawer.html +22 -22
- package/registry/examples/questionnaire.html +2 -2
- package/registry/examples/resizable.html +88 -15
- package/registry/examples/select.html +7 -0
- package/registry/examples/sheet.html +23 -23
- package/registry/examples/sidebar.html +11 -11
- package/registry/examples/toast.html +11 -0
- package/registry/primitives/dialog-shell.css +5 -0
- package/registry/registry.json +280 -1
- package/registry/schema.json +17 -0
- package/registry/styles/luma/resizable.css +10 -0
- package/registry/styles/lyra/resizable.css +10 -0
- package/registry/styles/maia/resizable.css +10 -0
- package/registry/styles/mira/resizable.css +10 -0
- package/registry/styles/nova/resizable.css +10 -0
- package/registry/styles/rhea/resizable.css +10 -0
- package/registry/styles/sera/resizable.css +10 -0
- package/registry/styles/vega/resizable.css +10 -0
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
3
|
+
import { init, on, uniqueId } from "./core.js";
|
|
4
|
+
import {
|
|
5
|
+
filterOptions,
|
|
6
|
+
firstOption,
|
|
7
|
+
highlight,
|
|
8
|
+
lastOption,
|
|
9
|
+
listboxIn,
|
|
10
|
+
moveHighlight,
|
|
11
|
+
resetFilter
|
|
12
|
+
} from "./listbox.js";
|
|
13
|
+
|
|
14
|
+
/** @param {Element} root */
|
|
15
|
+
function parts(root) {
|
|
16
|
+
const input = root.querySelector("input");
|
|
17
|
+
const listbox = listboxIn(root);
|
|
18
|
+
return {
|
|
19
|
+
input: input instanceof HTMLInputElement ? input : null,
|
|
20
|
+
listbox
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** @param {HTMLInputElement} input @param {HTMLElement} listbox */
|
|
25
|
+
function link(input, listbox) {
|
|
26
|
+
if (!listbox.id) listbox.id = uniqueId("loseta-listbox");
|
|
27
|
+
if (!input.hasAttribute("role")) input.setAttribute("role", "combobox");
|
|
28
|
+
if (!input.hasAttribute("aria-controls")) input.setAttribute("aria-controls", listbox.id);
|
|
29
|
+
if (!input.hasAttribute("aria-expanded")) input.setAttribute("aria-expanded", "true");
|
|
30
|
+
if (!input.hasAttribute("aria-autocomplete")) input.setAttribute("aria-autocomplete", "list");
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** @param {Element} root @param {HTMLInputElement} input */
|
|
34
|
+
function refresh(root, input) {
|
|
35
|
+
const { listbox } = parts(root);
|
|
36
|
+
if (!listbox) return;
|
|
37
|
+
|
|
38
|
+
link(input, listbox);
|
|
39
|
+
if (input.value.trim() === "") {
|
|
40
|
+
resetFilter(listbox);
|
|
41
|
+
} else {
|
|
42
|
+
filterOptions(listbox, input.value);
|
|
43
|
+
}
|
|
44
|
+
highlight(listbox, firstOption(listbox), input);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
on("focusin", ".command input", (_event, target) => {
|
|
48
|
+
if (!(target instanceof HTMLInputElement)) return;
|
|
49
|
+
const root = target.closest(".command");
|
|
50
|
+
if (!root) return;
|
|
51
|
+
const { listbox } = parts(root);
|
|
52
|
+
if (!listbox) return;
|
|
53
|
+
link(target, listbox);
|
|
54
|
+
if (!listbox.querySelector("[data-highlighted]")) {
|
|
55
|
+
highlight(listbox, firstOption(listbox), target);
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
on("input", ".command input", (_event, target) => {
|
|
60
|
+
if (!(target instanceof HTMLInputElement)) return;
|
|
61
|
+
const root = target.closest(".command");
|
|
62
|
+
if (root) refresh(root, target);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
on("keydown", ".command input", (event, target) => {
|
|
66
|
+
if (!(event instanceof KeyboardEvent) || !(target instanceof HTMLInputElement)) return;
|
|
67
|
+
const root = target.closest(".command");
|
|
68
|
+
if (!root) return;
|
|
69
|
+
const { listbox } = parts(root);
|
|
70
|
+
if (!listbox) return;
|
|
71
|
+
|
|
72
|
+
if (event.key === "ArrowDown" || event.key === "ArrowUp") {
|
|
73
|
+
const next = moveHighlight(listbox, event.key === "ArrowDown" ? 1 : -1);
|
|
74
|
+
if (next) highlight(listbox, next, target);
|
|
75
|
+
event.preventDefault();
|
|
76
|
+
return true;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (event.key === "Home" || event.key === "End") {
|
|
80
|
+
highlight(
|
|
81
|
+
listbox,
|
|
82
|
+
event.key === "Home" ? firstOption(listbox) : lastOption(listbox),
|
|
83
|
+
target
|
|
84
|
+
);
|
|
85
|
+
event.preventDefault();
|
|
86
|
+
return true;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (event.key === "Enter") {
|
|
90
|
+
const option = listbox.querySelector("[data-highlighted]");
|
|
91
|
+
if (option instanceof HTMLElement) option.click();
|
|
92
|
+
event.preventDefault();
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
on("pointermove", ".command [role='option']", (event, target) => {
|
|
98
|
+
if (!(event instanceof PointerEvent) || !(target instanceof HTMLElement)) return;
|
|
99
|
+
const root = target.closest(".command");
|
|
100
|
+
if (!root) return;
|
|
101
|
+
const { input, listbox } = parts(root);
|
|
102
|
+
if (listbox) highlight(listbox, target, input);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
init();
|
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Shared behavior primitives. Supports one delegated listener per event type
|
|
5
|
+
* and shared focus, typeahead, overlay and event helpers.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @typedef {object} Handler
|
|
10
|
+
* @property {string} event
|
|
11
|
+
* @property {string | null} selector
|
|
12
|
+
* @property {(event: Event, target: Element) => boolean | void} handler
|
|
13
|
+
* @property {number} priority
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/** @type {Map<string, Handler[]>} */
|
|
17
|
+
const handlers = new Map();
|
|
18
|
+
/** @type {Set<Document | Element>} */
|
|
19
|
+
const roots = new Set();
|
|
20
|
+
/** @type {Set<string>} */
|
|
21
|
+
const listening = new Set();
|
|
22
|
+
/** @type {WeakMap<Element, { value: string, time: number }>} */
|
|
23
|
+
const typeaheadBuffers = new WeakMap();
|
|
24
|
+
|
|
25
|
+
let installed = false;
|
|
26
|
+
let idCounter = 0;
|
|
27
|
+
|
|
28
|
+
const supportsDom = typeof document !== "undefined";
|
|
29
|
+
|
|
30
|
+
export const focusableSelector = [
|
|
31
|
+
"a[href]",
|
|
32
|
+
"button:not([disabled])",
|
|
33
|
+
"input:not([disabled]):not([type='hidden'])",
|
|
34
|
+
"select:not([disabled])",
|
|
35
|
+
"textarea:not([disabled])",
|
|
36
|
+
"summary",
|
|
37
|
+
"[tabindex]:not([tabindex='-1'])"
|
|
38
|
+
].join(", ");
|
|
39
|
+
|
|
40
|
+
/** @param {Element} element */
|
|
41
|
+
export function isDisabled(element) {
|
|
42
|
+
return element.matches(":disabled, [aria-disabled='true']");
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** @param {Element} element */
|
|
46
|
+
export function isVisible(element) {
|
|
47
|
+
return (
|
|
48
|
+
element instanceof HTMLElement &&
|
|
49
|
+
(element.offsetWidth > 0 ||
|
|
50
|
+
element.offsetHeight > 0 ||
|
|
51
|
+
element.getClientRects().length > 0)
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** @param {ParentNode} root */
|
|
56
|
+
export function focusables(root) {
|
|
57
|
+
return [...root.querySelectorAll(focusableSelector)].filter(
|
|
58
|
+
(element) => !isDisabled(element) && isVisible(element)
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** @param {string} prefix */
|
|
63
|
+
export function uniqueId(prefix = "loseta") {
|
|
64
|
+
idCounter += 1;
|
|
65
|
+
return `${prefix}-${idCounter}`;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Focuses an element once it is visible. Panels transition `visibility` on an
|
|
70
|
+
* early frame, so a focus call in the same task can be ignored.
|
|
71
|
+
* @param {Element} element
|
|
72
|
+
* @param {number} attempts
|
|
73
|
+
*/
|
|
74
|
+
export function focusWhenVisible(element, attempts = 20) {
|
|
75
|
+
/** @param {number} remaining */
|
|
76
|
+
const focus = (remaining) => {
|
|
77
|
+
if (!element.isConnected) return;
|
|
78
|
+
if (element.closest("[aria-hidden='true'], [hidden]")) return;
|
|
79
|
+
|
|
80
|
+
if (getComputedStyle(element).visibility === "visible") {
|
|
81
|
+
if (element instanceof HTMLElement) element.focus({ preventScroll: true });
|
|
82
|
+
if (
|
|
83
|
+
document.activeElement === element ||
|
|
84
|
+
element.contains(document.activeElement)
|
|
85
|
+
) {
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (remaining > 0) setTimeout(() => focus(remaining - 1), 16);
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
setTimeout(() => focus(attempts), 0);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** @param {Element} element */
|
|
97
|
+
export function isRTL(element) {
|
|
98
|
+
return getComputedStyle(element).direction === "rtl";
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* @param {Iterable<Element>} items
|
|
103
|
+
* @param {Element | null} current
|
|
104
|
+
* @param {1 | -1} direction
|
|
105
|
+
*/
|
|
106
|
+
export function nextEnabled(items, current, direction) {
|
|
107
|
+
const enabled = [...items].filter(
|
|
108
|
+
(item) => !isDisabled(item) && !item.hasAttribute("hidden")
|
|
109
|
+
);
|
|
110
|
+
if (enabled.length === 0) return null;
|
|
111
|
+
const index = current ? enabled.indexOf(current) : -1;
|
|
112
|
+
const next = index + direction;
|
|
113
|
+
return enabled[(next + enabled.length) % enabled.length] ?? null;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** @param {Iterable<Element>} items */
|
|
117
|
+
export function firstEnabled(items) {
|
|
118
|
+
return nextEnabled(items, null, 1);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** @param {Iterable<Element>} items */
|
|
122
|
+
export function lastEnabled(items) {
|
|
123
|
+
const enabled = [...items].filter(
|
|
124
|
+
(item) => !isDisabled(item) && !item.hasAttribute("hidden")
|
|
125
|
+
);
|
|
126
|
+
return enabled[enabled.length - 1] ?? null;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** @param {Element} trigger */
|
|
130
|
+
export function panelFor(trigger) {
|
|
131
|
+
const id = trigger.getAttribute("aria-controls");
|
|
132
|
+
if (!id || !supportsDom) return null;
|
|
133
|
+
return document.getElementById(id);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** @param {Element} trigger @param {Element} panel @param {boolean} open */
|
|
137
|
+
export function setOpen(trigger, panel, open) {
|
|
138
|
+
trigger.setAttribute("aria-expanded", String(open));
|
|
139
|
+
panel.setAttribute("aria-hidden", String(!open));
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** @param {Element} panel */
|
|
143
|
+
export function isOpen(panel) {
|
|
144
|
+
return panel.getAttribute("aria-hidden") === "false";
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* @param {Element} target
|
|
149
|
+
* @param {string} type
|
|
150
|
+
* @param {unknown} detail
|
|
151
|
+
*/
|
|
152
|
+
export function emit(target, type, detail) {
|
|
153
|
+
target.dispatchEvent(
|
|
154
|
+
new CustomEvent(type, { bubbles: true, cancelable: false, detail })
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** @param {Element} element */
|
|
159
|
+
export function labelOf(element) {
|
|
160
|
+
/** @param {string} value */
|
|
161
|
+
const normalize = (value) => value.replace(/\s+/g, " ").trim();
|
|
162
|
+
const text = element.querySelector("[data-slot='text']");
|
|
163
|
+
if (text) return normalize(text.textContent ?? "");
|
|
164
|
+
|
|
165
|
+
const shortcut = element.querySelector("[data-slot='shortcut']");
|
|
166
|
+
if (shortcut) {
|
|
167
|
+
const clone = element.cloneNode(true);
|
|
168
|
+
if (clone instanceof Element) {
|
|
169
|
+
for (const node of clone.querySelectorAll("[data-slot='shortcut']")) {
|
|
170
|
+
node.remove();
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return normalize(clone.textContent ?? "");
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
return normalize(element.textContent ?? "");
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* @param {Element} container
|
|
181
|
+
* @param {string} key
|
|
182
|
+
* @param {Iterable<Element>} items
|
|
183
|
+
* @param {Element | null} current
|
|
184
|
+
* @returns {Element | null}
|
|
185
|
+
*/
|
|
186
|
+
export function typeaheadTarget(container, key, items, current) {
|
|
187
|
+
if (key.length !== 1 || key.trim() === "" || key === " ") return null;
|
|
188
|
+
|
|
189
|
+
const now = Date.now();
|
|
190
|
+
const previous = typeaheadBuffers.get(container);
|
|
191
|
+
const repeat = previous !== undefined && now - previous.time < 500;
|
|
192
|
+
let value = key.toLowerCase();
|
|
193
|
+
let cycle = false;
|
|
194
|
+
|
|
195
|
+
if (repeat) {
|
|
196
|
+
if (previous.value === value) {
|
|
197
|
+
cycle = true;
|
|
198
|
+
} else {
|
|
199
|
+
value = `${previous.value}${value}`;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
typeaheadBuffers.set(container, { value, time: now });
|
|
204
|
+
|
|
205
|
+
/** @param {string} query */
|
|
206
|
+
const matches = (query) =>
|
|
207
|
+
[...items].filter((item) =>
|
|
208
|
+
labelOf(item).toLowerCase().startsWith(query)
|
|
209
|
+
);
|
|
210
|
+
|
|
211
|
+
let found = matches(value);
|
|
212
|
+
if (found.length === 0 && value.length > 1) {
|
|
213
|
+
value = key.toLowerCase();
|
|
214
|
+
typeaheadBuffers.set(container, { value, time: now });
|
|
215
|
+
found = matches(value);
|
|
216
|
+
}
|
|
217
|
+
if (found.length === 0) return null;
|
|
218
|
+
|
|
219
|
+
if (cycle && current) {
|
|
220
|
+
const index = found.indexOf(current);
|
|
221
|
+
return found[(index + 1) % found.length] ?? found[0] ?? null;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
return found[0] ?? null;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/** @param {string} event @param {string | null} selector @param {Handler["handler"]} handler @param {number} priority */
|
|
228
|
+
function addHandler(event, selector, handler, priority) {
|
|
229
|
+
const list = handlers.get(event) ?? [];
|
|
230
|
+
list.push({ event, selector, handler, priority });
|
|
231
|
+
list.sort((left, right) => right.priority - left.priority);
|
|
232
|
+
handlers.set(event, list);
|
|
233
|
+
if (installed) listen(event);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/** @param {Event} event */
|
|
237
|
+
function dispatch(event) {
|
|
238
|
+
if (!(event.target instanceof Element) || roots.size === 0) return;
|
|
239
|
+
|
|
240
|
+
const scoped = roots.has(document)
|
|
241
|
+
? [document]
|
|
242
|
+
: [...roots].filter((root) => root.isConnected);
|
|
243
|
+
const list = handlers.get(event.type) ?? [];
|
|
244
|
+
|
|
245
|
+
for (const root of scoped) {
|
|
246
|
+
if (root !== document && !root.contains(event.target)) continue;
|
|
247
|
+
|
|
248
|
+
for (const entry of list) {
|
|
249
|
+
if (entry.selector === null) {
|
|
250
|
+
if (entry.handler(event, event.target) === true) return;
|
|
251
|
+
continue;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
const matched = event.target.closest(entry.selector);
|
|
255
|
+
if (!matched) continue;
|
|
256
|
+
if (root !== document && !root.contains(matched)) continue;
|
|
257
|
+
if (entry.handler(event, matched) === true) return;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/** @param {string} event */
|
|
263
|
+
function listen(event) {
|
|
264
|
+
if (!supportsDom || listening.has(event)) return;
|
|
265
|
+
listening.add(event);
|
|
266
|
+
document.addEventListener(event, dispatch, true);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Registers a delegated handler. Use `init` to mount the document listeners.
|
|
271
|
+
* @param {string} event
|
|
272
|
+
* @param {string} selector
|
|
273
|
+
* @param {Handler["handler"]} handler
|
|
274
|
+
* @param {number} priority
|
|
275
|
+
*/
|
|
276
|
+
export function on(event, selector, handler, priority = 0) {
|
|
277
|
+
addHandler(event, selector, handler, priority);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Registers a document-level handler that runs for every event of the type.
|
|
282
|
+
* @param {string} event
|
|
283
|
+
* @param {Handler["handler"]} handler
|
|
284
|
+
* @param {number} priority
|
|
285
|
+
*/
|
|
286
|
+
export function onDocument(event, handler, priority = 0) {
|
|
287
|
+
addHandler(event, null, handler, priority);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* Mounts behavior handling on a root. Importing an entry module mounts the
|
|
292
|
+
* document root automatically; call this for scoped or late-mounting roots.
|
|
293
|
+
* @param {Document | Element} root
|
|
294
|
+
*/
|
|
295
|
+
export function init(root = document) {
|
|
296
|
+
if (!supportsDom) return;
|
|
297
|
+
roots.add(root);
|
|
298
|
+
installed = true;
|
|
299
|
+
for (const event of handlers.keys()) {
|
|
300
|
+
listen(event);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
3
|
+
import { emit, focusables, init, on } from "./core.js";
|
|
4
|
+
|
|
5
|
+
/** @type {WeakMap<HTMLDialogElement, HTMLElement>} */
|
|
6
|
+
const openers = new WeakMap();
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @param {HTMLDialogElement} dialog
|
|
10
|
+
* @param {HTMLElement} opener
|
|
11
|
+
*/
|
|
12
|
+
function trackClose(dialog, opener) {
|
|
13
|
+
dialog.addEventListener(
|
|
14
|
+
"close",
|
|
15
|
+
() => {
|
|
16
|
+
openers.delete(dialog);
|
|
17
|
+
emit(dialog, "toggle", { open: false });
|
|
18
|
+
if (opener.isConnected) opener.focus();
|
|
19
|
+
},
|
|
20
|
+
{ once: true }
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** @param {Element} trigger */
|
|
25
|
+
function openDialog(trigger) {
|
|
26
|
+
const id = trigger.getAttribute("data-open-dialog");
|
|
27
|
+
if (!id) return;
|
|
28
|
+
const dialog = document.getElementById(id);
|
|
29
|
+
if (!(dialog instanceof HTMLDialogElement) || dialog.open) return;
|
|
30
|
+
|
|
31
|
+
openers.set(dialog, /** @type {HTMLElement} */ (trigger));
|
|
32
|
+
dialog.showModal();
|
|
33
|
+
emit(dialog, "toggle", { open: true });
|
|
34
|
+
trackClose(dialog, /** @type {HTMLElement} */ (trigger));
|
|
35
|
+
|
|
36
|
+
queueMicrotask(() => {
|
|
37
|
+
const autofocus = dialog.querySelector("[autofocus]");
|
|
38
|
+
if (autofocus instanceof HTMLElement) {
|
|
39
|
+
autofocus.focus();
|
|
40
|
+
} else if (!dialog.contains(document.activeElement)) {
|
|
41
|
+
dialog.focus();
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
on("click", "[data-open-dialog]", (event, trigger) => {
|
|
47
|
+
openDialog(trigger);
|
|
48
|
+
event.preventDefault();
|
|
49
|
+
return true;
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
on("click", "[data-close-dialog]", (event, target) => {
|
|
53
|
+
const dialog = target.closest("dialog");
|
|
54
|
+
if (!(dialog instanceof HTMLDialogElement)) return;
|
|
55
|
+
dialog.close();
|
|
56
|
+
event.preventDefault();
|
|
57
|
+
return true;
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
// Native `<dialog>` reports backdrop clicks with the dialog itself as target.
|
|
61
|
+
on("click", "dialog", (event, dialog) => {
|
|
62
|
+
if (event.target !== dialog) return;
|
|
63
|
+
if (dialog.matches(".alert-dialog")) return true;
|
|
64
|
+
if (dialog instanceof HTMLDialogElement) dialog.close();
|
|
65
|
+
return true;
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
// Native focus containment differs across engines; keep Tab inside the modal.
|
|
69
|
+
on("keydown", "dialog", (event, dialog) => {
|
|
70
|
+
if (!(event instanceof KeyboardEvent) || event.key !== "Tab") return;
|
|
71
|
+
if (!(dialog instanceof HTMLDialogElement) || !dialog.open) return;
|
|
72
|
+
|
|
73
|
+
const items = focusables(dialog);
|
|
74
|
+
const first = items[0];
|
|
75
|
+
const last = items[items.length - 1];
|
|
76
|
+
const active = document.activeElement;
|
|
77
|
+
|
|
78
|
+
if (items.length === 0) {
|
|
79
|
+
dialog.focus();
|
|
80
|
+
event.preventDefault();
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (event.shiftKey && (active === first || !dialog.contains(active))) {
|
|
85
|
+
if (last instanceof HTMLElement) last.focus();
|
|
86
|
+
event.preventDefault();
|
|
87
|
+
return true;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (!event.shiftKey && (active === last || !dialog.contains(active))) {
|
|
91
|
+
if (first instanceof HTMLElement) first.focus();
|
|
92
|
+
event.preventDefault();
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
init();
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
3
|
+
import { emit, init, on, onDocument } from "./core.js";
|
|
4
|
+
|
|
5
|
+
const WRAPPER_SELECTOR = ".tooltip, .hover-card";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @typedef {object} OverlayState
|
|
9
|
+
* @property {boolean} hovered
|
|
10
|
+
* @property {boolean} focused
|
|
11
|
+
* @property {boolean} dismissed
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/** @type {WeakMap<HTMLElement, OverlayState>} */
|
|
15
|
+
const states = new WeakMap();
|
|
16
|
+
/** @type {WeakSet<HTMLElement>} */
|
|
17
|
+
const ignoreFocus = new WeakSet();
|
|
18
|
+
/** @type {HTMLElement[]} */
|
|
19
|
+
const open = [];
|
|
20
|
+
|
|
21
|
+
/** @param {HTMLElement} wrapper */
|
|
22
|
+
function panelFor(wrapper) {
|
|
23
|
+
const selector = wrapper.matches(".tooltip")
|
|
24
|
+
? ":scope > [data-slot='content']"
|
|
25
|
+
: ":scope > [data-popover]";
|
|
26
|
+
const panel = wrapper.querySelector(selector);
|
|
27
|
+
return panel instanceof HTMLElement ? panel : null;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** @param {HTMLElement} wrapper @param {HTMLElement} panel */
|
|
31
|
+
function triggerFor(wrapper, panel) {
|
|
32
|
+
for (const element of wrapper.querySelectorAll(
|
|
33
|
+
"button, a[href], input, select, textarea, [tabindex]"
|
|
34
|
+
)) {
|
|
35
|
+
if (panel.contains(element)) continue;
|
|
36
|
+
if (element instanceof HTMLElement) return element;
|
|
37
|
+
}
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** @param {HTMLElement} wrapper */
|
|
42
|
+
function stateFor(wrapper) {
|
|
43
|
+
const existing = states.get(wrapper);
|
|
44
|
+
if (existing) return existing;
|
|
45
|
+
const state = { hovered: false, focused: false, dismissed: false };
|
|
46
|
+
states.set(wrapper, state);
|
|
47
|
+
return state;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** @param {HTMLElement} wrapper */
|
|
51
|
+
function openOverlay(wrapper) {
|
|
52
|
+
const panel = panelFor(wrapper);
|
|
53
|
+
if (!panel) return;
|
|
54
|
+
if (states.get(wrapper)?.dismissed) return;
|
|
55
|
+
if (wrapper.matches(".tooltip") && (panel.textContent ?? "").trim() === "") {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
if (open.includes(wrapper)) return;
|
|
59
|
+
|
|
60
|
+
for (const other of [...open]) {
|
|
61
|
+
if (other !== wrapper) closeOverlay(other, false);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
panel.setAttribute("aria-hidden", "false");
|
|
65
|
+
open.push(wrapper);
|
|
66
|
+
emit(wrapper, "toggle", { open: true });
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* @param {HTMLElement} wrapper
|
|
71
|
+
* @param {boolean} focusTrigger
|
|
72
|
+
*/
|
|
73
|
+
function closeOverlay(wrapper, focusTrigger) {
|
|
74
|
+
const panel = panelFor(wrapper);
|
|
75
|
+
if (panel) panel.setAttribute("aria-hidden", "true");
|
|
76
|
+
|
|
77
|
+
if (focusTrigger) {
|
|
78
|
+
const state = states.get(wrapper);
|
|
79
|
+
if (state) {
|
|
80
|
+
state.dismissed = true;
|
|
81
|
+
state.focused = false;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const index = open.indexOf(wrapper);
|
|
86
|
+
if (index !== -1) {
|
|
87
|
+
open.splice(index, 1);
|
|
88
|
+
emit(wrapper, "toggle", { open: false });
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (focusTrigger && panel) {
|
|
92
|
+
const trigger = triggerFor(wrapper, panel);
|
|
93
|
+
if (trigger) {
|
|
94
|
+
if (document.activeElement !== trigger) ignoreFocus.add(wrapper);
|
|
95
|
+
trigger.focus({ preventScroll: true });
|
|
96
|
+
queueMicrotask(() => ignoreFocus.delete(wrapper));
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** @param {HTMLElement} wrapper */
|
|
102
|
+
function sync(wrapper) {
|
|
103
|
+
const state = stateFor(wrapper);
|
|
104
|
+
if (state.hovered || state.focused) {
|
|
105
|
+
openOverlay(wrapper);
|
|
106
|
+
} else {
|
|
107
|
+
closeOverlay(wrapper, false);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
on("pointerover", WRAPPER_SELECTOR, (_event, element) => {
|
|
112
|
+
if (!(element instanceof HTMLElement)) return;
|
|
113
|
+
const state = stateFor(element);
|
|
114
|
+
if (!state.hovered) state.dismissed = false;
|
|
115
|
+
state.hovered = true;
|
|
116
|
+
openOverlay(element);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
on("pointerout", WRAPPER_SELECTOR, (event, element) => {
|
|
120
|
+
if (!(element instanceof HTMLElement)) return;
|
|
121
|
+
const related = event instanceof PointerEvent ? event.relatedTarget : null;
|
|
122
|
+
if (related instanceof Node && element.contains(related)) return;
|
|
123
|
+
const state = states.get(element);
|
|
124
|
+
if (!state) return;
|
|
125
|
+
state.hovered = false;
|
|
126
|
+
state.dismissed = false;
|
|
127
|
+
sync(element);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
on("focusin", WRAPPER_SELECTOR, (_event, element) => {
|
|
131
|
+
if (!(element instanceof HTMLElement)) return;
|
|
132
|
+
const state = stateFor(element);
|
|
133
|
+
if (ignoreFocus.has(element)) {
|
|
134
|
+
ignoreFocus.delete(element);
|
|
135
|
+
state.focused = false;
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
if (!state.focused) state.dismissed = false;
|
|
139
|
+
state.focused = true;
|
|
140
|
+
openOverlay(element);
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
on("focusout", WRAPPER_SELECTOR, (event, element) => {
|
|
144
|
+
if (!(element instanceof HTMLElement) || !(event instanceof FocusEvent)) {
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
const related = event.relatedTarget;
|
|
148
|
+
if (related instanceof Node && element.contains(related)) return;
|
|
149
|
+
const state = states.get(element);
|
|
150
|
+
if (!state) return;
|
|
151
|
+
state.focused = false;
|
|
152
|
+
sync(element);
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
onDocument(
|
|
156
|
+
"keydown",
|
|
157
|
+
(event) => {
|
|
158
|
+
if (!(event instanceof KeyboardEvent) || event.key !== "Escape") return;
|
|
159
|
+
|
|
160
|
+
let wrapper = open[open.length - 1] ?? null;
|
|
161
|
+
if (!wrapper) {
|
|
162
|
+
for (const element of document.querySelectorAll(WRAPPER_SELECTOR)) {
|
|
163
|
+
if (!(element instanceof HTMLElement)) continue;
|
|
164
|
+
if (!(element.matches(":hover") || element.matches(":focus-within"))) {
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
if (!panelFor(element)) continue;
|
|
168
|
+
wrapper = element;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
if (!wrapper) return;
|
|
172
|
+
|
|
173
|
+
closeOverlay(wrapper, true);
|
|
174
|
+
event.preventDefault();
|
|
175
|
+
return true;
|
|
176
|
+
},
|
|
177
|
+
10
|
|
178
|
+
);
|
|
179
|
+
|
|
180
|
+
init();
|