Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug/select issue #3313

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 48 additions & 27 deletions packages/react/dropdown-menu/src/DropdownMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ const DropdownMenuContent = React.forwardRef<DropdownMenuContentElement, Dropdow
if (!context.modal || isRightClick) hasInteractedOutsideRef.current = true;
})}
style={{
...contentProps.style,
...props.style,
// re-namespace exposed content custom properties
...{
Expand Down Expand Up @@ -421,6 +422,16 @@ const DropdownMenuSub: React.FC<DropdownMenuSubProps> = (
onChange: onOpenChange,
});

useEffect(() => {
// Close submenu on window focus
const handleWindowFocus = () => setOpen(false);

window.addEventListener('focus', handleWindowFocus);
return () => {
window.removeEventListener('focus', handleWindowFocus);
};
}, [setOpen]);

return (
<MenuPrimitive.Sub {...menuScope} open={open} onOpenChange={setOpen}>
{children}
Expand Down Expand Up @@ -455,36 +466,40 @@ DropdownMenuSubTrigger.displayName = SUB_TRIGGER_NAME;

const SUB_CONTENT_NAME = 'DropdownMenuSubContent';

type DropdownMenuSubContentElement = React.ElementRef<typeof MenuPrimitive.Content>;
type DropdownMenuSubContentElement = React.ElementRef<typeof MenuPrimitive.SubContent>;
type MenuSubContentProps = React.ComponentPropsWithoutRef<typeof MenuPrimitive.SubContent>;
interface DropdownMenuSubContentProps extends MenuSubContentProps {}

const DropdownMenuSubContent = React.forwardRef<
DropdownMenuSubContentElement,
DropdownMenuSubContentProps
>((props: ScopedProps<DropdownMenuSubContentProps>, forwardedRef) => {
const { __scopeDropdownMenu, ...subContentProps } = props;
const menuScope = useMenuScope(__scopeDropdownMenu);

return (
<MenuPrimitive.SubContent
{...menuScope}
{...subContentProps}
ref={forwardedRef}
style={{
...props.style,
// re-namespace exposed content custom properties
...{
'--radix-dropdown-menu-content-transform-origin': 'var(--radix-popper-transform-origin)',
'--radix-dropdown-menu-content-available-width': 'var(--radix-popper-available-width)',
'--radix-dropdown-menu-content-available-height': 'var(--radix-popper-available-height)',
'--radix-dropdown-menu-trigger-width': 'var(--radix-popper-anchor-width)',
'--radix-dropdown-menu-trigger-height': 'var(--radix-popper-anchor-height)',
},
}}
/>
);
});
const DropdownMenuSubContent = React.memo(
React.forwardRef<DropdownMenuSubContentElement, DropdownMenuSubContentProps>(
(props: ScopedProps<DropdownMenuSubContentProps>, forwardedRef) => {
const { __scopeDropdownMenu, style, ...subContentProps } = props;
const menuScope = useMenuScope(__scopeDropdownMenu);

return (
<MenuPrimitive.SubContent
{...menuScope}
{...subContentProps}
ref={forwardedRef}
style={{
...props.style,
// re-namespace exposed content custom properties
...{
'--radix-dropdown-menu-content-transform-origin':
'var(--radix-popper-transform-origin)',
'--radix-dropdown-menu-content-available-width':
'var(--radix-popper-available-width)',
'--radix-dropdown-menu-content-available-height':
'var(--radix-popper-available-height)',
'--radix-dropdown-menu-trigger-width': 'var(--radix-popper-anchor-width)',
'--radix-dropdown-menu-trigger-height': 'var(--radix-popper-anchor-height)',
},
}}
/>
);
}
)
);

DropdownMenuSubContent.displayName = SUB_CONTENT_NAME;

Expand Down Expand Up @@ -562,3 +577,9 @@ export type {
DropdownMenuSubTriggerProps,
DropdownMenuSubContentProps,
};
function useEffect(
arg0: () => () => void,
arg1: React.Dispatch<React.SetStateAction<boolean | undefined>>[]
) {
throw new Error('Function not implemented.');
}