@tangible/ui 0.2.49 → 0.2.51

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,5 @@
1
1
  import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
2
- import React, { forwardRef, useEffect, useId, useMemo, useRef, cloneElement, isValidElement, Children, } from 'react';
2
+ import React, { forwardRef, useCallback, useEffect, useId, useLayoutEffect, useState, useMemo, useRef, cloneElement, isValidElement, Children, } from 'react';
3
3
  import { cx } from '../../utils/cx.js';
4
4
  import { isDev } from '../../utils/is-dev.js';
5
5
  import { FieldContext, useFieldContext, defaultFieldLabels } from './FieldContext.js';
@@ -13,6 +13,12 @@ export const Field = forwardRef(function Field({ error = false, required = false
13
13
  // Reset on each render — Label will set it back to true during its render
14
14
  labelRendered.current = false;
15
15
  const labels = useMemo(() => ({ ...defaultFieldLabels, ...labelsProp }), [labelsProp]);
16
+ // Counts, not booleans: a remount overlapping an unmount must not clear it
17
+ const [shown, setShown] = useState({ helper: 0, error: 0 });
18
+ const registerDescription = useCallback((part) => {
19
+ setShown((s) => ({ ...s, [part]: s[part] + 1 }));
20
+ return () => setShown((s) => ({ ...s, [part]: s[part] - 1 }));
21
+ }, []);
16
22
  const contextValue = useMemo(() => ({
17
23
  controlId,
18
24
  labelId,
@@ -22,8 +28,11 @@ export const Field = forwardRef(function Field({ error = false, required = false
22
28
  required,
23
29
  disabled,
24
30
  labelRendered,
31
+ helperShown: shown.helper > 0,
32
+ errorShown: shown.error > 0,
33
+ registerDescription,
25
34
  labels,
26
- }), [controlId, labelId, helperTextId, errorId, error, required, disabled, labels]);
35
+ }), [controlId, labelId, helperTextId, errorId, error, required, disabled, labels, shown, registerDescription]);
27
36
  const classes = cx('tui-field', error && 'is-error', disabled && 'is-disabled', inline && 'is-layout-inline', className);
28
37
  return (_jsx(FieldContext.Provider, { value: contextValue, children: _jsx("div", { ref: ref, className: classes, children: children }) }));
29
38
  });
@@ -46,23 +55,26 @@ FieldLabel.displayName = 'Field.Label';
46
55
  // Field.Control
47
56
  // =============================================================================
48
57
  function FieldControl({ children }) {
49
- const { controlId, labelId, helperTextId, errorId, hasError, required, disabled, labelRendered, } = useFieldContext();
58
+ const { controlId, labelId, helperTextId, errorId, hasError, required, disabled, labelRendered, helperShown, errorShown, } = useFieldContext();
50
59
  const child = Children.only(children);
51
60
  if (!isValidElement(child)) {
52
61
  throw new Error('Field.Control expects a single React element as its child');
53
62
  }
54
63
  const childProps = child.props;
55
- // Build aria-describedby, merging with any existing value from child
56
- // Per ARIA spec, referencing non-existent IDs is valid (AT ignores them)
64
+ // Build aria-describedby, merging with any existing value from child.
65
+ // Only ids that are rendered: a missing one is ignored by AT, but it's
66
+ // still an invalid reference (axe aria-valid-attr-value).
57
67
  const describedByParts = [];
58
68
  if (typeof childProps['aria-describedby'] === 'string') {
59
69
  describedByParts.push(childProps['aria-describedby']);
60
70
  }
61
- describedByParts.push(helperTextId);
62
- if (hasError) {
71
+ if (helperShown) {
72
+ describedByParts.push(helperTextId);
73
+ }
74
+ if (hasError && errorShown) {
63
75
  describedByParts.push(errorId);
64
76
  }
65
- const describedBy = describedByParts.join(' ');
77
+ const describedBy = describedByParts.join(' ') || undefined;
66
78
  // Only inject aria-labelledby when Field.Label is present in the tree.
67
79
  // Without a rendered label, the ID points at nothing — and per AccName 1.2
68
80
  // step 2B, aria-labelledby overrides ALL other name sources (including
@@ -96,8 +108,10 @@ FieldControl.displayName = 'Field.Control';
96
108
  // Field.HelperText
97
109
  // =============================================================================
98
110
  function FieldHelperText({ className, children, ...rest }) {
99
- const { helperTextId } = useFieldContext();
100
- if (!children)
111
+ const { helperTextId, registerDescription } = useFieldContext();
112
+ const shown = !!children;
113
+ useLayoutEffect(() => (shown ? registerDescription('helper') : undefined), [shown, registerDescription]);
114
+ if (!shown)
101
115
  return null;
102
116
  const classes = cx('tui-field__helper', className);
103
117
  return (_jsx("div", { id: helperTextId, className: classes, ...rest, children: children }));
@@ -107,9 +121,11 @@ FieldHelperText.displayName = 'Field.HelperText';
107
121
  // Field.Error
108
122
  // =============================================================================
109
123
  function FieldError({ className, children, ...rest }) {
110
- const { errorId, hasError } = useFieldContext();
124
+ const { errorId, hasError, registerDescription } = useFieldContext();
111
125
  // Only render if there's an error and content
112
- if (!hasError || !children)
126
+ const shown = hasError && !!children;
127
+ useLayoutEffect(() => (shown ? registerDescription('error') : undefined), [shown, registerDescription]);
128
+ if (!shown)
113
129
  return null;
114
130
  const classes = cx('tui-field__error', className);
115
131
  // role="alert" implies aria-live="assertive" - no need to add aria-live
@@ -27,6 +27,14 @@ export type FieldContextValue = {
27
27
  labelRendered: {
28
28
  current: boolean;
29
29
  };
30
+ /** Whether Field.HelperText / Field.Error are rendered, so Field.Control
31
+ * only points aria-describedby at elements that exist. They follow the
32
+ * control in JSX, so they register in a layout effect (a re-render before
33
+ * paint) rather than a render-time flag like `labelRendered`. */
34
+ helperShown: boolean;
35
+ errorShown: boolean;
36
+ /** Registers a rendered HelperText or Error; returns the unregister. */
37
+ registerDescription: (part: 'helper' | 'error') => () => void;
30
38
  /** Resolved i18n labels for Field sub-components */
31
39
  labels: Required<FieldLabels>;
32
40
  };
@@ -18,6 +18,9 @@ function SidebarRoot(props) {
18
18
  const open = props.drawer ? (props.open ?? false) : false;
19
19
  const onClose = props.drawer ? props.onClose : undefined;
20
20
  const initialFocusSelector = props.drawer ? props.initialFocusSelector : undefined;
21
+ // Read at close time, so a ref filled after open (or a getter) still counts
22
+ const restoreFocusToRef = useRef(props.drawer ? props.restoreFocusTo : undefined);
23
+ restoreFocusToRef.current = props.drawer ? props.restoreFocusTo : undefined;
21
24
  // Collapse is static-mode only, narrowed the same way. A drawer already
22
25
  // shows and hides wholesale; a rail inside one would sit beside nothing.
23
26
  const collapsible = !drawer && props.collapsible === true;
@@ -91,13 +94,31 @@ function SidebarRoot(props) {
91
94
  // animation-end handler — these are the actual unmount points, so focus
92
95
  // must be restored here (not in a useEffect, which may not fire before
93
96
  // the synchronous re-render unmounts the component).
97
+ //
98
+ // `restoreFocusTo` first, then whatever had focus at open. Each must still be
99
+ // in the document and actually take focus: focus() on a removed or disabled
100
+ // element is a silent no-op. <body> is skipped, which is what Safari
101
+ // captures when the opening button was clicked (it doesn't focus buttons).
94
102
  const dismiss = useCallback(() => {
95
103
  setVisible(false);
96
- const el = restoreRef.current;
97
- if (el && typeof el.focus === 'function' && document.contains(el)) {
104
+ const target = restoreFocusToRef.current;
105
+ const explicit = typeof target === 'function' ? target() : target?.current ?? null;
106
+ const captured = restoreRef.current;
107
+ restoreRef.current = null;
108
+ for (const el of [explicit, captured]) {
109
+ if (!el || !el.isConnected || el === document.body)
110
+ continue;
98
111
  el.focus();
112
+ if (document.activeElement === el)
113
+ return;
114
+ }
115
+ // Only when a real target was lost, as Modal does: Safari's captured
116
+ // <body> on an ordinary close stays silent.
117
+ if (isDev() && (explicit || (captured && captured !== document.body))) {
118
+ console.warn('[TUI Sidebar] Focus had nowhere to return when the drawer closed: the element focused at open ' +
119
+ 'is gone or can\'t take focus, so focus fell to <body> (WCAG 2.4.3). Pass `restoreFocusTo` ' +
120
+ 'with an element that will still be there, such as the button that opens the drawer.');
99
121
  }
100
- restoreRef.current = null;
101
122
  }, []);
102
123
  // When closing, check if animation will actually play (reduced motion).
103
124
  // If not, dismiss immediately to avoid getting stuck.
@@ -1,4 +1,4 @@
1
- import type { ReactNode } from 'react';
1
+ import type { ReactNode, RefObject } from 'react';
2
2
  import type { IconButtonAsButton } from '../IconButton/types';
3
3
  type SidebarBaseProps = {
4
4
  /**
@@ -88,6 +88,14 @@ type SidebarDrawerProps = SidebarBaseProps & {
88
88
  * Header's close button. That's what this prop is for.
89
89
  */
90
90
  initialFocusSelector?: string;
91
+ /**
92
+ * Where focus goes when the drawer closes, read once it has finished closing: usually the
93
+ * button that opens it. Defaults to whatever had focus when it opened, which
94
+ * in Safari is often nothing: a clicked button doesn't take focus there, so
95
+ * focus would fall to <body>. Same prop as `Modal`'s. A function suits a
96
+ * target that only exists by close time.
97
+ */
98
+ restoreFocusTo?: RefObject<HTMLElement | null> | (() => HTMLElement | null);
91
99
  };
92
100
  export type SidebarProps = SidebarStaticProps | SidebarDrawerProps;
93
101
  export type SidebarHeaderProps = {
package/figma-tokens.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.2.49",
2
+ "version": "0.2.51",
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/icons/icons.svg CHANGED
@@ -1,4 +1,4 @@
1
- <svg xmlns="http://www.w3.org/2000/svg" data-tui-icon-sprite="all" data-tui-icon-sprite-version="0.2.49" aria-hidden="true" focusable="false" style="position:absolute;width:0;height:0;overflow:hidden">
1
+ <svg xmlns="http://www.w3.org/2000/svg" data-tui-icon-sprite="all" data-tui-icon-sprite-version="0.2.51" aria-hidden="true" focusable="false" style="position:absolute;width:0;height:0;overflow:hidden">
2
2
  <symbol id="tui-cred-badge-check" viewBox="0 0 24 24"><path fill-rule="evenodd" d="M10.893 4.558a1.377 1.377 0 0 1 2.214 0c.381.515 1.068.7 1.656.444a1.377 1.377 0 0 1 1.917 1.106c.072.637.575 1.14 1.212 1.213a1.377 1.377 0 0 1 1.106 1.916 1.38 1.38 0 0 0 .444 1.656c.744.55.744 1.663 0 2.214a1.38 1.38 0 0 0-.444 1.656 1.377 1.377 0 0 1-1.106 1.917 1.38 1.38 0 0 0-1.212 1.212 1.377 1.377 0 0 1-1.917 1.106 1.38 1.38 0 0 0-1.656.444 1.377 1.377 0 0 1-2.214 0 1.38 1.38 0 0 0-1.656-.444 1.377 1.377 0 0 1-1.916-1.106 1.38 1.38 0 0 0-1.213-1.212 1.377 1.377 0 0 1-1.106-1.917 1.38 1.38 0 0 0-.444-1.656 1.377 1.377 0 0 1 0-2.214c.515-.381.7-1.068.444-1.656a1.377 1.377 0 0 1 1.106-1.916 1.38 1.38 0 0 0 1.213-1.213 1.377 1.377 0 0 1 1.916-1.106 1.38 1.38 0 0 0 1.656-.444m5.193 5.91a.75.75 0 0 0-1.172-.936l-3.531 4.414-1.914-1.532a.75.75 0 1 0-.937 1.172l3.085 2.468z" clip-rule="evenodd"/></symbol>
3
3
  <symbol id="tui-cred-badge-ribbon" viewBox="0 0 24 24"><path d="M11.032 3.488a1.205 1.205 0 0 1 1.936 0c.334.451.935.612 1.45.388a1.205 1.205 0 0 1 1.677.969c.063.557.503.997 1.06 1.06.805.092 1.291.935.969 1.678a1.204 1.204 0 0 0 .388 1.449c.65.481.65 1.455 0 1.936a1.205 1.205 0 0 0-.388 1.45 1.205 1.205 0 0 1-.969 1.677 1.204 1.204 0 0 0-1.06 1.06 1.205 1.205 0 0 1-1.678.969 1.205 1.205 0 0 0-1.449.388c-.481.65-1.455.65-1.936 0a1.204 1.204 0 0 0-1.45-.388 1.205 1.205 0 0 1-1.677-.969 1.205 1.205 0 0 0-1.06-1.06 1.205 1.205 0 0 1-.969-1.678 1.205 1.205 0 0 0-.388-1.449 1.205 1.205 0 0 1 0-1.936c.451-.334.612-.935.388-1.45a1.205 1.205 0 0 1 .969-1.677 1.205 1.205 0 0 0 1.06-1.06 1.205 1.205 0 0 1 1.678-.969 1.205 1.205 0 0 0 1.449-.388M8 17.482c.442.175.953.185 1.437-.025a.88.88 0 0 1 1.054.282 1.877 1.877 0 0 0 3.018 0 .88.88 0 0 1 1.054-.282c.484.21.995.2 1.437.025V21l-4-2.286L8 21z"/></symbol>
4
4
  <symbol id="tui-cred-badge" viewBox="0 0 24 24"><path d="M13.107 4.558a1.377 1.377 0 0 0-2.214 0c-.381.515-1.068.7-1.656.444a1.377 1.377 0 0 0-1.916 1.106 1.38 1.38 0 0 1-1.213 1.213 1.377 1.377 0 0 0-1.106 1.916 1.38 1.38 0 0 1-.444 1.656 1.377 1.377 0 0 0 0 2.214c.515.381.7 1.068.444 1.656a1.377 1.377 0 0 0 1.106 1.917c.637.072 1.14.575 1.213 1.212a1.377 1.377 0 0 0 1.916 1.106 1.38 1.38 0 0 1 1.656.444c.55.744 1.663.744 2.214 0a1.38 1.38 0 0 1 1.656-.444 1.377 1.377 0 0 0 1.917-1.106 1.38 1.38 0 0 1 1.212-1.212 1.377 1.377 0 0 0 1.106-1.917 1.38 1.38 0 0 1 .444-1.656 1.377 1.377 0 0 0 0-2.214 1.38 1.38 0 0 1-.444-1.656 1.377 1.377 0 0 0-1.106-1.916 1.38 1.38 0 0 1-1.212-1.213 1.377 1.377 0 0 0-1.917-1.106 1.38 1.38 0 0 1-1.656-.444"/></symbol>
@@ -1 +1 @@
1
- export const SPRITE_VERSION = '0.2.49';
1
+ export const SPRITE_VERSION = '0.2.51';
package/icons/sprite.php CHANGED
@@ -21,7 +21,7 @@
21
21
  */
22
22
 
23
23
  // Replaced with the package version at build time.
24
- $GLOBALS['tangible_ui_icon_sprite_copies'][__DIR__] = '0.2.49';
24
+ $GLOBALS['tangible_ui_icon_sprite_copies'][__DIR__] = '0.2.51';
25
25
 
26
26
  if (!function_exists('tangible_ui_print_icon_sprite')) {
27
27
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tangible/ui",
3
- "version": "0.2.49",
3
+ "version": "0.2.51",
4
4
  "description": "React design system for Tangible's products: standalone React apps and WordPress plugins. Components, CSS custom property tokens, cascade layers.",
5
5
  "type": "module",
6
6
  "main": "./components/index.js",
@@ -721,7 +721,7 @@ synthesise a second toggle. For a second control that toggles the same item (an
721
721
  ## Component names, and what isn't there
722
722
 
723
723
  46 components. If you are hunting for a name: **Alert → `Notice`**, **Menu → `Dropdown`**,
724
- **Table → `DataTable`**, **Pagination → `Pager`**, **Drawer / side sheet / off-canvas panel → `Sidebar`** (`<Sidebar drawer open onClose position="left">`: a modal dialog with a focus trap, Escape and backdrop dismissal; `initialFocusSelector` or `data-autofocus` picks where focus lands on open; don't dock a `Modal` to an edge), **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; Items can sit straight in `NavMenu` (they get an implicit list) or in a `NavMenu.Group` when the cluster needs a name, `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; the leading icon's colour via `--tui-nav-menu-{level-}item-icon-fg` / `-icon-fg-hover` / `-icon-fg-current` (default: the row's text colour), its size and spacing via `--tui-nav-menu-icon-size` and `--tui-nav-menu-icon-gap`; a `NavMenu.Sub` under a row with an icon lines its labels up with the parent's label (set `--tui-nav-menu-sub-indent` to place it yourself); don't hand-roll menus with `role="menu"`),
724
+ **Table → `DataTable`**, **Pagination → `Pager`**, **Drawer / side sheet / off-canvas panel → `Sidebar`** (`<Sidebar drawer open onClose position="left">`: a modal dialog with a focus trap, Escape and backdrop dismissal; `initialFocusSelector` or `data-autofocus` picks where focus lands on open; pass `restoreFocusTo` the button that opens it, since Safari doesn't focus a clicked button and focus would otherwise fall to the page on close; don't dock a `Modal` to an edge), **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; Items can sit straight in `NavMenu` (they get an implicit list) or in a `NavMenu.Group` when the cluster needs a name, `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; the leading icon's colour via `--tui-nav-menu-{level-}item-icon-fg` / `-icon-fg-hover` / `-icon-fg-current` (default: the row's text colour), its size and spacing via `--tui-nav-menu-icon-size` and `--tui-nav-menu-icon-gap`; a `NavMenu.Sub` under a row with an icon lines its labels up with the parent's label (set `--tui-nav-menu-sub-indent` to place it yourself); don't hand-roll menus with `role="menu"`),
725
725
  **Badge → `Chip`** or `ContentIndicator`, **Spinner → `Progress`** or `Button loading`,
726
726
  **ConfirmDialog / AlertDialog → `Modal role="alertdialog"`** (needs both `aria-labelledby` and
727
727
  `aria-describedby`; the backdrop stops dismissing by default, Escape still cancels; put the actions in
package/tui-manifest.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
- "version": "0.2.49",
3
- "generated": "2026-11-23T01:50:38.472Z",
2
+ "version": "0.2.51",
3
+ "generated": "2026-11-23T04:11:26.423Z",
4
4
  "components": {
5
5
  "Accordion": {
6
6
  "props": {
@@ -3876,6 +3876,11 @@
3876
3876
  "type": "string",
3877
3877
  "required": false,
3878
3878
  "description": "CSS selector, matched inside the drawer when it opens, for the element\nthat takes focus: say, the current lesson in a long outline, so keyboard\nand screen reader users start where they are. Escape still closes and the\nclose button is a Shift+Tab away. Don't point it at a destructive action.\n\nThe target must be able to take focus (a control, a link, or anything\nwith `tabindex`, -1 included) and be rendered at open. Otherwise a dev\nwarning fires and focus goes to the default: an element marked\n`data-autofocus`, else the first control. It's read once, when the drawer\nopens; changing it while open doesn't move focus.\n\nSame prop as `Modal`'s, but the default differs: Modal skips its own close\nbutton, while a drawer has none, so its first control is often your\nHeader's close button. That's what this prop is for."
3879
+ },
3880
+ "restoreFocusTo": {
3881
+ "type": "RefObject<HTMLElement> | (() => HTMLElement)",
3882
+ "required": false,
3883
+ "description": "Where focus goes when the drawer closes, read once it has finished closing: usually the\nbutton that opens it. Defaults to whatever had focus when it opened, which\nin Safari is often nothing: a clicked button doesn't take focus there, so\nfocus would fall to <body>. Same prop as `Modal`'s. A function suits a\ntarget that only exists by close time."
3879
3884
  }
3880
3885
  },
3881
3886
  "cssTokens": [