Skip to content

Commit

Permalink
Don't close when dragging outside submenu
Browse files Browse the repository at this point in the history
  • Loading branch information
devongovett committed Nov 27, 2024
1 parent b776da9 commit 8bc9880
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
10 changes: 0 additions & 10 deletions packages/@react-spectrum/menu/src/Menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {mergeProps, useLayoutEffect, useSlotId, useSyncRef} from '@react-aria/ut
import React, {ReactElement, useContext, useEffect, useRef, useState} from 'react';
import {SpectrumMenuProps} from '@react-types/menu';
import styles from '@adobe/spectrum-css-temp/components/menu/vars.css';
import {useInteractOutside} from '@react-aria/interactions';
import {useLocale, useLocalizedStringFormatter} from '@react-aria/i18n';
import {useMenu} from '@react-aria/menu';
import {useTreeState} from '@react-stately/tree';
Expand Down Expand Up @@ -70,15 +69,6 @@ export const Menu = React.forwardRef(function Menu<T extends object>(props: Spec
let nextMenuLevel = state.collection.getItem(nextMenuLevelKey);
hasOpenSubmenu = nextMenuLevel != null;
}
useInteractOutside({
ref: domRef,
onInteractOutside: (e) => {
if (!popoverContainer?.contains(e.target as Node) && !trayContainerRef.current?.contains(e.target as Node)) {
rootMenuTriggerState?.close();
}
},
isDisabled: isSubmenu || !hasOpenSubmenu
});

return (
<MenuStateContext.Provider value={{popoverContainer, trayContainerRef, menu: domRef, submenu: submenuRef, rootMenuTriggerState, state}}>
Expand Down
18 changes: 15 additions & 3 deletions packages/@react-spectrum/menu/src/MenuTrigger.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
* governing permissions and limitations under the License.
*/

import {classNames, SlotProvider, useDOMRef, useIsMobileDevice} from '@react-spectrum/utils';
import {classNames, SlotProvider, unwrapDOMRef, useDOMRef, useIsMobileDevice} from '@react-spectrum/utils';
import {DOMRef} from '@react-types/shared';
import {MenuContext} from './context';
import {Placement} from '@react-types/overlays';
import {Popover, Tray} from '@react-spectrum/overlays';
import {PressResponder} from '@react-aria/interactions';
import {PressResponder, useInteractOutside} from '@react-aria/interactions';
import React, {forwardRef, Fragment, useRef} from 'react';
import {SpectrumMenuTriggerProps} from '@react-types/menu';
import styles from '@adobe/spectrum-css-temp/components/menu/vars.css';
Expand Down Expand Up @@ -74,17 +74,29 @@ export const MenuTrigger = forwardRef(function MenuTrigger(props: SpectrumMenuTr
state
};

// Close when clicking outside the root menu when a submenu is open.
let rootOverlayRef = useRef(null);
let rootOverlayDomRef = unwrapDOMRef(rootOverlayRef);
useInteractOutside({
ref: rootOverlayDomRef,
onInteractOutside: () => {
state?.close();
},
isDisabled: !state.isOpen || state.expandedKeysStack.length === 0
});

// On small screen devices, the menu is rendered in a tray, otherwise a popover.
let overlay;
if (isMobile) {
overlay = (
<Tray state={state} isFixedHeight>
<Tray state={state} isFixedHeight ref={rootOverlayRef}>
{menu}
</Tray>
);
} else {
overlay = (
<Popover
ref={rootOverlayRef}
UNSAFE_style={{clipPath: 'unset', overflow: 'visible', filter: 'unset', borderWidth: '0px'}}
state={state}
triggerRef={menuTriggerRef}
Expand Down

0 comments on commit 8bc9880

Please sign in to comment.