@tangible/ui 0.2.24 → 0.2.26

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,10 +1,17 @@
1
1
  import { type ReactNode } from 'react';
2
- import type { NavMenuItemProps, NavMenuSubProps } from './types';
2
+ import type { NavMenuFlyoutTrigger, NavMenuItemProps, NavMenuLabels, NavMenuOrientation, NavMenuSubProps } from './types';
3
3
  /**
4
4
  * Navigation menu rows: groups with headings, the current item, nested items.
5
- * Not a landmark: put it inside `Sidebar.Nav` or your own labelled `<nav>`.
5
+ * Vertical by default (sidebars). `orientation="horizontal"` makes a bar whose
6
+ * items with a `NavMenu.Sub` open as flyouts, following the APG disclosure
7
+ * navigation pattern: buttons with aria-expanded, never role="menu", never
8
+ * hover-to-open. Not a landmark: put it inside `Sidebar.Nav` or a labelled
9
+ * `<nav>`.
6
10
  */
7
11
  declare const NavMenuRoot: import("react").ForwardRefExoticComponent<Omit<import("react").DetailedHTMLProps<import("react").HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "ref"> & {
12
+ orientation?: NavMenuOrientation;
13
+ flyoutTrigger?: NavMenuFlyoutTrigger;
14
+ labels?: NavMenuLabels;
8
15
  children: ReactNode;
9
16
  } & import("react").RefAttributes<HTMLDivElement>>;
10
17
  declare const NavMenuGroup: import("react").ForwardRefExoticComponent<Omit<Omit<import("react").DetailedHTMLProps<import("react").HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "ref">, "children"> & {
@@ -1,38 +1,85 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
2
- import { Children, cloneElement, createContext, forwardRef, isValidElement, useContext, useEffect, useId, useRef, } from 'react';
2
+ import { Children, cloneElement, createContext, forwardRef, isValidElement, useCallback, useContext, useEffect, useId, useMemo, useRef, useState, } from 'react';
3
+ import { useFloating, autoUpdate, offset, flip, shift } from '@floating-ui/react';
3
4
  import { cx } from '../../utils/cx.js';
4
5
  import { isDev } from '../../utils/is-dev.js';
5
6
  import { mergeProps } from '../../utils/merge-props.js';
6
7
  import { composeRefs, getElementRef } from '../../utils/compose-refs.js';
7
8
  import { Icon } from '../Icon/index.js';
9
+ import { createHoverWarmth, isWarm, markClosed, markOpen, sampleHoverIntent } from '../../utils/hover-intent.js';
10
+ import { defaultNavMenuLabels } from './types.js';
11
+ // Hover intent and warmth come from utils/hover-intent (shared with tooltips).
12
+ // A panel opens when the pointer settles, not after a fixed delay; once the
13
+ // bar is warm (a panel open, or one closed within PRIMED_WINDOW) the next
14
+ // opens at once.
15
+ const PRIMED_WINDOW = 400;
16
+ // Grace period for crossing into the panel.
17
+ const HOVER_CLOSE_DELAY = 300;
8
18
  // Items render <li>, so they're only valid inside a Group or Sub list. The
9
19
  // context exists to warn when one isn't.
10
20
  const ListContext = createContext(false);
21
+ const RootContext = createContext({
22
+ orientation: 'vertical',
23
+ flyoutTrigger: 'click',
24
+ labels: defaultNavMenuLabels,
25
+ openId: null,
26
+ setOpenId: () => { },
27
+ rootRef: { current: null },
28
+ warmth: createHoverWarmth(),
29
+ });
30
+ // True inside a flyout panel: everything there lays out vertically, and an
31
+ // item with its own Sub nests inline instead of opening another flyout.
32
+ const FlyoutContext = createContext(false);
11
33
  // =============================================================================
12
34
  // NavMenu (root)
13
35
  // =============================================================================
14
36
  /**
15
37
  * Navigation menu rows: groups with headings, the current item, nested items.
16
- * Not a landmark: put it inside `Sidebar.Nav` or your own labelled `<nav>`.
38
+ * Vertical by default (sidebars). `orientation="horizontal"` makes a bar whose
39
+ * items with a `NavMenu.Sub` open as flyouts, following the APG disclosure
40
+ * navigation pattern: buttons with aria-expanded, never role="menu", never
41
+ * hover-to-open. Not a landmark: put it inside `Sidebar.Nav` or a labelled
42
+ * `<nav>`.
17
43
  */
18
- const NavMenuRoot = forwardRef(function NavMenu({ className, children, ...rest }, ref) {
19
- return (_jsx("div", { ref: ref, ...rest, className: cx('tui-nav-menu', className), children: children }));
44
+ const NavMenuRoot = forwardRef(function NavMenu({ orientation = 'vertical', flyoutTrigger = 'click', labels, className, children, ...rest }, ref) {
45
+ const [openId, setOpenId] = useState(null);
46
+ const rootRef = useRef(null);
47
+ // One warmth group per bar, created once (useState's lazy init).
48
+ const [warmth] = useState(createHoverWarmth);
49
+ const value = useMemo(() => ({ orientation, flyoutTrigger, labels: { ...defaultNavMenuLabels, ...labels }, openId, setOpenId, rootRef, warmth }), [orientation, flyoutTrigger, labels, openId, warmth]);
50
+ return (_jsx("div", { ref: composeRefs(ref, rootRef), ...rest, "data-orientation": orientation, className: cx('tui-nav-menu', orientation === 'horizontal' && 'is-horizontal', className), children: _jsx(RootContext.Provider, { value: value, children: children }) }));
20
51
  });
21
52
  // =============================================================================
22
53
  // NavMenu.Group
23
54
  // =============================================================================
24
55
  const NavMenuGroup = forwardRef(function NavMenuGroup({ label, labelAs: Label = 'h2', className, children, ...rest }, ref) {
25
56
  const labelId = useId();
57
+ const { orientation } = useContext(RootContext);
58
+ const inFlyout = useContext(FlyoutContext);
26
59
  const hasLabel = label !== undefined && label !== null && label !== false;
27
- return (_jsxs("div", { ref: ref, ...rest, className: cx('tui-nav-menu__group', className), children: [hasLabel && (
28
- // The class is the override hook for the overline style; the
29
- // stylesheet also applies it, so static markup stays styled.
30
- _jsx(Label, { id: labelId, className: "tui-nav-menu__group-label tui-text-style-overline", children: label })), _jsx("ul", { className: "tui-nav-menu__list", "aria-labelledby": hasLabel ? labelId : undefined, children: _jsx(ListContext.Provider, { value: true, children: children }) })] }));
60
+ // In a bar, a group is an inline cluster: its heading stays (it still names
61
+ // the list for AT) but is visually hidden. Inside a flyout it shows.
62
+ const headingHidden = orientation === 'horizontal' && !inFlyout;
63
+ return (_jsxs("div", { ref: ref, ...rest, className: cx('tui-nav-menu__group', className), children: [hasLabel &&
64
+ (headingHidden ? (
65
+ // In a bar the label only names the list. A visually hidden heading
66
+ // would show up in heading navigation with nothing under it, so it
67
+ // renders as a plain span instead.
68
+ _jsx("span", { id: labelId, className: "tui-visually-hidden", children: label })) : (
69
+ // The class is the override hook for the overline style; the
70
+ // stylesheet also applies it, so static markup stays styled.
71
+ _jsx(Label, { id: labelId, className: "tui-nav-menu__group-label tui-text-style-overline", children: label }))), _jsx("ul", { className: "tui-nav-menu__list", "aria-labelledby": hasLabel ? labelId : undefined, children: _jsx(ListContext.Provider, { value: true, children: children }) })] }));
31
72
  });
32
73
  // =============================================================================
33
74
  // NavMenu.Sub
34
75
  // =============================================================================
76
+ const isGroup = (node) => isValidElement(node) && node.type === NavMenuGroup;
35
77
  function NavMenuSub({ className, children, ...rest }) {
78
+ // Groups bring their own headed lists, so a Sub of groups is a plain
79
+ // container; a Sub of items is the list itself.
80
+ if (Children.toArray(children).some(isGroup)) {
81
+ return (_jsx("div", { ...rest, className: cx('tui-nav-menu__sub', 'has-groups', className), children: children }));
82
+ }
36
83
  return (_jsx("ul", { ...rest, className: cx('tui-nav-menu__sub', className), children: _jsx(ListContext.Provider, { value: true, children: children }) }));
37
84
  }
38
85
  NavMenuSub.displayName = 'NavMenu.Sub';
@@ -48,6 +95,8 @@ const isSub = (node) => isValidElement(node) && node.type === NavMenuSub;
48
95
  const NavMenuItem = forwardRef(function NavMenuItem(props, ref) {
49
96
  const { current, icon, disabled, asChild, className, children, ...rest } = props;
50
97
  const inList = useContext(ListContext);
98
+ const { orientation, flyoutTrigger, labels, openId, setOpenId, rootRef, warmth } = useContext(RootContext);
99
+ const inFlyout = useContext(FlyoutContext);
51
100
  const warned = useRef(false);
52
101
  useEffect(() => {
53
102
  if (isDev() && !inList && !warned.current) {
@@ -59,11 +108,217 @@ const NavMenuItem = forwardRef(function NavMenuItem(props, ref) {
59
108
  const all = Children.toArray(children);
60
109
  const subs = all.filter(isSub);
61
110
  const label = all.filter((c) => !isSub(c));
111
+ // -- Flyout (horizontal bar, item with a Sub) -------------------------------
112
+ const id = useId();
113
+ const panelId = `${id}-panel`;
114
+ const labelId = `${id}-label`;
115
+ const toggleTextId = `${id}-toggle`;
116
+ const flyout = orientation === 'horizontal' && !inFlyout && subs.length > 0 && !disabled;
117
+ const open = flyout && openId === id;
118
+ // Close with an updater, never the rendered openId: a timer scheduled while
119
+ // this item was open runs later, when another item may own openId, and a
120
+ // stale `openId === id` check closed that other item (hover A, then B
121
+ // quickly: B opened, then A's close timer shut it).
122
+ const setOpen = useCallback((next) => {
123
+ if (next)
124
+ setOpenId(id);
125
+ else
126
+ setOpenId((current) => (current === id ? null : current));
127
+ }, [id, setOpenId]);
128
+ // -- Hover (flyoutTrigger="hover") ------------------------------------------
129
+ // Opt-in, mouse only, and built to WCAG 1.4.13 (content on hover):
130
+ // - Hover intent: a short open delay, so sweeping across the bar doesn't
131
+ // flash panels.
132
+ // - Hoverable: a close delay, so the pointer can cross the gap into the
133
+ // panel without it closing.
134
+ // - Dismissible: Escape closes it and it stays closed until the pointer
135
+ // leaves, so it never re-opens under a pointer that's still there.
136
+ // Clicking a hover-opened toggle pins it open rather than yanking it shut,
137
+ // and keyboard behaviour is unchanged.
138
+ const hover = flyout && flyoutTrigger === 'hover';
139
+ const hoverTimer = useRef(null);
140
+ const openedByHover = useRef(false);
141
+ const dismissedWhileHovered = useRef(false);
142
+ const pointerInside = useRef(false);
143
+ // Another panel opening means the pointer (or focus) has moved on, so a
144
+ // pending "dismissed while hovered" no longer applies here.
145
+ // Its pending hover timer is void too: cancelled here so it can't fire into
146
+ // the other item's panel.
147
+ useEffect(() => {
148
+ if (openId !== null && openId !== id) {
149
+ if (hoverTimer.current)
150
+ clearTimeout(hoverTimer.current);
151
+ hoverTimer.current = null;
152
+ openedByHover.current = false;
153
+ dismissedWhileHovered.current = false;
154
+ pointerInside.current = false;
155
+ }
156
+ }, [openId, id]);
157
+ const clearHoverTimer = () => {
158
+ if (hoverTimer.current)
159
+ clearTimeout(hoverTimer.current);
160
+ hoverTimer.current = null;
161
+ };
162
+ useEffect(() => clearHoverTimer, []);
163
+ useEffect(() => () => cancelIntent.current?.(), []);
164
+ // Warmth membership: open counts toward the bar being warm, and closing
165
+ // keeps it warm briefly, so drifting off and straight on to a neighbour
166
+ // isn't a cold start. A dismissal (Escape) cools it instead.
167
+ const dismissed = useRef(false);
168
+ useEffect(() => {
169
+ if (!open)
170
+ return;
171
+ dismissed.current = false;
172
+ markOpen(warmth);
173
+ return () => markClosed(warmth, dismissed.current ? 0 : PRIMED_WINDOW);
174
+ }, [open, warmth]);
175
+ // Intent sampling: the latest pointer position, read by the sampler.
176
+ const pointerPos = useRef({ x: 0, y: 0 });
177
+ const cancelIntent = useRef(null);
178
+ const stopIntent = () => {
179
+ cancelIntent.current?.();
180
+ cancelIntent.current = null;
181
+ };
182
+ const openFromHover = () => {
183
+ clearHoverTimer();
184
+ stopIntent();
185
+ openedByHover.current = true;
186
+ setOpen(true);
187
+ };
188
+ const onEntryPointerEnter = (e) => {
189
+ if (e.pointerType === 'mouse')
190
+ pointerPos.current = { x: e.clientX, y: e.clientY };
191
+ if (e.pointerType === 'mouse')
192
+ pointerInside.current = true;
193
+ if (!hover || e.pointerType !== 'mouse' || dismissedWhileHovered.current)
194
+ return;
195
+ clearHoverTimer();
196
+ stopIntent();
197
+ if (open)
198
+ return;
199
+ // Warm: another panel is open, or one just closed. Open at once, as a bar
200
+ // does; otherwise wait for the pointer to settle.
201
+ if (openId !== null || isWarm(warmth)) {
202
+ openFromHover();
203
+ return;
204
+ }
205
+ cancelIntent.current = sampleHoverIntent({ readPointer: () => pointerPos.current, onIntent: openFromHover });
206
+ };
207
+ const onEntryPointerMove = (e) => {
208
+ if (e.pointerType === 'mouse')
209
+ pointerPos.current = { x: e.clientX, y: e.clientY };
210
+ };
211
+ const onEntryPointerLeave = (e) => {
212
+ if (e.pointerType === 'mouse')
213
+ pointerInside.current = false;
214
+ if (!hover || e.pointerType !== 'mouse')
215
+ return;
216
+ dismissedWhileHovered.current = false;
217
+ clearHoverTimer();
218
+ stopIntent();
219
+ if (!open || !openedByHover.current)
220
+ return;
221
+ // Keyboard presence pins it: closing under focus would strand focus on
222
+ // <body> (SC 2.4.3) when the mouse drifts off a panel the user tabbed into.
223
+ if (entryRef.current?.contains(document.activeElement)) {
224
+ openedByHover.current = false;
225
+ return;
226
+ }
227
+ hoverTimer.current = setTimeout(() => {
228
+ openedByHover.current = false;
229
+ setOpen(false);
230
+ }, HOVER_CLOSE_DELAY);
231
+ };
232
+ const toggleOnClick = (e) => {
233
+ clearHoverTimer();
234
+ // A pointer click on a hover-opened panel pins it. Keyboard activation
235
+ // (detail 0) toggles as always, so Enter never looks like a no-op.
236
+ if (open && openedByHover.current && e.detail > 0) {
237
+ openedByHover.current = false; // pinned
238
+ return;
239
+ }
240
+ openedByHover.current = false;
241
+ setOpen(!open);
242
+ };
243
+ const entryRef = useRef(null);
244
+ const toggleRef = useRef(null);
245
+ const { refs, floatingStyles, isPositioned } = useFloating({
246
+ open,
247
+ placement: 'bottom-start',
248
+ strategy: 'absolute',
249
+ middleware: [offset(4), flip(), shift({ padding: 8 })],
250
+ whileElementsMounted: autoUpdate,
251
+ });
252
+ // Moving to another flyout toggle in the same menu is a switch, not a
253
+ // dismissal: its click hands openId over in one update. Closing here on
254
+ // pointerdown or focusout instead left a frame with no panel at all, and
255
+ // set both chevrons animating (Julia: "the menu briefly disappears and the
256
+ // indicator animates back and forth").
257
+ const isSiblingToggle = useCallback((node) => node instanceof Element &&
258
+ !entryRef.current?.contains(node) &&
259
+ (rootRef.current?.contains(node) ?? false) &&
260
+ node.closest('.tui-nav-menu__entry.has-flyout [aria-expanded]') !== null, [rootRef]);
261
+ // While open: a click or tap outside closes it, and Escape closes it from
262
+ // anywhere. Escape is on the document, not the entry, because a
263
+ // hover-opened panel never has focus, and WCAG 1.4.13 needs hover content
264
+ // dismissible without moving focus. Focus returns to the toggle only if it
265
+ // was inside the entry, so a mouse user's focus isn't pulled anywhere.
266
+ // Focus leaving is handled by onBlur below.
267
+ useEffect(() => {
268
+ if (!open)
269
+ return;
270
+ const onPointerDown = (e) => {
271
+ if (!entryRef.current?.contains(e.target) && !isSiblingToggle(e.target))
272
+ setOpen(false);
273
+ };
274
+ const onKeyDown = (e) => {
275
+ if (e.key !== 'Escape')
276
+ return;
277
+ e.preventDefault();
278
+ clearHoverTimer();
279
+ // Only "dismissed while hovered" if the pointer really is still there;
280
+ // otherwise there'd be no pointerleave to clear it.
281
+ if (openedByHover.current && pointerInside.current)
282
+ dismissedWhileHovered.current = true;
283
+ openedByHover.current = false;
284
+ dismissed.current = true;
285
+ const focusWasInside = entryRef.current?.contains(document.activeElement) ?? false;
286
+ setOpen(false);
287
+ if (focusWasInside)
288
+ toggleRef.current?.focus();
289
+ };
290
+ document.addEventListener('pointerdown', onPointerDown);
291
+ document.addEventListener('keydown', onKeyDown);
292
+ return () => {
293
+ document.removeEventListener('pointerdown', onPointerDown);
294
+ document.removeEventListener('keydown', onKeyDown);
295
+ };
296
+ }, [open, setOpen, isSiblingToggle]);
297
+ const onEntryBlur = (e) => {
298
+ if (open && !entryRef.current?.contains(e.relatedTarget) && !isSiblingToggle(e.relatedTarget)) {
299
+ setOpen(false);
300
+ }
301
+ };
302
+ // Safari and Firefox on macOS don't focus a button on click. Without this,
303
+ // clicking the toggle to close an open flyout blurs the panel first (closing
304
+ // it), and the click then re-opens it. Only while open: Chromium treats a
305
+ // programmatic focus inside a pointer event as focus-visible, so doing it on
306
+ // every click would ring the toggle for mouse users.
307
+ const focusOnPointerDown = (e) => {
308
+ if (open)
309
+ e.currentTarget.focus();
310
+ };
311
+ // -- Row --------------------------------------------------------------------
62
312
  const ariaCurrent = toAriaCurrent(current);
63
- const itemClass = cx('tui-nav-menu__item', className);
313
+ // Top-level rows of a bar. Rows inside a flyout panel keep the vertical look.
314
+ const bar = orientation === 'horizontal' && !inFlyout;
315
+ const itemClass = cx('tui-nav-menu__item', bar && 'is-bar', className);
64
316
  const iconNode = icon ? (_jsx("span", { className: "tui-nav-menu__icon", "aria-hidden": "true", children: _jsx(Icon, { name: icon }) })) : null;
65
- const content = (inner) => (_jsxs(_Fragment, { children: [iconNode, _jsx("span", { className: "tui-nav-menu__label", children: inner })] }));
317
+ const content = (inner) => (_jsxs(_Fragment, { children: [iconNode, _jsx("span", { className: "tui-nav-menu__label", id: flyout ? labelId : undefined, children: inner })] }));
318
+ const chevron = (_jsx("span", { className: "tui-nav-menu__chevron", "aria-hidden": "true", children: _jsx(Icon, { name: "system/chevron-down-small" }) }));
66
319
  let row;
320
+ // A flyout parent with no destination of its own: the row IS the toggle.
321
+ const rowIsToggle = flyout && !asChild && props.href === undefined;
67
322
  if (disabled) {
68
323
  // Inert: the APG disabled-link pattern. An <a> with no href is out of the
69
324
  // tab order; role="link" keeps it announced as a link, and aria-disabled
@@ -73,6 +328,14 @@ const NavMenuItem = forwardRef(function NavMenuItem(props, ref) {
73
328
  delete inertRest.href;
74
329
  row = (_jsx("a", { ref: ref, ...inertRest, role: "link", className: itemClass, "aria-disabled": "true", "aria-current": ariaCurrent, children: content(asChild && isValidElement(label[0]) ? label[0].props.children : label) }));
75
330
  }
331
+ else if (rowIsToggle) {
332
+ const restProps = rest;
333
+ row = (_jsxs("button", { ref: composeRefs(ref, toggleRef), type: "button", ...rest, className: cx(itemClass, 'is-toggle'), "aria-current": ariaCurrent, "aria-expanded": open, "aria-controls": panelId, onPointerDown: focusOnPointerDown, onClick: (e) => {
334
+ restProps.onClick?.(e);
335
+ if (!e.defaultPrevented)
336
+ toggleOnClick(e);
337
+ }, children: [content(label), chevron] }));
338
+ }
76
339
  else if (asChild) {
77
340
  const child = label[0];
78
341
  if (!isValidElement(child)) {
@@ -101,7 +364,29 @@ const NavMenuItem = forwardRef(function NavMenuItem(props, ref) {
101
364
  else {
102
365
  row = (_jsx("button", { ref: ref, type: "button", ...rest, className: itemClass, "aria-current": ariaCurrent, children: content(label) }));
103
366
  }
104
- return (_jsxs("li", { className: "tui-nav-menu__entry", children: [row, subs] }));
367
+ if (!flyout) {
368
+ return (_jsxs("li", { className: "tui-nav-menu__entry", children: [row, !(disabled && bar) && subs] }));
369
+ }
370
+ return (_jsxs("li", { ref: composeRefs(entryRef, refs.setReference), className: cx('tui-nav-menu__entry', 'has-flyout', !rowIsToggle && 'is-split'), onBlur: onEntryBlur, onPointerEnter: onEntryPointerEnter, onPointerMove: hover ? onEntryPointerMove : undefined, onPointerLeave: onEntryPointerLeave, children: [row, !rowIsToggle && (
371
+ // A link can't also toggle, so a parent with a destination gets its own
372
+ // toggle. It's named "<label> <submenu>", so the bar never has a row
373
+ // of identical "toggle" buttons.
374
+ _jsxs("button", { ref: toggleRef, type: "button", className: cx('tui-nav-menu__toggle', 'is-bar'), "aria-expanded": open, "aria-controls": panelId, "aria-labelledby": `${labelId} ${toggleTextId}`, onPointerDown: focusOnPointerDown, onClick: toggleOnClick, children: [chevron, _jsx("span", { id: toggleTextId, hidden: true, children: labels.submenu })] })), _jsx("div", { id: panelId, ref: refs.setFloating,
375
+ // While hidden the panel has no size, so its last position is stale.
376
+ // Until Floating UI has placed it for this opening it's transparent,
377
+ // or it paints a frame at the stale spot (past the viewport edge, at
378
+ // the bar's end) before jumping into place. Opacity, not visibility:
379
+ // it must stay focusable and in the accessibility tree, so Enter then
380
+ // an immediate Tab still lands inside.
381
+ style: { ...floatingStyles, opacity: open && !isPositioned ? 0 : undefined }, className: "tui-nav-menu__flyout", hidden: !open,
382
+ // Following a link inside closes the panel (APG): in a single-page
383
+ // app nothing else would, and it would sit over the new page. Enter
384
+ // on a link fires click too. Router onClicks compose: this runs after
385
+ // the link's own handler, on the bubble.
386
+ onClick: (e) => {
387
+ if (e.target.closest('a[href]'))
388
+ setOpen(false);
389
+ }, children: _jsx(FlyoutContext.Provider, { value: true, children: subs }) })] }));
105
390
  });
106
391
  export const NavMenu = Object.assign(NavMenuRoot, {
107
392
  Group: NavMenuGroup,
@@ -1,2 +1,3 @@
1
1
  export { NavMenu } from './NavMenu';
2
- export type { NavMenuProps, NavMenuGroupProps, NavMenuGroupLabelElement, NavMenuItemProps, NavMenuItemAsLink, NavMenuItemAsButton, NavMenuItemAsChild, NavMenuCurrent, NavMenuSubProps, } from './types';
2
+ export { defaultNavMenuLabels } from './types';
3
+ export type { NavMenuProps, NavMenuOrientation, NavMenuFlyoutTrigger, NavMenuLabels, NavMenuGroupProps, NavMenuGroupLabelElement, NavMenuItemProps, NavMenuItemAsLink, NavMenuItemAsButton, NavMenuItemAsChild, NavMenuCurrent, NavMenuSubProps, } from './types';
@@ -1 +1,2 @@
1
1
  export { NavMenu } from './NavMenu.js';
2
+ export { defaultNavMenuLabels } from './types.js';
@@ -1,10 +1,47 @@
1
1
  import type { ComponentPropsWithoutRef, ReactNode } from 'react';
2
2
  import type { IconName } from '../../icons/registry';
3
+ export type NavMenuOrientation = 'vertical' | 'horizontal';
4
+ export type NavMenuFlyoutTrigger = 'click' | 'hover';
5
+ /** Visually hidden strings, for localisation. */
6
+ export type NavMenuLabels = {
7
+ /**
8
+ * Word appended to a flyout parent's label to name its separate toggle
9
+ * button (a parent that is also a link): "Catalog submenu".
10
+ * @default 'submenu'
11
+ */
12
+ submenu?: string;
13
+ };
14
+ export declare const defaultNavMenuLabels: Required<NavMenuLabels>;
3
15
  /**
4
16
  * Root. Not a landmark: put it inside `Sidebar.Nav` or your own `<nav>`,
5
17
  * which owns the label.
6
18
  */
7
19
  export type NavMenuProps = ComponentPropsWithoutRef<'div'> & {
20
+ /**
21
+ * `vertical` (sidebars) stacks groups and nests a `NavMenu.Sub` inline.
22
+ * `horizontal` makes a bar: groups become inline clusters (headings visually
23
+ * hidden, lists still labelled), and an item with a `NavMenu.Sub` opens it
24
+ * as a flyout. A flyout follows the APG disclosure pattern: a button with
25
+ * `aria-expanded`, opened by click or Enter/Space (never hover), closed by
26
+ * Escape (focus returns to the button), Tab-ing out or clicking elsewhere.
27
+ * One flyout is open at a time. Groups inside a flyout show their headings.
28
+ * @default 'vertical'
29
+ */
30
+ orientation?: NavMenuOrientation;
31
+ /**
32
+ * How a horizontal bar's flyouts open. `click` (default): click or
33
+ * Enter/Space. `hover`: also opens for a mouse once the pointer settles on
34
+ * an item (hover intent, as in WordPress's Toolbar: under 6px of movement in
35
+ * 100ms), so sweeping across the bar opens nothing. Once primed (a panel is
36
+ * open, or one just closed), the next opens at once. There's a grace period
37
+ * for crossing into the panel. Escape dismisses it until the pointer leaves
38
+ * (WCAG 1.4.13); clicking pins it open. Touch and keyboard behave as `click`
39
+ * either way.
40
+ * @default 'click'
41
+ */
42
+ flyoutTrigger?: NavMenuFlyoutTrigger;
43
+ /** Visually hidden strings. */
44
+ labels?: NavMenuLabels;
8
45
  children: ReactNode;
9
46
  };
10
47
  export type NavMenuGroupLabelElement = 'h2' | 'h3' | 'h4' | 'h5' | 'h6' | 'span';
@@ -43,9 +80,13 @@ type NavMenuItemBaseProps = {
43
80
  */
44
81
  disabled?: boolean;
45
82
  /**
46
- * The label, plus an optional `NavMenu.Sub` of nested items. Sub lists render
47
- * after the link, inside the same list item. Whether a Sub renders at all is
48
- * up to you (e.g. only while its parent is current).
83
+ * The label, plus an optional `NavMenu.Sub` of nested items.
84
+ *
85
+ * Vertical: the Sub renders after the link, inside the same list item, and
86
+ * whether it renders at all is up to you (e.g. only while its parent is
87
+ * current). Horizontal: the Sub is a flyout. With no `href`, the row itself
88
+ * toggles it; with an `href` (or `asChild` link), the row navigates and a
89
+ * separate toggle button opens it.
49
90
  */
50
91
  children: ReactNode;
51
92
  };
@@ -1,4 +1,6 @@
1
1
  // =============================================================================
2
2
  // NavMenu Types
3
3
  // =============================================================================
4
- export {};
4
+ export const defaultNavMenuLabels = {
5
+ submenu: 'submenu',
6
+ };
@@ -1,6 +1,7 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
2
  import React, { useCallback, useEffect, useRef, cloneElement, isValidElement, } from 'react';
3
3
  import { cx } from '../../utils/cx.js';
4
+ import { TooltipWarmthScope } from '../Tooltip/TooltipWarmthScope.js';
4
5
  import { isDev } from '../../utils/is-dev.js';
5
6
  // =============================================================================
6
7
  // Toolbar Component
@@ -204,7 +205,7 @@ function ToolbarRoot({ 'aria-label': ariaLabel, 'aria-labelledby': ariaLabelledB
204
205
  return (_jsx("div", { ref: containerRef, role: "toolbar", "aria-label": ariaLabel, "aria-labelledby": ariaLabelledBy, "aria-orientation": orientation, className: cx('tui-toolbar', className), style: style,
205
206
  // Capture phase catches focus on inner elements (SVG icons, spans) before
206
207
  // they handle it, allowing us to update roving tabindex correctly
207
- onFocusCapture: handleFocusIn, onKeyDown: handleKeyDown, children: children }));
208
+ onFocusCapture: handleFocusIn, onKeyDown: handleKeyDown, children: _jsx(TooltipWarmthScope, { children: children }) }));
208
209
  }
209
210
  // =============================================================================
210
211
  // Toolbar Group
@@ -1,5 +1,5 @@
1
1
  import type { TooltipProviderProps, TooltipProps, TooltipTriggerProps, TooltipContentProps } from './types';
2
- declare function TooltipProviderComponent({ delayDuration, closeDelayDuration, children, }: TooltipProviderProps): import("react/jsx-runtime").JSX.Element;
2
+ declare function TooltipProviderComponent({ delayDuration, closeDelayDuration, skipDelayDuration, children, }: TooltipProviderProps): import("react/jsx-runtime").JSX.Element;
3
3
  declare function TooltipRoot({ open: controlledOpen, onOpenChange, defaultOpen, delayDuration: localDelay, children, }: TooltipProps): import("react/jsx-runtime").JSX.Element;
4
4
  declare function TooltipTriggerComponent({ asChild, 'aria-label': ariaLabel, children, }: TooltipTriggerProps): import("react/jsx-runtime").JSX.Element;
5
5
  declare function TooltipContentComponent(props: TooltipContentProps): import("react/jsx-runtime").JSX.Element | null;
@@ -1,5 +1,6 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
2
  import React, { useCallback, useEffect, useId, useMemo, useRef, useState, cloneElement, isValidElement, } from 'react';
3
+ import { createHoverWarmth, isWarm, markClosed, markOpen } from '../../utils/hover-intent.js';
3
4
  import { isDev } from '../../utils/is-dev.js';
4
5
  import { useFloating, offset, flip, shift, arrow, autoUpdate, FloatingPortal, FloatingArrow, } from '@floating-ui/react';
5
6
  import { cx } from '../../utils/cx.js';
@@ -10,8 +11,10 @@ import { toPlacement } from './types.js';
10
11
  // =============================================================================
11
12
  // TooltipProvider
12
13
  // =============================================================================
13
- function TooltipProviderComponent({ delayDuration = 400, closeDelayDuration = 150, children, }) {
14
- const value = useMemo(() => ({ delayDuration, closeDelayDuration }), [delayDuration, closeDelayDuration]);
14
+ function TooltipProviderComponent({ delayDuration = 400, closeDelayDuration = 150, skipDelayDuration = 300, children, }) {
15
+ const [warmth] = useState(createHoverWarmth);
16
+ const [activeClose] = useState(() => ({ current: null }));
17
+ const value = useMemo(() => ({ delayDuration, closeDelayDuration, skipDelayDuration, warmth, activeClose }), [delayDuration, closeDelayDuration, skipDelayDuration, warmth, activeClose]);
15
18
  return (_jsx(TooltipProviderContext.Provider, { value: value, children: children }));
16
19
  }
17
20
  // =============================================================================
@@ -52,10 +55,52 @@ function TooltipRoot({ open: controlledOpen, onOpenChange, defaultOpen = false,
52
55
  closeTimeoutRef.current = null;
53
56
  }
54
57
  }, []);
58
+ const { warmth, skipDelayDuration, activeClose } = providerContext;
55
59
  const handleOpen = useCallback(() => {
56
60
  clearAllTimeouts();
61
+ // Warm group: a tooltip is showing, or one just closed. Open at once.
62
+ if (warmth && isWarm(warmth)) {
63
+ setOpen(true);
64
+ return;
65
+ }
57
66
  openTimeoutRef.current = setTimeout(() => setOpen(true), resolvedDelay);
58
- }, [clearAllTimeouts, resolvedDelay, setOpen]);
67
+ }, [clearAllTimeouts, resolvedDelay, setOpen, warmth]);
68
+ // Warmth membership. Open counts toward the group being warm; closing keeps
69
+ // it warm for skipDelayDuration, unless it was dismissed. One at a time: a
70
+ // tooltip opening in a warm group closes the previous one at once, rather
71
+ // than both showing through its close delay.
72
+ const dismissedRef = useRef(false);
73
+ // Read through a ref: an inline onOpenChange makes setOpen a new function
74
+ // every render, and the membership effect must not re-run (and churn the
75
+ // warm window) for that.
76
+ const setOpenRef = useRef(setOpen);
77
+ setOpenRef.current = setOpen;
78
+ useEffect(() => {
79
+ if (!open || !warmth)
80
+ return;
81
+ dismissedRef.current = false;
82
+ markOpen(warmth);
83
+ const closeNow = () => {
84
+ clearAllTimeouts();
85
+ setOpenRef.current(false);
86
+ };
87
+ if (activeClose) {
88
+ const previous = activeClose.current;
89
+ activeClose.current = closeNow;
90
+ if (previous)
91
+ previous();
92
+ }
93
+ return () => {
94
+ markClosed(warmth, dismissedRef.current ? 0 : skipDelayDuration);
95
+ if (activeClose?.current === closeNow)
96
+ activeClose.current = null;
97
+ };
98
+ }, [open, warmth, activeClose, skipDelayDuration, clearAllTimeouts]);
99
+ const dismiss = useCallback(() => {
100
+ dismissedRef.current = true;
101
+ clearAllTimeouts();
102
+ setOpen(false);
103
+ }, [clearAllTimeouts, setOpen]);
59
104
  const handleClose = useCallback(() => {
60
105
  clearAllTimeouts();
61
106
  if (resolvedCloseDelay > 0) {
@@ -79,7 +124,8 @@ function TooltipRoot({ open: controlledOpen, onOpenChange, defaultOpen = false,
79
124
  handleOpen,
80
125
  handleClose,
81
126
  cancelClose,
82
- }), [open, setOpen, contentId, resolvedDelay, resolvedCloseDelay, handleOpen, handleClose, cancelClose]);
127
+ dismiss,
128
+ }), [open, setOpen, contentId, resolvedDelay, resolvedCloseDelay, handleOpen, handleClose, cancelClose, dismiss]);
83
129
  return (_jsx(TooltipContext.Provider, { value: contextValue, children: children }));
84
130
  }
85
131
  // =============================================================================
@@ -132,7 +178,7 @@ function TooltipContentComponent(props) {
132
178
  }
133
179
  // Inner component: only mounted when open. All Floating UI hooks live here.
134
180
  function TooltipContentInner({ side = 'top', align = 'center', sideOffset = 8, theme = 'dark', className, children, }) {
135
- const { setOpen, triggerRef, contentId, cancelClose, handleClose } = useTooltipContext();
181
+ const { triggerRef, contentId, cancelClose, handleClose, dismiss } = useTooltipContext();
136
182
  const arrowRef = useRef(null);
137
183
  const { refs, floatingStyles, context } = useFloating({
138
184
  placement: toPlacement(side, align),
@@ -161,7 +207,7 @@ function TooltipContentInner({ side = 'top', align = 'center', sideOffset = 8, t
161
207
  const activeEl = document.activeElement;
162
208
  const trigger = triggerRef.current;
163
209
  const focusIsOnTrigger = trigger && (trigger === activeEl || trigger.contains(activeEl));
164
- setOpen(false);
210
+ dismiss();
165
211
  if (focusIsOnTrigger) {
166
212
  e.stopPropagation(); // Prevent bubbling to modal's Escape handler
167
213
  }
@@ -169,7 +215,7 @@ function TooltipContentInner({ side = 'top', align = 'center', sideOffset = 8, t
169
215
  };
170
216
  document.addEventListener('keydown', handleKeyDown);
171
217
  return () => document.removeEventListener('keydown', handleKeyDown);
172
- }, [setOpen, triggerRef]);
218
+ }, [dismiss, triggerRef]);
173
219
  // Get portal root inside .tui-interface
174
220
  const portalRoot = getPortalRootFor(triggerRef.current);
175
221
  // Dev warning: tooltips should not contain interactive content (WCAG 1.4.13)
@@ -1,3 +1,4 @@
1
+ import type { HoverWarmth } from '../../utils/hover-intent';
1
2
  import type { TooltipContextValue } from './types';
2
3
  /**
3
4
  * Context for Tooltip compound components.
@@ -10,6 +11,17 @@ export declare function useTooltipContext(): TooltipContextValue;
10
11
  export type TooltipProviderContextValue = {
11
12
  delayDuration: number;
12
13
  closeDelayDuration: number;
14
+ skipDelayDuration: number;
15
+ /**
16
+ * The warmth group (utils/hover-intent), or null with no provider: bare
17
+ * tooltips stay independent, as before. Scoped rather than global so warmth
18
+ * never leaks between unrelated regions (or test cases).
19
+ */
20
+ warmth: HoverWarmth | null;
21
+ /** Closes the group's currently open tooltip: one at a time when warm. */
22
+ activeClose: {
23
+ current: (() => void) | null;
24
+ } | null;
13
25
  };
14
26
  export declare const TooltipProviderContext: import("react").Context<TooltipProviderContextValue>;
15
27
  export declare function useTooltipProviderContext(): TooltipProviderContextValue;
@@ -19,6 +19,9 @@ export function useTooltipContext() {
19
19
  export const TooltipProviderContext = createContext({
20
20
  delayDuration: 400,
21
21
  closeDelayDuration: 0,
22
+ skipDelayDuration: 300,
23
+ warmth: null,
24
+ activeClose: null,
22
25
  });
23
26
  export function useTooltipProviderContext() {
24
27
  return useContext(TooltipProviderContext);