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

Commands: Allow disabling and enabling comments #57205

Merged
merged 1 commit into from Dec 19, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/commands/CHANGELOG.md
Expand Up @@ -2,6 +2,10 @@

## Unreleased

## Enhancements

- Support conditional commands and commands loaders using the "disabled" config.

## 0.19.0 (2023-12-13)

## 0.18.0 (2023-11-29)
Expand Down
4 changes: 4 additions & 0 deletions packages/commands/src/hooks/use-command-loader.js
Expand Up @@ -82,6 +82,9 @@ export default function useCommandLoader( loader ) {
const { registerCommandLoader, unregisterCommandLoader } =
useDispatch( commandsStore );
useEffect( () => {
if ( loader.disabled ) {
return;
}
registerCommandLoader( {
name: loader.name,
hook: loader.hook,
Expand All @@ -94,6 +97,7 @@ export default function useCommandLoader( loader ) {
loader.name,
loader.hook,
loader.context,
loader.disabled,
registerCommandLoader,
unregisterCommandLoader,
] );
Expand Down
4 changes: 4 additions & 0 deletions packages/commands/src/hooks/use-command.js
Expand Up @@ -38,6 +38,9 @@ export default function useCommand( command ) {
}, [ command.callback ] );

useEffect( () => {
if ( command.disabled ) {
return;
}
registerCommand( {
name: command.name,
context: command.context,
Expand All @@ -55,6 +58,7 @@ export default function useCommand( command ) {
command.searchLabel,
command.icon,
command.context,
command.disabled,
registerCommand,
unregisterCommand,
] );
Expand Down
8 changes: 5 additions & 3 deletions packages/commands/src/store/actions.js
Expand Up @@ -11,6 +11,7 @@
* @property {string=} context Command context.
* @property {JSX.Element} icon Command icon.
* @property {Function} callback Command callback.
* @property {boolean} disabled Whether to disable the command.
*/

/**
Expand All @@ -22,9 +23,10 @@
*
* @typedef {Object} WPCommandLoaderConfig
*
* @property {string} name Command loader name.
* @property {string=} context Command loader context.
* @property {WPCommandLoaderHook} hook Command loader hook.
* @property {string} name Command loader name.
* @property {string=} context Command loader context.
* @property {WPCommandLoaderHook} hook Command loader hook.
* @property {boolean} disabled Whether to disable the command loader.
*/

/**
Expand Down