Skip to content

Commit 707583e

Browse files
committed
Show shortcuts on hover only
1 parent e242533 commit 707583e

File tree

2 files changed

+114
-26
lines changed

2 files changed

+114
-26
lines changed

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

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import ExpandMore from '@mui/icons-material/ExpandMore';
2727
import QrCode from '@mui/icons-material/QrCode';
2828
import { Route } from 'react-router-dom';
2929

30-
import { Layout, Menu, Title } from '.';
30+
import { Layout, Menu, MenuItemLinkClasses, Title } from '.';
3131

3232
export default { title: 'ra-ui-materialui/layout/Menu' };
3333

@@ -173,7 +173,7 @@ export const WithKeyboardShortcuts = () => {
173173
<Menu.Item
174174
to="/customers"
175175
leftIcon={<PeopleOutlined />}
176-
primaryText="Customers"
176+
primaryText="Customers very long"
177177
keyboardShortcut="G>C"
178178
/>
179179
<Menu.ResourceItem
@@ -225,7 +225,7 @@ export const WithCustomKeyboardShortcutRepresentation = () => {
225225
<Menu.Item
226226
to="/customers"
227227
leftIcon={<PeopleOutlined />}
228-
primaryText="Customers"
228+
primaryText="Customers very long"
229229
keyboardShortcut="ctrl+alt+C"
230230
keyboardShortcutRepresentation="ctrl+alt+C"
231231
/>
@@ -262,6 +262,80 @@ export const WithCustomKeyboardShortcutRepresentation = () => {
262262
);
263263
};
264264

265+
export const WithCustomKeyboardShortcutRepresentationUsingMenuItemClasses =
266+
() => {
267+
const CustomMenu = () => (
268+
<Menu>
269+
<Menu.DashboardItem
270+
keyboardShortcut="ctrl+alt+D"
271+
keyboardShortcutRepresentation={
272+
<div className={MenuItemLinkClasses.shortcut}>
273+
ctrl+alt+D
274+
</div>
275+
}
276+
/>
277+
<Menu.Item
278+
to="/sales"
279+
leftIcon={<PieChartOutlined />}
280+
primaryText="Sales"
281+
keyboardShortcut="ctrl+alt+S"
282+
keyboardShortcutRepresentation={
283+
<div className={MenuItemLinkClasses.shortcut}>
284+
ctrl+alt+S
285+
</div>
286+
}
287+
/>
288+
<Menu.Item
289+
to="/customers"
290+
leftIcon={<PeopleOutlined />}
291+
primaryText="Customers very long"
292+
keyboardShortcut="ctrl+alt+C"
293+
keyboardShortcutRepresentation={
294+
<div className={MenuItemLinkClasses.shortcut}>
295+
ctrl+alt+C
296+
</div>
297+
}
298+
/>
299+
<Menu.ResourceItem
300+
name="products"
301+
leftIcon={<Inventory />}
302+
keyboardShortcut="ctrl+alt+P"
303+
keyboardShortcutRepresentation={
304+
<div className={MenuItemLinkClasses.shortcut}>
305+
ctrl+alt+P
306+
</div>
307+
}
308+
/>
309+
</Menu>
310+
);
311+
const CustomLayout = ({ children }) => (
312+
<Layout menu={CustomMenu}>{children}</Layout>
313+
);
314+
315+
const Dashboard = () => <Page title="Dashboard" />;
316+
return (
317+
<TestMemoryRouter initialEntries={['/']}>
318+
<Admin
319+
dataProvider={testDataProvider()}
320+
layout={CustomLayout}
321+
dashboard={Dashboard}
322+
>
323+
<Resource
324+
name="products"
325+
list={<Page title="Products" />}
326+
/>
327+
<CustomRoutes>
328+
<Route path="/sales" element={<Page title="Sales" />} />
329+
<Route
330+
path="/customers"
331+
element={<Page title="Customers" />}
332+
/>
333+
</CustomRoutes>
334+
</Admin>
335+
</TestMemoryRouter>
336+
);
337+
};
338+
265339
const Page = ({ title }) => (
266340
<>
267341
<Typography variant="h5" mt={2}>

packages/ra-ui-materialui/src/layout/MenuItemLink.tsx

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121
useMediaQuery,
2222
Theme,
2323
useForkRef,
24-
Box,
24+
Typography,
2525
} from '@mui/material';
2626
import { useTranslate, useBasename, useEvent } from 'ra-core';
2727
import { useHotkeys } from 'react-hotkeys-hook';
@@ -133,7 +133,7 @@ export const MenuItemLink = forwardRef<any, MenuItemLinkProps>(
133133
const renderMenuItem = () => {
134134
return (
135135
<StyledMenuItem
136-
className={clsx(className, {
136+
className={clsx(className, MenuItemLinkClasses.root, {
137137
[MenuItemLinkClasses.active]: !!match,
138138
})}
139139
// @ts-ignore
@@ -143,27 +143,24 @@ export const MenuItemLink = forwardRef<any, MenuItemLinkProps>(
143143
{...rest}
144144
onClick={handleMenuTap}
145145
>
146-
<Box
147-
sx={{
148-
flexGrow: 1,
149-
display: 'flex',
150-
alignItems: 'center',
151-
}}
152-
>
153-
{leftIcon && (
154-
<ListItemIcon className={MenuItemLinkClasses.icon}>
155-
{leftIcon}
156-
</ListItemIcon>
157-
)}
146+
{leftIcon && (
147+
<ListItemIcon className={MenuItemLinkClasses.icon}>
148+
{leftIcon}
149+
</ListItemIcon>
150+
)}
151+
<Typography variant="inherit" noWrap sx={{ flexGrow: 1 }}>
158152
{children
159153
? children
160154
: typeof primaryText === 'string'
161-
? translate(primaryText, { _: primaryText })
155+
? translate(primaryText, {
156+
_: primaryText,
157+
})
162158
: primaryText}
163-
</Box>
159+
</Typography>
164160
{keyboardShortcut
165161
? keyboardShortcutRepresentation ?? (
166162
<KeyboardShortcut
163+
className={MenuItemLinkClasses.shortcut}
167164
keyboardShortcut={keyboardShortcut}
168165
/>
169166
)
@@ -210,17 +207,34 @@ export type MenuItemLinkProps = Omit<
210207
const PREFIX = 'RaMenuItemLink';
211208

212209
export const MenuItemLinkClasses = {
210+
root: `${PREFIX}-root`,
213211
active: `${PREFIX}-active`,
214212
icon: `${PREFIX}-icon`,
213+
shortcut: `${PREFIX}-shortcut`,
215214
};
216215

217216
const StyledMenuItem = styled(MenuItem, {
218217
name: PREFIX,
219218
overridesResolver: (props, styles) => styles.root,
220219
})(({ theme }) => ({
221220
color: (theme.vars || theme).palette.text.secondary,
222-
justifyContent: 'space-between',
223-
gap: theme.spacing(1),
221+
222+
[`& .${MenuItemLinkClasses.icon}`]: {
223+
color: (theme.vars || theme).palette.text.secondary,
224+
},
225+
226+
[`& .${MenuItemLinkClasses.shortcut}`]: {
227+
color: (theme.vars || theme).palette.text.secondary,
228+
fontSize: theme.typography.body2.fontSize,
229+
opacity: 0,
230+
display: 'none',
231+
transition: 'opacity 0.3s',
232+
},
233+
234+
[`&:hover .${MenuItemLinkClasses.shortcut}`]: {
235+
opacity: 0.7,
236+
display: 'inline-flex',
237+
},
224238

225239
[`&.${MenuItemLinkClasses.active}`]: {
226240
color: (theme.vars || theme).palette.text.primary,
@@ -238,19 +252,19 @@ const LinkRef = forwardRef<HTMLAnchorElement, LinkProps>((props, ref) => (
238252

239253
declare module '@mui/material/styles' {
240254
interface ComponentNameToClassKey {
241-
RaMenuItemLink: 'root' | 'active' | 'icon';
255+
[PREFIX]: 'root' | 'active' | 'icon' | 'shortcut';
242256
}
243257

244258
interface ComponentsPropsList {
245-
RaMenuItemLink: Partial<MenuItemLinkProps>;
259+
[PREFIX]: Partial<MenuItemLinkProps>;
246260
}
247261

248262
interface Components {
249-
RaMenuItemLink?: {
250-
defaultProps?: ComponentsPropsList['RaMenuItemLink'];
263+
[PREFIX]?: {
264+
defaultProps?: ComponentsPropsList[typeof PREFIX];
251265
styleOverrides?: ComponentsOverrides<
252266
Omit<Theme, 'components'>
253-
>['RaMenuItemLink'];
267+
>[typeof PREFIX];
254268
};
255269
}
256270
}

0 commit comments

Comments
 (0)