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

[docs] Fix the reopening menu problem in MenuUnstyled demo #33890

Merged
merged 1 commit into from Aug 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions docs/data/base/components/menu/MenuSimple.js
Expand Up @@ -108,15 +108,30 @@ export default function UnstyledMenuSimple() {
const isOpen = Boolean(anchorEl);
const buttonRef = React.useRef(null);
const menuActions = React.useRef(null);
const preventReopen = React.useRef(false);

const handleButtonClick = (event) => {
if (preventReopen.current) {
event.preventDefault();
preventReopen.current = false;
return;
}

if (isOpen) {
setAnchorEl(null);
} else {
setAnchorEl(event.currentTarget);
}
};

const handleButtonMouseDown = () => {
if (isOpen) {
// Prevents the menu from reopening right after closing
// when clicking the button.
preventReopen.current = true;
}
};

const handleButtonKeyDown = (event) => {
if (event.key === 'ArrowDown' || event.key === 'ArrowUp') {
event.preventDefault();
Expand Down Expand Up @@ -145,6 +160,7 @@ export default function UnstyledMenuSimple() {
type="button"
onClick={handleButtonClick}
onKeyDown={handleButtonKeyDown}
onMouseDown={handleButtonMouseDown}
ref={buttonRef}
aria-controls={isOpen ? 'simple-menu' : undefined}
aria-expanded={isOpen || undefined}
Expand Down
16 changes: 16 additions & 0 deletions docs/data/base/components/menu/MenuSimple.tsx
Expand Up @@ -108,15 +108,30 @@ export default function UnstyledMenuSimple() {
const isOpen = Boolean(anchorEl);
const buttonRef = React.useRef<HTMLButtonElement>(null);
const menuActions = React.useRef<MenuUnstyledActions>(null);
const preventReopen = React.useRef(false);

const handleButtonClick = (event: React.MouseEvent<HTMLButtonElement>) => {
if (preventReopen.current) {
event.preventDefault();
preventReopen.current = false;
return;
}

if (isOpen) {
setAnchorEl(null);
} else {
setAnchorEl(event.currentTarget);
}
};

const handleButtonMouseDown = () => {
if (isOpen) {
// Prevents the menu from reopening right after closing
// when clicking the button.
preventReopen.current = true;
}
};

const handleButtonKeyDown = (event: React.KeyboardEvent<HTMLButtonElement>) => {
if (event.key === 'ArrowDown' || event.key === 'ArrowUp') {
event.preventDefault();
Expand Down Expand Up @@ -145,6 +160,7 @@ export default function UnstyledMenuSimple() {
type="button"
onClick={handleButtonClick}
onKeyDown={handleButtonKeyDown}
onMouseDown={handleButtonMouseDown}
ref={buttonRef}
aria-controls={isOpen ? 'simple-menu' : undefined}
aria-expanded={isOpen || undefined}
Expand Down
16 changes: 16 additions & 0 deletions docs/data/base/components/menu/WrappedMenuItems.js
Expand Up @@ -146,15 +146,30 @@ export default function WrappedMenuItems() {
const isOpen = Boolean(anchorEl);
const buttonRef = React.useRef(null);
const menuActions = React.useRef(null);
const preventReopen = React.useRef(false);

const handleButtonClick = (event) => {
if (preventReopen.current) {
event.preventDefault();
preventReopen.current = false;
return;
}

if (isOpen) {
setAnchorEl(null);
} else {
setAnchorEl(event.currentTarget);
}
};

const handleButtonMouseDown = () => {
if (isOpen) {
// Prevents the menu from reopening right after closing
// when clicking the button.
preventReopen.current = true;
}
};

const handleButtonKeyDown = (event) => {
if (event.key === 'ArrowDown' || event.key === 'ArrowUp') {
event.preventDefault();
Expand Down Expand Up @@ -183,6 +198,7 @@ export default function WrappedMenuItems() {
type="button"
onClick={handleButtonClick}
onKeyDown={handleButtonKeyDown}
onMouseDown={handleButtonMouseDown}
ref={buttonRef}
aria-controls={isOpen ? 'wrapped-menu' : undefined}
aria-expanded={isOpen || undefined}
Expand Down
16 changes: 16 additions & 0 deletions docs/data/base/components/menu/WrappedMenuItems.tsx
Expand Up @@ -145,15 +145,30 @@ export default function WrappedMenuItems() {
const isOpen = Boolean(anchorEl);
const buttonRef = React.useRef<HTMLButtonElement>(null);
const menuActions = React.useRef<MenuUnstyledActions>(null);
const preventReopen = React.useRef(false);

const handleButtonClick = (event: React.MouseEvent<HTMLButtonElement>) => {
if (preventReopen.current) {
event.preventDefault();
preventReopen.current = false;
return;
}

if (isOpen) {
setAnchorEl(null);
} else {
setAnchorEl(event.currentTarget);
}
};

const handleButtonMouseDown = () => {
if (isOpen) {
// Prevents the menu from reopening right after closing
// when clicking the button.
preventReopen.current = true;
}
};

const handleButtonKeyDown = (event: React.KeyboardEvent<HTMLButtonElement>) => {
if (event.key === 'ArrowDown' || event.key === 'ArrowUp') {
event.preventDefault();
Expand Down Expand Up @@ -182,6 +197,7 @@ export default function WrappedMenuItems() {
type="button"
onClick={handleButtonClick}
onKeyDown={handleButtonKeyDown}
onMouseDown={handleButtonMouseDown}
ref={buttonRef}
aria-controls={isOpen ? 'wrapped-menu' : undefined}
aria-expanded={isOpen || undefined}
Expand Down