@tangible/ui 0.2.26 → 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.
@@ -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: isDisabled ? undefined : href, className: classes, "aria-label": loadingLabel, "aria-disabled": isDisabled || undefined, "aria-busy": loading || undefined, tabIndex: isDisabled ? -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 }))] }));
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
- return (_jsx("button", { ref: ref, type: buttonRest.type ?? 'button', className: classes, "aria-label": loadingLabel, disabled: isDisabled, "aria-busy": loading || undefined, onClick: onClick, "data-loading": loading || undefined, style: style, ...buttonRest, children: content }));
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. Shows spinner and prevents interaction.
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: isDisabled ? undefined : href, className: classes, "aria-label": resolvedLabel, "aria-disabled": isDisabled || undefined, "aria-busy": loading || undefined, tabIndex: isDisabled ? -1 : tabIndex, onClick: handleClick, target: target, rel: safeRel, ...anchorRest, children: iconContent }));
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
- element = (_jsx("button", { ref: ref, type: buttonRest.type ?? 'button', className: classes, "aria-label": resolvedLabel, disabled: isDisabled, "aria-busy": loading || undefined, "aria-pressed": pressed, ...buttonRest, children: iconContent }));
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 — shows spinner, disables interaction.
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: preventScrollOnRestore });
122
+ el.focus({ preventScroll: preventScrollOnRestoreRef.current });
120
123
  }
121
124
  restoreRef.current = null;
122
125
  setMount(null);
@@ -12,6 +12,12 @@ import { defaultNavMenuLabels } from './types.js';
12
12
  // A panel opens when the pointer settles, not after a fixed delay; once the
13
13
  // bar is warm (a panel open, or one closed within PRIMED_WINDOW) the next
14
14
  // opens at once.
15
+ const INTERACTIVE = [
16
+ 'a[href]', 'area[href]', 'button', 'input:not([type=hidden])', 'select', 'textarea', 'summary',
17
+ 'iframe', 'audio[controls]', 'video[controls]', '[contenteditable]:not([contenteditable=false])',
18
+ '[tabindex]:not([tabindex="-1"])', '[role=button]', '[role=link]', '[role=checkbox]', '[role=switch]',
19
+ '[role=menuitem]', '[role=tab]', '[aria-haspopup]',
20
+ ].join(', ');
15
21
  const PRIMED_WINDOW = 400;
16
22
  // Grace period for crossing into the panel.
17
23
  const HOVER_CLOSE_DELAY = 300;
@@ -93,7 +99,7 @@ function toAriaCurrent(current) {
93
99
  }
94
100
  const isSub = (node) => isValidElement(node) && node.type === NavMenuSub;
95
101
  const NavMenuItem = forwardRef(function NavMenuItem(props, ref) {
96
- const { current, icon, disabled, asChild, className, children, ...rest } = props;
102
+ const { current, icon, prefix, suffix, disabled, asChild, className, children, ...rest } = props;
97
103
  const inList = useContext(ListContext);
98
104
  const { orientation, flyoutTrigger, labels, openId, setOpenId, rootRef, warmth } = useContext(RootContext);
99
105
  const inFlyout = useContext(FlyoutContext);
@@ -104,6 +110,31 @@ const NavMenuItem = forwardRef(function NavMenuItem(props, ref) {
104
110
  console.warn('NavMenu.Item renders an <li>: place it inside NavMenu.Group or NavMenu.Sub.');
105
111
  }
106
112
  }, [inList]);
113
+ // The slots render inside the row's <a>/<button>, where interactive content
114
+ // is invalid HTML and unreachable for keyboard users.
115
+ const prefixRef = useRef(null);
116
+ const suffixRef = useRef(null);
117
+ const slotWarned = useRef({ both: false, interactive: false, named: false });
118
+ const restProps = rest;
119
+ const hasSlot = prefix != null || suffix != null;
120
+ const namedOver = hasSlot && (restProps['aria-label'] != null || restProps['aria-labelledby'] != null);
121
+ useEffect(() => {
122
+ if (!isDev())
123
+ return;
124
+ const w = slotWarned.current;
125
+ if (!w.both && prefix != null && icon) {
126
+ w.both = true;
127
+ console.warn('NavMenu.Item: `prefix` replaces `icon`; pass one or the other.');
128
+ }
129
+ if (!w.named && namedOver) {
130
+ w.named = true;
131
+ console.warn('NavMenu.Item: `aria-label`/`aria-labelledby` replaces the whole accessible name, so the `prefix`/`suffix` text is never announced. Let the row be named by its content.');
132
+ }
133
+ if (!w.interactive && [prefixRef.current, suffixRef.current].some((slot) => slot?.querySelector(INTERACTIVE))) {
134
+ w.interactive = true;
135
+ console.warn('NavMenu.Item: `prefix`/`suffix` render inside the row link, so they must not contain interactive content (links, buttons, inputs, tabindex).');
136
+ }
137
+ });
107
138
  // Sub lists render after the link, inside the same <li>.
108
139
  const all = Children.toArray(children);
109
140
  const subs = all.filter(isSub);
@@ -313,8 +344,11 @@ const NavMenuItem = forwardRef(function NavMenuItem(props, ref) {
313
344
  // Top-level rows of a bar. Rows inside a flyout panel keep the vertical look.
314
345
  const bar = orientation === 'horizontal' && !inFlyout;
315
346
  const itemClass = cx('tui-nav-menu__item', bar && 'is-bar', className);
316
- const iconNode = icon ? (_jsx("span", { className: "tui-nav-menu__icon", "aria-hidden": "true", children: _jsx(Icon, { name: icon }) })) : null;
317
- const content = (inner) => (_jsxs(_Fragment, { children: [iconNode, _jsx("span", { className: "tui-nav-menu__label", id: flyout ? labelId : undefined, children: inner })] }));
347
+ const leading = prefix != null ? (_jsx("span", { className: "tui-nav-menu__prefix", ref: prefixRef, children: prefix })) : icon ? (_jsx("span", { className: "tui-nav-menu__icon", "aria-hidden": "true", children: _jsx(Icon, { name: icon }) })) : null;
348
+ // The ' ' text nodes keep the slots as separate words in the accessible
349
+ // name ("Inbox 3 unread", not "Inbox3 unread"). A flex container doesn't
350
+ // render whitespace-only text, so layout is unaffected.
351
+ const content = (inner) => (_jsxs(_Fragment, { children: [leading, leading && prefix != null && ' ', _jsx("span", { className: "tui-nav-menu__label", id: flyout ? labelId : undefined, children: inner }), suffix != null && (_jsxs(_Fragment, { children: [' ', _jsx("span", { className: "tui-nav-menu__suffix", ref: suffixRef, children: suffix })] }))] }));
318
352
  const chevron = (_jsx("span", { className: "tui-nav-menu__chevron", "aria-hidden": "true", children: _jsx(Icon, { name: "system/chevron-down-small" }) }));
319
353
  let row;
320
354
  // A flyout parent with no destination of its own: the row IS the toggle.
@@ -73,6 +73,28 @@ type NavMenuItemBaseProps = {
73
73
  current?: NavMenuCurrent;
74
74
  /** Leading icon, from the registry. Sized by `--tui-nav-menu-icon-size`. */
75
75
  icon?: IconName;
76
+ /**
77
+ * Custom leading content in the icon's place (an avatar, a glyph not in the
78
+ * registry). Replaces `icon` when both are set. Unlike `icon`, it isn't
79
+ * hidden from assistive tech: mark decorative content `aria-hidden`
80
+ * yourself, or its text joins the name. A shape drawn with CSS `background`
81
+ * disappears in forced colors; give it a border if it carries meaning.
82
+ */
83
+ prefix?: ReactNode;
84
+ /**
85
+ * Trailing content pushed to the row's inline end: a badge, a count. It sits
86
+ * inside the row, so its text joins the accessible name: give a bare number
87
+ * context, so `3 <span class="tui-visually-hidden">unread</span>` reads
88
+ * "Inbox 3 unread" (put the space outside the hidden span). Must not be
89
+ * interactive (no buttons or links): a control nested in a link is invalid.
90
+ *
91
+ * Both slots reach the name only through the row's content, so don't pass
92
+ * `aria-label`/`aria-labelledby` to an item with a slot: either replaces
93
+ * the whole name. On a row that is itself the flyout's disclosure button,
94
+ * the suffix is part of the button's name; on a split row it belongs to the
95
+ * link (the destination), and the toggle stays "Reports submenu".
96
+ */
97
+ suffix?: ReactNode;
76
98
  /**
77
99
  * Inert item: rendered as a disabled link (`role="link"`, `aria-disabled`,
78
100
  * no href), out of the tab order. For destinations that exist but aren't
@@ -91,12 +113,12 @@ type NavMenuItemBaseProps = {
91
113
  children: ReactNode;
92
114
  };
93
115
  /** Link item: renders an `<a>`. */
94
- export type NavMenuItemAsLink = NavMenuItemBaseProps & Omit<ComponentPropsWithoutRef<'a'>, 'children' | 'aria-current'> & {
116
+ export type NavMenuItemAsLink = NavMenuItemBaseProps & Omit<ComponentPropsWithoutRef<'a'>, 'children' | 'aria-current' | 'prefix'> & {
95
117
  href: string;
96
118
  asChild?: never;
97
119
  };
98
120
  /** Action item: renders a `<button type="button">`. */
99
- export type NavMenuItemAsButton = NavMenuItemBaseProps & Omit<ComponentPropsWithoutRef<'button'>, 'children' | 'aria-current' | 'disabled'> & {
121
+ export type NavMenuItemAsButton = NavMenuItemBaseProps & Omit<ComponentPropsWithoutRef<'button'>, 'children' | 'aria-current' | 'prefix' | 'disabled'> & {
100
122
  href?: never;
101
123
  asChild?: never;
102
124
  };
@@ -105,7 +127,7 @@ export type NavMenuItemAsButton = NavMenuItemBaseProps & Omit<ComponentPropsWith
105
127
  * `<Link>`, say). It receives the item's class, `aria-current`, the icon and
106
128
  * a label wrapper. It must render a real `<a href>`.
107
129
  */
108
- export type NavMenuItemAsChild = NavMenuItemBaseProps & Omit<ComponentPropsWithoutRef<'a'>, 'children' | 'aria-current' | 'href'> & {
130
+ export type NavMenuItemAsChild = NavMenuItemBaseProps & Omit<ComponentPropsWithoutRef<'a'>, 'children' | 'aria-current' | 'prefix' | 'href'> & {
109
131
  asChild: true;
110
132
  href?: never;
111
133
  };
package/figma-tokens.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.2.26",
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tangible/ui",
3
- "version": "0.2.26",
3
+ "version": "0.2.28",
4
4
  "description": "Tangible Design System",
5
5
  "type": "module",
6
6
  "main": "./components/index.js",
@@ -399,7 +399,7 @@ synthesise a second toggle. For a second control that toggles the same item (an
399
399
  ## Component names, and what isn't there
400
400
 
401
401
  46 components. If you are hunting for a name: **Alert → `Notice`**, **Menu → `Dropdown`**,
402
- **Table → `DataTable`**, **Pagination → `Pager`**, **Drawer → `Sidebar`** (drawer mode), **Nav menu / sidebar links / top nav bar → `NavMenu`** (inside `Sidebar.Nav` or your `<nav>`; `asChild` for router links, `current` for `aria-current`; `orientation="horizontal"` for a bar whose items open flyouts, `flyoutTrigger="hover"` to also open on mouse hover — don't hand-roll menus with `role="menu"`),
402
+ **Table → `DataTable`**, **Pagination → `Pager`**, **Drawer → `Sidebar`** (drawer mode), **Nav menu / sidebar links / top nav bar → `NavMenu`** (inside `Sidebar.Nav` or your `<nav>`; `asChild` for router links, `current` for `aria-current`; `orientation="horizontal"` for a bar whose items open flyouts, `flyoutTrigger="hover"` to also open on mouse hover; `suffix` for a row badge/count — a `Chip`, with visually hidden context for bare numbers, never interactive; row states restyle via `--tui-nav-menu-item-*`, per level via `--tui-nav-menu-{sub,bar,flyout}-item-*` — set `bar-item-*` for a dark header so flyout rows keep the panel's colours; don't hand-roll menus with `role="menu"`),
403
403
  **Badge → `Chip`** or `ContentIndicator`, **Spinner → `Progress`** or `Button loading`,
404
404
  **ConfirmDialog / AlertDialog → `Modal role="alertdialog"`** (needs both `aria-labelledby` and
405
405
  `aria-describedby`; the backdrop stops dismissing by default, Escape still cancels; put the actions in