@y14e/portal 0.0.1
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 +21 -0
- package/README.md +44 -0
- package/dist/index.cjs +444 -0
- package/dist/index.d.cts +23 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.js +441 -0
- package/package.json +58 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Yusuke Kamiyamane
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# Portal
|
|
2
|
+
|
|
3
|
+
Lightweight DOM portal (teleport) utility with fully focus management. Designed for accessible dialogs, menus, overlays, popovers, and etc.
|
|
4
|
+
|
|
5
|
+
> [!NOTE]
|
|
6
|
+
> Focus traversal works across portals using invisible sentinels and composed-tree-aware focus detection powered by [Power Focusable](https://github.com/y14e/power-focusable).
|
|
7
|
+
|
|
8
|
+
## Install
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm i y14e@portal
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
```ts
|
|
15
|
+
// npm
|
|
16
|
+
import { createPortal } from 'y14e@portal';
|
|
17
|
+
|
|
18
|
+
// CDNs
|
|
19
|
+
import { createPortal } from 'https://esm.sh/y14e@portal'
|
|
20
|
+
// or
|
|
21
|
+
import { createPortal } from 'https://cdn.jsdelivr.net/npm/y14e@portal/dist/index.js';
|
|
22
|
+
// or
|
|
23
|
+
import { createPortal } from 'https://unpkg.com/y14e@portal/dist/index.js';
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## 📦 APIs
|
|
27
|
+
|
|
28
|
+
### `createPortal`
|
|
29
|
+
|
|
30
|
+
Creates a portal and preserves keyboard focus flow between the original DOM position and the portal.
|
|
31
|
+
|
|
32
|
+
```ts
|
|
33
|
+
const portal = createPortal(source, container);
|
|
34
|
+
// => { element: Element, cleanup: () => void }
|
|
35
|
+
//
|
|
36
|
+
// source: Element
|
|
37
|
+
// container (optional): Element (default: <body>)
|
|
38
|
+
|
|
39
|
+
// Element
|
|
40
|
+
console.log(portal.element);
|
|
41
|
+
|
|
42
|
+
// Cleanup
|
|
43
|
+
portal.cleanup();
|
|
44
|
+
```
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,444 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// node_modules/power-focusable/dist/index.js
|
|
4
|
+
var FOCUSABLE_SELECTOR = `:is(a[href], area[href], button, embed, iframe, input:not([type="hidden" i]), object, select, details > summary:first-of-type, textarea, [contenteditable]:not([contenteditable="false" i]), [controls], [tabindex]):not(:disabled, [hidden], [inert], [tabindex="-1"])`;
|
|
5
|
+
function getFocusables(container = document.body, options = {}) {
|
|
6
|
+
if (!(container instanceof Element)) {
|
|
7
|
+
console.warn("Invalid container element. Fallback: <body> element.");
|
|
8
|
+
container = document.body;
|
|
9
|
+
}
|
|
10
|
+
const { composed = false, filter = () => true } = options;
|
|
11
|
+
const elements = [];
|
|
12
|
+
if (composed) {
|
|
13
|
+
let traverse2 = function(node) {
|
|
14
|
+
if (node instanceof Element) {
|
|
15
|
+
if (isFocusable(node)) {
|
|
16
|
+
elements[elements.length] = node;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
const children = getComposedChildren(node);
|
|
20
|
+
for (let i = 0, l = children.length; i < l; i++) {
|
|
21
|
+
const child = children[i];
|
|
22
|
+
if (!child) {
|
|
23
|
+
continue;
|
|
24
|
+
}
|
|
25
|
+
traverse2(child);
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
traverse2(container);
|
|
29
|
+
} else {
|
|
30
|
+
const candidates = container.querySelectorAll(FOCUSABLE_SELECTOR);
|
|
31
|
+
for (let i = 0, l = candidates.length; i < l; i++) {
|
|
32
|
+
const candidate = candidates[i];
|
|
33
|
+
if (!(candidate instanceof Element)) {
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
if (isFocusable(candidate)) {
|
|
37
|
+
elements[elements.length] = candidate;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return normalizeRadioGroup(sortByTabIndex(elements)).filter(filter);
|
|
42
|
+
}
|
|
43
|
+
function getNextFocusable(container = document.body, options = {}) {
|
|
44
|
+
if (!(container instanceof Element)) {
|
|
45
|
+
console.warn("Invalid container element. Fallback: <body> element.");
|
|
46
|
+
container = document.body;
|
|
47
|
+
}
|
|
48
|
+
return getRelativeFocusable(container, 1, options);
|
|
49
|
+
}
|
|
50
|
+
function getPreviousFocusable(container = document.body, options = {}) {
|
|
51
|
+
if (!(container instanceof Element)) {
|
|
52
|
+
console.warn("Invalid container element. Fallback: <body> element.");
|
|
53
|
+
container = document.body;
|
|
54
|
+
}
|
|
55
|
+
return getRelativeFocusable(container, -1, options);
|
|
56
|
+
}
|
|
57
|
+
function isFocusable(element) {
|
|
58
|
+
if (!(element instanceof Element)) {
|
|
59
|
+
console.warn("Invalid element");
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
if (element.hasAttribute("hidden") || isInert(element)) {
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
if (getTabIndex(element) < 0) {
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
if (!element.matches(FOCUSABLE_SELECTOR)) {
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
if (isDisabledDeep(element)) {
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
if (!element.checkVisibility({
|
|
75
|
+
contentVisibilityAuto: true,
|
|
76
|
+
opacityProperty: true,
|
|
77
|
+
visibilityProperty: true
|
|
78
|
+
})) {
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
function getRelativeFocusable(container, offset, options) {
|
|
84
|
+
const {
|
|
85
|
+
anchor = getActiveElement(),
|
|
86
|
+
composed = false,
|
|
87
|
+
filter = () => true,
|
|
88
|
+
wrap = false
|
|
89
|
+
} = options;
|
|
90
|
+
const focusables = getFocusables(container, { composed, filter });
|
|
91
|
+
const { length } = focusables;
|
|
92
|
+
if (!length) {
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
if (!anchor || !containsComposed(container, anchor)) {
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
if (!(anchor instanceof Element)) {
|
|
99
|
+
return null;
|
|
100
|
+
}
|
|
101
|
+
const currentIndex = focusables.indexOf(anchor);
|
|
102
|
+
if (currentIndex === -1) {
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
const offsetIndex = currentIndex + offset;
|
|
106
|
+
if ((offsetIndex < 0 || offsetIndex >= length) && !wrap) {
|
|
107
|
+
return null;
|
|
108
|
+
}
|
|
109
|
+
return focusables[(offsetIndex + length) % length] ?? null;
|
|
110
|
+
}
|
|
111
|
+
function isDisabledDeep(element) {
|
|
112
|
+
let current = element;
|
|
113
|
+
while (current) {
|
|
114
|
+
if (current instanceof ShadowRoot) {
|
|
115
|
+
if (current.mode !== "open") {
|
|
116
|
+
return false;
|
|
117
|
+
}
|
|
118
|
+
current = current.host;
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
if (!(current instanceof Element)) {
|
|
122
|
+
current = current.parentNode;
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
if (current === element && isFormControl(current) && isDisabled(current)) {
|
|
126
|
+
return true;
|
|
127
|
+
}
|
|
128
|
+
if (isInert(current)) {
|
|
129
|
+
return true;
|
|
130
|
+
}
|
|
131
|
+
if (isFormControl(element) && current.tagName === "FIELDSET" && isDisabled(current)) {
|
|
132
|
+
if (!current.querySelector(":scope > legend:first-of-type")?.contains(element)) {
|
|
133
|
+
return true;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
current = current.parentNode;
|
|
137
|
+
}
|
|
138
|
+
return false;
|
|
139
|
+
}
|
|
140
|
+
function normalizeRadioGroup(elements) {
|
|
141
|
+
let map = null;
|
|
142
|
+
for (let i = 0, l = elements.length; i < l; i++) {
|
|
143
|
+
const element = elements[i];
|
|
144
|
+
if (!(element instanceof HTMLInputElement)) {
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
if (!isUngroupedRadio(element)) {
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
if (!map) {
|
|
151
|
+
map = /* @__PURE__ */ new Map();
|
|
152
|
+
}
|
|
153
|
+
const key = `${element.form?.id ?? "no-form"}::${element.name}`;
|
|
154
|
+
const group = map.get(key) ?? map.set(key, []).get(key);
|
|
155
|
+
if (group) {
|
|
156
|
+
group[group.length] = element;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
if (!map) {
|
|
160
|
+
return elements;
|
|
161
|
+
}
|
|
162
|
+
const placeholder = /* @__PURE__ */ new Set();
|
|
163
|
+
for (const group of map.values()) {
|
|
164
|
+
placeholder.add(group.find((radio) => radio.checked) ?? group[0]);
|
|
165
|
+
}
|
|
166
|
+
return elements.filter((element) => {
|
|
167
|
+
if (isUngroupedRadio(element)) {
|
|
168
|
+
return placeholder.has(element);
|
|
169
|
+
}
|
|
170
|
+
return true;
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
function sortByTabIndex(elements) {
|
|
174
|
+
const ordered = [];
|
|
175
|
+
const natural = [];
|
|
176
|
+
for (let i = 0, l = elements.length; i < l; i++) {
|
|
177
|
+
const element = elements[i];
|
|
178
|
+
if (!element) {
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
const target = getTabIndex(element) > 0 ? ordered : natural;
|
|
182
|
+
target[target.length] = element;
|
|
183
|
+
}
|
|
184
|
+
ordered.sort((a, b) => getTabIndex(a) - getTabIndex(b));
|
|
185
|
+
let count = 0;
|
|
186
|
+
const sorted = new Array(ordered.length + natural.length);
|
|
187
|
+
for (let i = 0, l = ordered.length; i < l; i++) {
|
|
188
|
+
sorted[count++] = ordered[i];
|
|
189
|
+
}
|
|
190
|
+
for (let i = 0, l = natural.length; i < l; i++) {
|
|
191
|
+
sorted[count++] = natural[i];
|
|
192
|
+
}
|
|
193
|
+
return sorted;
|
|
194
|
+
}
|
|
195
|
+
function containsComposed(container, element) {
|
|
196
|
+
let current = element;
|
|
197
|
+
while (current) {
|
|
198
|
+
if (current === container) {
|
|
199
|
+
return true;
|
|
200
|
+
}
|
|
201
|
+
current = current instanceof ShadowRoot ? current.mode === "open" ? current.host : null : current.parentNode;
|
|
202
|
+
}
|
|
203
|
+
return false;
|
|
204
|
+
}
|
|
205
|
+
function getComposedChildren(node) {
|
|
206
|
+
if (node instanceof ShadowRoot) {
|
|
207
|
+
return getChildren(node);
|
|
208
|
+
}
|
|
209
|
+
if (!(node instanceof Element)) {
|
|
210
|
+
return [];
|
|
211
|
+
}
|
|
212
|
+
if (node instanceof HTMLSlotElement) {
|
|
213
|
+
const assigned = node.assignedElements({ flatten: true });
|
|
214
|
+
if (assigned.length) {
|
|
215
|
+
return assigned;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
if (node instanceof HTMLElement && node.shadowRoot?.mode === "open") {
|
|
219
|
+
return getChildren(node.shadowRoot);
|
|
220
|
+
}
|
|
221
|
+
return getChildren(node);
|
|
222
|
+
}
|
|
223
|
+
function getActiveElement() {
|
|
224
|
+
let current = document.activeElement;
|
|
225
|
+
while (current?.shadowRoot?.activeElement) {
|
|
226
|
+
current = current.shadowRoot.activeElement;
|
|
227
|
+
}
|
|
228
|
+
return current;
|
|
229
|
+
}
|
|
230
|
+
function getChildren(node) {
|
|
231
|
+
const elements = [];
|
|
232
|
+
for (let child = node.firstElementChild; child; child = child.nextElementSibling) {
|
|
233
|
+
elements[elements.length] = child;
|
|
234
|
+
}
|
|
235
|
+
return elements;
|
|
236
|
+
}
|
|
237
|
+
function getTabIndex(element) {
|
|
238
|
+
return "tabIndex" in element ? Number(element.tabIndex) : 0;
|
|
239
|
+
}
|
|
240
|
+
function isDisabled(element) {
|
|
241
|
+
return "disabled" in element && !!element.disabled;
|
|
242
|
+
}
|
|
243
|
+
function isFormControl(element) {
|
|
244
|
+
const name = element.tagName;
|
|
245
|
+
return name === "BUTTON" || name === "INPUT" || name === "SELECT" || name === "TEXTAREA";
|
|
246
|
+
}
|
|
247
|
+
function isInert(element) {
|
|
248
|
+
return "inert" in element && !!element.inert;
|
|
249
|
+
}
|
|
250
|
+
function isUngroupedRadio(element) {
|
|
251
|
+
return element instanceof HTMLInputElement && element.type === "radio" && !!element.name;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// src/index.ts
|
|
255
|
+
var VISUALLY_HIDDEN_CSS = `border: 0; clip: rect(0, 0, 0, 0); height: 1px; margin: -1px; overflow: hidden; padding: 0; position: absolute; user-select: none; white-space: nowrap; width: 1px;`;
|
|
256
|
+
function createPortal(source, container = document.body) {
|
|
257
|
+
if (!(source instanceof Element)) {
|
|
258
|
+
throw new Error("Invalid source element");
|
|
259
|
+
}
|
|
260
|
+
if (!(container instanceof Element)) {
|
|
261
|
+
console.warn("Invalid container element. Fallback: <body> element.");
|
|
262
|
+
container = document.body;
|
|
263
|
+
}
|
|
264
|
+
const portal = new Portal(source, container);
|
|
265
|
+
return {
|
|
266
|
+
element: portal.getElement(),
|
|
267
|
+
cleanup: () => portal.destroy()
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
var Portal = class {
|
|
271
|
+
#source;
|
|
272
|
+
#container;
|
|
273
|
+
#portal;
|
|
274
|
+
#entranceSentinel;
|
|
275
|
+
#exitSentinel;
|
|
276
|
+
#tabIndexes = /* @__PURE__ */ new WeakMap();
|
|
277
|
+
#controller = null;
|
|
278
|
+
#isDestroyed = false;
|
|
279
|
+
constructor(source, container = document.body) {
|
|
280
|
+
this.#source = source;
|
|
281
|
+
this.#container = container;
|
|
282
|
+
this.#portal = document.createElement("div");
|
|
283
|
+
this.#portal.setAttribute("data-portal", "");
|
|
284
|
+
this.#portal.setAttribute("tabindex", "-1");
|
|
285
|
+
this.#entranceSentinel = this.#createSentinel();
|
|
286
|
+
this.#exitSentinel = this.#createSentinel();
|
|
287
|
+
this.#initialize();
|
|
288
|
+
}
|
|
289
|
+
destroy() {
|
|
290
|
+
if (this.#isDestroyed) {
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
this.#isDestroyed = true;
|
|
294
|
+
this.#controller?.abort();
|
|
295
|
+
this.#controller = null;
|
|
296
|
+
this.#getFocusables().forEach((focusable) => {
|
|
297
|
+
const index = this.#tabIndexes.get(focusable);
|
|
298
|
+
if (index === null) {
|
|
299
|
+
focusable.removeAttribute("tabindex");
|
|
300
|
+
} else {
|
|
301
|
+
focusable.setAttribute("tabindex", String(index));
|
|
302
|
+
}
|
|
303
|
+
});
|
|
304
|
+
this.#exitSentinel.after(this.#source);
|
|
305
|
+
this.#portal.remove();
|
|
306
|
+
this.#entranceSentinel.remove();
|
|
307
|
+
this.#exitSentinel.remove();
|
|
308
|
+
}
|
|
309
|
+
getElement() {
|
|
310
|
+
return this.#portal;
|
|
311
|
+
}
|
|
312
|
+
#initialize() {
|
|
313
|
+
this.#source.before(this.#entranceSentinel);
|
|
314
|
+
this.#entranceSentinel.after(this.#exitSentinel);
|
|
315
|
+
this.#portal.append(this.#source);
|
|
316
|
+
this.#container.append(this.#portal);
|
|
317
|
+
getFocusables(this.#source, { composed: true }).forEach(
|
|
318
|
+
(focusable) => {
|
|
319
|
+
const index = focusable.getAttribute("tabindex")?.trim();
|
|
320
|
+
this.#tabIndexes.set(focusable, index === null ? null : Number(index));
|
|
321
|
+
focusable.setAttribute("tabindex", "-1");
|
|
322
|
+
}
|
|
323
|
+
);
|
|
324
|
+
this.#controller = new AbortController();
|
|
325
|
+
const { signal } = this.#controller;
|
|
326
|
+
document.addEventListener("focusin", this.#onFocusIn, {
|
|
327
|
+
capture: true,
|
|
328
|
+
signal
|
|
329
|
+
});
|
|
330
|
+
document.addEventListener("keydown", this.#onKeyDown, {
|
|
331
|
+
capture: true,
|
|
332
|
+
signal
|
|
333
|
+
});
|
|
334
|
+
}
|
|
335
|
+
#onFocusIn = (event) => {
|
|
336
|
+
const current = event.target;
|
|
337
|
+
const before = event.relatedTarget;
|
|
338
|
+
if (!(before instanceof Element)) {
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
const focusables = this.#getFocusables();
|
|
342
|
+
if (current === this.#entranceSentinel) {
|
|
343
|
+
if (this.#source.contains(before)) {
|
|
344
|
+
this.#focusOutside("backward");
|
|
345
|
+
} else {
|
|
346
|
+
const first = focusables[0];
|
|
347
|
+
first && focus(first);
|
|
348
|
+
}
|
|
349
|
+
} else if (current === this.#exitSentinel) {
|
|
350
|
+
if (this.#source.contains(before)) {
|
|
351
|
+
this.#focusOutside("forward");
|
|
352
|
+
} else {
|
|
353
|
+
const last = focusables.at(-1);
|
|
354
|
+
last && focus(last);
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
};
|
|
358
|
+
#onKeyDown = (event) => {
|
|
359
|
+
if (event.key !== "Tab" || event.altKey || event.ctrlKey || event.metaKey) {
|
|
360
|
+
return;
|
|
361
|
+
}
|
|
362
|
+
const active = getActiveElement2();
|
|
363
|
+
if (!(active instanceof Element)) {
|
|
364
|
+
return;
|
|
365
|
+
}
|
|
366
|
+
if (!this.#source.contains(active)) {
|
|
367
|
+
return;
|
|
368
|
+
}
|
|
369
|
+
const focusables = this.#getFocusables();
|
|
370
|
+
if (!focusables.length) {
|
|
371
|
+
event.preventDefault();
|
|
372
|
+
(event.shiftKey ? this.#entranceSentinel : this.#exitSentinel).focus();
|
|
373
|
+
}
|
|
374
|
+
const index = focusables.indexOf(active);
|
|
375
|
+
if (index === -1) {
|
|
376
|
+
return;
|
|
377
|
+
}
|
|
378
|
+
event.preventDefault();
|
|
379
|
+
const focusable = focusables[index + (event.shiftKey ? -1 : 1)];
|
|
380
|
+
if (focusable) {
|
|
381
|
+
focus(focusable);
|
|
382
|
+
} else {
|
|
383
|
+
(event.shiftKey ? this.#entranceSentinel : this.#exitSentinel).focus();
|
|
384
|
+
}
|
|
385
|
+
};
|
|
386
|
+
#createSentinel() {
|
|
387
|
+
const sentinel = document.createElement("span");
|
|
388
|
+
sentinel.setAttribute("aria-hidden", "true");
|
|
389
|
+
sentinel.setAttribute("tabindex", "0");
|
|
390
|
+
sentinel.style.cssText += VISUALLY_HIDDEN_CSS;
|
|
391
|
+
return sentinel;
|
|
392
|
+
}
|
|
393
|
+
#focusOutside(direction) {
|
|
394
|
+
const options = {
|
|
395
|
+
anchor: direction === "backward" ? this.#entranceSentinel : this.#exitSentinel,
|
|
396
|
+
composed: true
|
|
397
|
+
};
|
|
398
|
+
const focusable = direction === "backward" ? getPreviousFocusable(document.body, options) : getNextFocusable(document.body, options);
|
|
399
|
+
focusable && focus(focusable);
|
|
400
|
+
}
|
|
401
|
+
#getFocusables() {
|
|
402
|
+
return getFocusables(this.#source, { composed: true });
|
|
403
|
+
}
|
|
404
|
+
};
|
|
405
|
+
function focus(element) {
|
|
406
|
+
"focus" in element && typeof element.focus === "function" && element.focus();
|
|
407
|
+
}
|
|
408
|
+
function getActiveElement2() {
|
|
409
|
+
let current = document.activeElement;
|
|
410
|
+
while (current?.shadowRoot?.activeElement) {
|
|
411
|
+
current = current.shadowRoot.activeElement;
|
|
412
|
+
}
|
|
413
|
+
return current;
|
|
414
|
+
}
|
|
415
|
+
/**
|
|
416
|
+
* Portal
|
|
417
|
+
* Lightweight DOM portal (teleport) utility with fully focus management.
|
|
418
|
+
* Designed for accessible dialogs, menus, overlays, popovers, and etc.
|
|
419
|
+
*
|
|
420
|
+
* @version 0.0.1
|
|
421
|
+
* @author Yusuke Kamiyamane
|
|
422
|
+
* @license MIT
|
|
423
|
+
* @copyright Copyright (c) Yusuke Kamiyamane
|
|
424
|
+
* @see {@link https://github.com/y14e/portal}
|
|
425
|
+
*/
|
|
426
|
+
/*! Bundled license information:
|
|
427
|
+
|
|
428
|
+
power-focusable/dist/index.js:
|
|
429
|
+
(**
|
|
430
|
+
* Power Focusable
|
|
431
|
+
* High-precision focus management utility with full composed tree support.
|
|
432
|
+
* Handles complex focus rules including tabindex ordering, radio groups, inert,
|
|
433
|
+
* and shadow DOM.
|
|
434
|
+
*
|
|
435
|
+
* @version 4.0.2
|
|
436
|
+
* @author Yusuke Kamiyamane
|
|
437
|
+
* @license MIT
|
|
438
|
+
* @copyright Copyright (c) Yusuke Kamiyamane
|
|
439
|
+
* @see {@link https://github.com/y14e/power-focusable}
|
|
440
|
+
*)
|
|
441
|
+
*/
|
|
442
|
+
|
|
443
|
+
exports.Portal = Portal;
|
|
444
|
+
exports.createPortal = createPortal;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Portal
|
|
3
|
+
* Lightweight DOM portal (teleport) utility with fully focus management.
|
|
4
|
+
* Designed for accessible dialogs, menus, overlays, popovers, and etc.
|
|
5
|
+
*
|
|
6
|
+
* @version 0.0.1
|
|
7
|
+
* @author Yusuke Kamiyamane
|
|
8
|
+
* @license MIT
|
|
9
|
+
* @copyright Copyright (c) Yusuke Kamiyamane
|
|
10
|
+
* @see {@link https://github.com/y14e/portal}
|
|
11
|
+
*/
|
|
12
|
+
declare function createPortal(source: Element, container?: HTMLElement): {
|
|
13
|
+
element: Element;
|
|
14
|
+
cleanup: () => void;
|
|
15
|
+
};
|
|
16
|
+
declare class Portal {
|
|
17
|
+
#private;
|
|
18
|
+
constructor(source: Element, container?: HTMLElement);
|
|
19
|
+
destroy(): void;
|
|
20
|
+
getElement(): HTMLElement;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export { Portal, createPortal };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Portal
|
|
3
|
+
* Lightweight DOM portal (teleport) utility with fully focus management.
|
|
4
|
+
* Designed for accessible dialogs, menus, overlays, popovers, and etc.
|
|
5
|
+
*
|
|
6
|
+
* @version 0.0.1
|
|
7
|
+
* @author Yusuke Kamiyamane
|
|
8
|
+
* @license MIT
|
|
9
|
+
* @copyright Copyright (c) Yusuke Kamiyamane
|
|
10
|
+
* @see {@link https://github.com/y14e/portal}
|
|
11
|
+
*/
|
|
12
|
+
declare function createPortal(source: Element, container?: HTMLElement): {
|
|
13
|
+
element: Element;
|
|
14
|
+
cleanup: () => void;
|
|
15
|
+
};
|
|
16
|
+
declare class Portal {
|
|
17
|
+
#private;
|
|
18
|
+
constructor(source: Element, container?: HTMLElement);
|
|
19
|
+
destroy(): void;
|
|
20
|
+
getElement(): HTMLElement;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export { Portal, createPortal };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,441 @@
|
|
|
1
|
+
// node_modules/power-focusable/dist/index.js
|
|
2
|
+
var FOCUSABLE_SELECTOR = `:is(a[href], area[href], button, embed, iframe, input:not([type="hidden" i]), object, select, details > summary:first-of-type, textarea, [contenteditable]:not([contenteditable="false" i]), [controls], [tabindex]):not(:disabled, [hidden], [inert], [tabindex="-1"])`;
|
|
3
|
+
function getFocusables(container = document.body, options = {}) {
|
|
4
|
+
if (!(container instanceof Element)) {
|
|
5
|
+
console.warn("Invalid container element. Fallback: <body> element.");
|
|
6
|
+
container = document.body;
|
|
7
|
+
}
|
|
8
|
+
const { composed = false, filter = () => true } = options;
|
|
9
|
+
const elements = [];
|
|
10
|
+
if (composed) {
|
|
11
|
+
let traverse2 = function(node) {
|
|
12
|
+
if (node instanceof Element) {
|
|
13
|
+
if (isFocusable(node)) {
|
|
14
|
+
elements[elements.length] = node;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
const children = getComposedChildren(node);
|
|
18
|
+
for (let i = 0, l = children.length; i < l; i++) {
|
|
19
|
+
const child = children[i];
|
|
20
|
+
if (!child) {
|
|
21
|
+
continue;
|
|
22
|
+
}
|
|
23
|
+
traverse2(child);
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
traverse2(container);
|
|
27
|
+
} else {
|
|
28
|
+
const candidates = container.querySelectorAll(FOCUSABLE_SELECTOR);
|
|
29
|
+
for (let i = 0, l = candidates.length; i < l; i++) {
|
|
30
|
+
const candidate = candidates[i];
|
|
31
|
+
if (!(candidate instanceof Element)) {
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
if (isFocusable(candidate)) {
|
|
35
|
+
elements[elements.length] = candidate;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return normalizeRadioGroup(sortByTabIndex(elements)).filter(filter);
|
|
40
|
+
}
|
|
41
|
+
function getNextFocusable(container = document.body, options = {}) {
|
|
42
|
+
if (!(container instanceof Element)) {
|
|
43
|
+
console.warn("Invalid container element. Fallback: <body> element.");
|
|
44
|
+
container = document.body;
|
|
45
|
+
}
|
|
46
|
+
return getRelativeFocusable(container, 1, options);
|
|
47
|
+
}
|
|
48
|
+
function getPreviousFocusable(container = document.body, options = {}) {
|
|
49
|
+
if (!(container instanceof Element)) {
|
|
50
|
+
console.warn("Invalid container element. Fallback: <body> element.");
|
|
51
|
+
container = document.body;
|
|
52
|
+
}
|
|
53
|
+
return getRelativeFocusable(container, -1, options);
|
|
54
|
+
}
|
|
55
|
+
function isFocusable(element) {
|
|
56
|
+
if (!(element instanceof Element)) {
|
|
57
|
+
console.warn("Invalid element");
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
if (element.hasAttribute("hidden") || isInert(element)) {
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
if (getTabIndex(element) < 0) {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
if (!element.matches(FOCUSABLE_SELECTOR)) {
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
if (isDisabledDeep(element)) {
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
if (!element.checkVisibility({
|
|
73
|
+
contentVisibilityAuto: true,
|
|
74
|
+
opacityProperty: true,
|
|
75
|
+
visibilityProperty: true
|
|
76
|
+
})) {
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
return true;
|
|
80
|
+
}
|
|
81
|
+
function getRelativeFocusable(container, offset, options) {
|
|
82
|
+
const {
|
|
83
|
+
anchor = getActiveElement(),
|
|
84
|
+
composed = false,
|
|
85
|
+
filter = () => true,
|
|
86
|
+
wrap = false
|
|
87
|
+
} = options;
|
|
88
|
+
const focusables = getFocusables(container, { composed, filter });
|
|
89
|
+
const { length } = focusables;
|
|
90
|
+
if (!length) {
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
if (!anchor || !containsComposed(container, anchor)) {
|
|
94
|
+
return null;
|
|
95
|
+
}
|
|
96
|
+
if (!(anchor instanceof Element)) {
|
|
97
|
+
return null;
|
|
98
|
+
}
|
|
99
|
+
const currentIndex = focusables.indexOf(anchor);
|
|
100
|
+
if (currentIndex === -1) {
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
const offsetIndex = currentIndex + offset;
|
|
104
|
+
if ((offsetIndex < 0 || offsetIndex >= length) && !wrap) {
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
return focusables[(offsetIndex + length) % length] ?? null;
|
|
108
|
+
}
|
|
109
|
+
function isDisabledDeep(element) {
|
|
110
|
+
let current = element;
|
|
111
|
+
while (current) {
|
|
112
|
+
if (current instanceof ShadowRoot) {
|
|
113
|
+
if (current.mode !== "open") {
|
|
114
|
+
return false;
|
|
115
|
+
}
|
|
116
|
+
current = current.host;
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
if (!(current instanceof Element)) {
|
|
120
|
+
current = current.parentNode;
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
if (current === element && isFormControl(current) && isDisabled(current)) {
|
|
124
|
+
return true;
|
|
125
|
+
}
|
|
126
|
+
if (isInert(current)) {
|
|
127
|
+
return true;
|
|
128
|
+
}
|
|
129
|
+
if (isFormControl(element) && current.tagName === "FIELDSET" && isDisabled(current)) {
|
|
130
|
+
if (!current.querySelector(":scope > legend:first-of-type")?.contains(element)) {
|
|
131
|
+
return true;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
current = current.parentNode;
|
|
135
|
+
}
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
function normalizeRadioGroup(elements) {
|
|
139
|
+
let map = null;
|
|
140
|
+
for (let i = 0, l = elements.length; i < l; i++) {
|
|
141
|
+
const element = elements[i];
|
|
142
|
+
if (!(element instanceof HTMLInputElement)) {
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
if (!isUngroupedRadio(element)) {
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
if (!map) {
|
|
149
|
+
map = /* @__PURE__ */ new Map();
|
|
150
|
+
}
|
|
151
|
+
const key = `${element.form?.id ?? "no-form"}::${element.name}`;
|
|
152
|
+
const group = map.get(key) ?? map.set(key, []).get(key);
|
|
153
|
+
if (group) {
|
|
154
|
+
group[group.length] = element;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
if (!map) {
|
|
158
|
+
return elements;
|
|
159
|
+
}
|
|
160
|
+
const placeholder = /* @__PURE__ */ new Set();
|
|
161
|
+
for (const group of map.values()) {
|
|
162
|
+
placeholder.add(group.find((radio) => radio.checked) ?? group[0]);
|
|
163
|
+
}
|
|
164
|
+
return elements.filter((element) => {
|
|
165
|
+
if (isUngroupedRadio(element)) {
|
|
166
|
+
return placeholder.has(element);
|
|
167
|
+
}
|
|
168
|
+
return true;
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
function sortByTabIndex(elements) {
|
|
172
|
+
const ordered = [];
|
|
173
|
+
const natural = [];
|
|
174
|
+
for (let i = 0, l = elements.length; i < l; i++) {
|
|
175
|
+
const element = elements[i];
|
|
176
|
+
if (!element) {
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
const target = getTabIndex(element) > 0 ? ordered : natural;
|
|
180
|
+
target[target.length] = element;
|
|
181
|
+
}
|
|
182
|
+
ordered.sort((a, b) => getTabIndex(a) - getTabIndex(b));
|
|
183
|
+
let count = 0;
|
|
184
|
+
const sorted = new Array(ordered.length + natural.length);
|
|
185
|
+
for (let i = 0, l = ordered.length; i < l; i++) {
|
|
186
|
+
sorted[count++] = ordered[i];
|
|
187
|
+
}
|
|
188
|
+
for (let i = 0, l = natural.length; i < l; i++) {
|
|
189
|
+
sorted[count++] = natural[i];
|
|
190
|
+
}
|
|
191
|
+
return sorted;
|
|
192
|
+
}
|
|
193
|
+
function containsComposed(container, element) {
|
|
194
|
+
let current = element;
|
|
195
|
+
while (current) {
|
|
196
|
+
if (current === container) {
|
|
197
|
+
return true;
|
|
198
|
+
}
|
|
199
|
+
current = current instanceof ShadowRoot ? current.mode === "open" ? current.host : null : current.parentNode;
|
|
200
|
+
}
|
|
201
|
+
return false;
|
|
202
|
+
}
|
|
203
|
+
function getComposedChildren(node) {
|
|
204
|
+
if (node instanceof ShadowRoot) {
|
|
205
|
+
return getChildren(node);
|
|
206
|
+
}
|
|
207
|
+
if (!(node instanceof Element)) {
|
|
208
|
+
return [];
|
|
209
|
+
}
|
|
210
|
+
if (node instanceof HTMLSlotElement) {
|
|
211
|
+
const assigned = node.assignedElements({ flatten: true });
|
|
212
|
+
if (assigned.length) {
|
|
213
|
+
return assigned;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
if (node instanceof HTMLElement && node.shadowRoot?.mode === "open") {
|
|
217
|
+
return getChildren(node.shadowRoot);
|
|
218
|
+
}
|
|
219
|
+
return getChildren(node);
|
|
220
|
+
}
|
|
221
|
+
function getActiveElement() {
|
|
222
|
+
let current = document.activeElement;
|
|
223
|
+
while (current?.shadowRoot?.activeElement) {
|
|
224
|
+
current = current.shadowRoot.activeElement;
|
|
225
|
+
}
|
|
226
|
+
return current;
|
|
227
|
+
}
|
|
228
|
+
function getChildren(node) {
|
|
229
|
+
const elements = [];
|
|
230
|
+
for (let child = node.firstElementChild; child; child = child.nextElementSibling) {
|
|
231
|
+
elements[elements.length] = child;
|
|
232
|
+
}
|
|
233
|
+
return elements;
|
|
234
|
+
}
|
|
235
|
+
function getTabIndex(element) {
|
|
236
|
+
return "tabIndex" in element ? Number(element.tabIndex) : 0;
|
|
237
|
+
}
|
|
238
|
+
function isDisabled(element) {
|
|
239
|
+
return "disabled" in element && !!element.disabled;
|
|
240
|
+
}
|
|
241
|
+
function isFormControl(element) {
|
|
242
|
+
const name = element.tagName;
|
|
243
|
+
return name === "BUTTON" || name === "INPUT" || name === "SELECT" || name === "TEXTAREA";
|
|
244
|
+
}
|
|
245
|
+
function isInert(element) {
|
|
246
|
+
return "inert" in element && !!element.inert;
|
|
247
|
+
}
|
|
248
|
+
function isUngroupedRadio(element) {
|
|
249
|
+
return element instanceof HTMLInputElement && element.type === "radio" && !!element.name;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// src/index.ts
|
|
253
|
+
var VISUALLY_HIDDEN_CSS = `border: 0; clip: rect(0, 0, 0, 0); height: 1px; margin: -1px; overflow: hidden; padding: 0; position: absolute; user-select: none; white-space: nowrap; width: 1px;`;
|
|
254
|
+
function createPortal(source, container = document.body) {
|
|
255
|
+
if (!(source instanceof Element)) {
|
|
256
|
+
throw new Error("Invalid source element");
|
|
257
|
+
}
|
|
258
|
+
if (!(container instanceof Element)) {
|
|
259
|
+
console.warn("Invalid container element. Fallback: <body> element.");
|
|
260
|
+
container = document.body;
|
|
261
|
+
}
|
|
262
|
+
const portal = new Portal(source, container);
|
|
263
|
+
return {
|
|
264
|
+
element: portal.getElement(),
|
|
265
|
+
cleanup: () => portal.destroy()
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
var Portal = class {
|
|
269
|
+
#source;
|
|
270
|
+
#container;
|
|
271
|
+
#portal;
|
|
272
|
+
#entranceSentinel;
|
|
273
|
+
#exitSentinel;
|
|
274
|
+
#tabIndexes = /* @__PURE__ */ new WeakMap();
|
|
275
|
+
#controller = null;
|
|
276
|
+
#isDestroyed = false;
|
|
277
|
+
constructor(source, container = document.body) {
|
|
278
|
+
this.#source = source;
|
|
279
|
+
this.#container = container;
|
|
280
|
+
this.#portal = document.createElement("div");
|
|
281
|
+
this.#portal.setAttribute("data-portal", "");
|
|
282
|
+
this.#portal.setAttribute("tabindex", "-1");
|
|
283
|
+
this.#entranceSentinel = this.#createSentinel();
|
|
284
|
+
this.#exitSentinel = this.#createSentinel();
|
|
285
|
+
this.#initialize();
|
|
286
|
+
}
|
|
287
|
+
destroy() {
|
|
288
|
+
if (this.#isDestroyed) {
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
this.#isDestroyed = true;
|
|
292
|
+
this.#controller?.abort();
|
|
293
|
+
this.#controller = null;
|
|
294
|
+
this.#getFocusables().forEach((focusable) => {
|
|
295
|
+
const index = this.#tabIndexes.get(focusable);
|
|
296
|
+
if (index === null) {
|
|
297
|
+
focusable.removeAttribute("tabindex");
|
|
298
|
+
} else {
|
|
299
|
+
focusable.setAttribute("tabindex", String(index));
|
|
300
|
+
}
|
|
301
|
+
});
|
|
302
|
+
this.#exitSentinel.after(this.#source);
|
|
303
|
+
this.#portal.remove();
|
|
304
|
+
this.#entranceSentinel.remove();
|
|
305
|
+
this.#exitSentinel.remove();
|
|
306
|
+
}
|
|
307
|
+
getElement() {
|
|
308
|
+
return this.#portal;
|
|
309
|
+
}
|
|
310
|
+
#initialize() {
|
|
311
|
+
this.#source.before(this.#entranceSentinel);
|
|
312
|
+
this.#entranceSentinel.after(this.#exitSentinel);
|
|
313
|
+
this.#portal.append(this.#source);
|
|
314
|
+
this.#container.append(this.#portal);
|
|
315
|
+
getFocusables(this.#source, { composed: true }).forEach(
|
|
316
|
+
(focusable) => {
|
|
317
|
+
const index = focusable.getAttribute("tabindex")?.trim();
|
|
318
|
+
this.#tabIndexes.set(focusable, index === null ? null : Number(index));
|
|
319
|
+
focusable.setAttribute("tabindex", "-1");
|
|
320
|
+
}
|
|
321
|
+
);
|
|
322
|
+
this.#controller = new AbortController();
|
|
323
|
+
const { signal } = this.#controller;
|
|
324
|
+
document.addEventListener("focusin", this.#onFocusIn, {
|
|
325
|
+
capture: true,
|
|
326
|
+
signal
|
|
327
|
+
});
|
|
328
|
+
document.addEventListener("keydown", this.#onKeyDown, {
|
|
329
|
+
capture: true,
|
|
330
|
+
signal
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
#onFocusIn = (event) => {
|
|
334
|
+
const current = event.target;
|
|
335
|
+
const before = event.relatedTarget;
|
|
336
|
+
if (!(before instanceof Element)) {
|
|
337
|
+
return;
|
|
338
|
+
}
|
|
339
|
+
const focusables = this.#getFocusables();
|
|
340
|
+
if (current === this.#entranceSentinel) {
|
|
341
|
+
if (this.#source.contains(before)) {
|
|
342
|
+
this.#focusOutside("backward");
|
|
343
|
+
} else {
|
|
344
|
+
const first = focusables[0];
|
|
345
|
+
first && focus(first);
|
|
346
|
+
}
|
|
347
|
+
} else if (current === this.#exitSentinel) {
|
|
348
|
+
if (this.#source.contains(before)) {
|
|
349
|
+
this.#focusOutside("forward");
|
|
350
|
+
} else {
|
|
351
|
+
const last = focusables.at(-1);
|
|
352
|
+
last && focus(last);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
};
|
|
356
|
+
#onKeyDown = (event) => {
|
|
357
|
+
if (event.key !== "Tab" || event.altKey || event.ctrlKey || event.metaKey) {
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
360
|
+
const active = getActiveElement2();
|
|
361
|
+
if (!(active instanceof Element)) {
|
|
362
|
+
return;
|
|
363
|
+
}
|
|
364
|
+
if (!this.#source.contains(active)) {
|
|
365
|
+
return;
|
|
366
|
+
}
|
|
367
|
+
const focusables = this.#getFocusables();
|
|
368
|
+
if (!focusables.length) {
|
|
369
|
+
event.preventDefault();
|
|
370
|
+
(event.shiftKey ? this.#entranceSentinel : this.#exitSentinel).focus();
|
|
371
|
+
}
|
|
372
|
+
const index = focusables.indexOf(active);
|
|
373
|
+
if (index === -1) {
|
|
374
|
+
return;
|
|
375
|
+
}
|
|
376
|
+
event.preventDefault();
|
|
377
|
+
const focusable = focusables[index + (event.shiftKey ? -1 : 1)];
|
|
378
|
+
if (focusable) {
|
|
379
|
+
focus(focusable);
|
|
380
|
+
} else {
|
|
381
|
+
(event.shiftKey ? this.#entranceSentinel : this.#exitSentinel).focus();
|
|
382
|
+
}
|
|
383
|
+
};
|
|
384
|
+
#createSentinel() {
|
|
385
|
+
const sentinel = document.createElement("span");
|
|
386
|
+
sentinel.setAttribute("aria-hidden", "true");
|
|
387
|
+
sentinel.setAttribute("tabindex", "0");
|
|
388
|
+
sentinel.style.cssText += VISUALLY_HIDDEN_CSS;
|
|
389
|
+
return sentinel;
|
|
390
|
+
}
|
|
391
|
+
#focusOutside(direction) {
|
|
392
|
+
const options = {
|
|
393
|
+
anchor: direction === "backward" ? this.#entranceSentinel : this.#exitSentinel,
|
|
394
|
+
composed: true
|
|
395
|
+
};
|
|
396
|
+
const focusable = direction === "backward" ? getPreviousFocusable(document.body, options) : getNextFocusable(document.body, options);
|
|
397
|
+
focusable && focus(focusable);
|
|
398
|
+
}
|
|
399
|
+
#getFocusables() {
|
|
400
|
+
return getFocusables(this.#source, { composed: true });
|
|
401
|
+
}
|
|
402
|
+
};
|
|
403
|
+
function focus(element) {
|
|
404
|
+
"focus" in element && typeof element.focus === "function" && element.focus();
|
|
405
|
+
}
|
|
406
|
+
function getActiveElement2() {
|
|
407
|
+
let current = document.activeElement;
|
|
408
|
+
while (current?.shadowRoot?.activeElement) {
|
|
409
|
+
current = current.shadowRoot.activeElement;
|
|
410
|
+
}
|
|
411
|
+
return current;
|
|
412
|
+
}
|
|
413
|
+
/**
|
|
414
|
+
* Portal
|
|
415
|
+
* Lightweight DOM portal (teleport) utility with fully focus management.
|
|
416
|
+
* Designed for accessible dialogs, menus, overlays, popovers, and etc.
|
|
417
|
+
*
|
|
418
|
+
* @version 0.0.1
|
|
419
|
+
* @author Yusuke Kamiyamane
|
|
420
|
+
* @license MIT
|
|
421
|
+
* @copyright Copyright (c) Yusuke Kamiyamane
|
|
422
|
+
* @see {@link https://github.com/y14e/portal}
|
|
423
|
+
*/
|
|
424
|
+
/*! Bundled license information:
|
|
425
|
+
|
|
426
|
+
power-focusable/dist/index.js:
|
|
427
|
+
(**
|
|
428
|
+
* Power Focusable
|
|
429
|
+
* High-precision focus management utility with full composed tree support.
|
|
430
|
+
* Handles complex focus rules including tabindex ordering, radio groups, inert,
|
|
431
|
+
* and shadow DOM.
|
|
432
|
+
*
|
|
433
|
+
* @version 4.0.2
|
|
434
|
+
* @author Yusuke Kamiyamane
|
|
435
|
+
* @license MIT
|
|
436
|
+
* @copyright Copyright (c) Yusuke Kamiyamane
|
|
437
|
+
* @see {@link https://github.com/y14e/power-focusable}
|
|
438
|
+
*)
|
|
439
|
+
*/
|
|
440
|
+
|
|
441
|
+
export { Portal, createPortal };
|
package/package.json
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@y14e/portal",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Lightweight DOM portal (teleport) utility with fully focus management",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"require": "./dist/index.cjs"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist",
|
|
18
|
+
"LICENSE",
|
|
19
|
+
"README.md"
|
|
20
|
+
],
|
|
21
|
+
"scripts": {
|
|
22
|
+
"build": "tsup",
|
|
23
|
+
"prepublishOnly": "npm run build",
|
|
24
|
+
"lint": "tsc --noEmit"
|
|
25
|
+
},
|
|
26
|
+
"keywords": [
|
|
27
|
+
"a11y",
|
|
28
|
+
"accessibility",
|
|
29
|
+
"focus",
|
|
30
|
+
"focus-management",
|
|
31
|
+
"focusable",
|
|
32
|
+
"portal",
|
|
33
|
+
"shadow-dom",
|
|
34
|
+
"shadowdom",
|
|
35
|
+
"teleport",
|
|
36
|
+
"typescript",
|
|
37
|
+
"utility"
|
|
38
|
+
],
|
|
39
|
+
"author": "Yusuke Kamiyamane",
|
|
40
|
+
"license": "MIT",
|
|
41
|
+
"repository": {
|
|
42
|
+
"type": "git",
|
|
43
|
+
"url": "git+https://github.com/y14e/portal.git"
|
|
44
|
+
},
|
|
45
|
+
"bugs": {
|
|
46
|
+
"url": "https://github.com/y14e/portal/issues"
|
|
47
|
+
},
|
|
48
|
+
"homepage": "https://github.com/y14e/portal#readme",
|
|
49
|
+
"devDependencies": {
|
|
50
|
+
"bun-types": "latest",
|
|
51
|
+
"power-focusable": "^4.0.2",
|
|
52
|
+
"tsup": "^8.0.0",
|
|
53
|
+
"typescript": "^5.6.0"
|
|
54
|
+
},
|
|
55
|
+
"engines": {
|
|
56
|
+
"node": ">=18"
|
|
57
|
+
}
|
|
58
|
+
}
|