Skip to content

Commit df331ae

Browse files
committed
Toolbar: Add command palette trigger button.
Props wildworks, hmbashar, bpayton, mcsf, joedolson, sabernhardt, westonruter. See #64672. git-svn-id: https://develop.svn.wordpress.org/trunk@61912 602fd350-edb4-49c9-b593-d223f7449a82
1 parent db24a90 commit df331ae

2 files changed

Lines changed: 41 additions & 0 deletions

File tree

src/wp-includes/admin-bar.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -934,6 +934,44 @@ function wp_admin_bar_edit_menu( $wp_admin_bar ) {
934934
}
935935
}
936936

937+
/**
938+
* Adds the command palette trigger button.
939+
*
940+
* Displays a button in the admin bar that shows the keyboard shortcut
941+
* for opening the command palette.
942+
*
943+
* @since 7.0.0
944+
*
945+
* @param WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar instance.
946+
*/
947+
function wp_admin_bar_command_palette_menu( WP_Admin_Bar $wp_admin_bar ): void {
948+
if ( ! is_admin() ) {
949+
return;
950+
}
951+
952+
$is_apple_os = (bool) preg_match( '/Macintosh|Mac OS X|Mac_PowerPC/i', $_SERVER['HTTP_USER_AGENT'] ?? '' );
953+
$shortcut_label = $is_apple_os
954+
? _x( '⌘K', 'keyboard shortcut to open the command palette' )
955+
: _x( 'Ctrl+K', 'keyboard shortcut to open the command palette' );
956+
$title = sprintf(
957+
'<span class="ab-label"><kbd>%s</kbd><span class="screen-reader-text"> %s</span></span>',
958+
$shortcut_label,
959+
/* translators: Hidden accessibility text. */
960+
__( 'Open command palette' ),
961+
);
962+
$wp_admin_bar->add_node(
963+
array(
964+
'id' => 'command-palette',
965+
'title' => $title,
966+
'href' => '#',
967+
'meta' => array(
968+
'class' => 'hide-if-no-js',
969+
'onclick' => 'wp.data.dispatch( "core/commands" ).open(); return false;',
970+
),
971+
)
972+
);
973+
}
974+
937975
/**
938976
* Adds "Add New" menu.
939977
*

src/wp-includes/class-wp-admin-bar.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,9 @@ public function add_menus() {
661661
add_action( 'admin_bar_menu', 'wp_admin_bar_customize_menu', 40 );
662662
add_action( 'admin_bar_menu', 'wp_admin_bar_updates_menu', 50 );
663663

664+
// Command palette.
665+
add_action( 'admin_bar_menu', 'wp_admin_bar_command_palette_menu', 55 );
666+
664667
// Content-related.
665668
if ( ! is_network_admin() && ! is_user_admin() ) {
666669
add_action( 'admin_bar_menu', 'wp_admin_bar_comments_menu', 60 );

0 commit comments

Comments
 (0)