Skip to content
Merged
81 changes: 81 additions & 0 deletions lib/compat/wordpress-7.0/command-palette.php
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'] ?? '' );
Copy link
Copy Markdown
Contributor Author

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_FAMILY cannot be used. This is because PHP_OS_FAMILY determines the OS of the machine on which PHP is running. For example, if a Windows user runs a local environment in WSL2, PHP_OS_FAMILY will be Darwin, not Windows. This is not the expected result.

Copy link
Copy Markdown
Member

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.platform for this.

Copy link
Copy Markdown
Contributor Author

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 🤔

Copy link
Copy Markdown
Member

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 😅

Copy link
Copy Markdown
Contributor Author

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.

$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' => '#',
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This link actually triggers Command Palette, and because of the event.preventDefault(), there shouldn't be a jump.

https://github.com/WordPress/gutenberg/pull/75757/changes#diff-f439362706c44e4cb24183333fd5edb4890f457ca40b1f573d8168217ae6829aR54

Actually, qw should render a button element here, but as far as I know, there is no precedent for rendering a button element in the admin bar. Furthermore, some CSS adjustments would be required, so I deliberately used the a element to avoid that.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

  • Yes, a button would be more appropriate than a link.
  • No, there is no precedent in core for a button element in the admin toolbar. The admin menu toggle for small screens used a link with href="#" (wp_admin_bar_sidebar_toggle).
  • Yes, a button would require several CSS adjustments, including focus styles.

'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' );
1 change: 1 addition & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ function gutenberg_is_experiment_enabled( $name ) {
require __DIR__ . '/compat/wordpress-7.0/blocks.php';
require __DIR__ . '/compat/wordpress-7.0/kses.php';
require __DIR__ . '/compat/wordpress-7.0/media.php';
require __DIR__ . '/compat/wordpress-7.0/command-palette.php';

// Experimental features.
require __DIR__ . '/experimental/block-editor-settings-mobile.php';
Expand Down
Loading