Skip to content

Commit

Permalink
[docs][Menu][joy] Explain how to control the open state (#38355)
Browse files Browse the repository at this point in the history
Signed-off-by: Michał Dudak <[email protected]>
Co-authored-by: Sam Sycamore <[email protected]>
  • Loading branch information
michaldudak and samuelsycamore authored Aug 11, 2023
1 parent aa41bc0 commit a80bc05
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
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>
7 changes: 7 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,13 @@ 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.
To control it programmatically from the outside, apply the Dropdown's `open` and `onOpenChange` props as shown below:

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

### `MenuList` composition

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

0 comments on commit a80bc05

Please sign in to comment.