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

Extensibility: Plugin sidebar api #5430

Merged
merged 88 commits into from
Mar 19, 2018
Merged

Extensibility: Plugin sidebar api #5430

merged 88 commits into from
Mar 19, 2018

Conversation

abotteram
Copy link
Contributor

@abotteram abotteram commented Mar 6, 2018

Description

Also props to @IreneStr.

Usage:

const { Fragment } = wp.element;
const { PluginSidebar, registerPlugin } = wp.editPost;

// Separate component to avoid "unnamed component" warning.
const Component = () => {
	return (
		<Fragment>
			<PluginSidebar name="sidebar" title="Sidebar title">
				<h2>Some content!</h2>
			</PluginSidebar>
			<PluginSidebar name="second-sidebar" title="Sidebar 2 title">
				<h2>Some more content!</h2>
			</PluginSidebar>
		</Fragment>
	);
};

// Register the plugin using the new API
registerPlugin( {
	name: "plugin-namespace", // Should start with a lowercase letter and can only contain lowercase letters, numbers and dashes.
	render: Component,
} );

Important: Due to a bug in the current API you should open and close the editor sidebar before executing the following line.

Then to activate the sidebar execute the following line in the console:
wp.data.dispatch( 'core/edit-post' ).openGeneralSidebar( 'plugin', 'plugin-namespace/sidebar' );
or
wp.data.dispatch( 'core/edit-post' ).openGeneralSidebar( 'plugin', 'plugin-namespace/second-sidebar' );

Known issues:

  • Providing an unknown namespace/sidebarName id opens a white sidebar.
  • A way to activate the sidebar without needing the console.

How Has This Been Tested?

  • Tested in editor, using console.
  • Added unit tests.

Screenshots (jpeg or gifs if applicable):

Types of changes

New feature.

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code has proper inline documentation.

@abotteram abotteram mentioned this pull request Mar 6, 2018
3 tasks
@gziolo gziolo self-requested a review March 6, 2018 09:58
@@ -44,7 +45,7 @@ function PluginsPanel( { onClose, plugin } ) {
/>
</div>
<div className="edit-post-plugins-panel__content">
Copy link
Member

@gziolo gziolo Mar 6, 2018

Choose a reason for hiding this comment

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

It seems like PluginsPanel should be converted into the slot.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, that's the plan.

const Context = plugin.context;
return (
<Context.Provider key={ plugin.name } value={ plugin.name }>
{ plugin.render() }
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't you wrap this with <PluginSidebar /> to activate the fill?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

PluginSidebar is the component the plugin developer uses in the registerPlugin API call. When rendering the plugins you don't know what kind of ui-element it is (Sidebar, screen takeover, etc).

{ map( plugins, plugin => {
const Context = plugin.context;
return (
<Context.Provider key={ plugin.name } value={ plugin.name }>
Copy link
Member

Choose a reason for hiding this comment

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

This is also a good place to put logic to make sure that sidebar is only rendered when a given plugin is active.

@Soean Soean added the [Status] In Progress Tracking issues with work in progress label Mar 6, 2018
/**
* External dependencies
*/
import PropTypes from 'prop-types';
Copy link
Member

Choose a reason for hiding this comment

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

We are not using PropTypes at all in Gutenberg. It probably wasn't removed from package.json.

registerSidebar as __experimentalRegisterSidebar,
activateSidebar as __experimentalActivateSidebar,
} from './sidebar';
registerPlugin,
Copy link
Member

Choose a reason for hiding this comment

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

Nice 👍

Copy link
Member

Choose a reason for hiding this comment

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

To repeat what I said above. Let's mark it as experimental for one release.


const tree = renderer.toJSON();

expect( tree.children[ 0 ] ).toEqual( 'plugin-namespace' );
Copy link
Member

Choose a reason for hiding this comment

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

Gutenberg has jest-enzyme bundled, it is possible to use snapshots here and mount helper. See: https://github.com/WordPress/gutenberg/blob/master/docs/testing-overview.md#snapshot-testing.

Alexander Botteram added 2 commits March 7, 2018 14:39

function GeneralSidebar( { openedGeneralSidebar } ) {
switch ( openedGeneralSidebar ) {
case 'editor':
return <Sidebar />;
case 'plugin':
return <PluginsPanel />;
return <PluginSidebarSlot />;
Copy link
Member

Choose a reason for hiding this comment

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

It would be nice to avoid using the slot part in the name of the component. It should be known only internally.

@@ -111,6 +107,7 @@ function Layout( {
openedGeneralSidebar={ openedGeneralSidebar } />
}
<Popover.Slot />
<PluginFills />
Copy link
Member

Choose a reason for hiding this comment

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

This component might be renamed to PluginArea or something like this to better reflect its role.

*/
const SLOT_NAME = 'PluginSidebar';

class SidebarErrorBoundary extends Component {
Copy link
Member

Choose a reason for hiding this comment

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

This component should be put in its own file. In the long run, it should be generalized to support or types of plugins.

constructor( props ) {
super( props );

if ( typeof props.name !== 'string' ) {
Copy link
Member

Choose a reason for hiding this comment

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

Do we need to include this validation in here? I don't feel like anything bad would happen if the non-string value would be provided. It would be converted to a string and it wouldn't be possible to activate such sidebar.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's a matter of providing accurate feedback to the developer I think?

Copy link
Member

@gziolo gziolo Mar 8, 2018

Choose a reason for hiding this comment

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

It feels like this kind of behavior should be enabled only in development env. I don't know, @mtias and @aduth - do we have some kind of strategy in place?

Copy link
Member

Choose a reason for hiding this comment

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

We shouldn't throw.

#5430 (comment)

@aduth aduth added the [Feature] Extensibility The ability to extend blocks or the editing experience label Mar 7, 2018
@aduth
Copy link
Member

aduth commented Mar 15, 2018

Take a look at 6540b05 as what I had in mind. Let me know what you think.

Specifically, it's leveraging Slot/Fills, generating the name of the intended visible plugin's sidebar as the name of the slot, so only it would be shown.

Copy link
Contributor Author

@abotteram abotteram left a comment

Choose a reason for hiding this comment

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

A few points I would like to address in regards to @aduth's changes:

  • It's allowed to have 2 sidebars with the same name to be rendered, without any feedback why only one is showing.
  • It's not enforcing the plugin-sidebar naming conventions.

I still think it's wise to wrap the plugin sidebars in a separate error boundary, because we should protect the editor from crashing because of plugin code as much as possible.


return activeGeneralSidebar.startsWith( 'plugin-sidebar/' );
const activeGeneralSidebar = getActiveGeneralSidebarName( state );
return !! activeGeneralSidebar && ! isEditorSidebarOpened( state );
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was actually what I was trying to prevent because it doesn't allow any more sidebar types. But I guess that's a concern for later.

Copy link
Member

Choose a reason for hiding this comment

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

It's the nice thing about a selector is that it abstracts away these underlying implementation-detail concerns.


const registry = PluginRegistry.getInstance();
settings.name = name;
settings.sidebar = {};
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This doesn't do anything

@aduth
Copy link
Member

aduth commented Mar 16, 2018

I don't disagree, but those things are not strictly necessary for an MVP, and can be introduced and discussed in a separate follow-up pull request.

I do have thoughts on them though, if you'd like a few more rounds of back-and-forth 😉

Copy link
Member

@aduth aduth left a comment

Choose a reason for hiding this comment

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

One more thing: The sidebar doesn't look great on mobile. It appears the default sidebars apply a 100% width in a separate selector that this is not benefitting from.

Aside: We need to consolidate our concept of what a sidebar is, so we're not addressing each individually like this.

atimmer added 2 commits March 16, 2018 14:14
The className for the plugin sidebar was changed in 644b7e8. But
this was missed in one location. This commit changes the class there
too.
@atimmer atimmer added this to the 2.5 milestone Mar 16, 2018
Copy link
Member

@aduth aduth left a comment

Choose a reason for hiding this comment

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

👍

@@ -19,6 +19,7 @@ import {
} from '@wordpress/editor';
import { withDispatch, withSelect } from '@wordpress/data';
import { compose } from '@wordpress/element';
import { PluginArea } from '@wordpress/plugins';
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 add an entry to .eslintrc.js?

gutenberg/.eslintrc.js

Lines 77 to 80 in 10df23c

{
selector: 'ImportDeclaration[source.value=/^viewport$/]',
message: 'Use @wordpress/viewport as import path instead.',
},

Copy link
Member

Choose a reason for hiding this comment

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

Should we add an entry to .eslintrc.js?

Added in a1de97b.

</Fragment>
);

wp.plugins.__experimental.registerPlugin( 'plugin-names', {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is outdated, wp.plugins.registerPlugin is available. (PR description is also outdated.)

Copy link
Member

Choose a reason for hiding this comment

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

This is outdated, wp.plugins.registerPlugin is available. (PR description is also outdated.)

I missed this. I assumed we were still exporting as __experimental. I'm fine with "unexperimental"-izing it at this point.

Copy link
Member

Choose a reason for hiding this comment

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

This is outdated, wp.plugins.registerPlugin is available. (PR description is also outdated.)

Updated in 853655c.

@@ -99,7 +99,7 @@ zip -r gutenberg.zip \
blocks/library/*/*.php \
post-content.js \
$vendor_scripts \
{blocks,components,date,editor,element,hooks,i18n,data,utils,edit-post,viewport}/build/*.{js,map} \
{blocks,components,date,editor,element,hooks,i18n,data,utils,edit-post,viewport,plugins}/build/*.{js,map} \
Copy link
Contributor

Choose a reason for hiding this comment

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

Tangent: Perhaps by reading webpack.config.js, it'd be nice if we didn't have to manually keep the build script, the ESLint config, etc. in sync.

Copy link
Member

Choose a reason for hiding this comment

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

Tangent: Perhaps by reading webpack.config.js, it'd be nice if we didn't have to manually keep the build script, the ESLint config, etc. in sync.

Yes, this has become an increasingly frequent source of friction.

Copy link
Contributor

Choose a reason for hiding this comment

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

--> #5664, for starters

@abotteram
Copy link
Contributor Author

abotteram commented Mar 17, 2018

@mcsf Completely agree, but these are tooling issues. Have you created issues for the points you are bringing up?

Copy link
Member

@atimmer atimmer left a comment

Choose a reason for hiding this comment

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

Looks like the tooling issues are resolved (or worked on). Giving my own 👍 here and then merging.

@atimmer atimmer merged commit 3ced7fb into WordPress:master Mar 19, 2018
@atimmer atimmer deleted the add/slot-fill-sidebar-api branch March 19, 2018 08:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Extensibility The ability to extend blocks or the editing experience Framework Issues related to broader framework topics, especially as it relates to javascript
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants