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][Menu][joy] Explain how to control the open state #38355

Merged
merged 2 commits into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
24 changes: 24 additions & 0 deletions docs/data/joy/components/menu/ControlledDropdown.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import * as React from 'react';
import Dropdown from '@mui/joy/Dropdown';
import Menu from '@mui/joy/Menu';
import MenuButton from '@mui/joy/MenuButton';
import MenuItem from '@mui/joy/MenuItem';

export default function ControlledDropdown() {
const [open, setOpen] = React.useState(false);

const handleOpenChange = React.useCallback((event, isOpen) => {
setOpen(isOpen);
}, []);

return (
<Dropdown open={open} onOpenChange={handleOpenChange}>
<MenuButton>Dashboard...</MenuButton>
<Menu>
<MenuItem>Profile</MenuItem>
<MenuItem>My account</MenuItem>
<MenuItem>Logout</MenuItem>
</Menu>
</Dropdown>
);
}
27 changes: 27 additions & 0 deletions docs/data/joy/components/menu/ControlledDropdown.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import * as React from 'react';
import Dropdown from '@mui/joy/Dropdown';
import Menu from '@mui/joy/Menu';
import MenuButton from '@mui/joy/MenuButton';
import MenuItem from '@mui/joy/MenuItem';

export default function ControlledDropdown() {
const [open, setOpen] = React.useState(false);

const handleOpenChange = React.useCallback(
(event: React.SyntheticEvent | null, isOpen: boolean) => {
setOpen(isOpen);
},
[],
);

return (
<Dropdown open={open} onOpenChange={handleOpenChange}>
<MenuButton>Dashboard...</MenuButton>
<Menu>
<MenuItem>Profile</MenuItem>
<MenuItem>My account</MenuItem>
<MenuItem>Logout</MenuItem>
</Menu>
</Dropdown>
);
}
8 changes: 8 additions & 0 deletions docs/data/joy/components/menu/ControlledDropdown.tsx.preview
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Dropdown open={open} onOpenChange={handleOpenChange}>
<MenuButton>Dashboard...</MenuButton>
<Menu>
<MenuItem>Profile</MenuItem>
<MenuItem>My account</MenuItem>
<MenuItem>Logout</MenuItem>
</Menu>
</Dropdown>
8 changes: 8 additions & 0 deletions docs/data/joy/components/menu/menu.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ For example, this is how you'd go for displaying the menu on the bottom-end of t

{{"demo": "GroupMenu.js"}}

### Controlling the open state

By default, the open/close state of the menu is managed internally by the Dropdown component.
It is possible to control it programmatically from the outside, though.
To do this, use the `open` and `onOpenChange` props of the Dropdown component, as shown in the demo:
michaldudak marked this conversation as resolved.
Show resolved Hide resolved

{{"demo": "ControlledDropdown.js"}}

### `MenuList` composition

To get full control of the DOM structure, use the `MenuList` component.
Expand Down