@tangible/ui 0.2.28 → 0.2.29

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.
@@ -77,6 +77,55 @@
77
77
  padding-inline-end: calc(var(--_spacing) + var(--_offset-end));
78
78
  }
79
79
 
80
+ // Native <dialog> host, for the vanilla build (src/vanilla/modal.ts). The
81
+ // <dialog> IS the .tui-modal layer above: full-viewport, scrolling, padding
82
+ // the box in from the edges. It paints the backdrop as its own background
83
+ // instead of through ::backdrop, so the colour reads --_backdrop from the
84
+ // element's normal cascade, and a click on the <dialog> itself is a click on
85
+ // the backdrop. The rules below undo the UA dialog box (fit-content size,
86
+ // centring margin, border, Canvas background).
87
+ dialog.#{sys.$prefix}modal {
88
+ inline-size: auto;
89
+ block-size: auto;
90
+ max-inline-size: none;
91
+ max-block-size: none;
92
+ margin: 0;
93
+ border: 0;
94
+ background: var(--_backdrop);
95
+ // The host paints no text of its own; the box sets its fg.
96
+ color: inherit;
97
+ // A tall box scrolls the <dialog>; don't hand the scroll on to the page.
98
+ overscroll-behavior: contain;
99
+
100
+ // .tui-modal's display: flex would otherwise show a closed dialog.
101
+ &:not([open]) {
102
+ display: none;
103
+ }
104
+
105
+ &::backdrop {
106
+ background: transparent;
107
+ }
108
+
109
+ // The <dialog> takes focus only as a fallback; the ring goes on the box.
110
+ &:focus-visible {
111
+ outline: none;
112
+ }
113
+ }
114
+
115
+ // A video or other embed filling the body at 16:9, the common case for a
116
+ // modal that exists to play something.
117
+ .#{sys.$prefix}modal__embed {
118
+ display: block;
119
+ inline-size: 100%;
120
+ aspect-ratio: 16 / 9;
121
+ border: 0;
122
+ }
123
+
124
+ dialog.#{sys.$prefix}modal:focus-visible > .#{sys.$prefix}modal__dialog {
125
+ outline: var(--#{sys.$prefix}focus-ring-width) solid var(--#{sys.$prefix}focus-ring-color);
126
+ outline-offset: var(--#{sys.$prefix}focus-ring-offset);
127
+ }
128
+
80
129
  .#{sys.$prefix}modal__backdrop {
81
130
  position: fixed; // Fixed so it covers viewport even when container scrolls
82
131
  inset: 0;
@@ -320,6 +369,18 @@
320
369
  transform: none;
321
370
  opacity: 1;
322
371
  }
372
+
373
+ // Native host: fade the backdrop it paints. The box above already keys
374
+ // off data-state, which the script sets after showModal() and flips to
375
+ // "closing" for the exit.
376
+ dialog.#{sys.$prefix}modal {
377
+ background-color: transparent;
378
+ transition: background-color var(--#{sys.$prefix}motion-duration) var(--#{sys.$prefix}motion-timing);
379
+ }
380
+
381
+ dialog.#{sys.$prefix}modal[data-state="open"] {
382
+ background-color: var(--_backdrop);
383
+ }
323
384
  }
324
385
 
325
386
  // Windows High Contrast Mode support
package/tui-manifest.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
- "version": "0.2.28",
3
- "generated": "2026-10-02T21:31:01.904Z",
2
+ "version": "0.2.29",
3
+ "generated": "2026-10-19T00:42:30.786Z",
4
4
  "components": {
5
5
  "Accordion": {
6
6
  "props": {
@@ -1665,7 +1665,7 @@
1665
1665
  "role": {
1666
1666
  "type": "\"dialog\" | \"alertdialog\"",
1667
1667
  "required": false,
1668
- "description": "ARIA role of the dialog element. Default `'dialog'`.\n\n`'alertdialog'` is for a dialog that interrupts the user's workflow to demand a response —\na destructive-action confirm is the textbook case. Focus trap, `aria-modal`, Escape and\nfocus restore behave identically for both roles; what changes is the contract around them:\nthe APG alert dialog pattern expects BOTH `aria-labelledby` (the heading) and\n`aria-describedby` (the message) — a dev warning fires if either is missing — and the\nbackdrop stops dismissing on click unless `closeOnBackdropClick` says otherwise, since a\nstray click outside is not a response. Escape still closes: it is the keyboard \"cancel\".\n\nInitial focus lands on the first focusable element in DOM order (unless\n`initialFocusSelector` says otherwise), and the APG wants that to be the safest action —\nso put Cancel before the destructive action in `Modal.Foot`. If the visual order must\ndiffer (`Modal.Foot justify=\"between\"` with a destructive action first), point\n`initialFocusSelector` at the safe one.\nhttps://www.w3.org/WAI/ARIA/apg/patterns/alertdialog/"
1668
+ "description": "ARIA role of the dialog element. Default `'dialog'`.\n\n`'alertdialog'` is for a dialog that interrupts the user's workflow to demand a response —\na destructive-action confirm is the textbook case. Focus trap, `aria-modal`, Escape and\nfocus restore behave identically for both roles; what changes is the contract around them:\nthe APG alert dialog pattern expects BOTH `aria-labelledby` (the heading) and\n`aria-describedby` (the message) — a dev warning fires if either is missing — and the\nbackdrop stops dismissing on click unless `closeOnBackdropClick` says otherwise, since a\nstray click outside is not a response. Escape still closes: it is the keyboard \"cancel\".\n\nInitial focus lands on the first focusable element in DOM order, skipping the built-in\nclose button (unless `initialFocusSelector` says otherwise), and the APG wants that to be\nthe safest action —\nso put Cancel before the destructive action in `Modal.Foot`. If the visual order must\ndiffer (`Modal.Foot justify=\"between\"` with a destructive action first), point\n`initialFocusSelector` at the safe one.\nhttps://www.w3.org/WAI/ARIA/apg/patterns/alertdialog/"
1669
1669
  },
1670
1670
  "closeOnBackdropClick": {
1671
1671
  "type": "boolean",
@@ -1676,6 +1676,11 @@
1676
1676
  "type": "boolean",
1677
1677
  "required": false,
1678
1678
  "description": "When true, prevents the browser from scrolling to the trigger element\nwhen focus is restored on close. Useful when the trigger may be off-screen\ninside a scrollable container. Default: false."
1679
+ },
1680
+ "restoreFocusTo": {
1681
+ "type": "RefObject<HTMLElement> | (() => HTMLElement)",
1682
+ "required": false,
1683
+ "description": "Where focus goes on close, read at close time. Defaults to whatever had\nfocus when the modal opened. Set it when that element can be gone by then:\na \"Delete\" in a list row whose confirm removes the row. Point it at the\nnext row, the list heading or the \"Add\" button. A function suits a target\nthat only exists by close time (the row after the deleted one). If neither\nthis nor the captured element can take focus, focus falls to <body> and a\ndev warning names this prop (WCAG 2.4.3)."
1679
1684
  }
1680
1685
  },
1681
1686
  "cssTokens": [
@@ -1,18 +1,4 @@
1
- /**
2
- * CSS selector for focusable elements.
3
- * Used by focus trap and initial focus logic.
4
- */
5
- export declare const FOCUSABLE_SELECTOR: string;
6
- /**
7
- * Whether an element matched by FOCUSABLE_SELECTOR can actually take focus.
8
- * The selector alone over-matches: a button inside a disabled fieldset, an
9
- * inert subtree or a display:none ancestor (a closed accordion panel, a hidden
10
- * tab panel) still matches it. Picking one of those as the trap's first/last
11
- * edge lets Tab walk straight out of the dialog, so filter them here.
12
- */
13
- export declare function isTabbable(el: HTMLElement, container?: HTMLElement): boolean;
14
- /** The focusable elements in `container` that can actually take focus, in DOM order. */
15
- export declare function getTabbable(container: HTMLElement): HTMLElement[];
1
+ export { FOCUSABLE_SELECTOR, isTabbable, getTabbable, getInitialFocus } from './tabbable';
16
2
  /**
17
3
  * Options for the focus trap hook.
18
4
  */
@@ -39,12 +25,3 @@ export type UseFocusTrapOptions = {
39
25
  * ```
40
26
  */
41
27
  export declare function useFocusTrap(containerRef: React.RefObject<HTMLElement | null>, options: UseFocusTrapOptions): void;
42
- /**
43
- * Finds the first focusable element within a container.
44
- * Optionally accepts a custom selector to try first.
45
- *
46
- * @param container - The container element to search within
47
- * @param preferredSelector - Optional selector to try before fallback
48
- * @returns The found element, or the container if nothing found
49
- */
50
- export declare function getInitialFocus(container: HTMLElement, preferredSelector?: string): HTMLElement;
@@ -1,51 +1,6 @@
1
1
  import { useEffect } from 'react';
2
- /**
3
- * CSS selector for focusable elements.
4
- * Used by focus trap and initial focus logic.
5
- */
6
- export const FOCUSABLE_SELECTOR = [
7
- '[data-autofocus]',
8
- 'a[href]',
9
- 'area[href]',
10
- 'button:not([disabled])',
11
- 'input:not([disabled]):not([type="hidden"])',
12
- 'select:not([disabled])',
13
- 'textarea:not([disabled])',
14
- 'iframe',
15
- 'audio[controls]',
16
- 'video[controls]',
17
- '[contenteditable]:not([contenteditable="false"])',
18
- '[tabindex]:not([tabindex="-1"])',
19
- ].join(',');
20
- /**
21
- * Whether an element matched by FOCUSABLE_SELECTOR can actually take focus.
22
- * The selector alone over-matches: a button inside a disabled fieldset, an
23
- * inert subtree or a display:none ancestor (a closed accordion panel, a hidden
24
- * tab panel) still matches it. Picking one of those as the trap's first/last
25
- * edge lets Tab walk straight out of the dialog, so filter them here.
26
- */
27
- export function isTabbable(el, container) {
28
- if (el.matches(':disabled') || el.closest('[inert]'))
29
- return false;
30
- if (typeof el.checkVisibility === 'function') {
31
- return el.checkVisibility({ visibilityProperty: true });
32
- }
33
- // No checkVisibility (jsdom, older engines): walk up to the container.
34
- for (let node = el; node && node !== container; node = node.parentElement) {
35
- if (node.hidden)
36
- return false;
37
- const style = getComputedStyle(node);
38
- if (style.display === 'none')
39
- return false;
40
- if (node === el && style.visibility === 'hidden')
41
- return false;
42
- }
43
- return true;
44
- }
45
- /** The focusable elements in `container` that can actually take focus, in DOM order. */
46
- export function getTabbable(container) {
47
- return Array.from(container.querySelectorAll(FOCUSABLE_SELECTOR)).filter((n) => isTabbable(n, container));
48
- }
2
+ import { getTabbable } from './tabbable.js';
3
+ export { FOCUSABLE_SELECTOR, isTabbable, getTabbable, getInitialFocus } from './tabbable.js';
49
4
  /**
50
5
  * Traps focus within a container element.
51
6
  * Tab and Shift+Tab cycle through focusable elements inside the container.
@@ -103,21 +58,3 @@ export function useFocusTrap(containerRef, options) {
103
58
  return () => document.removeEventListener('keydown', handleKeyDown);
104
59
  }, [isActive, containerRef, onEscape, escapeDeactivates]);
105
60
  }
106
- /**
107
- * Finds the first focusable element within a container.
108
- * Optionally accepts a custom selector to try first.
109
- *
110
- * @param container - The container element to search within
111
- * @param preferredSelector - Optional selector to try before fallback
112
- * @returns The found element, or the container if nothing found
113
- */
114
- export function getInitialFocus(container, preferredSelector) {
115
- if (preferredSelector) {
116
- const preferred = container.querySelector(preferredSelector);
117
- if (preferred)
118
- return preferred;
119
- }
120
- // [data-autofocus] first, then DOM order, skipping anything that can't take focus.
121
- const tabbable = getTabbable(container);
122
- return tabbable.find((n) => n.matches('[data-autofocus]')) ?? tabbable[0] ?? container;
123
- }
@@ -0,0 +1,4 @@
1
+ /** Locks body scroll. Pair every call with one `unlockScroll()`. */
2
+ export declare function lockScroll(): void;
3
+ /** Releases one lock; scroll returns when none are left. */
4
+ export declare function unlockScroll(): void;
@@ -0,0 +1,25 @@
1
+ // Body scroll lock shared by every modal on the page: the React Modal and the
2
+ // vanilla one, which can be open at the same time when Fields (React) and
3
+ // Blocks or L&L markup (vanilla) share a page. The count lives on <body>, not
4
+ // in module scope, because each vanilla script is its own bundle with its own
5
+ // copy of this module. The lock lifts only when the last holder releases it.
6
+ const CLASS = 'tui-modal-open';
7
+ const COUNT = 'tuiScrollLock'; // data-tui-scroll-lock
8
+ function count() {
9
+ return Number(document.body.dataset[COUNT] ?? 0) || 0;
10
+ }
11
+ /** Locks body scroll. Pair every call with one `unlockScroll()`. */
12
+ export function lockScroll() {
13
+ document.body.dataset[COUNT] = String(count() + 1);
14
+ document.body.classList.add(CLASS);
15
+ }
16
+ /** Releases one lock; scroll returns when none are left. */
17
+ export function unlockScroll() {
18
+ const next = count() - 1;
19
+ if (next > 0) {
20
+ document.body.dataset[COUNT] = String(next);
21
+ return;
22
+ }
23
+ delete document.body.dataset[COUNT];
24
+ document.body.classList.remove(CLASS);
25
+ }
@@ -0,0 +1,29 @@
1
+ /**
2
+ * CSS selector for focusable elements.
3
+ * Used by focus trap and initial focus logic.
4
+ */
5
+ export declare const FOCUSABLE_SELECTOR: string;
6
+ /**
7
+ * Whether an element matched by FOCUSABLE_SELECTOR can actually take focus.
8
+ * The selector alone over-matches: a button inside a disabled fieldset, an
9
+ * inert subtree or a display:none ancestor (a closed accordion panel, a hidden
10
+ * tab panel) still matches it. Picking one of those as the trap's first/last
11
+ * edge lets Tab walk straight out of the dialog, so filter them here.
12
+ */
13
+ export declare function isTabbable(el: HTMLElement, container?: HTMLElement): boolean;
14
+ /** The focusable elements in `container` that can actually take focus, in DOM order. */
15
+ export declare function getTabbable(container: HTMLElement): HTMLElement[];
16
+ /**
17
+ * Finds the first focusable element within a container.
18
+ * Optionally accepts a custom selector to try first.
19
+ *
20
+ * @param container - The container element to search within
21
+ * @param preferredSelector - Optional selector to try before fallback
22
+ * @param options.lastResort - Selector for controls that shouldn't take initial
23
+ * focus while anything else can (a dialog's close button: opening a form on
24
+ * the control that discards it). Still chosen when it's all there is.
25
+ * @returns The found element, or the container if nothing found
26
+ */
27
+ export declare function getInitialFocus(container: HTMLElement, preferredSelector?: string, { lastResort }?: {
28
+ lastResort?: string;
29
+ }): HTMLElement;
@@ -0,0 +1,74 @@
1
+ // React-free focus helpers. Shared by the React focus trap and the vanilla
2
+ // builds (src/vanilla), which must not pull React into a drop-in script.
3
+ /**
4
+ * CSS selector for focusable elements.
5
+ * Used by focus trap and initial focus logic.
6
+ */
7
+ export const FOCUSABLE_SELECTOR = [
8
+ '[data-autofocus]',
9
+ 'a[href]',
10
+ 'area[href]',
11
+ 'button:not([disabled])',
12
+ 'input:not([disabled]):not([type="hidden"])',
13
+ 'select:not([disabled])',
14
+ 'textarea:not([disabled])',
15
+ 'iframe',
16
+ 'audio[controls]',
17
+ 'video[controls]',
18
+ '[contenteditable]:not([contenteditable="false"])',
19
+ '[tabindex]:not([tabindex="-1"])',
20
+ ].join(',');
21
+ /**
22
+ * Whether an element matched by FOCUSABLE_SELECTOR can actually take focus.
23
+ * The selector alone over-matches: a button inside a disabled fieldset, an
24
+ * inert subtree or a display:none ancestor (a closed accordion panel, a hidden
25
+ * tab panel) still matches it. Picking one of those as the trap's first/last
26
+ * edge lets Tab walk straight out of the dialog, so filter them here.
27
+ */
28
+ export function isTabbable(el, container) {
29
+ if (el.matches(':disabled') || el.closest('[inert]'))
30
+ return false;
31
+ if (typeof el.checkVisibility === 'function') {
32
+ return el.checkVisibility({ visibilityProperty: true });
33
+ }
34
+ // No checkVisibility (jsdom, older engines): walk up to the container.
35
+ for (let node = el; node && node !== container; node = node.parentElement) {
36
+ if (node.hidden)
37
+ return false;
38
+ const style = getComputedStyle(node);
39
+ if (style.display === 'none')
40
+ return false;
41
+ if (node === el && style.visibility === 'hidden')
42
+ return false;
43
+ }
44
+ return true;
45
+ }
46
+ /** The focusable elements in `container` that can actually take focus, in DOM order. */
47
+ export function getTabbable(container) {
48
+ return Array.from(container.querySelectorAll(FOCUSABLE_SELECTOR)).filter((n) => isTabbable(n, container));
49
+ }
50
+ /**
51
+ * Finds the first focusable element within a container.
52
+ * Optionally accepts a custom selector to try first.
53
+ *
54
+ * @param container - The container element to search within
55
+ * @param preferredSelector - Optional selector to try before fallback
56
+ * @param options.lastResort - Selector for controls that shouldn't take initial
57
+ * focus while anything else can (a dialog's close button: opening a form on
58
+ * the control that discards it). Still chosen when it's all there is.
59
+ * @returns The found element, or the container if nothing found
60
+ */
61
+ export function getInitialFocus(container, preferredSelector, { lastResort } = {}) {
62
+ if (preferredSelector) {
63
+ const preferred = container.querySelector(preferredSelector);
64
+ if (preferred)
65
+ return preferred;
66
+ }
67
+ // [data-autofocus] first, then DOM order, skipping anything that can't take focus.
68
+ const tabbable = getTabbable(container);
69
+ const autofocus = tabbable.find((n) => n.matches('[data-autofocus]'));
70
+ if (autofocus)
71
+ return autofocus;
72
+ const first = lastResort ? tabbable.find((n) => !n.matches(lastResort)) : tabbable[0];
73
+ return first ?? tabbable[0] ?? container;
74
+ }
@@ -0,0 +1,37 @@
1
+ /** Fired on the <dialog> after it opens. Bubbles, so one document listener covers every modal. */
2
+ export declare const MODAL_OPEN_EVENT = "tui-modal-open";
3
+ /** Fired on the <dialog> after it closes, however it closed. Bubbles. */
4
+ export declare const MODAL_CLOSE_EVENT = "tui-modal-close";
5
+ /** A dialog's id, or the element itself. */
6
+ export type ModalTarget = string | HTMLDialogElement;
7
+ export type ModalEventDetail = {
8
+ /** The element focus returns to on close: the trigger, or whatever had focus at open. */
9
+ opener: HTMLElement | null;
10
+ };
11
+ export type ModalCloseEventDetail = ModalEventDetail & {
12
+ /** The dialog's `returnValue`: the value of the button that submitted a `<form method="dialog">`. */
13
+ returnValue: string;
14
+ };
15
+ /**
16
+ * Opens a modal. Returns false when the target can't be found or opened.
17
+ *
18
+ * `opener` is where focus returns on close. Delegated triggers pass themselves;
19
+ * from script, pass the control that asked, because Safari doesn't focus a
20
+ * button on click and `document.activeElement` can be stale.
21
+ */
22
+ export declare function open(target: ModalTarget, opener?: HTMLElement | null): boolean;
23
+ /**
24
+ * Closes a modal after its exit transition. The promise resolves when the
25
+ * exit ends: with the dialog closed, or still open if `open()` was called
26
+ * during the exit. A close already in progress returns the same promise.
27
+ */
28
+ export declare function close(target: ModalTarget): Promise<void>;
29
+ /**
30
+ * Wires up `data-tui-modal-open` / `data-tui-modal-close`, the backdrop and
31
+ * Escape for the whole document. Listeners are delegated, so markup added
32
+ * later needs no second call. Calls are counted: the listeners come off when
33
+ * every caller has run its teardown.
34
+ *
35
+ * The standalone script (`vanilla/modal.min.js`) calls this itself.
36
+ */
37
+ export declare function init(): () => void;