Skip to content

Commit

Permalink
fix(MenuButton): call onMenuHide on all cases where menu is closed …
Browse files Browse the repository at this point in the history
…[prerelease]
  • Loading branch information
YossiSaadi committed Mar 19, 2024
1 parent a4e05b6 commit 9ac1e7f
Showing 1 changed file with 22 additions and 21 deletions.
43 changes: 22 additions & 21 deletions packages/core/src/components/MenuButton/MenuButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export interface MenuButtonProps extends VibeComponentProps {
*/
onMenuShow?: () => void;
/*
Callback function to be called when the menu is shown
Callback function to be called when the menu is hidden
*/
onMenuHide?: () => void;
/**
Expand Down Expand Up @@ -191,35 +191,36 @@ const MenuButton: VibeComponent<MenuButtonProps> & {
const [isOpen, setIsOpen] = useState(open);
const isActive = active ?? isOpen;

const onMenuDidClose = useCallback(
(event: React.KeyboardEvent) => {
const isEscapeClicked = event && event.key === "Escape";
if (closeMenuOnItemClick || isEscapeClicked) {
setIsOpen(false);
const handleMenuClose = useCallback(
(focusOnMenuButtonAfterClose: boolean) => {
onMenuHide();
setIsOpen(false);
const button = componentRef.current;
if (!button || !focusOnMenuButtonAfterClose) {
return;
}
window.requestAnimationFrame(() => {
button.focus();
});
},
[onMenuHide]
);

if (isEscapeClicked) {
const button = componentRef.current;
window.requestAnimationFrame(() => {
button.focus();
});
const onMenuDidClose = useCallback(
(event: React.KeyboardEvent) => {
const isEscapeKey = event?.key === "Escape";
if (isEscapeKey || closeMenuOnItemClick) {
handleMenuClose(isEscapeKey);
}
},
[closeMenuOnItemClick]
[closeMenuOnItemClick, handleMenuClose]
);

const onDialogDidHide = useCallback(
(event: DialogEvent, hideEvent: string) => {
setIsOpen(false);
onMenuHide();
const button = componentRef.current;
window.requestAnimationFrame(() => {
if (button && hideEvent === Dialog.hideShowTriggers.ESCAPE_KEY) {
button.focus();
}
});
handleMenuClose(hideEvent === Dialog.hideShowTriggers.ESCAPE_KEY);
},
[setIsOpen, onMenuHide, componentRef]
[handleMenuClose]
);

const onDialogDidShow = useCallback(() => {
Expand Down

0 comments on commit 9ac1e7f

Please sign in to comment.