@tangible/ui 0.2.25 → 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.
- package/components/NavMenu/NavMenu.d.ts +2 -1
- package/components/NavMenu/NavMenu.js +190 -22
- package/components/NavMenu/index.d.ts +1 -1
- package/components/NavMenu/types.d.ts +13 -0
- package/components/Toolbar/Toolbar.js +2 -1
- package/components/Tooltip/Tooltip.d.ts +1 -1
- package/components/Tooltip/Tooltip.js +53 -7
- package/components/Tooltip/TooltipContext.d.ts +12 -0
- package/components/Tooltip/TooltipContext.js +3 -0
- package/components/Tooltip/TooltipWarmthScope.d.ts +13 -0
- package/components/Tooltip/TooltipWarmthScope.js +28 -0
- package/components/Tooltip/types.d.ts +12 -0
- package/components/index.d.ts +1 -1
- package/figma-tokens.json +1 -1
- package/package.json +1 -1
- package/skills/building-with-tui/SKILL.md +3 -1
- package/styles/all.css +1 -1
- package/styles/all.expanded.css +6 -10
- package/styles/all.expanded.unlayered.css +6 -10
- package/styles/all.unlayered.css +1 -1
- package/styles/components/nav-menu.scss +20 -22
- package/tui-manifest.json +8 -5
- package/utils/hover-intent.d.ts +38 -0
- package/utils/hover-intent.js +67 -0
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type ReactNode } from 'react';
|
|
2
|
-
import type { NavMenuItemProps, NavMenuLabels, NavMenuOrientation, 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
5
|
* Vertical by default (sidebars). `orientation="horizontal"` makes a bar whose
|
|
@@ -10,6 +10,7 @@ import type { NavMenuItemProps, NavMenuLabels, NavMenuOrientation, NavMenuSubPro
|
|
|
10
10
|
*/
|
|
11
11
|
declare const NavMenuRoot: import("react").ForwardRefExoticComponent<Omit<import("react").DetailedHTMLProps<import("react").HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "ref"> & {
|
|
12
12
|
orientation?: NavMenuOrientation;
|
|
13
|
+
flyoutTrigger?: NavMenuFlyoutTrigger;
|
|
13
14
|
labels?: NavMenuLabels;
|
|
14
15
|
children: ReactNode;
|
|
15
16
|
} & import("react").RefAttributes<HTMLDivElement>>;
|
|
@@ -6,15 +6,26 @@ import { isDev } from '../../utils/is-dev.js';
|
|
|
6
6
|
import { mergeProps } from '../../utils/merge-props.js';
|
|
7
7
|
import { composeRefs, getElementRef } from '../../utils/compose-refs.js';
|
|
8
8
|
import { Icon } from '../Icon/index.js';
|
|
9
|
+
import { createHoverWarmth, isWarm, markClosed, markOpen, sampleHoverIntent } from '../../utils/hover-intent.js';
|
|
9
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;
|
|
10
18
|
// Items render <li>, so they're only valid inside a Group or Sub list. The
|
|
11
19
|
// context exists to warn when one isn't.
|
|
12
20
|
const ListContext = createContext(false);
|
|
13
21
|
const RootContext = createContext({
|
|
14
22
|
orientation: 'vertical',
|
|
23
|
+
flyoutTrigger: 'click',
|
|
15
24
|
labels: defaultNavMenuLabels,
|
|
16
25
|
openId: null,
|
|
17
26
|
setOpenId: () => { },
|
|
27
|
+
rootRef: { current: null },
|
|
28
|
+
warmth: createHoverWarmth(),
|
|
18
29
|
});
|
|
19
30
|
// True inside a flyout panel: everything there lays out vertically, and an
|
|
20
31
|
// item with its own Sub nests inline instead of opening another flyout.
|
|
@@ -30,10 +41,13 @@ const FlyoutContext = createContext(false);
|
|
|
30
41
|
* hover-to-open. Not a landmark: put it inside `Sidebar.Nav` or a labelled
|
|
31
42
|
* `<nav>`.
|
|
32
43
|
*/
|
|
33
|
-
const NavMenuRoot = forwardRef(function NavMenu({ orientation = 'vertical', labels, className, children, ...rest }, ref) {
|
|
44
|
+
const NavMenuRoot = forwardRef(function NavMenu({ orientation = 'vertical', flyoutTrigger = 'click', labels, className, children, ...rest }, ref) {
|
|
34
45
|
const [openId, setOpenId] = useState(null);
|
|
35
|
-
const
|
|
36
|
-
|
|
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 }) }));
|
|
37
51
|
});
|
|
38
52
|
// =============================================================================
|
|
39
53
|
// NavMenu.Group
|
|
@@ -81,7 +95,7 @@ const isSub = (node) => isValidElement(node) && node.type === NavMenuSub;
|
|
|
81
95
|
const NavMenuItem = forwardRef(function NavMenuItem(props, ref) {
|
|
82
96
|
const { current, icon, disabled, asChild, className, children, ...rest } = props;
|
|
83
97
|
const inList = useContext(ListContext);
|
|
84
|
-
const { orientation, labels, openId, setOpenId } = useContext(RootContext);
|
|
98
|
+
const { orientation, flyoutTrigger, labels, openId, setOpenId, rootRef, warmth } = useContext(RootContext);
|
|
85
99
|
const inFlyout = useContext(FlyoutContext);
|
|
86
100
|
const warned = useRef(false);
|
|
87
101
|
useEffect(() => {
|
|
@@ -101,42 +115,189 @@ const NavMenuItem = forwardRef(function NavMenuItem(props, ref) {
|
|
|
101
115
|
const toggleTextId = `${id}-toggle`;
|
|
102
116
|
const flyout = orientation === 'horizontal' && !inFlyout && subs.length > 0 && !disabled;
|
|
103
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).
|
|
104
122
|
const setOpen = useCallback((next) => {
|
|
105
123
|
if (next)
|
|
106
124
|
setOpenId(id);
|
|
107
|
-
else
|
|
108
|
-
setOpenId(null);
|
|
109
|
-
}, [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
|
+
};
|
|
110
243
|
const entryRef = useRef(null);
|
|
111
244
|
const toggleRef = useRef(null);
|
|
112
|
-
const { refs, floatingStyles } = useFloating({
|
|
245
|
+
const { refs, floatingStyles, isPositioned } = useFloating({
|
|
113
246
|
open,
|
|
114
247
|
placement: 'bottom-start',
|
|
115
248
|
strategy: 'absolute',
|
|
116
249
|
middleware: [offset(4), flip(), shift({ padding: 8 })],
|
|
117
250
|
whileElementsMounted: autoUpdate,
|
|
118
251
|
});
|
|
119
|
-
//
|
|
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.
|
|
120
267
|
useEffect(() => {
|
|
121
268
|
if (!open)
|
|
122
269
|
return;
|
|
123
270
|
const onPointerDown = (e) => {
|
|
124
|
-
if (!entryRef.current?.contains(e.target))
|
|
271
|
+
if (!entryRef.current?.contains(e.target) && !isSiblingToggle(e.target))
|
|
125
272
|
setOpen(false);
|
|
126
273
|
};
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
const onEntryKeyDown = (e) => {
|
|
131
|
-
if (open && e.key === 'Escape') {
|
|
274
|
+
const onKeyDown = (e) => {
|
|
275
|
+
if (e.key !== 'Escape')
|
|
276
|
+
return;
|
|
132
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;
|
|
133
286
|
setOpen(false);
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
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]);
|
|
137
297
|
const onEntryBlur = (e) => {
|
|
138
|
-
if (open && !entryRef.current?.contains(e.relatedTarget))
|
|
298
|
+
if (open && !entryRef.current?.contains(e.relatedTarget) && !isSiblingToggle(e.relatedTarget)) {
|
|
139
299
|
setOpen(false);
|
|
300
|
+
}
|
|
140
301
|
};
|
|
141
302
|
// Safari and Firefox on macOS don't focus a button on click. Without this,
|
|
142
303
|
// clicking the toggle to close an open flyout blurs the panel first (closing
|
|
@@ -172,7 +333,7 @@ const NavMenuItem = forwardRef(function NavMenuItem(props, ref) {
|
|
|
172
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) => {
|
|
173
334
|
restProps.onClick?.(e);
|
|
174
335
|
if (!e.defaultPrevented)
|
|
175
|
-
|
|
336
|
+
toggleOnClick(e);
|
|
176
337
|
}, children: [content(label), chevron] }));
|
|
177
338
|
}
|
|
178
339
|
else if (asChild) {
|
|
@@ -206,11 +367,18 @@ const NavMenuItem = forwardRef(function NavMenuItem(props, ref) {
|
|
|
206
367
|
if (!flyout) {
|
|
207
368
|
return (_jsxs("li", { className: "tui-nav-menu__entry", children: [row, !(disabled && bar) && subs] }));
|
|
208
369
|
}
|
|
209
|
-
return (_jsxs("li", { ref: composeRefs(entryRef, refs.setReference), className: cx('tui-nav-menu__entry', 'has-flyout', !rowIsToggle && 'is-split'),
|
|
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 && (
|
|
210
371
|
// A link can't also toggle, so a parent with a destination gets its own
|
|
211
372
|
// toggle. It's named "<label> <submenu>", so the bar never has a row
|
|
212
373
|
// of identical "toggle" buttons.
|
|
213
|
-
_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:
|
|
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,
|
|
214
382
|
// Following a link inside closes the panel (APG): in a single-page
|
|
215
383
|
// app nothing else would, and it would sit over the new page. Enter
|
|
216
384
|
// on a link fires click too. Router onClicks compose: this runs after
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
export { NavMenu } from './NavMenu';
|
|
2
2
|
export { defaultNavMenuLabels } from './types';
|
|
3
|
-
export type { NavMenuProps, NavMenuOrientation, NavMenuLabels, NavMenuGroupProps, NavMenuGroupLabelElement, NavMenuItemProps, NavMenuItemAsLink, NavMenuItemAsButton, NavMenuItemAsChild, NavMenuCurrent, NavMenuSubProps, } from './types';
|
|
3
|
+
export type { NavMenuProps, NavMenuOrientation, NavMenuFlyoutTrigger, NavMenuLabels, NavMenuGroupProps, NavMenuGroupLabelElement, NavMenuItemProps, NavMenuItemAsLink, NavMenuItemAsButton, NavMenuItemAsChild, NavMenuCurrent, NavMenuSubProps, } from './types';
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { ComponentPropsWithoutRef, ReactNode } from 'react';
|
|
2
2
|
import type { IconName } from '../../icons/registry';
|
|
3
3
|
export type NavMenuOrientation = 'vertical' | 'horizontal';
|
|
4
|
+
export type NavMenuFlyoutTrigger = 'click' | 'hover';
|
|
4
5
|
/** Visually hidden strings, for localisation. */
|
|
5
6
|
export type NavMenuLabels = {
|
|
6
7
|
/**
|
|
@@ -27,6 +28,18 @@ export type NavMenuProps = ComponentPropsWithoutRef<'div'> & {
|
|
|
27
28
|
* @default 'vertical'
|
|
28
29
|
*/
|
|
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;
|
|
30
43
|
/** Visually hidden strings. */
|
|
31
44
|
labels?: NavMenuLabels;
|
|
32
45
|
children: ReactNode;
|
|
@@ -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
|
|
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
|
-
|
|
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 {
|
|
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
|
-
|
|
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
|
-
}, [
|
|
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);
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { type ReactNode } from 'react';
|
|
2
|
+
/**
|
|
3
|
+
* @internal Gives the tooltips inside a warmth group. Toolbar wraps its
|
|
4
|
+
* items in this, so toolbar tooltips warm up with no setup.
|
|
5
|
+
*
|
|
6
|
+
* Under an outer TooltipProvider it changes nothing: that provider's group
|
|
7
|
+
* and delays apply. Without one, it supplies the provider's defaults too, not
|
|
8
|
+
* the bare-tooltip fallback, whose 0ms close delay would make a toolbar's
|
|
9
|
+
* tooltips close before the pointer can cross into them (WCAG 1.4.13).
|
|
10
|
+
*/
|
|
11
|
+
export declare function TooltipWarmthScope({ children }: {
|
|
12
|
+
children: ReactNode;
|
|
13
|
+
}): import("react/jsx-runtime").JSX.Element;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Fragment as _Fragment, jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { useMemo, useState } from 'react';
|
|
3
|
+
import { createHoverWarmth } from '../../utils/hover-intent.js';
|
|
4
|
+
import { useTooltipProviderContext, TooltipProviderContext } from './TooltipContext.js';
|
|
5
|
+
/**
|
|
6
|
+
* @internal Gives the tooltips inside a warmth group. Toolbar wraps its
|
|
7
|
+
* items in this, so toolbar tooltips warm up with no setup.
|
|
8
|
+
*
|
|
9
|
+
* Under an outer TooltipProvider it changes nothing: that provider's group
|
|
10
|
+
* and delays apply. Without one, it supplies the provider's defaults too, not
|
|
11
|
+
* the bare-tooltip fallback, whose 0ms close delay would make a toolbar's
|
|
12
|
+
* tooltips close before the pointer can cross into them (WCAG 1.4.13).
|
|
13
|
+
*/
|
|
14
|
+
export function TooltipWarmthScope({ children }) {
|
|
15
|
+
const outer = useTooltipProviderContext();
|
|
16
|
+
const [warmth] = useState(createHoverWarmth);
|
|
17
|
+
const [activeClose] = useState(() => ({ current: null }));
|
|
18
|
+
const value = useMemo(() => ({
|
|
19
|
+
delayDuration: outer.delayDuration,
|
|
20
|
+
closeDelayDuration: 150,
|
|
21
|
+
skipDelayDuration: 300,
|
|
22
|
+
warmth,
|
|
23
|
+
activeClose,
|
|
24
|
+
}), [outer.delayDuration, warmth, activeClose]);
|
|
25
|
+
if (outer.warmth)
|
|
26
|
+
return _jsx(_Fragment, { children: children });
|
|
27
|
+
return _jsx(TooltipProviderContext.Provider, { value: value, children: children });
|
|
28
|
+
}
|
|
@@ -20,6 +20,16 @@ export type TooltipProviderProps = {
|
|
|
20
20
|
* @default 150
|
|
21
21
|
*/
|
|
22
22
|
closeDelayDuration?: number;
|
|
23
|
+
/**
|
|
24
|
+
* Warmth window in ms. Tooltips under one provider form a group: while one
|
|
25
|
+
* is showing, or for this long after it closes, the next opens at once
|
|
26
|
+
* instead of waiting out `delayDuration`. Gliding along a toolbar, each
|
|
27
|
+
* tooltip appears immediately, one at a time. Escape cools the group: a
|
|
28
|
+
* dismissed tooltip doesn't warm the next. Toolbar provides a group
|
|
29
|
+
* automatically.
|
|
30
|
+
* @default 300
|
|
31
|
+
*/
|
|
32
|
+
skipDelayDuration?: number;
|
|
23
33
|
children: React.ReactNode;
|
|
24
34
|
};
|
|
25
35
|
export type TooltipProps = {
|
|
@@ -105,6 +115,8 @@ export type TooltipContextValue = {
|
|
|
105
115
|
handleClose: () => void;
|
|
106
116
|
/** Cancel any pending close timeout (for WCAG 1.4.13 hover persistence). */
|
|
107
117
|
cancelClose: () => void;
|
|
118
|
+
/** Close now as a dismissal (Escape): it doesn't warm the group. */
|
|
119
|
+
dismiss: () => void;
|
|
108
120
|
};
|
|
109
121
|
/**
|
|
110
122
|
* Convert side + align to Floating UI placement.
|
package/components/index.d.ts
CHANGED
|
@@ -78,7 +78,7 @@ export type { RadioGroupProps, RadioProps } from './Radio';
|
|
|
78
78
|
export type { RatingProps } from './Rating';
|
|
79
79
|
export type { SegmentedControlProps, SegmentedControlItemProps, SegmentedControlValue, SegmentedControlVariant, SegmentedControlSize, SegmentedControlOrientation, } from './SegmentedControl';
|
|
80
80
|
export type { SidebarProps, SidebarHeaderProps, SidebarFooterProps, SidebarNavProps, SidebarToggleProps, SidebarToggleLabels, } from './Sidebar';
|
|
81
|
-
export type { NavMenuProps, NavMenuOrientation, NavMenuLabels, NavMenuGroupProps, NavMenuGroupLabelElement, NavMenuItemProps, NavMenuItemAsLink, NavMenuItemAsButton, NavMenuItemAsChild, NavMenuCurrent, NavMenuSubProps, } from './NavMenu';
|
|
81
|
+
export type { NavMenuProps, NavMenuOrientation, NavMenuFlyoutTrigger, NavMenuLabels, NavMenuGroupProps, NavMenuGroupLabelElement, NavMenuItemProps, NavMenuItemAsLink, NavMenuItemAsButton, NavMenuItemAsChild, NavMenuCurrent, NavMenuSubProps, } from './NavMenu';
|
|
82
82
|
export type { StepIndicatorProps, StepStatus } from './StepIndicator';
|
|
83
83
|
export type { StepListProps, StepListItemProps } from './StepList';
|
|
84
84
|
export type { DataTableProps } from './Table';
|
package/figma-tokens.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "0.2.
|
|
2
|
+
"version": "0.2.26",
|
|
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
|
@@ -258,6 +258,8 @@ the text defaults to the panel's own fg, which is tuned for TUI's default fills.
|
|
|
258
258
|
|
|
259
259
|
**Fonts:** TUI inherits the host's font. If your page has none (you see the browser's serif), add `tui-font-sans` next to `tui-interface`, or set `--tui-font-family-base` anywhere. `--tui-font-family-sans` / `-mono` replace the stacks those utilities use.
|
|
260
260
|
|
|
261
|
+
**Tooltips warm up in groups:** wrap a set of tooltips in `TooltipProvider` (a `Toolbar` does it for you), and after the first shows, the rest appear at once while you move between them (`skipDelayDuration`, default 300ms). Don't hand-roll delay logic per tooltip.
|
|
262
|
+
|
|
261
263
|
`className` is for layout integration (margin, grid placement), not appearance. Beyond the token
|
|
262
264
|
API the sanctioned escape hatch is to **swizzle** — copy the component source and own the fork.
|
|
263
265
|
|
|
@@ -397,7 +399,7 @@ synthesise a second toggle. For a second control that toggles the same item (an
|
|
|
397
399
|
## Component names, and what isn't there
|
|
398
400
|
|
|
399
401
|
46 components. If you are hunting for a name: **Alert → `Notice`**, **Menu → `Dropdown`**,
|
|
400
|
-
**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 — 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 — don't hand-roll menus with `role="menu"`),
|
|
401
403
|
**Badge → `Chip`** or `ContentIndicator`, **Spinner → `Progress`** or `Button loading`,
|
|
402
404
|
**ConfirmDialog / AlertDialog → `Modal role="alertdialog"`** (needs both `aria-labelledby` and
|
|
403
405
|
`aria-describedby`; the backdrop stops dismissing by default, Escape still cancels; put the actions in
|