Skip to content

Commit

Permalink
focus button after closing the menu
Browse files Browse the repository at this point in the history
  • Loading branch information
ZeeshanTamboli committed Sep 23, 2022
1 parent 2253f0b commit 8a3cc07
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
3 changes: 3 additions & 0 deletions docs/data/base/components/menu/UseMenu.js
Expand Up @@ -198,6 +198,7 @@ MenuItem.propTypes = {
export default function UseMenu() {
const [anchorEl, setAnchorEl] = React.useState(null);
const preventReopen = React.useRef(false);
const buttonRef = React.useRef(null);

const handleOnClick = (event) => {
if (preventReopen.current) {
Expand All @@ -211,6 +212,7 @@ export default function UseMenu() {

const handleOnClose = () => {
setAnchorEl(null);
buttonRef.current.focus();
};

const open = Boolean(anchorEl);
Expand All @@ -231,6 +233,7 @@ export default function UseMenu() {
className="button"
onClick={handleOnClick}
onMouseDown={handleButtonMouseDown}
ref={buttonRef}
>
Commands
</button>
Expand Down
3 changes: 3 additions & 0 deletions docs/data/base/components/menu/UseMenu.tsx
Expand Up @@ -200,6 +200,7 @@ const MenuItem = React.forwardRef(function MenuItem(
export default function UseMenu() {
const [anchorEl, setAnchorEl] = React.useState<null | HTMLElement>(null);
const preventReopen = React.useRef(false);
const buttonRef = React.useRef<HTMLButtonElement>(null);

const handleOnClick = (event: React.MouseEvent<HTMLElement>) => {
if (preventReopen.current) {
Expand All @@ -213,6 +214,7 @@ export default function UseMenu() {

const handleOnClose = () => {
setAnchorEl(null);
buttonRef.current!.focus();
};

const open = Boolean(anchorEl);
Expand All @@ -233,6 +235,7 @@ export default function UseMenu() {
className="button"
onClick={handleOnClick}
onMouseDown={handleButtonMouseDown}
ref={buttonRef}
>
Commands
</button>
Expand Down

0 comments on commit 8a3cc07

Please sign in to comment.