Skip to content

Conversation

djhi
Copy link
Collaborator

@djhi djhi commented Jun 18, 2025

This PR adds support for keyboard shortcuts to <MenuItemLink>

How To Test

https://react-admin-storybook-git-menu-item-keyboard-shortcuts-marmelab.vercel.app/?path=/story/ra-ui-materialui-layout-menu--with-keyboard-shortcuts

Additional Checks

  • The PR targets master for a bugfix or a documentation fix, or next for a feature
  • The PR includes unit tests (if not possible, describe why)
  • The PR includes one or several stories (if not possible, describe why)
  • The documentation is up to date

Also, please make sure to read the contributing guidelines.

Copy link
Contributor

@Madeorsk Madeorsk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work! I love this feature!

className,
hasDashboard: hasDashboardProp,
...rest
} = props;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you just want to discard hasDashboard, what about using _ instead of hasDashboardProp?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's how we usually do it

Copy link
Member

@fzaninotto fzaninotto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good start but you should also add a general "keyboard shortcut" chapter to make it discoverable through search ,explaining how to add shortcuts for menu items and for custom actions.

Also, could you please add shortcuts to the simple example menu ?

if (open && keyboardShortcut != null) {
return (
<Tooltip
title={getKeyboardShortcutLabel(keyboardShortcut)}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not fond of the way we surface these shortcuts:

  1. They only display on hover,
  2. They only display when the sidebar is open

Why not show them on the right side of the MenuItemLink instead? It's common practice.

image

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know how to represent sequential shortcuts like this

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We decided to just add a space between all keys, sequential or not

useHotkeys,
} from 'react-hotkeys-hook';

export const KeyboardShortcut = (props: KeyboardShortcutProps) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add jsDoc to explain why we need that (to add a keyboard shortcut conditionally? I so, why don't we use the "enabled" option?)

Comment on lines 3 to 10
export const getKeyboardShortcutLabel = (keyboardShortcut: Keys) => {
if (typeof keyboardShortcut === 'string') {
return keyboardShortcut.split('+').join(' + ');
}
return keyboardShortcut
.map(shortcut => getKeyboardShortcutLabel(shortcut))
.join(', ');
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit short, we'd need to be smarter than that and show icons for key modifiers dependent on the platform (e.g. command / windows)

@djhi
Copy link
Collaborator Author

djhi commented Jun 23, 2025

That's a good start but you should also add a general "keyboard shortcut" chapter to make it discoverable through search ,explaining how to add shortcuts for menu items and for custom actions.

Also, could you please add shortcuts to the simple example menu ?

If you don't mind, I'll add this new page when we'll have more components supporting keyboard shortcuts (buttons, etc.)

djhi and others added 2 commits June 25, 2025 16:54
Co-authored-by: Jean-Baptiste Kaiser <jb@marmelab.com>
Copy link
Member

@fzaninotto fzaninotto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome user experience! I even tested the shortcuts when editing a fom, and the hook is smart enough not to trigger them in this case.

@@ -0,0 +1,119 @@
import * as React from 'react';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seeing this file at the root of src, it seems to have a bigger importance that it actually has. I'd put this component (and its story) under the layout directory.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You remember that we will probably use it for buttons too?

@fzaninotto fzaninotto merged commit f233324 into next Jun 27, 2025
15 checks passed
@fzaninotto fzaninotto deleted the menu-item-keyboard-shortcuts branch June 27, 2025 08:06
@fzaninotto fzaninotto added this to the 5.10.0 milestone Jun 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
RFR Ready For Review
Development

Successfully merging this pull request may close these issues.

4 participants