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

Make SaveButton extensible #56807

Open
wants to merge 7 commits into
base: trunk
Choose a base branch
from
Open
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
27 changes: 25 additions & 2 deletions packages/edit-site/src/components/save-button/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/**
* WordPress dependencies
*/
import { useMemo, useCallback } from '@wordpress/element';
import { applyFilters } from '@wordpress/hooks';
import { useSelect, useDispatch } from '@wordpress/data';
import { Button } from '@wordpress/components';
import { __, sprintf } from '@wordpress/i18n';
Expand Down Expand Up @@ -52,6 +54,7 @@ export default function SaveButton( {
previewingThemeName: previewingTheme?.name?.rendered,
};
}, [] );

const { setIsSaveViewOpened } = useDispatch( editSiteStore );

const activateSaveEnabled = isPreviewingTheme() || isDirty;
Expand All @@ -78,7 +81,27 @@ export default function SaveButton( {
}
return __( 'Save' );
};
const label = getLabel();

/**
* We focus on adding the customization to the SaveButton's `onClick` and `label` for now.
* We will provide the customization to the other entry points (e.g., SavePanel, SaveHub) in the future if needed.
* @see https://github.com/WordPress/gutenberg/pull/56807
*/
const onClick = useCallback( () => {
const callback = applyFilters( 'edit-site.SaveButton.onClick', () =>
setIsSaveViewOpened( true )
);
callback();
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we can just return the function but it's optional 🙂

Copy link
Contributor

Choose a reason for hiding this comment

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

I like this idea too.

}, [ setIsSaveViewOpened ] );
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we also add the dependencies here. Did you get a warning? See #24914

const label = useMemo( () => {
return applyFilters( 'edit-site.SaveButton.label', getLabel(), {
isSaving,
disabled,
isPreviewingTheme,
Copy link
Contributor

Choose a reason for hiding this comment

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

It might be better to add it to the deps as well even if it won't be changed.

But the naming is still confusing as people may not notice it's a function instead of the boolean value. However, it's out of the scope of this PR 🙂

defaultLabel,
isDirty,
} );
}, [ isSaving, disabled, defaultLabel, isDirty, getLabel ] );

return (
<Button
Expand All @@ -87,7 +110,7 @@ export default function SaveButton( {
aria-disabled={ disabled }
aria-expanded={ isSaveViewOpen }
isBusy={ isSaving }
onClick={ disabled ? undefined : () => setIsSaveViewOpened( true ) }
onClick={ disabled ? undefined : onClick }
label={ label }
/*
* We want the tooltip to show the keyboard shortcut only when the
Expand Down
28 changes: 28 additions & 0 deletions packages/edit-site/src/components/save-hub/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { store as blockEditorStore } from '@wordpress/block-editor';
import { check } from '@wordpress/icons';
import { privateApis as routerPrivateApis } from '@wordpress/router';
import { store as noticesStore } from '@wordpress/notices';
import { addFilter, removeFilter } from '@wordpress/hooks';
import { useEffect } from '@wordpress/element';

/**
* Internal dependencies
Expand Down Expand Up @@ -115,6 +117,32 @@ export default function SaveHub() {
};
}, [] );

// This is only for testing purposes.
useEffect( () => {
addFilter(
'edit-site.SaveButton.onClick',
'my-plugin',
( originalFunction ) => {
return () => {
// eslint-disable-next-line no-alert
window.alert( 'Custom action' );
originalFunction();
};
}
);
addFilter(
'edit-site.SaveButton.label',
'my-plugin',
( originalLabel ) => {
return `Custom ${ originalLabel }`;
}
);
return () => {
removeFilter( 'edit-site.SaveButton.onClick', 'my-plugin' );
removeFilter( 'edit-site.SaveButton.label', 'my-plugin' );
};
}, [] );

const saveCurrentEntity = async () => {
if ( ! dirtyCurrentEntity ) return;

Expand Down
Loading