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

[Doc] fix examples of custom UserMenu items across the docs to support keyboard navigation #8837

Merged
merged 9 commits into from
Apr 24, 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
14 changes: 11 additions & 3 deletions docs/AppBar.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,21 +238,29 @@ The content of the user menu depends on the return value of `authProvider.getIde
You can customize the user menu by passing a `userMenu` prop to the `<AppBar>` component.

```jsx
import * as React from 'react';
import { AppBar, UserMenu, useUserMenu } from 'react-admin';
import { MenuItem, ListItemIcon, ListItemText } from '@mui/material';
import SettingsIcon from '@mui/icons-material/Settings';

const SettingsMenuItem = () => {
// It's important to pass the ref to allow MUI to manage the keyboard navigation
const SettingsMenuItem = React.forwardRef((props, ref) => {
// We are not using MenuItemLink so we retrieve the onClose function from the UserContext
const { onClose } = useUserMenu();
return (
<MenuItem onClick={onClose}>
<MenuItem
onClick={onClose}
ref={ref}
// It's important to pass the props to allow MUI to manage the keyboard navigation
{...props}
>
<ListItemIcon>
<SettingsIcon fontSize="small" />
</ListItemIcon>
<ListItemText>Customize</ListItemText>
</MenuItem>
);
};
});

const MyAppBar = () => (
<AppBar
Expand Down
3 changes: 3 additions & 0 deletions docs/Authentication.md
Original file line number Diff line number Diff line change
Expand Up @@ -342,13 +342,16 @@ import { useLogout } from 'react-admin';
import MenuItem from '@mui/material/MenuItem';
import ExitIcon from '@mui/icons-material/PowerSettingsNew';

// It's important to pass the ref to allow MUI to manage the keyboard navigation
const MyLogoutButton = forwardRef((props, ref) => {
const logout = useLogout();
const handleClick = () => logout();
return (
<MenuItem
onClick={handleClick}
ref={ref}
// It's important to pass the props to allow MUI to manage the keyboard navigation
{...props}
>
<ExitIcon /> Logout
</MenuItem>
Expand Down
3 changes: 3 additions & 0 deletions docs/ContainerLayout.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,16 +161,19 @@ By default, the `<ContainerLayout>` shows a user menu with a single item (logout

{% raw %}
```jsx
import * as React from 'react';
import { Logout, UserMenu, useUserMenu } from 'react-admin';
import { MenuList, MenuItem, ListItemIcon, ListItemText } from '@mui/material';
import SettingsIcon from '@mui/icons-material/Settings';
import { ContainerLayout } from '@react-admin/ra-navigation';

// It's important to pass the ref to allow MUI to manage the keyboard navigation
const ConfigurationMenu = React.forwardRef((props, ref) => {
const { onClose } = useUserMenu();
return (
<MenuItem
ref={ref}
// It's important to pass the props to allow MUI to manage the keyboard navigation
{...props}
to="/configuration"
onClick={onClose}
Expand Down
2 changes: 1 addition & 1 deletion docs/Theming.md
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ const ConfigurationMenu = React.forwardRef((props, ref) => {
});

// It's important to pass the ref to allow MUI to manage the keyboard navigation
const SwitchLanguage = forwardRef((props, ref) => {
const SwitchLanguage = React.forwardRef((props, ref) => {
const [locale, setLocale] = useLocaleState();
// We are not using MenuItemLink so we retrieve the onClose function from the UserContext
const { onClose } = useUserMenu();
Expand Down
3 changes: 3 additions & 0 deletions docs/useLogout.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@ import { AppBar, Layout, UserMenu, useLogout } from 'react-admin';
import { MenuItem } from '@mui/material';
import ExitIcon from '@mui/icons-material/PowerSettingsNew';

// It's important to pass the ref to allow MUI to manage the keyboard navigation
const MyLogoutButton = forwardRef((props, ref) => {
const logout = useLogout();
const handleClick = () => logout();
return (
<MenuItem
onClick={handleClick}
ref={ref}
// It's important to pass the props to allow MUI to manage the keyboard navigation
{...props}
>
<ExitIcon /> Logout
</MenuItem>
Expand Down
32 changes: 21 additions & 11 deletions packages/ra-ui-materialui/src/layout/AppBar.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
ListItemText,
TextField,
Skeleton,
MenuItemProps,
} from '@mui/material';
import SettingsIcon from '@mui/icons-material/Settings';
import { QueryClientProvider, QueryClient } from 'react-query';
Expand Down Expand Up @@ -197,17 +198,26 @@ export const UserMenuCustom = () => (
</Wrapper>
);

const SettingsMenuItem = () => {
const { onClose } = useUserMenu();
return (
<MenuItem onClick={onClose}>
<ListItemIcon>
<SettingsIcon fontSize="small" />
</ListItemIcon>
<ListItemText>Customize</ListItemText>
</MenuItem>
);
};
// It's important to pass the ref to allow MUI to manage the keyboard navigation
const SettingsMenuItem: React.FC<MenuItemProps> = React.forwardRef(
(props, ref) => {
// We are not using MenuItemLink so we retrieve the onClose function from the UserContext
const { onClose } = useUserMenu();
return (
<MenuItem
onClick={onClose}
ref={ref}
// It's important to pass the props to allow MUI to manage the keyboard navigation
{...props}
>
<ListItemIcon>
<SettingsIcon fontSize="small" />
</ListItemIcon>
<ListItemText>Customize</ListItemText>
</MenuItem>
);
}
);

export const UserMenuElements = () => (
<Wrapper>
Expand Down