Skip to content
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

Add support for registering commands without icons #53647

Merged
merged 5 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/commands/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"@wordpress/icons": "file:../icons",
"@wordpress/keyboard-shortcuts": "file:../keyboard-shortcuts",
"@wordpress/private-apis": "file:../private-apis",
"classnames": "^2.3.1",
"cmdk": "^0.2.0",
"rememo": "^4.0.2"
},
Expand Down
16 changes: 12 additions & 4 deletions packages/commands/src/components/command-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* External dependencies
*/
import { Command, useCommandState } from 'cmdk';
import classnames from 'classnames';

/**
* WordPress dependencies
Expand Down Expand Up @@ -53,9 +54,14 @@ function CommandMenuLoader( { name, search, hook, setLoader, close } ) {
>
<HStack
alignment="left"
className="commands-command-menu__item"
className={ classnames(
'commands-command-menu__item',
{
'has-icon': command.icon,
}
) }
>
<Icon icon={ command.icon } />
{ command.icon && <Icon icon={ command.icon } /> }
<span>
<TextHighlight
text={ command.label }
Expand Down Expand Up @@ -123,9 +129,11 @@ export function CommandMenuGroup( { isContextual, search, setLoader, close } ) {
>
<HStack
alignment="left"
className="commands-command-menu__item"
className={ classnames( 'commands-command-menu__item', {
'has-icon': command.icon,
} ) }
>
<Icon icon={ command.icon } />
{ command.icon && <Icon icon={ command.icon } /> }
<span>
<TextHighlight
text={ command.label }
Expand Down
11 changes: 10 additions & 1 deletion packages/commands/src/components/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@
cursor: pointer;
display: flex;
align-items: center;
padding: $grid-unit;
color: $gray-900;
font-size: $default-font-size;
min-height: $button-size-next-default-40px;

&[aria-selected="true"],
&:active {
Expand All @@ -88,6 +88,15 @@
svg {
fill: $gray-900;
}

> div {
padding: $grid-unit;
padding-left: $grid-unit-50; // Account for commands without icons.
}

> .has-icon {
padding-left: $grid-unit;
}
}

[cmdk-root] > [cmdk-list] {
Expand Down
6 changes: 0 additions & 6 deletions packages/edit-post/src/hooks/commands/use-common-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { useSelect, useDispatch } from '@wordpress/data';
import { __, isRTL } from '@wordpress/i18n';
import {
code,
cog,
drawerLeft,
drawerRight,
blockDefault,
Expand Down Expand Up @@ -92,7 +91,6 @@ export default function useCommonCommands() {
useCommand( {
name: 'core/toggle-distraction-free',
label: __( 'Toggle distraction free' ),
icon: cog,
callback: ( { close } ) => {
toggle( 'core/edit-post', 'distractionFree' );
close();
Expand All @@ -102,7 +100,6 @@ export default function useCommonCommands() {
useCommand( {
name: 'core/toggle-spotlight-mode',
label: __( 'Toggle spotlight mode' ),
icon: cog,
callback: ( { close } ) => {
toggle( 'core/edit-post', 'focusMode' );
close();
Expand Down Expand Up @@ -132,7 +129,6 @@ export default function useCommonCommands() {
useCommand( {
name: 'core/toggle-top-toolbar',
label: __( 'Toggle top toolbar' ),
icon: cog,
callback: ( { close } ) => {
toggle( 'core/edit-post', 'fixedToolbar' );
close();
Expand All @@ -152,7 +148,6 @@ export default function useCommonCommands() {
useCommand( {
name: 'core/open-preferences',
label: __( 'Editor preferences' ),
icon: cog,
callback: () => {
openModal( PREFERENCES_MODAL_NAME );
},
Expand All @@ -172,7 +167,6 @@ export default function useCommonCommands() {
label: showBlockBreadcrumbs
? __( 'Hide block breadcrumbs' )
: __( 'Show block breadcrumbs' ),
icon: cog,
callback: ( { close } ) => {
toggle( 'core/edit-post', 'showBlockBreadcrumbs' );
close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
drawerLeft,
drawerRight,
blockDefault,
cog,
code,
keyboard,
} from '@wordpress/icons';
Expand Down Expand Up @@ -256,7 +255,6 @@ function useEditUICommands() {
commands.push( {
name: 'core/toggle-spotlight-mode',
label: __( 'Toggle spotlight mode' ),
icon: cog,
callback: ( { close } ) => {
toggle( 'core/edit-site', 'focusMode' );
close();
Expand All @@ -266,7 +264,6 @@ function useEditUICommands() {
commands.push( {
name: 'core/toggle-distraction-free',
label: __( 'Toggle distraction free' ),
icon: cog,
callback: ( { close } ) => {
setPreference( 'core/edit-site', 'fixedToolbar', false );
setIsInserterOpened( false );
Expand All @@ -289,7 +286,6 @@ function useEditUICommands() {
commands.push( {
name: 'core/toggle-top-toolbar',
label: __( 'Toggle top toolbar' ),
icon: cog,
callback: ( { close } ) => {
toggle( 'core/edit-site', 'fixedToolbar' );
close();
Expand All @@ -311,7 +307,6 @@ function useEditUICommands() {
commands.push( {
name: 'core/open-preferences',
label: __( 'Editor preferences' ),
icon: cog,
callback: () => {
openModal( PREFERENCES_MODAL_NAME );
},
Expand All @@ -331,7 +326,6 @@ function useEditUICommands() {
label: showBlockBreadcrumbs
? __( 'Hide block breadcrumbs' )
: __( 'Show block breadcrumbs' ),
icon: cog,
callback: ( { close } ) => {
toggle( 'core/edit-site', 'showBlockBreadcrumbs' );
close();
Expand Down