Skip to content

Commit

Permalink
fix(MenuButton): fix dialog close behavior (#1912)
Browse files Browse the repository at this point in the history
  • Loading branch information
talkor committed Jan 29, 2024
1 parent b53bca4 commit 61a50dd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export default function useMenuItemKeyboardEvents({

if (isActive && hasChildren) {
setActiveItemIndex(index);
if (!splitMenuItem) {
if (!splitMenuItem || isMouseEnterIconButton) {
setSubMenuIsOpenByIndex(index, true);
return;
}
Expand Down
17 changes: 15 additions & 2 deletions src/components/MenuButton/MenuButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ interface MenuButtonProps extends VibeComponentProps {
onClick?: (event: React.MouseEvent) => void;
zIndex?: number;
ariaLabel?: string;
// TODO: remove in next major version
/**
* @deprecated use closeMenuOnItemClick instead
*/
closeDialogOnContentClick?: boolean;
/*
Class name to provide the element which wraps the popover/modal/dialog
Expand Down Expand Up @@ -119,6 +123,10 @@ interface MenuButtonProps extends VibeComponentProps {
* Element to be used as the trigger element for the Menu - default is button
*/
triggerElement?: React.ElementType;
/**
* Close the menu when an item is clicked
*/
closeMenuOnItemClick?: boolean;
}

const MenuButton: VibeComponent<MenuButtonProps> & {
Expand All @@ -144,6 +152,7 @@ const MenuButton: VibeComponent<MenuButtonProps> & {
zIndex = null,
ariaLabel = "Menu",
closeDialogOnContentClick = false,
closeMenuOnItemClick,
dialogOffset = MOVE_BY,
dialogPosition = Dialog.positions.BOTTOM_START,
dialogClassName,
Expand Down Expand Up @@ -179,15 +188,19 @@ const MenuButton: VibeComponent<MenuButtonProps> & {

const onMenuDidClose = useCallback(
(event: React.KeyboardEvent) => {
if (event && event.key === "Escape") {
const isEscapeClicked = event && event.key === "Escape";
if (closeMenuOnItemClick || isEscapeClicked) {
setIsOpen(false);
}

if (isEscapeClicked) {
const button = componentRef.current;
window.requestAnimationFrame(() => {
button.focus();
});
}
},
[componentRef, setIsOpen]
[closeMenuOnItemClick]
);

const onDialogDidHide = useCallback(
Expand Down

0 comments on commit 61a50dd

Please sign in to comment.