@huin-core/react-navigation-menu 1.0.2 → 1.0.3
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/index.d.mts +137 -0
- package/dist/index.d.ts +137 -0
- package/dist/index.js +1002 -0
- package/dist/index.js.map +7 -0
- package/dist/index.mjs +970 -0
- package/dist/index.mjs.map +7 -0
- package/package.json +1 -1
package/dist/index.js
ADDED
@@ -0,0 +1,1002 @@
|
|
1
|
+
"use strict";
|
2
|
+
"use client";
|
3
|
+
var __create = Object.create;
|
4
|
+
var __defProp = Object.defineProperty;
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
9
|
+
var __export = (target, all) => {
|
10
|
+
for (var name in all)
|
11
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
12
|
+
};
|
13
|
+
var __copyProps = (to, from, except, desc) => {
|
14
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
15
|
+
for (let key of __getOwnPropNames(from))
|
16
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
17
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
18
|
+
}
|
19
|
+
return to;
|
20
|
+
};
|
21
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
22
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
23
|
+
// file that has been converted to a CommonJS file using a Babel-
|
24
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
25
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
26
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
27
|
+
mod
|
28
|
+
));
|
29
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
30
|
+
|
31
|
+
// packages/react/navigation-menu/src/index.ts
|
32
|
+
var src_exports = {};
|
33
|
+
__export(src_exports, {
|
34
|
+
NavigationMenu: () => NavigationMenu,
|
35
|
+
NavigationMenuContent: () => NavigationMenuContent,
|
36
|
+
NavigationMenuIndicator: () => NavigationMenuIndicator,
|
37
|
+
NavigationMenuItem: () => NavigationMenuItem,
|
38
|
+
NavigationMenuLink: () => NavigationMenuLink,
|
39
|
+
NavigationMenuList: () => NavigationMenuList,
|
40
|
+
NavigationMenuSub: () => NavigationMenuSub,
|
41
|
+
NavigationMenuTrigger: () => NavigationMenuTrigger,
|
42
|
+
NavigationMenuViewport: () => NavigationMenuViewport,
|
43
|
+
Root: () => Root,
|
44
|
+
createNavigationMenuScope: () => createNavigationMenuScope
|
45
|
+
});
|
46
|
+
module.exports = __toCommonJS(src_exports);
|
47
|
+
|
48
|
+
// packages/react/navigation-menu/src/NavigationMenu.tsx
|
49
|
+
var React6 = __toESM(require("react"));
|
50
|
+
var import_react_context = require("@huin-core/react-context");
|
51
|
+
var import_react_primitive6 = require("@huin-core/react-primitive");
|
52
|
+
var import_react_use_controllable_state2 = require("@huin-core/react-use-controllable-state");
|
53
|
+
var import_react_compose_refs3 = require("@huin-core/react-compose-refs");
|
54
|
+
var import_react_direction = require("@huin-core/react-direction");
|
55
|
+
var import_react_collection = require("@huin-core/react-collection");
|
56
|
+
|
57
|
+
// packages/react/navigation-menu/src/NavigationMenuSub.tsx
|
58
|
+
var import_react = __toESM(require("react"));
|
59
|
+
var import_react_primitive = require("@huin-core/react-primitive");
|
60
|
+
var import_react_use_controllable_state = require("@huin-core/react-use-controllable-state");
|
61
|
+
var import_react_id = require("@huin-core/react-id");
|
62
|
+
var import_react_use_previous = require("@huin-core/react-use-previous");
|
63
|
+
var import_react_use_callback_ref = require("@huin-core/react-use-callback-ref");
|
64
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
65
|
+
var SUB_NAME = "NavigationMenuSub";
|
66
|
+
var NavigationMenuSub = import_react.default.forwardRef((props, forwardedRef) => {
|
67
|
+
const {
|
68
|
+
__scopeNavigationMenu,
|
69
|
+
value: valueProp,
|
70
|
+
onValueChange,
|
71
|
+
defaultValue,
|
72
|
+
orientation = "horizontal",
|
73
|
+
...subProps
|
74
|
+
} = props;
|
75
|
+
const context = useNavigationMenuContext(SUB_NAME, __scopeNavigationMenu);
|
76
|
+
const [value = "", setValue] = (0, import_react_use_controllable_state.useControllableState)({
|
77
|
+
prop: valueProp,
|
78
|
+
onChange: onValueChange,
|
79
|
+
defaultProp: defaultValue
|
80
|
+
});
|
81
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
82
|
+
NavigationMenuProvider,
|
83
|
+
{
|
84
|
+
scope: __scopeNavigationMenu,
|
85
|
+
isRootMenu: false,
|
86
|
+
value,
|
87
|
+
dir: context.dir,
|
88
|
+
orientation,
|
89
|
+
rootNavigationMenu: context.rootNavigationMenu,
|
90
|
+
onTriggerEnter: (itemValue) => setValue(itemValue),
|
91
|
+
onItemSelect: (itemValue) => setValue(itemValue),
|
92
|
+
onItemDismiss: () => setValue(""),
|
93
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
94
|
+
import_react_primitive.Primitive.div,
|
95
|
+
{
|
96
|
+
"data-orientation": orientation,
|
97
|
+
...subProps,
|
98
|
+
ref: forwardedRef
|
99
|
+
}
|
100
|
+
)
|
101
|
+
}
|
102
|
+
);
|
103
|
+
});
|
104
|
+
NavigationMenuSub.displayName = SUB_NAME;
|
105
|
+
var NavigationMenuProvider = (props) => {
|
106
|
+
const {
|
107
|
+
scope,
|
108
|
+
isRootMenu,
|
109
|
+
rootNavigationMenu,
|
110
|
+
dir,
|
111
|
+
orientation,
|
112
|
+
children,
|
113
|
+
value,
|
114
|
+
onItemSelect,
|
115
|
+
onItemDismiss,
|
116
|
+
onTriggerEnter,
|
117
|
+
onTriggerLeave,
|
118
|
+
onContentEnter,
|
119
|
+
onContentLeave
|
120
|
+
} = props;
|
121
|
+
const [viewport, setViewport] = import_react.default.useState(null);
|
122
|
+
const [viewportContent, setViewportContent] = import_react.default.useState(/* @__PURE__ */ new Map());
|
123
|
+
const [indicatorTrack, setIndicatorTrack] = import_react.default.useState(null);
|
124
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
125
|
+
NavigationMenuProviderImpl,
|
126
|
+
{
|
127
|
+
scope,
|
128
|
+
isRootMenu,
|
129
|
+
rootNavigationMenu,
|
130
|
+
value,
|
131
|
+
previousValue: (0, import_react_use_previous.usePrevious)(value),
|
132
|
+
baseId: (0, import_react_id.useId)(),
|
133
|
+
dir,
|
134
|
+
orientation,
|
135
|
+
viewport,
|
136
|
+
onViewportChange: setViewport,
|
137
|
+
indicatorTrack,
|
138
|
+
onIndicatorTrackChange: setIndicatorTrack,
|
139
|
+
onTriggerEnter: (0, import_react_use_callback_ref.useCallbackRef)(onTriggerEnter),
|
140
|
+
onTriggerLeave: (0, import_react_use_callback_ref.useCallbackRef)(onTriggerLeave),
|
141
|
+
onContentEnter: (0, import_react_use_callback_ref.useCallbackRef)(onContentEnter),
|
142
|
+
onContentLeave: (0, import_react_use_callback_ref.useCallbackRef)(onContentLeave),
|
143
|
+
onItemSelect: (0, import_react_use_callback_ref.useCallbackRef)(onItemSelect),
|
144
|
+
onItemDismiss: (0, import_react_use_callback_ref.useCallbackRef)(onItemDismiss),
|
145
|
+
onViewportContentChange: import_react.default.useCallback(
|
146
|
+
(contentValue, contentData) => {
|
147
|
+
setViewportContent((prevContent) => {
|
148
|
+
prevContent.set(contentValue, contentData);
|
149
|
+
return new Map(prevContent);
|
150
|
+
});
|
151
|
+
},
|
152
|
+
[]
|
153
|
+
),
|
154
|
+
onViewportContentRemove: import_react.default.useCallback((contentValue) => {
|
155
|
+
setViewportContent((prevContent) => {
|
156
|
+
if (!prevContent.has(contentValue)) return prevContent;
|
157
|
+
prevContent.delete(contentValue);
|
158
|
+
return new Map(prevContent);
|
159
|
+
});
|
160
|
+
}, []),
|
161
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Collection.Provider, { scope, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(ViewportContentProvider, { scope, items: viewportContent, children }) })
|
162
|
+
}
|
163
|
+
);
|
164
|
+
};
|
165
|
+
|
166
|
+
// packages/react/navigation-menu/src/NavigationMenuItem.tsx
|
167
|
+
var import_react5 = __toESM(require("react"));
|
168
|
+
var import_react_primitive5 = require("@huin-core/react-primitive");
|
169
|
+
var import_react_id2 = require("@huin-core/react-id");
|
170
|
+
|
171
|
+
// packages/react/navigation-menu/src/NavigationMenuLink.tsx
|
172
|
+
var import_react4 = __toESM(require("react"));
|
173
|
+
var import_react_primitive4 = require("@huin-core/react-primitive");
|
174
|
+
var import_primitive3 = require("@huin-core/primitive");
|
175
|
+
|
176
|
+
// packages/react/navigation-menu/src/NavigationMenuContent.tsx
|
177
|
+
var import_react3 = __toESM(require("react"));
|
178
|
+
var import_react_compose_refs2 = require("@huin-core/react-compose-refs");
|
179
|
+
var import_react_dismissable_layer = require("@huin-core/react-dismissable-layer");
|
180
|
+
var import_react_presence2 = require("@huin-core/react-presence");
|
181
|
+
var import_primitive2 = require("@huin-core/primitive");
|
182
|
+
var import_react_use_layout_effect2 = require("@huin-core/react-use-layout-effect");
|
183
|
+
|
184
|
+
// packages/react/navigation-menu/src/NavigationMenuViewport.tsx
|
185
|
+
var import_react2 = __toESM(require("react"));
|
186
|
+
var import_react_primitive2 = require("@huin-core/react-primitive");
|
187
|
+
var import_react_presence = require("@huin-core/react-presence");
|
188
|
+
var import_react_use_callback_ref2 = require("@huin-core/react-use-callback-ref");
|
189
|
+
var import_react_use_layout_effect = require("@huin-core/react-use-layout-effect");
|
190
|
+
var import_react_compose_refs = require("@huin-core/react-compose-refs");
|
191
|
+
var import_primitive = require("@huin-core/primitive");
|
192
|
+
var import_jsx_runtime2 = require("react/jsx-runtime");
|
193
|
+
var VIEWPORT_NAME = "NavigationMenuViewport";
|
194
|
+
var NavigationMenuViewport = import_react2.default.forwardRef((props, forwardedRef) => {
|
195
|
+
const { forceMount, ...viewportProps } = props;
|
196
|
+
const context = useNavigationMenuContext(
|
197
|
+
VIEWPORT_NAME,
|
198
|
+
props.__scopeNavigationMenu
|
199
|
+
);
|
200
|
+
const open = Boolean(context.value);
|
201
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_react_presence.Presence, { present: forceMount || open, children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(NavigationMenuViewportImpl, { ...viewportProps, ref: forwardedRef }) });
|
202
|
+
});
|
203
|
+
NavigationMenuViewport.displayName = VIEWPORT_NAME;
|
204
|
+
var NavigationMenuViewportImpl = import_react2.default.forwardRef((props, forwardedRef) => {
|
205
|
+
const { __scopeNavigationMenu, ...viewportImplProps } = props;
|
206
|
+
const context = useNavigationMenuContext(
|
207
|
+
VIEWPORT_NAME,
|
208
|
+
__scopeNavigationMenu
|
209
|
+
);
|
210
|
+
const composedRefs = (0, import_react_compose_refs.useComposedRefs)(forwardedRef, context.onViewportChange);
|
211
|
+
const viewportContentContext = useViewportContentContext(
|
212
|
+
CONTENT_NAME,
|
213
|
+
props.__scopeNavigationMenu
|
214
|
+
);
|
215
|
+
const [size, setSize] = import_react2.default.useState(null);
|
216
|
+
const [content, setContent] = import_react2.default.useState(null);
|
217
|
+
const viewportWidth = size ? size?.width + "px" : void 0;
|
218
|
+
const viewportHeight = size ? size?.height + "px" : void 0;
|
219
|
+
const open = Boolean(context.value);
|
220
|
+
const activeContentValue = open ? context.value : context.previousValue;
|
221
|
+
const handleSizeChange = () => {
|
222
|
+
if (content)
|
223
|
+
setSize({ width: content.offsetWidth, height: content.offsetHeight });
|
224
|
+
};
|
225
|
+
useResizeObserver(content, handleSizeChange);
|
226
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
|
227
|
+
import_react_primitive2.Primitive.div,
|
228
|
+
{
|
229
|
+
"data-state": getOpenState(open),
|
230
|
+
"data-orientation": context.orientation,
|
231
|
+
...viewportImplProps,
|
232
|
+
ref: composedRefs,
|
233
|
+
style: {
|
234
|
+
// Prevent interaction when animating out
|
235
|
+
pointerEvents: !open && context.isRootMenu ? "none" : void 0,
|
236
|
+
["--huin-core-navigation-menu-viewport-width"]: viewportWidth,
|
237
|
+
["--huin-core-navigation-menu-viewport-height"]: viewportHeight,
|
238
|
+
...viewportImplProps.style
|
239
|
+
},
|
240
|
+
onPointerEnter: (0, import_primitive.composeEventHandlers)(
|
241
|
+
props.onPointerEnter,
|
242
|
+
context.onContentEnter
|
243
|
+
),
|
244
|
+
onPointerLeave: (0, import_primitive.composeEventHandlers)(
|
245
|
+
props.onPointerLeave,
|
246
|
+
whenMouse(context.onContentLeave)
|
247
|
+
),
|
248
|
+
children: Array.from(viewportContentContext.items).map(
|
249
|
+
([value, { ref, forceMount, ...props2 }]) => {
|
250
|
+
const isActive = activeContentValue === value;
|
251
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_react_presence.Presence, { present: forceMount || isActive, children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
|
252
|
+
NavigationMenuContentImpl,
|
253
|
+
{
|
254
|
+
...props2,
|
255
|
+
ref: (0, import_react_compose_refs.composeRefs)(ref, (node) => {
|
256
|
+
if (isActive && node) setContent(node);
|
257
|
+
})
|
258
|
+
}
|
259
|
+
) }, value);
|
260
|
+
}
|
261
|
+
)
|
262
|
+
}
|
263
|
+
);
|
264
|
+
});
|
265
|
+
function useResizeObserver(element, onResize) {
|
266
|
+
const handleResize = (0, import_react_use_callback_ref2.useCallbackRef)(onResize);
|
267
|
+
(0, import_react_use_layout_effect.useLayoutEffect)(() => {
|
268
|
+
let rAF = 0;
|
269
|
+
if (element) {
|
270
|
+
const resizeObserver = new ResizeObserver(() => {
|
271
|
+
cancelAnimationFrame(rAF);
|
272
|
+
rAF = window.requestAnimationFrame(handleResize);
|
273
|
+
});
|
274
|
+
resizeObserver.observe(element);
|
275
|
+
return () => {
|
276
|
+
window.cancelAnimationFrame(rAF);
|
277
|
+
resizeObserver.unobserve(element);
|
278
|
+
};
|
279
|
+
}
|
280
|
+
}, [element, handleResize]);
|
281
|
+
}
|
282
|
+
function getOpenState(open) {
|
283
|
+
return open ? "open" : "closed";
|
284
|
+
}
|
285
|
+
function whenMouse(handler) {
|
286
|
+
return (event) => event.pointerType === "mouse" ? handler(event) : void 0;
|
287
|
+
}
|
288
|
+
|
289
|
+
// packages/react/navigation-menu/src/NavigationMenuContent.tsx
|
290
|
+
var import_react_primitive3 = require("@huin-core/react-primitive");
|
291
|
+
var import_jsx_runtime3 = require("react/jsx-runtime");
|
292
|
+
var CONTENT_NAME = "NavigationMenuContent";
|
293
|
+
var NavigationMenuContent = import_react3.default.forwardRef((props, forwardedRef) => {
|
294
|
+
const { forceMount, ...contentProps } = props;
|
295
|
+
const context = useNavigationMenuContext(
|
296
|
+
CONTENT_NAME,
|
297
|
+
props.__scopeNavigationMenu
|
298
|
+
);
|
299
|
+
const itemContext = useNavigationMenuItemContext(
|
300
|
+
CONTENT_NAME,
|
301
|
+
props.__scopeNavigationMenu
|
302
|
+
);
|
303
|
+
const composedRefs = (0, import_react_compose_refs2.useComposedRefs)(itemContext.contentRef, forwardedRef);
|
304
|
+
const open = itemContext.value === context.value;
|
305
|
+
const commonProps = {
|
306
|
+
value: itemContext.value,
|
307
|
+
triggerRef: itemContext.triggerRef,
|
308
|
+
focusProxyRef: itemContext.focusProxyRef,
|
309
|
+
wasEscapeCloseRef: itemContext.wasEscapeCloseRef,
|
310
|
+
onContentFocusOutside: itemContext.onContentFocusOutside,
|
311
|
+
onRootContentClose: itemContext.onRootContentClose,
|
312
|
+
...contentProps
|
313
|
+
};
|
314
|
+
return !context.viewport ? /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_react_presence2.Presence, { present: forceMount || open, children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
|
315
|
+
NavigationMenuContentImpl,
|
316
|
+
{
|
317
|
+
"data-state": getOpenState(open),
|
318
|
+
...commonProps,
|
319
|
+
ref: composedRefs,
|
320
|
+
onPointerEnter: (0, import_primitive2.composeEventHandlers)(
|
321
|
+
props.onPointerEnter,
|
322
|
+
context.onContentEnter
|
323
|
+
),
|
324
|
+
onPointerLeave: (0, import_primitive2.composeEventHandlers)(
|
325
|
+
props.onPointerLeave,
|
326
|
+
whenMouse(context.onContentLeave)
|
327
|
+
),
|
328
|
+
style: {
|
329
|
+
// Prevent interaction when animating out
|
330
|
+
pointerEvents: !open && context.isRootMenu ? "none" : void 0,
|
331
|
+
...commonProps.style
|
332
|
+
}
|
333
|
+
}
|
334
|
+
) }) : /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
|
335
|
+
ViewportContentMounter,
|
336
|
+
{
|
337
|
+
forceMount,
|
338
|
+
...commonProps,
|
339
|
+
ref: composedRefs
|
340
|
+
}
|
341
|
+
);
|
342
|
+
});
|
343
|
+
NavigationMenuContent.displayName = CONTENT_NAME;
|
344
|
+
var ViewportContentMounter = import_react3.default.forwardRef((props, forwardedRef) => {
|
345
|
+
const context = useNavigationMenuContext(
|
346
|
+
CONTENT_NAME,
|
347
|
+
props.__scopeNavigationMenu
|
348
|
+
);
|
349
|
+
const { onViewportContentChange, onViewportContentRemove } = context;
|
350
|
+
(0, import_react_use_layout_effect2.useLayoutEffect)(() => {
|
351
|
+
onViewportContentChange(props.value, {
|
352
|
+
ref: forwardedRef,
|
353
|
+
...props
|
354
|
+
});
|
355
|
+
}, [props, forwardedRef, onViewportContentChange]);
|
356
|
+
(0, import_react_use_layout_effect2.useLayoutEffect)(() => {
|
357
|
+
return () => onViewportContentRemove(props.value);
|
358
|
+
}, [props.value, onViewportContentRemove]);
|
359
|
+
return null;
|
360
|
+
});
|
361
|
+
var ROOT_CONTENT_DISMISS = "navigationMenu.rootContentDismiss";
|
362
|
+
var NavigationMenuContentImpl = import_react3.default.forwardRef((props, forwardedRef) => {
|
363
|
+
const {
|
364
|
+
__scopeNavigationMenu,
|
365
|
+
value,
|
366
|
+
triggerRef,
|
367
|
+
focusProxyRef,
|
368
|
+
wasEscapeCloseRef,
|
369
|
+
onRootContentClose,
|
370
|
+
onContentFocusOutside,
|
371
|
+
...contentProps
|
372
|
+
} = props;
|
373
|
+
const context = useNavigationMenuContext(CONTENT_NAME, __scopeNavigationMenu);
|
374
|
+
const ref = import_react3.default.useRef(null);
|
375
|
+
const composedRefs = (0, import_react_compose_refs2.useComposedRefs)(ref, forwardedRef);
|
376
|
+
const triggerId = makeTriggerId(context.baseId, value);
|
377
|
+
const contentId = makeContentId(context.baseId, value);
|
378
|
+
const getItems = useCollection(__scopeNavigationMenu);
|
379
|
+
const prevMotionAttributeRef = import_react3.default.useRef(null);
|
380
|
+
const { onItemDismiss } = context;
|
381
|
+
import_react3.default.useEffect(() => {
|
382
|
+
const content = ref.current;
|
383
|
+
if (context.isRootMenu && content) {
|
384
|
+
const handleClose = () => {
|
385
|
+
onItemDismiss();
|
386
|
+
onRootContentClose();
|
387
|
+
if (content.contains(document.activeElement))
|
388
|
+
triggerRef.current?.focus();
|
389
|
+
};
|
390
|
+
content.addEventListener(ROOT_CONTENT_DISMISS, handleClose);
|
391
|
+
return () => content.removeEventListener(ROOT_CONTENT_DISMISS, handleClose);
|
392
|
+
}
|
393
|
+
}, [
|
394
|
+
context.isRootMenu,
|
395
|
+
props.value,
|
396
|
+
triggerRef,
|
397
|
+
onItemDismiss,
|
398
|
+
onRootContentClose
|
399
|
+
]);
|
400
|
+
const motionAttribute = import_react3.default.useMemo(() => {
|
401
|
+
const items = getItems();
|
402
|
+
const values = items.map((item) => item.value);
|
403
|
+
if (context.dir === "rtl") values.reverse();
|
404
|
+
const index = values.indexOf(context.value);
|
405
|
+
const prevIndex = values.indexOf(context.previousValue);
|
406
|
+
const isSelected = value === context.value;
|
407
|
+
const wasSelected = prevIndex === values.indexOf(value);
|
408
|
+
if (!isSelected && !wasSelected) return prevMotionAttributeRef.current;
|
409
|
+
const attribute = (() => {
|
410
|
+
if (index !== prevIndex) {
|
411
|
+
if (isSelected && prevIndex !== -1)
|
412
|
+
return index > prevIndex ? "from-end" : "from-start";
|
413
|
+
if (wasSelected && index !== -1)
|
414
|
+
return index > prevIndex ? "to-start" : "to-end";
|
415
|
+
}
|
416
|
+
return null;
|
417
|
+
})();
|
418
|
+
prevMotionAttributeRef.current = attribute;
|
419
|
+
return attribute;
|
420
|
+
}, [context.previousValue, context.value, context.dir, getItems, value]);
|
421
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(FocusGroup, { asChild: true, children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
|
422
|
+
import_react_dismissable_layer.DismissableLayer,
|
423
|
+
{
|
424
|
+
id: contentId,
|
425
|
+
"aria-labelledby": triggerId,
|
426
|
+
"data-motion": motionAttribute,
|
427
|
+
"data-orientation": context.orientation,
|
428
|
+
...contentProps,
|
429
|
+
ref: composedRefs,
|
430
|
+
disableOutsidePointerEvents: false,
|
431
|
+
onDismiss: () => {
|
432
|
+
const rootContentDismissEvent = new Event(ROOT_CONTENT_DISMISS, {
|
433
|
+
bubbles: true,
|
434
|
+
cancelable: true
|
435
|
+
});
|
436
|
+
ref.current?.dispatchEvent(rootContentDismissEvent);
|
437
|
+
},
|
438
|
+
onFocusOutside: (0, import_primitive2.composeEventHandlers)(props.onFocusOutside, (event) => {
|
439
|
+
onContentFocusOutside();
|
440
|
+
const target = event.target;
|
441
|
+
if (context.rootNavigationMenu?.contains(target))
|
442
|
+
event.preventDefault();
|
443
|
+
}),
|
444
|
+
onPointerDownOutside: (0, import_primitive2.composeEventHandlers)(
|
445
|
+
props.onPointerDownOutside,
|
446
|
+
(event) => {
|
447
|
+
const target = event.target;
|
448
|
+
const isTrigger = getItems().some(
|
449
|
+
(item) => item.ref.current?.contains(target)
|
450
|
+
);
|
451
|
+
const isRootViewport = context.isRootMenu && context.viewport?.contains(target);
|
452
|
+
if (isTrigger || isRootViewport || !context.isRootMenu)
|
453
|
+
event.preventDefault();
|
454
|
+
}
|
455
|
+
),
|
456
|
+
onKeyDown: (0, import_primitive2.composeEventHandlers)(props.onKeyDown, (event) => {
|
457
|
+
const isMetaKey = event.altKey || event.ctrlKey || event.metaKey;
|
458
|
+
const isTabKey = event.key === "Tab" && !isMetaKey;
|
459
|
+
if (isTabKey) {
|
460
|
+
const candidates = getTabbableCandidates(event.currentTarget);
|
461
|
+
const focusedElement = document.activeElement;
|
462
|
+
const index = candidates.findIndex(
|
463
|
+
(candidate) => candidate === focusedElement
|
464
|
+
);
|
465
|
+
const isMovingBackwards = event.shiftKey;
|
466
|
+
const nextCandidates = isMovingBackwards ? candidates.slice(0, index).reverse() : candidates.slice(index + 1, candidates.length);
|
467
|
+
if (focusFirst(nextCandidates)) {
|
468
|
+
event.preventDefault();
|
469
|
+
} else {
|
470
|
+
focusProxyRef.current?.focus();
|
471
|
+
}
|
472
|
+
}
|
473
|
+
}),
|
474
|
+
onEscapeKeyDown: (0, import_primitive2.composeEventHandlers)(
|
475
|
+
props.onEscapeKeyDown,
|
476
|
+
(event) => {
|
477
|
+
wasEscapeCloseRef.current = true;
|
478
|
+
}
|
479
|
+
)
|
480
|
+
}
|
481
|
+
) });
|
482
|
+
});
|
483
|
+
var FOCUS_GROUP_NAME = "FocusGroup";
|
484
|
+
var FocusGroup = import_react3.default.forwardRef(
|
485
|
+
(props, forwardedRef) => {
|
486
|
+
const { __scopeNavigationMenu, ...groupProps } = props;
|
487
|
+
const context = useNavigationMenuContext(
|
488
|
+
FOCUS_GROUP_NAME,
|
489
|
+
__scopeNavigationMenu
|
490
|
+
);
|
491
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(FocusGroupCollection.Provider, { scope: __scopeNavigationMenu, children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(FocusGroupCollection.Slot, { scope: __scopeNavigationMenu, children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_react_primitive3.Primitive.div, { dir: context.dir, ...groupProps, ref: forwardedRef }) }) });
|
492
|
+
}
|
493
|
+
);
|
494
|
+
function makeTriggerId(baseId, value) {
|
495
|
+
return `${baseId}-trigger-${value}`;
|
496
|
+
}
|
497
|
+
function makeContentId(baseId, value) {
|
498
|
+
return `${baseId}-content-${value}`;
|
499
|
+
}
|
500
|
+
|
501
|
+
// packages/react/navigation-menu/src/NavigationMenuLink.tsx
|
502
|
+
var import_jsx_runtime4 = require("react/jsx-runtime");
|
503
|
+
var LINK_NAME = "NavigationMenuLink";
|
504
|
+
var LINK_SELECT = "navigationMenu.linkSelect";
|
505
|
+
var NavigationMenuLink = import_react4.default.forwardRef((props, forwardedRef) => {
|
506
|
+
const { __scopeNavigationMenu, active, onSelect, ...linkProps } = props;
|
507
|
+
return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(FocusGroupItem, { asChild: true, children: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
|
508
|
+
import_react_primitive4.Primitive.a,
|
509
|
+
{
|
510
|
+
"data-active": active ? "" : void 0,
|
511
|
+
"aria-current": active ? "page" : void 0,
|
512
|
+
...linkProps,
|
513
|
+
ref: forwardedRef,
|
514
|
+
onClick: (0, import_primitive3.composeEventHandlers)(
|
515
|
+
props.onClick,
|
516
|
+
(event) => {
|
517
|
+
const target = event.target;
|
518
|
+
const linkSelectEvent = new CustomEvent(LINK_SELECT, {
|
519
|
+
bubbles: true,
|
520
|
+
cancelable: true
|
521
|
+
});
|
522
|
+
target.addEventListener(LINK_SELECT, (event2) => onSelect?.(event2), {
|
523
|
+
once: true
|
524
|
+
});
|
525
|
+
(0, import_react_primitive4.dispatchDiscreteCustomEvent)(target, linkSelectEvent);
|
526
|
+
if (!linkSelectEvent.defaultPrevented && !event.metaKey) {
|
527
|
+
const rootContentDismissEvent = new CustomEvent(
|
528
|
+
ROOT_CONTENT_DISMISS,
|
529
|
+
{
|
530
|
+
bubbles: true,
|
531
|
+
cancelable: true
|
532
|
+
}
|
533
|
+
);
|
534
|
+
(0, import_react_primitive4.dispatchDiscreteCustomEvent)(target, rootContentDismissEvent);
|
535
|
+
}
|
536
|
+
},
|
537
|
+
{ checkForDefaultPrevented: false }
|
538
|
+
)
|
539
|
+
}
|
540
|
+
) });
|
541
|
+
});
|
542
|
+
NavigationMenuLink.displayName = LINK_NAME;
|
543
|
+
var ARROW_KEYS = ["ArrowRight", "ArrowLeft", "ArrowUp", "ArrowDown"];
|
544
|
+
var FOCUS_GROUP_ITEM_NAME = "FocusGroupItem";
|
545
|
+
var FocusGroupItem = import_react4.default.forwardRef((props, forwardedRef) => {
|
546
|
+
const { __scopeNavigationMenu, ...groupProps } = props;
|
547
|
+
const getItems = useFocusGroupCollection(__scopeNavigationMenu);
|
548
|
+
const context = useNavigationMenuContext(
|
549
|
+
FOCUS_GROUP_ITEM_NAME,
|
550
|
+
__scopeNavigationMenu
|
551
|
+
);
|
552
|
+
return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(FocusGroupCollection.ItemSlot, { scope: __scopeNavigationMenu, children: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
|
553
|
+
import_react_primitive4.Primitive.button,
|
554
|
+
{
|
555
|
+
...groupProps,
|
556
|
+
ref: forwardedRef,
|
557
|
+
onKeyDown: (0, import_primitive3.composeEventHandlers)(props.onKeyDown, (event) => {
|
558
|
+
const isFocusNavigationKey = ["Home", "End", ...ARROW_KEYS].includes(
|
559
|
+
event.key
|
560
|
+
);
|
561
|
+
if (isFocusNavigationKey) {
|
562
|
+
let candidateNodes = getItems().map((item) => item.ref.current);
|
563
|
+
const prevItemKey = context.dir === "rtl" ? "ArrowRight" : "ArrowLeft";
|
564
|
+
const prevKeys = [prevItemKey, "ArrowUp", "End"];
|
565
|
+
if (prevKeys.includes(event.key)) candidateNodes.reverse();
|
566
|
+
if (ARROW_KEYS.includes(event.key)) {
|
567
|
+
const currentIndex = candidateNodes.indexOf(event.currentTarget);
|
568
|
+
candidateNodes = candidateNodes.slice(currentIndex + 1);
|
569
|
+
}
|
570
|
+
setTimeout(() => focusFirst(candidateNodes));
|
571
|
+
event.preventDefault();
|
572
|
+
}
|
573
|
+
})
|
574
|
+
}
|
575
|
+
) });
|
576
|
+
});
|
577
|
+
function focusFirst(candidates) {
|
578
|
+
const previouslyFocusedElement = document.activeElement;
|
579
|
+
return candidates.some((candidate) => {
|
580
|
+
if (candidate === previouslyFocusedElement) return true;
|
581
|
+
candidate.focus();
|
582
|
+
return document.activeElement !== previouslyFocusedElement;
|
583
|
+
});
|
584
|
+
}
|
585
|
+
|
586
|
+
// packages/react/navigation-menu/src/NavigationMenuItem.tsx
|
587
|
+
var import_jsx_runtime5 = require("react/jsx-runtime");
|
588
|
+
var ITEM_NAME = "NavigationMenuItem";
|
589
|
+
var NavigationMenuItem = import_react5.default.forwardRef((props, forwardedRef) => {
|
590
|
+
const { __scopeNavigationMenu, value: valueProp, ...itemProps } = props;
|
591
|
+
const autoValue = (0, import_react_id2.useId)();
|
592
|
+
const value = valueProp || autoValue || "LEGACY_REACT_AUTO_VALUE";
|
593
|
+
const contentRef = import_react5.default.useRef(null);
|
594
|
+
const triggerRef = import_react5.default.useRef(null);
|
595
|
+
const focusProxyRef = import_react5.default.useRef(null);
|
596
|
+
const restoreContentTabOrderRef = import_react5.default.useRef(() => {
|
597
|
+
});
|
598
|
+
const wasEscapeCloseRef = import_react5.default.useRef(false);
|
599
|
+
const handleContentEntry = import_react5.default.useCallback((side = "start") => {
|
600
|
+
if (contentRef.current) {
|
601
|
+
restoreContentTabOrderRef.current();
|
602
|
+
const candidates = getTabbableCandidates(contentRef.current);
|
603
|
+
if (candidates.length)
|
604
|
+
focusFirst(side === "start" ? candidates : candidates.reverse());
|
605
|
+
}
|
606
|
+
}, []);
|
607
|
+
const handleContentExit = import_react5.default.useCallback(() => {
|
608
|
+
if (contentRef.current) {
|
609
|
+
const candidates = getTabbableCandidates(contentRef.current);
|
610
|
+
if (candidates.length)
|
611
|
+
restoreContentTabOrderRef.current = removeFromTabOrder(candidates);
|
612
|
+
}
|
613
|
+
}, []);
|
614
|
+
return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
|
615
|
+
NavigationMenuItemContextProvider,
|
616
|
+
{
|
617
|
+
scope: __scopeNavigationMenu,
|
618
|
+
value,
|
619
|
+
triggerRef,
|
620
|
+
contentRef,
|
621
|
+
focusProxyRef,
|
622
|
+
wasEscapeCloseRef,
|
623
|
+
onEntryKeyDown: handleContentEntry,
|
624
|
+
onFocusProxyEnter: handleContentEntry,
|
625
|
+
onRootContentClose: handleContentExit,
|
626
|
+
onContentFocusOutside: handleContentExit,
|
627
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(import_react_primitive5.Primitive.li, { ...itemProps, ref: forwardedRef })
|
628
|
+
}
|
629
|
+
);
|
630
|
+
});
|
631
|
+
NavigationMenuItem.displayName = ITEM_NAME;
|
632
|
+
function getTabbableCandidates(container) {
|
633
|
+
const nodes = [];
|
634
|
+
const walker = document.createTreeWalker(
|
635
|
+
container,
|
636
|
+
NodeFilter.SHOW_ELEMENT,
|
637
|
+
{
|
638
|
+
acceptNode: (node) => {
|
639
|
+
const isHiddenInput = node.tagName === "INPUT" && node.type === "hidden";
|
640
|
+
if (node.disabled || node.hidden || isHiddenInput)
|
641
|
+
return NodeFilter.FILTER_SKIP;
|
642
|
+
return node.tabIndex >= 0 ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP;
|
643
|
+
}
|
644
|
+
}
|
645
|
+
);
|
646
|
+
while (walker.nextNode()) nodes.push(walker.currentNode);
|
647
|
+
return nodes;
|
648
|
+
}
|
649
|
+
function removeFromTabOrder(candidates) {
|
650
|
+
candidates.forEach((candidate) => {
|
651
|
+
candidate.dataset.tabindex = candidate.getAttribute("tabindex") || "";
|
652
|
+
candidate.setAttribute("tabindex", "-1");
|
653
|
+
});
|
654
|
+
return () => {
|
655
|
+
candidates.forEach((candidate) => {
|
656
|
+
const prevTabIndex = candidate.dataset.tabindex;
|
657
|
+
candidate.setAttribute("tabindex", prevTabIndex);
|
658
|
+
});
|
659
|
+
};
|
660
|
+
}
|
661
|
+
|
662
|
+
// packages/react/navigation-menu/src/NavigationMenu.tsx
|
663
|
+
var import_jsx_runtime6 = require("react/jsx-runtime");
|
664
|
+
var NAVIGATION_MENU_NAME = "NavigationMenu";
|
665
|
+
var [Collection, useCollection, createCollectionScope] = (0, import_react_collection.createCollection)(
|
666
|
+
NAVIGATION_MENU_NAME
|
667
|
+
);
|
668
|
+
var [
|
669
|
+
FocusGroupCollection,
|
670
|
+
useFocusGroupCollection,
|
671
|
+
createFocusGroupCollectionScope
|
672
|
+
] = (0, import_react_collection.createCollection)(NAVIGATION_MENU_NAME);
|
673
|
+
var [createNavigationMenuContext, createNavigationMenuScope] = (0, import_react_context.createContextScope)(NAVIGATION_MENU_NAME, [
|
674
|
+
createCollectionScope,
|
675
|
+
createFocusGroupCollectionScope
|
676
|
+
]);
|
677
|
+
var [NavigationMenuItemContextProvider, useNavigationMenuItemContext] = createNavigationMenuContext(ITEM_NAME);
|
678
|
+
var [NavigationMenuProviderImpl, useNavigationMenuContext] = createNavigationMenuContext(NAVIGATION_MENU_NAME);
|
679
|
+
var [ViewportContentProvider, useViewportContentContext] = createNavigationMenuContext(NAVIGATION_MENU_NAME);
|
680
|
+
var NavigationMenu = React6.forwardRef((props, forwardedRef) => {
|
681
|
+
const {
|
682
|
+
__scopeNavigationMenu,
|
683
|
+
value: valueProp,
|
684
|
+
onValueChange,
|
685
|
+
defaultValue,
|
686
|
+
delayDuration = 200,
|
687
|
+
skipDelayDuration = 300,
|
688
|
+
orientation = "horizontal",
|
689
|
+
dir,
|
690
|
+
...NavigationMenuProps
|
691
|
+
} = props;
|
692
|
+
const [navigationMenu, setNavigationMenu] = React6.useState(null);
|
693
|
+
const composedRef = (0, import_react_compose_refs3.useComposedRefs)(
|
694
|
+
forwardedRef,
|
695
|
+
(node) => setNavigationMenu(node)
|
696
|
+
);
|
697
|
+
const direction = (0, import_react_direction.useDirection)(dir);
|
698
|
+
const openTimerRef = React6.useRef(0);
|
699
|
+
const closeTimerRef = React6.useRef(0);
|
700
|
+
const skipDelayTimerRef = React6.useRef(0);
|
701
|
+
const [isOpenDelayed, setIsOpenDelayed] = React6.useState(true);
|
702
|
+
const [value = "", setValue] = (0, import_react_use_controllable_state2.useControllableState)({
|
703
|
+
prop: valueProp,
|
704
|
+
onChange: (value2) => {
|
705
|
+
const isOpen = value2 !== "";
|
706
|
+
const hasSkipDelayDuration = skipDelayDuration > 0;
|
707
|
+
if (isOpen) {
|
708
|
+
window.clearTimeout(skipDelayTimerRef.current);
|
709
|
+
if (hasSkipDelayDuration) setIsOpenDelayed(false);
|
710
|
+
} else {
|
711
|
+
window.clearTimeout(skipDelayTimerRef.current);
|
712
|
+
skipDelayTimerRef.current = window.setTimeout(
|
713
|
+
() => setIsOpenDelayed(true),
|
714
|
+
skipDelayDuration
|
715
|
+
);
|
716
|
+
}
|
717
|
+
onValueChange?.(value2);
|
718
|
+
},
|
719
|
+
defaultProp: defaultValue
|
720
|
+
});
|
721
|
+
const startCloseTimer = React6.useCallback(() => {
|
722
|
+
window.clearTimeout(closeTimerRef.current);
|
723
|
+
closeTimerRef.current = window.setTimeout(() => setValue(""), 150);
|
724
|
+
}, [setValue]);
|
725
|
+
const handleOpen = React6.useCallback(
|
726
|
+
(itemValue) => {
|
727
|
+
window.clearTimeout(closeTimerRef.current);
|
728
|
+
setValue(itemValue);
|
729
|
+
},
|
730
|
+
[setValue]
|
731
|
+
);
|
732
|
+
const handleDelayedOpen = React6.useCallback(
|
733
|
+
(itemValue) => {
|
734
|
+
const isOpenItem = value === itemValue;
|
735
|
+
if (isOpenItem) {
|
736
|
+
window.clearTimeout(closeTimerRef.current);
|
737
|
+
} else {
|
738
|
+
openTimerRef.current = window.setTimeout(() => {
|
739
|
+
window.clearTimeout(closeTimerRef.current);
|
740
|
+
setValue(itemValue);
|
741
|
+
}, delayDuration);
|
742
|
+
}
|
743
|
+
},
|
744
|
+
[value, setValue, delayDuration]
|
745
|
+
);
|
746
|
+
React6.useEffect(() => {
|
747
|
+
return () => {
|
748
|
+
window.clearTimeout(openTimerRef.current);
|
749
|
+
window.clearTimeout(closeTimerRef.current);
|
750
|
+
window.clearTimeout(skipDelayTimerRef.current);
|
751
|
+
};
|
752
|
+
}, []);
|
753
|
+
return /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
|
754
|
+
NavigationMenuProvider,
|
755
|
+
{
|
756
|
+
scope: __scopeNavigationMenu,
|
757
|
+
isRootMenu: true,
|
758
|
+
value,
|
759
|
+
dir: direction,
|
760
|
+
orientation,
|
761
|
+
rootNavigationMenu: navigationMenu,
|
762
|
+
onTriggerEnter: (itemValue) => {
|
763
|
+
window.clearTimeout(openTimerRef.current);
|
764
|
+
if (isOpenDelayed) handleDelayedOpen(itemValue);
|
765
|
+
else handleOpen(itemValue);
|
766
|
+
},
|
767
|
+
onTriggerLeave: () => {
|
768
|
+
window.clearTimeout(openTimerRef.current);
|
769
|
+
startCloseTimer();
|
770
|
+
},
|
771
|
+
onContentEnter: () => window.clearTimeout(closeTimerRef.current),
|
772
|
+
onContentLeave: startCloseTimer,
|
773
|
+
onItemSelect: (itemValue) => {
|
774
|
+
setValue((prevValue) => prevValue === itemValue ? "" : itemValue);
|
775
|
+
},
|
776
|
+
onItemDismiss: () => setValue(""),
|
777
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
|
778
|
+
import_react_primitive6.Primitive.nav,
|
779
|
+
{
|
780
|
+
"aria-label": "Main",
|
781
|
+
"data-orientation": orientation,
|
782
|
+
dir: direction,
|
783
|
+
...NavigationMenuProps,
|
784
|
+
ref: composedRef
|
785
|
+
}
|
786
|
+
)
|
787
|
+
}
|
788
|
+
);
|
789
|
+
});
|
790
|
+
NavigationMenu.displayName = NAVIGATION_MENU_NAME;
|
791
|
+
var Root = NavigationMenu;
|
792
|
+
|
793
|
+
// packages/react/navigation-menu/src/NavigationMenuList.tsx
|
794
|
+
var import_react6 = __toESM(require("react"));
|
795
|
+
var import_react_primitive7 = require("@huin-core/react-primitive");
|
796
|
+
var import_jsx_runtime7 = require("react/jsx-runtime");
|
797
|
+
var LIST_NAME = "NavigationMenuList";
|
798
|
+
var NavigationMenuList = import_react6.default.forwardRef((props, forwardedRef) => {
|
799
|
+
const { __scopeNavigationMenu, ...listProps } = props;
|
800
|
+
const context = useNavigationMenuContext(LIST_NAME, __scopeNavigationMenu);
|
801
|
+
const list = /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(
|
802
|
+
import_react_primitive7.Primitive.ul,
|
803
|
+
{
|
804
|
+
"data-orientation": context.orientation,
|
805
|
+
...listProps,
|
806
|
+
ref: forwardedRef
|
807
|
+
}
|
808
|
+
);
|
809
|
+
return /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(
|
810
|
+
import_react_primitive7.Primitive.div,
|
811
|
+
{
|
812
|
+
style: { position: "relative" },
|
813
|
+
ref: context.onIndicatorTrackChange,
|
814
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(Collection.Slot, { scope: __scopeNavigationMenu, children: context.isRootMenu ? /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(FocusGroup, { asChild: true, children: list }) : list })
|
815
|
+
}
|
816
|
+
);
|
817
|
+
});
|
818
|
+
NavigationMenuList.displayName = LIST_NAME;
|
819
|
+
|
820
|
+
// packages/react/navigation-menu/src/NavigationMenuTrigger.tsx
|
821
|
+
var import_react7 = __toESM(require("react"));
|
822
|
+
var import_react_primitive8 = require("@huin-core/react-primitive");
|
823
|
+
var import_react_compose_refs4 = require("@huin-core/react-compose-refs");
|
824
|
+
var import_primitive4 = require("@huin-core/primitive");
|
825
|
+
var VisuallyHiddenPrimitive = __toESM(require("@huin-core/react-visually-hidden"));
|
826
|
+
var import_jsx_runtime8 = require("react/jsx-runtime");
|
827
|
+
var TRIGGER_NAME = "NavigationMenuTrigger";
|
828
|
+
var NavigationMenuTrigger = import_react7.default.forwardRef((props, forwardedRef) => {
|
829
|
+
const { __scopeNavigationMenu, disabled, ...triggerProps } = props;
|
830
|
+
const context = useNavigationMenuContext(
|
831
|
+
TRIGGER_NAME,
|
832
|
+
props.__scopeNavigationMenu
|
833
|
+
);
|
834
|
+
const itemContext = useNavigationMenuItemContext(
|
835
|
+
TRIGGER_NAME,
|
836
|
+
props.__scopeNavigationMenu
|
837
|
+
);
|
838
|
+
const ref = import_react7.default.useRef(null);
|
839
|
+
const composedRefs = (0, import_react_compose_refs4.useComposedRefs)(
|
840
|
+
ref,
|
841
|
+
itemContext.triggerRef,
|
842
|
+
forwardedRef
|
843
|
+
);
|
844
|
+
const triggerId = makeTriggerId(context.baseId, itemContext.value);
|
845
|
+
const contentId = makeContentId(context.baseId, itemContext.value);
|
846
|
+
const hasPointerMoveOpenedRef = import_react7.default.useRef(false);
|
847
|
+
const wasClickCloseRef = import_react7.default.useRef(false);
|
848
|
+
const open = itemContext.value === context.value;
|
849
|
+
return /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)(import_jsx_runtime8.Fragment, { children: [
|
850
|
+
/* @__PURE__ */ (0, import_jsx_runtime8.jsx)(
|
851
|
+
Collection.ItemSlot,
|
852
|
+
{
|
853
|
+
scope: __scopeNavigationMenu,
|
854
|
+
value: itemContext.value,
|
855
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(FocusGroupItem, { asChild: true, children: /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(
|
856
|
+
import_react_primitive8.Primitive.button,
|
857
|
+
{
|
858
|
+
id: triggerId,
|
859
|
+
disabled,
|
860
|
+
"data-disabled": disabled ? "" : void 0,
|
861
|
+
"data-state": getOpenState(open),
|
862
|
+
"aria-expanded": open,
|
863
|
+
"aria-controls": contentId,
|
864
|
+
...triggerProps,
|
865
|
+
ref: composedRefs,
|
866
|
+
onPointerEnter: (0, import_primitive4.composeEventHandlers)(props.onPointerEnter, () => {
|
867
|
+
wasClickCloseRef.current = false;
|
868
|
+
itemContext.wasEscapeCloseRef.current = false;
|
869
|
+
}),
|
870
|
+
onPointerMove: (0, import_primitive4.composeEventHandlers)(
|
871
|
+
props.onPointerMove,
|
872
|
+
whenMouse(() => {
|
873
|
+
if (disabled || wasClickCloseRef.current || itemContext.wasEscapeCloseRef.current || hasPointerMoveOpenedRef.current)
|
874
|
+
return;
|
875
|
+
context.onTriggerEnter(itemContext.value);
|
876
|
+
hasPointerMoveOpenedRef.current = true;
|
877
|
+
})
|
878
|
+
),
|
879
|
+
onPointerLeave: (0, import_primitive4.composeEventHandlers)(
|
880
|
+
props.onPointerLeave,
|
881
|
+
whenMouse(() => {
|
882
|
+
if (disabled) return;
|
883
|
+
context.onTriggerLeave();
|
884
|
+
hasPointerMoveOpenedRef.current = false;
|
885
|
+
})
|
886
|
+
),
|
887
|
+
onClick: (0, import_primitive4.composeEventHandlers)(props.onClick, () => {
|
888
|
+
context.onItemSelect(itemContext.value);
|
889
|
+
wasClickCloseRef.current = open;
|
890
|
+
}),
|
891
|
+
onKeyDown: (0, import_primitive4.composeEventHandlers)(props.onKeyDown, (event) => {
|
892
|
+
const verticalEntryKey = context.dir === "rtl" ? "ArrowLeft" : "ArrowRight";
|
893
|
+
const entryKey = {
|
894
|
+
horizontal: "ArrowDown",
|
895
|
+
vertical: verticalEntryKey
|
896
|
+
}[context.orientation];
|
897
|
+
if (open && event.key === entryKey) {
|
898
|
+
itemContext.onEntryKeyDown();
|
899
|
+
event.preventDefault();
|
900
|
+
}
|
901
|
+
})
|
902
|
+
}
|
903
|
+
) })
|
904
|
+
}
|
905
|
+
),
|
906
|
+
open && /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)(import_jsx_runtime8.Fragment, { children: [
|
907
|
+
/* @__PURE__ */ (0, import_jsx_runtime8.jsx)(
|
908
|
+
VisuallyHiddenPrimitive.Root,
|
909
|
+
{
|
910
|
+
"aria-hidden": true,
|
911
|
+
tabIndex: 0,
|
912
|
+
ref: itemContext.focusProxyRef,
|
913
|
+
onFocus: (event) => {
|
914
|
+
const content = itemContext.contentRef.current;
|
915
|
+
const prevFocusedElement = event.relatedTarget;
|
916
|
+
const wasTriggerFocused = prevFocusedElement === ref.current;
|
917
|
+
const wasFocusFromContent = content?.contains(prevFocusedElement);
|
918
|
+
if (wasTriggerFocused || !wasFocusFromContent) {
|
919
|
+
itemContext.onFocusProxyEnter(
|
920
|
+
wasTriggerFocused ? "start" : "end"
|
921
|
+
);
|
922
|
+
}
|
923
|
+
}
|
924
|
+
}
|
925
|
+
),
|
926
|
+
context.viewport && /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("span", { "aria-owns": contentId })
|
927
|
+
] })
|
928
|
+
] });
|
929
|
+
});
|
930
|
+
NavigationMenuTrigger.displayName = TRIGGER_NAME;
|
931
|
+
|
932
|
+
// packages/react/navigation-menu/src/NavigationMenuIndicator.tsx
|
933
|
+
var import_react8 = __toESM(require("react"));
|
934
|
+
var import_react_presence3 = require("@huin-core/react-presence");
|
935
|
+
var import_react_dom = __toESM(require("react-dom"));
|
936
|
+
var import_react_primitive9 = require("@huin-core/react-primitive");
|
937
|
+
var import_jsx_runtime9 = require("react/jsx-runtime");
|
938
|
+
var INDICATOR_NAME = "NavigationMenuIndicator";
|
939
|
+
var NavigationMenuIndicator = import_react8.default.forwardRef((props, forwardedRef) => {
|
940
|
+
const { forceMount, ...indicatorProps } = props;
|
941
|
+
const context = useNavigationMenuContext(
|
942
|
+
INDICATOR_NAME,
|
943
|
+
props.__scopeNavigationMenu
|
944
|
+
);
|
945
|
+
const isVisible = Boolean(context.value);
|
946
|
+
return context.indicatorTrack ? import_react_dom.default.createPortal(
|
947
|
+
/* @__PURE__ */ (0, import_jsx_runtime9.jsx)(import_react_presence3.Presence, { present: forceMount || isVisible, children: /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(NavigationMenuIndicatorImpl, { ...indicatorProps, ref: forwardedRef }) }),
|
948
|
+
context.indicatorTrack
|
949
|
+
) : null;
|
950
|
+
});
|
951
|
+
NavigationMenuIndicator.displayName = INDICATOR_NAME;
|
952
|
+
var NavigationMenuIndicatorImpl = import_react8.default.forwardRef((props, forwardedRef) => {
|
953
|
+
const { __scopeNavigationMenu, ...indicatorProps } = props;
|
954
|
+
const context = useNavigationMenuContext(
|
955
|
+
INDICATOR_NAME,
|
956
|
+
__scopeNavigationMenu
|
957
|
+
);
|
958
|
+
const getItems = useCollection(__scopeNavigationMenu);
|
959
|
+
const [activeTrigger, setActiveTrigger] = import_react8.default.useState(null);
|
960
|
+
const [position, setPosition] = import_react8.default.useState(null);
|
961
|
+
const isHorizontal = context.orientation === "horizontal";
|
962
|
+
const isVisible = Boolean(context.value);
|
963
|
+
import_react8.default.useEffect(() => {
|
964
|
+
const items = getItems();
|
965
|
+
const triggerNode = items.find((item) => item.value === context.value)?.ref.current;
|
966
|
+
if (triggerNode) setActiveTrigger(triggerNode);
|
967
|
+
}, [getItems, context.value]);
|
968
|
+
const handlePositionChange = () => {
|
969
|
+
if (activeTrigger) {
|
970
|
+
setPosition({
|
971
|
+
size: isHorizontal ? activeTrigger.offsetWidth : activeTrigger.offsetHeight,
|
972
|
+
offset: isHorizontal ? activeTrigger.offsetLeft : activeTrigger.offsetTop
|
973
|
+
});
|
974
|
+
}
|
975
|
+
};
|
976
|
+
useResizeObserver(activeTrigger, handlePositionChange);
|
977
|
+
useResizeObserver(context.indicatorTrack, handlePositionChange);
|
978
|
+
return position ? /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(
|
979
|
+
import_react_primitive9.Primitive.div,
|
980
|
+
{
|
981
|
+
"aria-hidden": true,
|
982
|
+
"data-state": isVisible ? "visible" : "hidden",
|
983
|
+
"data-orientation": context.orientation,
|
984
|
+
...indicatorProps,
|
985
|
+
ref: forwardedRef,
|
986
|
+
style: {
|
987
|
+
position: "absolute",
|
988
|
+
...isHorizontal ? {
|
989
|
+
left: 0,
|
990
|
+
width: position.size + "px",
|
991
|
+
transform: `translateX(${position.offset}px)`
|
992
|
+
} : {
|
993
|
+
top: 0,
|
994
|
+
height: position.size + "px",
|
995
|
+
transform: `translateY(${position.offset}px)`
|
996
|
+
},
|
997
|
+
...indicatorProps.style
|
998
|
+
}
|
999
|
+
}
|
1000
|
+
) : null;
|
1001
|
+
});
|
1002
|
+
//# sourceMappingURL=index.js.map
|