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

fix(MenuButton): call onMenuHide on all cases where menu is closed #2027

Merged
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
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