@tangible/ui 0.2.27 → 0.2.28
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/components/Button/Button.js +29 -3
- package/components/Button/types.d.ts +6 -1
- package/components/IconButton/IconButton.js +14 -3
- package/components/IconButton/types.d.ts +6 -1
- package/components/Modal/Modal.js +4 -1
- package/figma-tokens.json +1 -1
- package/package.json +1 -1
- package/tui-manifest.json +4 -4
- package/utils/focus-trap.d.ts +10 -0
- package/utils/focus-trap.js +33 -10
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
-
import React, { forwardRef } from 'react';
|
|
2
|
+
import React, { forwardRef, useEffect, useRef } from 'react';
|
|
3
|
+
import { isDev } from '../../utils/is-dev.js';
|
|
3
4
|
import { cx } from '../../utils/cx.js';
|
|
4
5
|
import { Icon } from '../Icon/index.js';
|
|
5
6
|
import { getSafeRel } from '../../utils/polymorphic.js';
|
|
@@ -7,7 +8,22 @@ import { defaultButtonLabels } from './types.js';
|
|
|
7
8
|
export const Button = forwardRef(({ label, children, size = 'md', theme = 'primary', variant = 'solid', fullWidth, disabled = false, loading = false, loadingLabel: loadingLabelProp, labels: labelsProp, leftIconName, rightIconName, leftIcon, rightIcon, className, target, rel, onClick, style, ...rest }, ref) => {
|
|
8
9
|
const isLink = typeof rest.href === 'string';
|
|
9
10
|
const isDisabled = disabled || loading;
|
|
11
|
+
// Loading must not take focus away: native `disabled` on a focused button
|
|
12
|
+
// (or dropping a focused link's href) sends focus to <body>, so keyboard
|
|
13
|
+
// users lose their place on every async action (WCAG 2.4.3). Loading is
|
|
14
|
+
// aria-disabled and blocked in the click handler; only an explicit
|
|
15
|
+
// `disabled` goes native.
|
|
16
|
+
const loadingOnly = loading && !disabled;
|
|
10
17
|
const labels = { ...defaultButtonLabels, ...labelsProp };
|
|
18
|
+
// A non-string label can't compose "X, loading", so without loadingLabel
|
|
19
|
+
// the busy state has no name change at all.
|
|
20
|
+
const loadingWarned = useRef(false);
|
|
21
|
+
useEffect(() => {
|
|
22
|
+
if (isDev() && loading && typeof label !== 'string' && !loadingLabelProp && !loadingWarned.current) {
|
|
23
|
+
loadingWarned.current = true;
|
|
24
|
+
console.warn('Button: `loading` with a non-string `label` needs `loadingLabel`, or the loading state has no accessible name.');
|
|
25
|
+
}
|
|
26
|
+
}, [loading, label, loadingLabelProp]);
|
|
11
27
|
// Compose loading state into aria-label for screen readers
|
|
12
28
|
const loadingLabel = loading
|
|
13
29
|
? (loadingLabelProp ?? (typeof label === 'string' ? `${label}, loading` : undefined))
|
|
@@ -32,9 +48,19 @@ export const Button = forwardRef(({ label, children, size = 'md', theme = 'prima
|
|
|
32
48
|
}
|
|
33
49
|
onClick?.(e);
|
|
34
50
|
};
|
|
35
|
-
return (_jsxs("a", { ref: ref, href:
|
|
51
|
+
return (_jsxs("a", { ref: ref, href: disabled ? undefined : href, className: classes, "aria-label": loadingLabel, "aria-disabled": isDisabled || undefined, "aria-busy": loading || undefined, tabIndex: disabled ? -1 : tabIndex, onClick: handleClick, "data-loading": loading || undefined, target: target, rel: safeRel, style: style, ...anchorRest, children: [content, target === '_blank' && (_jsx("span", { className: "tui-visually-hidden", children: labels.newTab }))] }));
|
|
36
52
|
}
|
|
37
53
|
const buttonRest = rest;
|
|
38
|
-
|
|
54
|
+
// Enter/Space and implicit form submission all arrive as a click, so one
|
|
55
|
+
// guard covers them; preventDefault cancels a submit.
|
|
56
|
+
const handleButtonClick = (e) => {
|
|
57
|
+
if (loadingOnly) {
|
|
58
|
+
e.preventDefault();
|
|
59
|
+
e.stopPropagation();
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
onClick?.(e);
|
|
63
|
+
};
|
|
64
|
+
return (_jsx("button", { ref: ref, type: buttonRest.type ?? 'button', className: classes, "aria-label": loadingLabel, disabled: disabled, "aria-disabled": loadingOnly || undefined, "aria-busy": loading || undefined, onClick: handleButtonClick, "data-loading": loading || undefined, style: style, ...buttonRest, children: content }));
|
|
39
65
|
});
|
|
40
66
|
Button.displayName = 'Button';
|
|
@@ -66,7 +66,12 @@ type CommonProps = {
|
|
|
66
66
|
*/
|
|
67
67
|
disabled?: boolean;
|
|
68
68
|
/**
|
|
69
|
-
* Loading state
|
|
69
|
+
* Loading state: shows a spinner and blocks activation while keeping the
|
|
70
|
+
* button focusable (`aria-disabled`, not native `disabled`), so a
|
|
71
|
+
* keyboard user who triggered it keeps their place. Only `onClick` is
|
|
72
|
+
* guarded: put the action there, not in `onKeyDown`/`onPointerDown`.
|
|
73
|
+
* The start and end of loading aren't announced; confirm completion
|
|
74
|
+
* through your own status message (a Toast, say).
|
|
70
75
|
* @default false
|
|
71
76
|
*/
|
|
72
77
|
loading?: boolean;
|
|
@@ -20,6 +20,9 @@ export const IconButton = forwardRef((props, ref) => {
|
|
|
20
20
|
const { icon, label, size = 'sm', theme = 'secondary', variant = 'ghost', disabled = false, loading = false, loadingLabel: loadingLabelProp, pressed, shape = 'square', showTooltip = false, tooltipSide = 'top', className, ...rest } = props;
|
|
21
21
|
const isLink = typeof rest.href === 'string';
|
|
22
22
|
const isDisabled = disabled || loading;
|
|
23
|
+
// Loading keeps focus (see Button): aria-disabled plus a click guard, never
|
|
24
|
+
// native `disabled` or a dropped href. Only explicit `disabled` goes native.
|
|
25
|
+
const loadingOnly = loading && !disabled;
|
|
23
26
|
const resolvedLabel = loading ? (loadingLabelProp ?? `${label}, loading`) : label;
|
|
24
27
|
const classes = cx('tui-icon-button', `is-size-${size}`, `is-theme-${theme}`, `is-style-${variant}`, isDisabled && 'is-disabled', shape === 'circle' && 'is-shape-circle', className);
|
|
25
28
|
const iconContent = (_jsxs(_Fragment, { children: [_jsx(Icon, { name: icon }), loading && _jsx("span", { className: "tui-icon-button__spinner", "aria-hidden": "true" })] }));
|
|
@@ -36,11 +39,19 @@ export const IconButton = forwardRef((props, ref) => {
|
|
|
36
39
|
}
|
|
37
40
|
onClick?.(e);
|
|
38
41
|
};
|
|
39
|
-
element = (_jsx("a", { ref: ref, href:
|
|
42
|
+
element = (_jsx("a", { ref: ref, href: disabled ? undefined : href, className: classes, "aria-label": resolvedLabel, "aria-disabled": isDisabled || undefined, "aria-busy": loading || undefined, tabIndex: disabled ? -1 : tabIndex, onClick: handleClick, target: target, rel: safeRel, ...anchorRest, children: iconContent }));
|
|
40
43
|
}
|
|
41
44
|
else {
|
|
42
|
-
const buttonRest = rest;
|
|
43
|
-
|
|
45
|
+
const { onClick, ...buttonRest } = rest;
|
|
46
|
+
const handleButtonClick = (e) => {
|
|
47
|
+
if (loadingOnly) {
|
|
48
|
+
e.preventDefault();
|
|
49
|
+
e.stopPropagation();
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
onClick?.(e);
|
|
53
|
+
};
|
|
54
|
+
element = (_jsx("button", { ref: ref, type: buttonRest.type ?? 'button', className: classes, "aria-label": resolvedLabel, disabled: disabled, "aria-disabled": loadingOnly || undefined, "aria-busy": loading || undefined, "aria-pressed": pressed, onClick: handleButtonClick, ...buttonRest, children: iconContent }));
|
|
44
55
|
}
|
|
45
56
|
// Wrap in tooltip if requested
|
|
46
57
|
if (showTooltip) {
|
|
@@ -50,7 +50,12 @@ type IconButtonBaseProps = {
|
|
|
50
50
|
*/
|
|
51
51
|
disabled?: boolean;
|
|
52
52
|
/**
|
|
53
|
-
* Loading state
|
|
53
|
+
* Loading state: shows a spinner and blocks activation while keeping the
|
|
54
|
+
* button focusable (`aria-disabled`, not native `disabled`), so a
|
|
55
|
+
* keyboard user who triggered it keeps their place. Only `onClick` is
|
|
56
|
+
* guarded: put the action there, not in `onKeyDown`/`onPointerDown`.
|
|
57
|
+
* The start and end of loading aren't announced; confirm completion
|
|
58
|
+
* through your own status message (a Toast, say).
|
|
54
59
|
*/
|
|
55
60
|
loading?: boolean;
|
|
56
61
|
/**
|
|
@@ -110,13 +110,16 @@ function ModalRoot({ open, onClose, size = 'md', stickyHead, stickyFoot, 'aria-l
|
|
|
110
110
|
'hidden container, or pass an explicit `container`.', buried);
|
|
111
111
|
}
|
|
112
112
|
}, [open, mount]);
|
|
113
|
+
// Read at close time, not a dependency: the effect must run on close only.
|
|
114
|
+
const preventScrollOnRestoreRef = useRef(preventScrollOnRestore);
|
|
115
|
+
preventScrollOnRestoreRef.current = preventScrollOnRestore;
|
|
113
116
|
// Reset state when modal closes, restore focus to trigger.
|
|
114
117
|
useEffect(() => {
|
|
115
118
|
if (open)
|
|
116
119
|
return;
|
|
117
120
|
const el = restoreRef.current;
|
|
118
121
|
if (el && typeof el.focus === 'function') {
|
|
119
|
-
el.focus({ preventScroll:
|
|
122
|
+
el.focus({ preventScroll: preventScrollOnRestoreRef.current });
|
|
120
123
|
}
|
|
121
124
|
restoreRef.current = null;
|
|
122
125
|
setMount(null);
|
package/figma-tokens.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "0.2.
|
|
2
|
+
"version": "0.2.28",
|
|
3
3
|
"source": "publish/styles/all.expanded.css",
|
|
4
4
|
"prefix": "tui-",
|
|
5
5
|
"note": "Layer 1 primitives only. Component API tokens (--tui-{component}-*) are never defined in CSS — they exist only as fallback reads — so they have no Figma representation. CONTRACT: consumers read TUI defaults from this file at build time (variables[].cssVar, the \"Light\"/\"Dark\" mode names, and alias chains into TUI/Primitives). Renaming a cssVar or a mode is a breaking change to those consumers even when the CSS value is unchanged — a rename silently becomes an undefined default downstream — so treat it as at least a minor bump and call it out in the CHANGELOG.",
|
package/package.json
CHANGED
package/tui-manifest.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "0.2.
|
|
3
|
-
"generated": "2026-10-
|
|
2
|
+
"version": "0.2.28",
|
|
3
|
+
"generated": "2026-10-02T21:31:01.904Z",
|
|
4
4
|
"components": {
|
|
5
5
|
"Accordion": {
|
|
6
6
|
"props": {
|
|
@@ -241,7 +241,7 @@
|
|
|
241
241
|
"type": "boolean",
|
|
242
242
|
"required": false,
|
|
243
243
|
"defaultValue": "false",
|
|
244
|
-
"description": "Loading state
|
|
244
|
+
"description": "Loading state: shows a spinner and blocks activation while keeping the\nbutton focusable (`aria-disabled`, not native `disabled`), so a\nkeyboard user who triggered it keeps their place. Only `onClick` is\nguarded: put the action there, not in `onKeyDown`/`onPointerDown`.\nThe start and end of loading aren't announced; confirm completion\nthrough your own status message (a Toast, say)."
|
|
245
245
|
},
|
|
246
246
|
"loadingLabel": {
|
|
247
247
|
"type": "string",
|
|
@@ -1525,7 +1525,7 @@
|
|
|
1525
1525
|
"loading": {
|
|
1526
1526
|
"type": "boolean",
|
|
1527
1527
|
"required": false,
|
|
1528
|
-
"description": "Loading state
|
|
1528
|
+
"description": "Loading state: shows a spinner and blocks activation while keeping the\nbutton focusable (`aria-disabled`, not native `disabled`), so a\nkeyboard user who triggered it keeps their place. Only `onClick` is\nguarded: put the action there, not in `onKeyDown`/`onPointerDown`.\nThe start and end of loading aren't announced; confirm completion\nthrough your own status message (a Toast, say)."
|
|
1529
1529
|
},
|
|
1530
1530
|
"loadingLabel": {
|
|
1531
1531
|
"type": "string",
|
package/utils/focus-trap.d.ts
CHANGED
|
@@ -3,6 +3,16 @@
|
|
|
3
3
|
* Used by focus trap and initial focus logic.
|
|
4
4
|
*/
|
|
5
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[];
|
|
6
16
|
/**
|
|
7
17
|
* Options for the focus trap hook.
|
|
8
18
|
*/
|
package/utils/focus-trap.js
CHANGED
|
@@ -17,6 +17,35 @@ export const FOCUSABLE_SELECTOR = [
|
|
|
17
17
|
'[contenteditable]:not([contenteditable="false"])',
|
|
18
18
|
'[tabindex]:not([tabindex="-1"])',
|
|
19
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
|
+
}
|
|
20
49
|
/**
|
|
21
50
|
* Traps focus within a container element.
|
|
22
51
|
* Tab and Shift+Tab cycle through focusable elements inside the container.
|
|
@@ -48,14 +77,7 @@ export function useFocusTrap(containerRef, options) {
|
|
|
48
77
|
// Tab trap
|
|
49
78
|
if (e.key !== 'Tab')
|
|
50
79
|
return;
|
|
51
|
-
const nodes =
|
|
52
|
-
if (n === document.activeElement)
|
|
53
|
-
return true;
|
|
54
|
-
// offsetParent is null for position:fixed elements — use computed style
|
|
55
|
-
// as a more reliable visibility check
|
|
56
|
-
const style = getComputedStyle(n);
|
|
57
|
-
return style.display !== 'none' && style.visibility !== 'hidden';
|
|
58
|
-
});
|
|
80
|
+
const nodes = getTabbable(container);
|
|
59
81
|
if (nodes.length === 0) {
|
|
60
82
|
e.preventDefault();
|
|
61
83
|
container.focus();
|
|
@@ -95,6 +117,7 @@ export function getInitialFocus(container, preferredSelector) {
|
|
|
95
117
|
if (preferred)
|
|
96
118
|
return preferred;
|
|
97
119
|
}
|
|
98
|
-
|
|
99
|
-
|
|
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;
|
|
100
123
|
}
|