inclusive-elements 0.2.0 → 0.2.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.
@@ -0,0 +1,400 @@
1
+ var T = Object.defineProperty;
2
+ var C = (a, o, t) => o in a ? T(a, o, { enumerable: !0, configurable: !0, writable: !0, value: t }) : a[o] = t;
3
+ var s = (a, o, t) => (C(a, typeof o != "symbol" ? o + "" : o, t), t);
4
+ import { move as E, hello as l, goodbye as u, cancel as k } from "hello-goodbye";
5
+ import { isFocusable as D, focusable as g } from "tabbable";
6
+ import { createFocusTrap as H } from "focus-trap";
7
+ import { autoUpdate as M, computePosition as A, shift as L, flip as x, size as O } from "@floating-ui/dom";
8
+ const v = class extends HTMLElement {
9
+ constructor() {
10
+ super(...arguments);
11
+ s(this, "timeouts", /* @__PURE__ */ new Map());
12
+ s(this, "index", 0);
13
+ }
14
+ connectedCallback() {
15
+ this.hasAttribute("role") || this.setAttribute("role", "status"), this.hasAttribute("aria-live") || this.setAttribute("aria-live", "polite"), this.hasAttribute("aria-relevant") || this.setAttribute("aria-relevant", "additions");
16
+ }
17
+ show(t, e = {}) {
18
+ const i = e.key || String(this.index++);
19
+ this.dismiss(i), t.dataset.key = i, E(this.children, () => {
20
+ this.append(t), l(t);
21
+ });
22
+ const n = typeof e.duration < "u" ? Number(e.duration) : v.duration;
23
+ return n > 0 && (this.startTimeout(t, n), t.addEventListener("mouseenter", this.clearTimeout.bind(this, t)), t.addEventListener("focusin", this.clearTimeout.bind(this, t)), t.addEventListener(
24
+ "mouseleave",
25
+ this.startTimeout.bind(this, t, n)
26
+ ), t.addEventListener(
27
+ "focusout",
28
+ this.startTimeout.bind(this, t, n)
29
+ )), i;
30
+ }
31
+ dismiss(t) {
32
+ if (typeof t == "string") {
33
+ this.querySelectorAll(
34
+ `[data-key="${t}"]`
35
+ ).forEach((e) => {
36
+ this.dismiss(e);
37
+ });
38
+ return;
39
+ }
40
+ E(this.children, () => {
41
+ u(t, {
42
+ finish: () => this.removeChild(t)
43
+ });
44
+ }), this.clearTimeout(t);
45
+ }
46
+ clear() {
47
+ Array.from(this.children).forEach((t) => {
48
+ this.dismiss(t);
49
+ });
50
+ }
51
+ speak(t) {
52
+ const e = document.createElement("div");
53
+ Object.assign(e.style, {
54
+ clip: "rect(0 0 0 0)",
55
+ clipPath: "inset(50%)",
56
+ height: "1px",
57
+ overflow: "hidden",
58
+ position: "absolute",
59
+ whiteSpace: "nowrap",
60
+ width: "1px"
61
+ }), e.textContent = t, this.show(e);
62
+ }
63
+ startTimeout(t, e) {
64
+ this.clearTimeout(t), this.timeouts.set(
65
+ t,
66
+ window.setTimeout(() => {
67
+ this.dismiss(t);
68
+ }, e)
69
+ );
70
+ }
71
+ clearTimeout(t) {
72
+ this.timeouts.has(t) && clearTimeout(this.timeouts.get(t));
73
+ }
74
+ };
75
+ let f = v;
76
+ s(f, "duration", 1e4);
77
+ const w = class extends HTMLElement {
78
+ constructor() {
79
+ super(...arguments);
80
+ s(this, "search", "");
81
+ s(this, "searchTimeout");
82
+ s(this, "onKeyDown", (t) => {
83
+ if (!this.hidden) {
84
+ if (t.key === "ArrowUp") {
85
+ this.navigate(-1), t.preventDefault();
86
+ return;
87
+ }
88
+ if (t.key === "ArrowDown") {
89
+ this.navigate(1), t.preventDefault();
90
+ return;
91
+ }
92
+ t.key.length > 1 || t.ctrlKey || t.metaKey || t.altKey || (this.search += t.key.toLowerCase(), t.preventDefault(), clearTimeout(this.searchTimeout), this.searchTimeout = window.setTimeout(() => {
93
+ this.search = "";
94
+ }, w.searchDelay), this.items.some((e) => {
95
+ var i;
96
+ return ((i = e.textContent) == null ? void 0 : i.trim().toLowerCase().indexOf(this.search)) === 0 ? (e.focus(), !0) : !1;
97
+ }));
98
+ }
99
+ });
100
+ }
101
+ connectedCallback() {
102
+ this.hasAttribute("role") || this.setAttribute("role", "menu"), this.hasAttribute("tabindex") || this.setAttribute("tabindex", "-1"), this.items.forEach((t) => {
103
+ t.hasAttribute("tabindex") || t.setAttribute("tabindex", "-1");
104
+ }), this.addEventListener("keydown", this.onKeyDown);
105
+ }
106
+ disconnectedCallback() {
107
+ this.removeEventListener("keydown", this.onKeyDown);
108
+ }
109
+ get items() {
110
+ return Array.from(
111
+ this.querySelectorAll("[role^=menuitem]")
112
+ ).filter((t) => D(t));
113
+ }
114
+ navigate(t) {
115
+ var n;
116
+ const e = this.items;
117
+ let i = (document.activeElement instanceof HTMLElement ? e.indexOf(document.activeElement) : -1) + t;
118
+ i < 0 && (i = e.length - 1), i >= e.length && (i = 0), (n = e[i]) == null || n.focus();
119
+ }
120
+ };
121
+ let m = w;
122
+ s(m, "searchDelay", 800);
123
+ const d = class extends HTMLElement {
124
+ constructor() {
125
+ super();
126
+ s(this, "focusTrap");
127
+ s(this, "connected", !1);
128
+ const t = document.createElement("template");
129
+ t.innerHTML = `
130
+ <div part="backdrop" style="position: fixed; top: 0; left: 0; right: 0; bottom: 0"></div>
131
+ <div part="content" style="z-index: 1"><slot></slot></div>
132
+ `;
133
+ const e = this.attachShadow({ mode: "open" });
134
+ e.appendChild(t.content.cloneNode(!0)), this.backdrop.addEventListener("click", () => {
135
+ var i;
136
+ this.hasAttribute("static") ? (i = d.attention) == null || i.call(d, e.children[1]) : this.close();
137
+ }), this.focusTrap = H(this, {
138
+ escapeDeactivates: !1,
139
+ allowOutsideClick: !0
140
+ });
141
+ }
142
+ static get observedAttributes() {
143
+ return ["open"];
144
+ }
145
+ connectedCallback() {
146
+ var t, e, i, n, r, y;
147
+ this.connected = !0, (t = this.content) != null && t.hasAttribute("role") || (e = this.content) == null || e.setAttribute("role", "dialog"), (i = this.content) != null && i.hasAttribute("aria-modal") || (n = this.content) == null || n.setAttribute("aria-modal", "true"), (r = this.content) != null && r.hasAttribute("tabindex") || (y = this.content) == null || y.setAttribute("tabindex", "-1"), this.addEventListener("keydown", (p) => {
148
+ p.key === "Escape" && this.open && (p.preventDefault(), p.stopPropagation(), this.close());
149
+ }), this.open && this.wasOpened();
150
+ }
151
+ disconnectedCallback() {
152
+ this.connected = !1;
153
+ }
154
+ get open() {
155
+ return this.hasAttribute("open");
156
+ }
157
+ set open(t) {
158
+ t ? this.setAttribute("open", "") : this.removeAttribute("open");
159
+ }
160
+ close() {
161
+ if (!this.open)
162
+ return;
163
+ const t = new Event("beforeclose", { cancelable: !0 });
164
+ this.dispatchEvent(t) && (this.open = !1);
165
+ }
166
+ attributeChangedCallback(t, e, i) {
167
+ t !== "open" || !this.connected || (i !== null ? this.wasOpened() : this.wasClosed());
168
+ }
169
+ wasOpened() {
170
+ var t, e;
171
+ this.hidden = !1, l(this), (t = this.focusTrap) == null || t.activate(), (e = this.querySelector("[autofocus]")) == null || e.focus(), this.dispatchEvent(new Event("open"));
172
+ }
173
+ wasClosed() {
174
+ var t;
175
+ (t = this.focusTrap) == null || t.deactivate(), u(this, {
176
+ finish: () => this.hidden = !0
177
+ }), this.dispatchEvent(new Event("close"));
178
+ }
179
+ get backdrop() {
180
+ var t;
181
+ return (t = this.shadowRoot) == null ? void 0 : t.firstElementChild;
182
+ }
183
+ get content() {
184
+ return this.firstElementChild;
185
+ }
186
+ };
187
+ let b = d;
188
+ s(b, "attention", (t) => t.animate(
189
+ [
190
+ { transform: "scale(1)" },
191
+ { transform: "scale(1.1)" },
192
+ { transform: "scale(1)" }
193
+ ],
194
+ 300
195
+ ));
196
+ class F extends HTMLElement {
197
+ constructor() {
198
+ super();
199
+ s(this, "cleanup");
200
+ const t = document.createElement("template");
201
+ t.innerHTML = `
202
+ <div part="backdrop" hidden style="position: fixed; top: 0; left: 0; right: 0; bottom: 0"></div>
203
+ <slot></slot>
204
+ `, this.attachShadow({ mode: "open" }).appendChild(t.content.cloneNode(!0)), this.backdrop.onclick = () => this.open = !1;
205
+ }
206
+ static get observedAttributes() {
207
+ return ["open"];
208
+ }
209
+ connectedCallback() {
210
+ this.backdrop.hidden = !0, this.content.hidden = !0, this.open = !1, this.button.setAttribute("aria-expanded", "false"), setTimeout(() => {
211
+ this.content.getAttribute("role") === "menu" && this.button.setAttribute("aria-haspopup", "true");
212
+ }), this.button.addEventListener("click", () => {
213
+ this.open = !this.open;
214
+ }), this.button.addEventListener("keydown", (t) => {
215
+ var e;
216
+ t.key === "ArrowDown" && (t.preventDefault(), this.open = !0, (e = g(this.content)[0]) == null || e.focus());
217
+ }), this.addEventListener("keydown", (t) => {
218
+ t.key === "Escape" && this.open && (t.preventDefault(), t.stopPropagation(), this.open = !1, this.button.focus());
219
+ }), this.addEventListener("focusout", (t) => {
220
+ (!(t.relatedTarget instanceof Node) || !this.contains(t.relatedTarget)) && (this.open = !1);
221
+ }), this.content.addEventListener("click", (t) => {
222
+ t.target instanceof Element && t.target.closest("[role=menuitem], [role=menuitemradio]") && (this.open = !1, this.button.focus());
223
+ });
224
+ }
225
+ disconnectedCallback() {
226
+ k(this.backdrop), k(this.content);
227
+ }
228
+ get open() {
229
+ return this.hasAttribute("open");
230
+ }
231
+ set open(t) {
232
+ t ? this.setAttribute("open", "") : this.removeAttribute("open");
233
+ }
234
+ attributeChangedCallback(t, e, i) {
235
+ t === "open" && (i !== null ? this.wasOpened() : this.wasClosed());
236
+ }
237
+ wasOpened() {
238
+ if (!this.content.hidden)
239
+ return;
240
+ this.content.hidden = !1, this.backdrop.hidden = !1, l(this.content), l(this.backdrop), this.button.setAttribute("aria-expanded", "true"), this.content.style.position = "absolute", this.cleanup = M(
241
+ this.button,
242
+ this.content,
243
+ () => {
244
+ A(this.button, this.content, {
245
+ placement: this.getAttribute("placement") || "bottom",
246
+ middleware: [
247
+ L(),
248
+ x(),
249
+ O({
250
+ apply: ({ availableWidth: e, availableHeight: i }) => {
251
+ Object.assign(this.content.style, {
252
+ maxWidth: "",
253
+ maxHeight: ""
254
+ });
255
+ const n = getComputedStyle(this.content);
256
+ (n.maxWidth === "none" || e < parseInt(n.maxWidth)) && (this.content.style.maxWidth = `${e}px`), (n.maxHeight === "none" || i < parseInt(n.maxHeight)) && (this.content.style.maxHeight = `${i}px`);
257
+ }
258
+ })
259
+ ]
260
+ }).then(({ x: e, y: i, placement: n }) => {
261
+ Object.assign(this.content.style, {
262
+ left: `${e}px`,
263
+ top: `${i}px`
264
+ }), this.content.dataset.placement = n;
265
+ });
266
+ },
267
+ { ancestorScroll: !1 }
268
+ );
269
+ const t = this.content.querySelector("[autofocus]");
270
+ t ? t.focus() : this.content.focus(), this.dispatchEvent(new Event("open"));
271
+ }
272
+ wasClosed() {
273
+ var t;
274
+ this.content.hidden || (this.button.setAttribute("aria-expanded", "false"), (t = this.cleanup) == null || t.call(this), u(this.backdrop, {
275
+ finish: () => this.backdrop.hidden = !0
276
+ }), u(this.content, {
277
+ finish: () => this.content.hidden = !0
278
+ }), this.dispatchEvent(new Event("close")));
279
+ }
280
+ get backdrop() {
281
+ var t;
282
+ return (t = this.shadowRoot) == null ? void 0 : t.firstElementChild;
283
+ }
284
+ get button() {
285
+ return this.children[0];
286
+ }
287
+ get content() {
288
+ return this.children[1];
289
+ }
290
+ }
291
+ class $ extends HTMLElement {
292
+ constructor() {
293
+ super(...arguments);
294
+ s(this, "onInitialFocus", () => {
295
+ this.removeAttribute("tabindex"), this.focusControlAtIndex(0);
296
+ });
297
+ s(this, "onKeyDown", (t) => {
298
+ if (t.key !== "ArrowRight" && t.key !== "ArrowLeft" && t.key !== "Home" && t.key !== "End")
299
+ return;
300
+ const e = this.controls, i = this.controls.length, n = e.indexOf(t.target);
301
+ if (n === -1)
302
+ return;
303
+ let r = 0;
304
+ t.key === "ArrowLeft" && (r = n - 1), t.key === "ArrowRight" && (r = n + 1), t.key === "End" && (r = i - 1), r < 0 && (r = i - 1), r > i - 1 && (r = 0), this.focusControlAtIndex(r), t.preventDefault();
305
+ });
306
+ }
307
+ connectedCallback() {
308
+ this.hasAttribute("role") || this.setAttribute("role", "toolbar"), this.setAttribute("tabindex", "0"), this.addEventListener("focus", this.onInitialFocus, { once: !0 }), this.addEventListener("keydown", this.onKeyDown);
309
+ }
310
+ disconnectedCallback() {
311
+ this.removeEventListener("keydown", this.onKeyDown);
312
+ }
313
+ focusControlAtIndex(t) {
314
+ this.controls.forEach((e, i) => {
315
+ i === t ? (e.setAttribute("tabindex", "0"), e.focus()) : e.setAttribute("tabindex", "-1");
316
+ });
317
+ }
318
+ get controls() {
319
+ return g(this);
320
+ }
321
+ }
322
+ const c = class extends HTMLElement {
323
+ constructor() {
324
+ super(...arguments);
325
+ s(this, "parent");
326
+ s(this, "tooltip");
327
+ s(this, "timeout");
328
+ s(this, "observer");
329
+ s(this, "showing", !1);
330
+ s(this, "onMouseEnter", this.afterDelay.bind(this, this.show));
331
+ s(this, "onFocus", this.show.bind(this));
332
+ s(this, "onMouseLeave", this.afterDelay.bind(this, this.hide));
333
+ s(this, "onBlur", this.hide.bind(this));
334
+ s(this, "onKeyDown", (t) => {
335
+ t.key === "Escape" && this.hide();
336
+ });
337
+ }
338
+ connectedCallback() {
339
+ this.parent = this.parentNode, this.parent && (this.parent.addEventListener("mouseenter", this.onMouseEnter), this.parent.addEventListener("focus", this.onFocus), this.parent.addEventListener("mouseleave", this.onMouseLeave), this.parent.addEventListener("blur", this.onBlur), this.parent.addEventListener("click", this.onBlur), this.observer = new MutationObserver((t) => {
340
+ t.forEach((e) => {
341
+ e.attributeName === "disabled" && this.hide();
342
+ });
343
+ }), this.observer.observe(this.parent, { attributes: !0 })), document.addEventListener("keydown", this.onKeyDown), document.addEventListener("scroll", this.onBlur);
344
+ }
345
+ disconnectedCallback() {
346
+ var t;
347
+ this.tooltip && (this.tooltip.remove(), this.tooltip = void 0), this.parent && (this.parent.removeEventListener("mouseenter", this.onMouseEnter), this.parent.removeEventListener("focus", this.onFocus), this.parent.removeEventListener("mouseleave", this.onMouseLeave), this.parent.removeEventListener("blur", this.onBlur), this.parent.removeEventListener("click", this.onBlur), this.parent = void 0), document.removeEventListener("keydown", this.onKeyDown), document.removeEventListener("scroll", this.onBlur), (t = this.observer) == null || t.disconnect(), clearTimeout(this.timeout);
348
+ }
349
+ get disabled() {
350
+ return this.hasAttribute("disabled");
351
+ }
352
+ set disabled(t) {
353
+ t ? this.setAttribute("disabled", "") : this.removeAttribute("disabled");
354
+ }
355
+ show() {
356
+ if (this.disabled)
357
+ return;
358
+ const t = this.createTooltip();
359
+ clearTimeout(this.timeout), this.showing || (t.hidden = !1, l(t), this.showing = !0), t.innerHTML !== this.innerHTML && (t.innerHTML = this.innerHTML), t.style.position = "absolute", A(this.parent, t, {
360
+ placement: this.getAttribute("placement") || c.placement,
361
+ middleware: [L(), x()]
362
+ }).then(({ x: e, y: i, placement: n }) => {
363
+ Object.assign(t.style, {
364
+ left: `${e}px`,
365
+ top: `${i}px`
366
+ }), t.dataset.placement = n;
367
+ });
368
+ }
369
+ hide() {
370
+ clearTimeout(this.timeout), this.showing && (this.showing = !1, u(this.tooltip, {
371
+ finish: () => {
372
+ this.tooltip && (this.tooltip.hidden = !0);
373
+ }
374
+ }));
375
+ }
376
+ afterDelay(t) {
377
+ clearTimeout(this.timeout);
378
+ const e = parseInt(this.getAttribute("delay") || "");
379
+ this.timeout = window.setTimeout(
380
+ t.bind(this),
381
+ isNaN(e) ? c.delay : e
382
+ );
383
+ }
384
+ createTooltip() {
385
+ return this.tooltip || (this.tooltip = document.createElement("div"), this.tooltip.className = this.getAttribute("tooltip-class") || c.tooltipClass, this.tooltip.hidden = !0, this.tooltip.addEventListener("mouseenter", this.show.bind(this)), this.tooltip.addEventListener(
386
+ "mouseleave",
387
+ this.afterDelay.bind(this, this.hide)
388
+ ), document.body.appendChild(this.tooltip)), this.tooltip;
389
+ }
390
+ };
391
+ let h = c;
392
+ s(h, "delay", 100), s(h, "placement", "top"), s(h, "tooltipClass", "tooltip");
393
+ export {
394
+ f as AlertsElement,
395
+ m as MenuElement,
396
+ b as ModalElement,
397
+ F as PopupElement,
398
+ $ as ToolbarElement,
399
+ h as TooltipElement
400
+ };
@@ -0,0 +1,17 @@
1
+ export declare type AlertOptions = {
2
+ key?: string;
3
+ duration?: number;
4
+ };
5
+ export default class AlertsElement extends HTMLElement {
6
+ static duration: number;
7
+ private timeouts;
8
+ private index;
9
+ connectedCallback(): void;
10
+ show(el: HTMLElement, options?: AlertOptions): string;
11
+ dismiss(el: HTMLElement): void;
12
+ dismiss(key: string): void;
13
+ clear(): void;
14
+ speak(message: string): void;
15
+ private startTimeout;
16
+ private clearTimeout;
17
+ }
@@ -0,0 +1,6 @@
1
+ export { default as AlertsElement } from './alerts/alerts';
2
+ export { default as MenuElement } from './menu/menu';
3
+ export { default as ModalElement } from './modal/modal';
4
+ export { default as PopupElement } from './popup/popup';
5
+ export { default as ToolbarElement } from './toolbar/toolbar';
6
+ export { default as TooltipElement } from './tooltip/tooltip';
@@ -0,0 +1,10 @@
1
+ export default class MenuElement extends HTMLElement {
2
+ static searchDelay: number;
3
+ private search;
4
+ private searchTimeout?;
5
+ connectedCallback(): void;
6
+ disconnectedCallback(): void;
7
+ private get items();
8
+ private onKeyDown;
9
+ private navigate;
10
+ }
@@ -0,0 +1,17 @@
1
+ export default class ModalElement extends HTMLElement {
2
+ static attention: (el: Element) => void;
3
+ static get observedAttributes(): string[];
4
+ private focusTrap?;
5
+ private connected;
6
+ constructor();
7
+ connectedCallback(): void;
8
+ disconnectedCallback(): void;
9
+ get open(): boolean;
10
+ set open(val: boolean);
11
+ close(): void;
12
+ attributeChangedCallback(name: string, oldValue: string, newValue: string): void;
13
+ private wasOpened;
14
+ private wasClosed;
15
+ private get backdrop();
16
+ private get content();
17
+ }
@@ -0,0 +1,15 @@
1
+ export default class PopupElement extends HTMLElement {
2
+ cleanup?: () => void;
3
+ static get observedAttributes(): string[];
4
+ constructor();
5
+ connectedCallback(): void;
6
+ disconnectedCallback(): void;
7
+ get open(): boolean;
8
+ set open(val: boolean);
9
+ attributeChangedCallback(name: string, oldValue: string, newValue: string): void;
10
+ private wasOpened;
11
+ private wasClosed;
12
+ private get backdrop();
13
+ private get button();
14
+ private get content();
15
+ }
@@ -0,0 +1,8 @@
1
+ export default class ToolbarElement extends HTMLElement {
2
+ connectedCallback(): void;
3
+ disconnectedCallback(): void;
4
+ private onInitialFocus;
5
+ private onKeyDown;
6
+ private focusControlAtIndex;
7
+ private get controls();
8
+ }
@@ -0,0 +1,23 @@
1
+ export default class TooltipElement extends HTMLElement {
2
+ static delay: number;
3
+ static placement: string;
4
+ static tooltipClass: string;
5
+ private parent?;
6
+ private tooltip?;
7
+ private timeout?;
8
+ private observer?;
9
+ private showing;
10
+ private onMouseEnter;
11
+ private onFocus;
12
+ private onMouseLeave;
13
+ private onBlur;
14
+ connectedCallback(): void;
15
+ disconnectedCallback(): void;
16
+ get disabled(): boolean;
17
+ set disabled(val: boolean);
18
+ private onKeyDown;
19
+ private show;
20
+ private hide;
21
+ private afterDelay;
22
+ private createTooltip;
23
+ }
package/package.json CHANGED
@@ -1,15 +1,14 @@
1
1
  {
2
2
  "name": "inclusive-elements",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "Accessible, lightweight, unstyled implementations of common UI controls.",
5
- "module": "./dist/inclusive-elements.js",
5
+ "module": "dist/inclusive-elements.js",
6
6
  "type": "module",
7
- "types": "./dist/types/index.d.ts",
7
+ "types": "dist/types/index.d.ts",
8
8
  "license": "MIT",
9
9
  "repository": "tobyzerner/inclusive-elements",
10
10
  "files": [
11
- "./dist",
12
- "./README.md"
11
+ "dist"
13
12
  ],
14
13
  "sideEffects": false,
15
14
  "scripts": {