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][base-ui] Fix Menu Hooks demo #38479

Merged
merged 1 commit into from
Aug 17, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 15 additions & 16 deletions docs/data/base/components/menu/UseMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ import { useMenuButton } from '@mui/base/useMenuButton';
const Menu = React.forwardRef(function Menu(props, ref) {
const { children, ...other } = props;

const { contextValue, getListboxProps } = useMenu({
const { open, triggerElement, contextValue, getListboxProps } = useMenu({
listboxRef: ref,
});

return (
<ul className="menu-root" {...other} {...getListboxProps()}>
<MenuProvider value={contextValue}>{children}</MenuProvider>
</ul>
<Popper open={open} anchorEl={triggerElement}>
<ul className="menu-root" {...other} {...getListboxProps()}>
<MenuProvider value={contextValue}>{children}</MenuProvider>
</ul>
</Popper>
);
});

Expand Down Expand Up @@ -62,8 +64,7 @@ const MenuButton = React.forwardRef(function MenuButton(props, forwardedRef) {
});

export default function UseMenu() {
const { contextValue: dropdownContextValue, open } = useDropdown();
const buttonRef = React.useRef(null);
const { contextValue: dropdownContextValue } = useDropdown();

const createHandleMenuClick = (menuItem) => {
return () => {
Expand All @@ -75,16 +76,14 @@ export default function UseMenu() {
<React.Fragment>
<GlobalStyles styles={styles} />
<DropdownContext.Provider value={dropdownContextValue}>
<MenuButton ref={buttonRef}>Theme</MenuButton>
<Popper open={open} anchorEl={buttonRef.current}>
<Menu id="hooks-menu">
<MenuItem onClick={createHandleMenuClick('OS Default')}>
OS default
</MenuItem>
<MenuItem onClick={createHandleMenuClick('Light')}>Light</MenuItem>
<MenuItem onClick={createHandleMenuClick('Dark')}>Dark</MenuItem>
</Menu>
</Popper>
<MenuButton>Theme</MenuButton>
<Menu id="hooks-menu">
<MenuItem onClick={createHandleMenuClick('OS Default')}>
OS default
</MenuItem>
<MenuItem onClick={createHandleMenuClick('Light')}>Light</MenuItem>
<MenuItem onClick={createHandleMenuClick('Dark')}>Dark</MenuItem>
</Menu>
</DropdownContext.Provider>
</React.Fragment>
);
Expand Down
31 changes: 15 additions & 16 deletions docs/data/base/components/menu/UseMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ const Menu = React.forwardRef(function Menu(
) {
const { children, ...other } = props;

const { contextValue, getListboxProps } = useMenu({
const { open, triggerElement, contextValue, getListboxProps } = useMenu({
listboxRef: ref,
});

return (
<ul className="menu-root" {...other} {...getListboxProps()}>
<MenuProvider value={contextValue}>{children}</MenuProvider>
</ul>
<Popper open={open} anchorEl={triggerElement}>
<ul className="menu-root" {...other} {...getListboxProps()}>
<MenuProvider value={contextValue}>{children}</MenuProvider>
</ul>
</Popper>
);
});

Expand Down Expand Up @@ -61,8 +63,7 @@ const MenuButton = React.forwardRef(function MenuButton(
});

export default function UseMenu() {
const { contextValue: dropdownContextValue, open } = useDropdown();
const buttonRef = React.useRef<HTMLButtonElement>(null);
const { contextValue: dropdownContextValue } = useDropdown();

const createHandleMenuClick = (menuItem: string) => {
return () => {
Expand All @@ -74,16 +75,14 @@ export default function UseMenu() {
<React.Fragment>
<GlobalStyles styles={styles} />
<DropdownContext.Provider value={dropdownContextValue}>
<MenuButton ref={buttonRef}>Theme</MenuButton>
<Popper open={open} anchorEl={buttonRef.current}>
<Menu id="hooks-menu">
<MenuItem onClick={createHandleMenuClick('OS Default')}>
OS default
</MenuItem>
<MenuItem onClick={createHandleMenuClick('Light')}>Light</MenuItem>
<MenuItem onClick={createHandleMenuClick('Dark')}>Dark</MenuItem>
</Menu>
</Popper>
<MenuButton>Theme</MenuButton>
<Menu id="hooks-menu">
<MenuItem onClick={createHandleMenuClick('OS Default')}>
OS default
</MenuItem>
<MenuItem onClick={createHandleMenuClick('Light')}>Light</MenuItem>
<MenuItem onClick={createHandleMenuClick('Dark')}>Dark</MenuItem>
</Menu>
</DropdownContext.Provider>
</React.Fragment>
);
Expand Down
18 changes: 8 additions & 10 deletions docs/data/base/components/menu/UseMenu.tsx.preview
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
<React.Fragment>
<GlobalStyles styles={styles} />
<DropdownContext.Provider value={dropdownContextValue}>
<MenuButton ref={buttonRef}>Theme</MenuButton>
<Popper open={open} anchorEl={buttonRef.current}>
<Menu id="hooks-menu">
<MenuItem onClick={createHandleMenuClick('OS Default')}>
OS default
</MenuItem>
<MenuItem onClick={createHandleMenuClick('Light')}>Light</MenuItem>
<MenuItem onClick={createHandleMenuClick('Dark')}>Dark</MenuItem>
</Menu>
</Popper>
<MenuButton>Theme</MenuButton>
<Menu id="hooks-menu">
<MenuItem onClick={createHandleMenuClick('OS Default')}>
OS default
</MenuItem>
<MenuItem onClick={createHandleMenuClick('Light')}>Light</MenuItem>
<MenuItem onClick={createHandleMenuClick('Dark')}>Dark</MenuItem>
</Menu>
</DropdownContext.Provider>
</React.Fragment>
Loading