Skip to content

Commit dbb8ba3

Browse files
authored
Merge pull request #8837 from smeng9/master
[Doc] fix examples of custom UserMenu items across the docs to support keyboard navigation
2 parents 0572a66 + c3c1ccb commit dbb8ba3

File tree

6 files changed

+42
-15
lines changed

6 files changed

+42
-15
lines changed

docs/AppBar.md

+11-3
Original file line numberDiff line numberDiff line change
@@ -238,21 +238,29 @@ The content of the user menu depends on the return value of `authProvider.getIde
238238
You can customize the user menu by passing a `userMenu` prop to the `<AppBar>` component.
239239

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

245-
const SettingsMenuItem = () => {
246+
// It's important to pass the ref to allow MUI to manage the keyboard navigation
247+
const SettingsMenuItem = React.forwardRef((props, ref) => {
248+
// We are not using MenuItemLink so we retrieve the onClose function from the UserContext
246249
const { onClose } = useUserMenu();
247250
return (
248-
<MenuItem onClick={onClose}>
251+
<MenuItem
252+
onClick={onClose}
253+
ref={ref}
254+
// It's important to pass the props to allow MUI to manage the keyboard navigation
255+
{...props}
256+
>
249257
<ListItemIcon>
250258
<SettingsIcon fontSize="small" />
251259
</ListItemIcon>
252260
<ListItemText>Customize</ListItemText>
253261
</MenuItem>
254262
);
255-
};
263+
});
256264

257265
const MyAppBar = () => (
258266
<AppBar

docs/Authentication.md

+3
Original file line numberDiff line numberDiff line change
@@ -342,13 +342,16 @@ import { useLogout } from 'react-admin';
342342
import MenuItem from '@mui/material/MenuItem';
343343
import ExitIcon from '@mui/icons-material/PowerSettingsNew';
344344

345+
// It's important to pass the ref to allow MUI to manage the keyboard navigation
345346
const MyLogoutButton = forwardRef((props, ref) => {
346347
const logout = useLogout();
347348
const handleClick = () => logout();
348349
return (
349350
<MenuItem
350351
onClick={handleClick}
351352
ref={ref}
353+
// It's important to pass the props to allow MUI to manage the keyboard navigation
354+
{...props}
352355
>
353356
<ExitIcon /> Logout
354357
</MenuItem>

docs/ContainerLayout.md

+3
Original file line numberDiff line numberDiff line change
@@ -161,16 +161,19 @@ By default, the `<ContainerLayout>` shows a user menu with a single item (logout
161161

162162
{% raw %}
163163
```jsx
164+
import * as React from 'react';
164165
import { Logout, UserMenu, useUserMenu } from 'react-admin';
165166
import { MenuList, MenuItem, ListItemIcon, ListItemText } from '@mui/material';
166167
import SettingsIcon from '@mui/icons-material/Settings';
167168
import { ContainerLayout } from '@react-admin/ra-navigation';
168169

170+
// It's important to pass the ref to allow MUI to manage the keyboard navigation
169171
const ConfigurationMenu = React.forwardRef((props, ref) => {
170172
const { onClose } = useUserMenu();
171173
return (
172174
<MenuItem
173175
ref={ref}
176+
// It's important to pass the props to allow MUI to manage the keyboard navigation
174177
{...props}
175178
to="/configuration"
176179
onClick={onClose}

docs/Theming.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ const ConfigurationMenu = React.forwardRef((props, ref) => {
591591
});
592592

593593
// It's important to pass the ref to allow MUI to manage the keyboard navigation
594-
const SwitchLanguage = forwardRef((props, ref) => {
594+
const SwitchLanguage = React.forwardRef((props, ref) => {
595595
const [locale, setLocale] = useLocaleState();
596596
// We are not using MenuItemLink so we retrieve the onClose function from the UserContext
597597
const { onClose } = useUserMenu();

docs/useLogout.md

+3
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@ import { AppBar, Layout, UserMenu, useLogout } from 'react-admin';
1515
import { MenuItem } from '@mui/material';
1616
import ExitIcon from '@mui/icons-material/PowerSettingsNew';
1717

18+
// It's important to pass the ref to allow MUI to manage the keyboard navigation
1819
const MyLogoutButton = forwardRef((props, ref) => {
1920
const logout = useLogout();
2021
const handleClick = () => logout();
2122
return (
2223
<MenuItem
2324
onClick={handleClick}
2425
ref={ref}
26+
// It's important to pass the props to allow MUI to manage the keyboard navigation
27+
{...props}
2528
>
2629
<ExitIcon /> Logout
2730
</MenuItem>

packages/ra-ui-materialui/src/layout/AppBar.stories.tsx

+21-11
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
ListItemText,
99
TextField,
1010
Skeleton,
11+
MenuItemProps,
1112
} from '@mui/material';
1213
import SettingsIcon from '@mui/icons-material/Settings';
1314
import { QueryClientProvider, QueryClient } from 'react-query';
@@ -197,17 +198,26 @@ export const UserMenuCustom = () => (
197198
</Wrapper>
198199
);
199200

200-
const SettingsMenuItem = () => {
201-
const { onClose } = useUserMenu();
202-
return (
203-
<MenuItem onClick={onClose}>
204-
<ListItemIcon>
205-
<SettingsIcon fontSize="small" />
206-
</ListItemIcon>
207-
<ListItemText>Customize</ListItemText>
208-
</MenuItem>
209-
);
210-
};
201+
// It's important to pass the ref to allow MUI to manage the keyboard navigation
202+
const SettingsMenuItem: React.FC<MenuItemProps> = React.forwardRef(
203+
(props, ref) => {
204+
// We are not using MenuItemLink so we retrieve the onClose function from the UserContext
205+
const { onClose } = useUserMenu();
206+
return (
207+
<MenuItem
208+
onClick={onClose}
209+
ref={ref}
210+
// It's important to pass the props to allow MUI to manage the keyboard navigation
211+
{...props}
212+
>
213+
<ListItemIcon>
214+
<SettingsIcon fontSize="small" />
215+
</ListItemIcon>
216+
<ListItemText>Customize</ListItemText>
217+
</MenuItem>
218+
);
219+
}
220+
);
211221

212222
export const UserMenuElements = () => (
213223
<Wrapper>

0 commit comments

Comments
 (0)