Skip to content

Commit

Permalink
[DataGrid] Restore focus after GridMenu closes (#10412)
Browse files Browse the repository at this point in the history
  • Loading branch information
romgrk authored Sep 21, 2023
1 parent 9addfb0 commit 20a3d3c
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion packages/grid/x-data-grid/src/components/menu/GridMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import * as React from 'react';
import PropTypes from 'prop-types';
import clsx from 'clsx';
import ClickAwayListener, { ClickAwayListenerProps } from '@mui/material/ClickAwayListener';
import { unstable_composeClasses as composeClasses, HTMLElementType } from '@mui/utils';
import {
unstable_composeClasses as composeClasses,
unstable_useEnhancedEffect as useEnhancedEffect,
HTMLElementType,
} from '@mui/utils';
import Grow, { GrowProps } from '@mui/material/Grow';
import Paper from '@mui/material/Paper';
import Popper, { PopperProps } from '@mui/material/Popper';
Expand Down Expand Up @@ -70,6 +74,17 @@ function GridMenu(props: GridMenuProps) {
const rootProps = useGridRootProps();
const classes = useUtilityClasses(rootProps);

const savedFocusRef = React.useRef<HTMLElement | null>(null);
useEnhancedEffect(() => {
if (open) {
savedFocusRef.current =
document.activeElement instanceof HTMLElement ? document.activeElement : null;
} else {
savedFocusRef.current?.focus?.();
savedFocusRef.current = null;
}
}, [open]);

React.useEffect(() => {
// Emit menuOpen or menuClose events
const eventName = open ? 'menuOpen' : 'menuClose';
Expand Down

0 comments on commit 20a3d3c

Please sign in to comment.