-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Add command palette trigger button to admin bar #75757
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
Changes from all commits
16531e4
16cd196
3a7b4f6
bf3d3b5
0b3866c
d8b1531
1fc6ec2
aae1832
4d92c41
ee88e67
779b377
5fb6abf
d7d3540
535b729
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| <?php | ||
| /** | ||
| * Adds the Command Palette trigger button to the admin bar. | ||
| * | ||
| * @param WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar instance. | ||
| */ | ||
| function gutenberg_admin_bar_command_palette_menu( WP_Admin_Bar $wp_admin_bar ): void { | ||
| if ( ! is_admin() || ! wp_script_is( 'wp-core-commands', 'enqueued' ) ) { | ||
| return; | ||
| } | ||
|
|
||
| $is_apple_os = (bool) preg_match( '/Macintosh|Mac OS X|Mac_PowerPC/i', $_SERVER['HTTP_USER_AGENT'] ?? '' ); | ||
| $shortcut_label = $is_apple_os | ||
| ? _x( '⌘K', 'keyboard shortcut to open the command palette' ) | ||
| : _x( 'Ctrl+K', 'keyboard shortcut to open the command palette' ); | ||
| $title = sprintf( | ||
| '<span class="ab-icon" aria-hidden="true"></span><span class="ab-label"><kbd>%s</kbd><span class="screen-reader-text"> %s</span></span>', | ||
| $shortcut_label, | ||
| /* translators: Hidden accessibility text. */ | ||
| __( 'Open command palette' ), | ||
| ); | ||
| $wp_admin_bar->add_node( | ||
| array( | ||
| 'id' => 'command-palette', | ||
| 'title' => $title, | ||
| 'href' => '#', | ||
t-hamano marked this conversation as resolved.
Show resolved
Hide resolved
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the command palette is opened by keyboard shortcut only, having a clickable button that just jumps to # is a UX regression.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This link actually triggers Command Palette, and because of the Actually, qw should render a
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| 'meta' => array( | ||
| 'class' => 'hide-if-no-js', | ||
| 'onclick' => 'wp.data.dispatch( "core/commands" ).open(); return false;', | ||
| ), | ||
| ) | ||
| ); | ||
| } | ||
| if ( has_action( 'admin_bar_menu', 'wp_admin_bar_command_palette_menu' ) ) { | ||
| remove_action( 'admin_bar_menu', 'wp_admin_bar_command_palette_menu', 55 ); | ||
| } | ||
| add_action( 'admin_bar_menu', 'gutenberg_admin_bar_command_palette_menu', 55 ); | ||
|
|
||
| function gutenberg_add_admin_bar_styles() { | ||
| if ( ! is_admin_bar_showing() ) { | ||
| return; | ||
| } | ||
| $css = <<<CSS | ||
| #wpadminbar #wp-admin-bar-command-palette .ab-icon { | ||
| display: none; /* Icon displayed only on mobile */ | ||
| } | ||
| #wpadminbar #wp-admin-bar-command-palette .ab-icon:before { | ||
| content: "\\f179"; | ||
| content: "\\f179" / ''; | ||
| } | ||
| #wpadminbar #wp-admin-bar-command-palette kbd { | ||
| background: transparent; | ||
| } | ||
| @media screen and (max-width: 782px) { | ||
| #wpadminbar #wp-admin-bar-command-palette .ab-icon { | ||
| display: block; /* Icon is only shown on mobile, while the keyboard shortcut is hidden */ | ||
| } | ||
| #wpadminbar #wp-admin-bar-command-palette .ab-icon:before { | ||
| text-indent: 0; | ||
| font: normal 32px/1 dashicons; | ||
| width: 52px; | ||
| text-align: center; | ||
| -webkit-font-smoothing: antialiased; | ||
| -moz-osx-font-smoothing: grayscale; | ||
| display: block; | ||
| font-size: 34px; | ||
| height: 46px; | ||
| line-height: 1.38235294; | ||
| top: 0; | ||
| } | ||
| #wpadminbar li#wp-admin-bar-command-palette { | ||
| display: block; | ||
| } | ||
| #wpadminbar #wp-admin-bar-command-palette { | ||
| position: static; | ||
| } | ||
| } | ||
| CSS; | ||
| wp_add_inline_style( 'admin-bar', $css ); | ||
| } | ||
| add_action( 'admin_bar_init', 'gutenberg_add_admin_bar_styles' ); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This might be the most difficult part.
By the way,
PHP_OS_FAMILYcannot be used. This is becausePHP_OS_FAMILYdetermines the OS of the machine on which PHP is running. For example, if a Windows user runs a local environment in WSL2,PHP_OS_FAMILYwill beDarwin, notWindows. This is not the expected result.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the decision is moved to JS, you can use
navigator.platformfor this.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we need the shortcut keys to be translatable, then we will need to check the device on the server side. If no translation is needed, then we can of course use your client-side approach. I'm not sure if they should be translatable or not 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think they need to be translated, but I'm not an expert on that. However, something to keep in mind is any scenario where page responses will be cached. If so, this would break the user agent sniffing.
This surely won't be happening in the admin, and the command palette isn't available on the frontend (yet?). But if it were available on the frontend, even so the admin bar isn't shown unless logged-in, although this is just a default. Nevertheless, it seems quite unlikely that the command palette would be served to unauthenticated users even then.
All that to say, it's probably safe for this to be server-rendered 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've tried dynamically rendering label text on the client side, but that can cause flashes when the page loads. Below is the behavior when simulating Slow 4G network in Chrome.
ba3c7da4d634d3f758dd54ecffc13502.mp4
Whether we translate shortcut keys or not, I think it's better to prepare label text on the server side.