MCPcopy Create free account
hub / github.com/adobe/react-spectrum / MenuTrigger

Function MenuTrigger

packages/@react-spectrum/s2/src/Menu.tsx:969–1041  ·  view source on GitHub ↗

* The MenuTrigger serves as a wrapper around a Menu and its associated trigger, * linking the Menu's open state with the trigger's press state.

(props: MenuTriggerProps)

Source from the content-addressed store, hash-verified

967 * linking the Menu's open state with the trigger's press state.
968 */
969function MenuTrigger(props: MenuTriggerProps): ReactNode {
970 // RAC sets isPressed via PressResponder when the menu is open.
971 // We don't want press scaling to appear to get "stuck", so override this.
972 // For mouse interactions, menus open on press start. When the popover underlay appears
973 // it covers the trigger button, causing onPressEnd to fire immediately and no press scaling
974 // to occur. We override this by listening for pointerup on the document ourselves.
975 let [isPressed, setPressed] = useState(false);
976 let {addGlobalListener} = useGlobalListeners();
977 let onPressStart = (e: PressEvent) => {
978 if (e.pointerType !== 'mouse') {
979 return;
980 }
981 setPressed(true);
982 addGlobalListener(
983 document,
984 'pointerup',
985 () => {
986 setPressed(false);
987 },
988 {once: true, capture: true}
989 );
990 };
991
992 let {align = 'start', direction = 'bottom', shouldFlip, trigger = 'press'} = props;
993 let placement: Placement;
994 switch (direction) {
995 case 'left':
996 case 'right':
997 case 'start':
998 case 'end':
999 placement = `${direction} ${align === 'end' ? 'bottom' : 'top'}` as Placement;
1000 break;
1001 case 'bottom':
1002 case 'top':
1003 default:
1004 placement = `${direction} ${align}` as Placement;
1005 }
1006 let holdAffordance = trigger === 'longPress';
1007 let actionButtonContext = useSlottedContext(ActionButtonContext) || {};
1008 let toggleButtonContext = useSlottedContext(ToggleButtonContext) || {};
1009
1010 return (
1011 <Provider
1012 values={[
1013 [ActionButtonContext, {...actionButtonContext, holdAffordance}],
1014 [ToggleButtonContext, {...toggleButtonContext, holdAffordance}]
1015 ]}>
1016 <InternalMenuTriggerContext.Provider
1017 value={{
1018 align: props.align,
1019 direction: props.direction,
1020 shouldFlip: props.shouldFlip
1021 }}>
1022 <PopoverContext.Provider
1023 value={{
1024 hideArrow: true,
1025 offset: props.trigger === 'contextMenu' ? 0 : 8,
1026 crossOffset: 0,

Callers

nothing calls this directly

Calls 2

useGlobalListenersFunction · 0.90
useSlottedContextFunction · 0.85

Tested by

no test coverage detected