-
-
Notifications
You must be signed in to change notification settings - Fork 470
Expand file tree
/
Copy pathMenuSubmenuTrigger.tsx
More file actions
244 lines (218 loc) · 7.64 KB
/
Copy pathMenuSubmenuTrigger.tsx
File metadata and controls
244 lines (218 loc) · 7.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
'use client';
import * as React from 'react';
import { isElementDisabled } from '@base-ui/utils/isElementDisabled';
import { useIsoLayoutEffect } from '@base-ui/utils/useIsoLayoutEffect';
import { warn } from '@base-ui/utils/warn';
import { SafeReact } from '@base-ui/utils/safeReact';
import { EMPTY_OBJECT } from '@base-ui/utils/empty';
import { safePolygon, useClick, useHoverReferenceInteraction } from '../../floating-ui-react';
import { BaseUIComponentProps, NonNativeButtonProps } from '../../internals/types';
import { useMenuRootContext } from '../root/MenuRootContext';
import { useBaseUiId } from '../../internals/useBaseUiId';
import { triggerOpenStateMapping } from '../../utils/popupStateMapping';
import { useCompositeListItem } from '../../internals/composite/list/useCompositeListItem';
import { useMenuItem } from '../item/useMenuItem';
import { useRenderElement } from '../../internals/useRenderElement';
import { useMenuPositionerContext } from '../positioner/MenuPositionerContext';
import { useTriggerRegistration } from '../../utils/popups';
import { useMenuSubmenuRootContext } from '../submenu-root/MenuSubmenuRootContext';
/**
* A menu item that opens a submenu.
* Renders a `<div>` element.
*
* Documentation: [Base UI Menu](https://base-ui.com/react/components/menu)
*/
export const MenuSubmenuTrigger = React.forwardRef(function MenuSubmenuTrigger(
componentProps: MenuSubmenuTrigger.Props,
forwardedRef: React.ForwardedRef<HTMLElement>,
) {
const {
render,
className,
style,
label,
id: idProp,
nativeButton = false,
openOnHover = true,
delay = 100,
closeDelay = 0,
disabled: disabledProp = false,
...elementProps
} = componentProps;
const listItem = useCompositeListItem({ label });
const menuPositionerContext = useMenuPositionerContext();
const { store } = useMenuRootContext();
const thisTriggerId = useBaseUiId(idProp);
const open = store.useState('open');
const floatingRootContext = store.useState('floatingRootContext');
const floatingTreeRoot = store.useState('floatingTreeRoot');
const popupId = store.useState('triggerPopupId', thisTriggerId);
const baseRegisterTrigger = useTriggerRegistration(thisTriggerId, store);
const registerTrigger = React.useCallback(
(element: Element | null) => {
const cleanup = baseRegisterTrigger(element);
if (element !== null && store.select('open') && store.select('activeTriggerId') == null) {
store.update({
activeTriggerId: thisTriggerId,
activeTriggerElement: element,
closeDelay,
});
}
return cleanup;
},
[baseRegisterTrigger, closeDelay, store, thisTriggerId],
);
const triggerElementRef = React.useRef<HTMLElement | null>(null);
const handleTriggerElementRef = React.useCallback(
(el: HTMLElement | null) => {
triggerElementRef.current = el;
store.set('activeTriggerElement', el);
},
[store],
);
const submenuRootContext = useMenuSubmenuRootContext();
if (!submenuRootContext?.parentMenu) {
throw new Error('Base UI: <Menu.SubmenuTrigger> must be placed in <Menu.SubmenuRoot>.');
}
store.useSyncedValue('closeDelay', closeDelay);
const parentMenuStore = submenuRootContext.parentMenu;
const rootDisabled = store.useState('disabled');
const parentDisabled = parentMenuStore.useState('disabled');
const disabled = disabledProp || rootDisabled || parentDisabled;
if (process.env.NODE_ENV !== 'production') {
// eslint-disable-next-line react-hooks/rules-of-hooks
useIsoLayoutEffect(() => {
const element = triggerElementRef.current;
if (element && isElementDisabled(element) && !disabled) {
const ownerStackMessage = SafeReact.captureOwnerStack?.() || '';
warn(
`A disabled element was detected on <Menu.SubmenuTrigger>. To properly disable the trigger, use the \`disabled\` prop on the component instead of setting it on the rendered element.${ownerStackMessage}`,
);
}
});
}
const itemProps = parentMenuStore.useState('itemProps');
const highlighted = parentMenuStore.useState('isActive', listItem.index);
const itemMetadata = React.useMemo(
() => ({
type: 'submenu-trigger' as const,
setActive() {
if (parentMenuStore.select('highlightItemOnHover')) {
parentMenuStore.set('activeIndex', listItem.index);
}
},
}),
[parentMenuStore, listItem.index],
);
const { getItemProps, itemRef } = useMenuItem({
closeOnClick: false,
disabled,
highlighted,
id: thisTriggerId,
store,
typingRef: parentMenuStore.context.typingRef,
nativeButton,
itemMetadata,
nodeId: menuPositionerContext?.context.nodeId,
});
const hoverEnabled = store.useState('hoverEnabled');
const hoverProps = useHoverReferenceInteraction(floatingRootContext, {
enabled: hoverEnabled && openOnHover && !disabled,
handleClose: safePolygon({ blockPointerEvents: true }),
mouseOnly: true,
move: true,
restMs: delay,
delay: { open: delay, close: closeDelay },
shouldOpen: delay > 0 ? () => parentMenuStore.select('allowMouseEnter') : undefined,
triggerElementRef,
externalTree: floatingTreeRoot,
isClosing: () => store.select('transitionStatus') === 'ending',
});
const click = useClick(floatingRootContext, {
enabled: !disabled,
event: 'mousedown',
toggle: !openOnHover,
ignoreMouse: openOnHover,
stickIfOpen: false,
});
const localInteractionProps = click.reference ?? EMPTY_OBJECT;
const rootTriggerProps = store.useState('triggerProps', true);
const state: MenuSubmenuTriggerState = { disabled, highlighted, open };
const element = useRenderElement('div', componentProps, {
state,
stateAttributesMapping: triggerOpenStateMapping,
props: [
localInteractionProps,
hoverProps,
rootTriggerProps,
itemProps,
{
'aria-controls': popupId,
tabIndex: open || highlighted ? 0 : -1,
onBlur() {
if (highlighted) {
parentMenuStore.set('activeIndex', null);
}
},
},
elementProps,
getItemProps,
],
ref: [forwardedRef, listItem.ref, itemRef, registerTrigger, handleTriggerElementRef],
});
return element;
});
export interface MenuSubmenuTriggerState {
/**
* Whether the component should ignore user interaction.
*/
disabled: boolean;
/**
* Whether the item is highlighted.
*/
highlighted: boolean;
/**
* Whether the menu is currently open.
*/
open: boolean;
}
export interface MenuSubmenuTriggerProps
extends NonNativeButtonProps, BaseUIComponentProps<'div', MenuSubmenuTriggerState> {
onClick?: BaseUIComponentProps<'div', MenuSubmenuTriggerState>['onClick'] | undefined;
/**
* Overrides the text label to use when the item is matched during keyboard text navigation.
*/
label?: string | undefined;
/**
* @ignore
*/
id?: string | undefined;
/**
* Whether the component should ignore user interaction.
* @default false
*/
disabled?: boolean | undefined;
/**
* How long to wait before the menu may be opened on hover. Specified in milliseconds.
*
* Requires the `openOnHover` prop.
* @default 100
*/
delay?: number | undefined;
/**
* How long to wait before closing the menu that was opened on hover.
* Specified in milliseconds.
*
* Requires the `openOnHover` prop.
* @default 0
*/
closeDelay?: number | undefined;
/**
* Whether the menu should also open when the trigger is hovered.
*/
openOnHover?: boolean | undefined;
}
export namespace MenuSubmenuTrigger {
export type Props = MenuSubmenuTriggerProps;
export type State = MenuSubmenuTriggerState;
}