Extensibility: Plugin sidebar api#5430
Extensibility: Plugin sidebar api#5430atimmer merged 88 commits intoWordPress:masterfrom Yoast:add/slot-fill-sidebar-api
Conversation
| @@ -44,7 +45,7 @@ function PluginsPanel( { onClose, plugin } ) { | |||
| /> | |||
| </div> | |||
| <div className="edit-post-plugins-panel__content"> | |||
There was a problem hiding this comment.
It seems like PluginsPanel should be converted into the slot.
There was a problem hiding this comment.
Yes, that's the plan.
edit-post/api/plugin.js
Outdated
| const Context = plugin.context; | ||
| return ( | ||
| <Context.Provider key={ plugin.name } value={ plugin.name }> | ||
| { plugin.render() } |
There was a problem hiding this comment.
Shouldn't you wrap this with <PluginSidebar /> to activate the fill?
There was a problem hiding this comment.
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).
edit-post/api/plugin.js
Outdated
| { map( plugins, plugin => { | ||
| const Context = plugin.context; | ||
| return ( | ||
| <Context.Provider key={ plugin.name } value={ plugin.name }> |
There was a problem hiding this comment.
This is also a good place to put logic to make sure that sidebar is only rendered when a given plugin is active.
edit-post/api/components/context.js
Outdated
| /** | ||
| * External dependencies | ||
| */ | ||
| import PropTypes from 'prop-types'; |
There was a problem hiding this comment.
We are not using PropTypes at all in Gutenberg. It probably wasn't removed from package.json.
edit-post/api/index.js
Outdated
| registerSidebar as __experimentalRegisterSidebar, | ||
| activateSidebar as __experimentalActivateSidebar, | ||
| } from './sidebar'; | ||
| registerPlugin, |
There was a problem hiding this comment.
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' ); |
There was a problem hiding this comment.
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.
…separate sidebar layout component
edit-post/components/layout/index.js
Outdated
| return <Sidebar />; | ||
| case 'plugin': | ||
| return <PluginsPanel />; | ||
| return <PluginSidebarSlot />; |
There was a problem hiding this comment.
It would be nice to avoid using the slot part in the name of the component. It should be known only internally.
edit-post/components/layout/index.js
Outdated
| openedGeneralSidebar={ openedGeneralSidebar } /> | ||
| } | ||
| <Popover.Slot /> | ||
| <PluginFills /> |
There was a problem hiding this comment.
This component might be renamed to PluginArea or something like this to better reflect its role.
| */ | ||
| const SLOT_NAME = 'PluginSidebar'; | ||
|
|
||
| class SidebarErrorBoundary extends Component { |
There was a problem hiding this comment.
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' ) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
It's a matter of providing accurate feedback to the developer I think?
There was a problem hiding this comment.
We shouldn't throw.
…/'-prefix in sidebar identifier
|
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. |
Use createElement
Apparently the convention we established (see: Popover)
abotteram
left a comment
There was a problem hiding this comment.
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 ); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
It's the nice thing about a selector is that it abstracts away these underlying implementation-detail concerns.
plugins/api/index.js
Outdated
|
|
||
| const registry = PluginRegistry.getInstance(); | ||
| settings.name = name; | ||
| settings.sidebar = {}; |
There was a problem hiding this comment.
This doesn't do anything
|
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 😉 |
aduth
left a comment
There was a problem hiding this comment.
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.
The className for the plugin sidebar was changed in 644b7e8. But this was missed in one location. This commit changes the class there too.
| } from '@wordpress/editor'; | ||
| import { withDispatch, withSelect } from '@wordpress/data'; | ||
| import { compose } from '@wordpress/element'; | ||
| import { PluginArea } from '@wordpress/plugins'; |
plugins/README.md
Outdated
| </Fragment> | ||
| ); | ||
|
|
||
| wp.plugins.__experimental.registerPlugin( 'plugin-names', { |
There was a problem hiding this comment.
This is outdated, wp.plugins.registerPlugin is available. (PR description is also outdated.)
There was a problem hiding this comment.
This is outdated,
wp.plugins.registerPluginis 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.
There was a problem hiding this comment.
This is outdated,
wp.plugins.registerPluginis available. (PR description is also outdated.)
Updated in 853655c.
| 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} \ |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
|
@mcsf Completely agree, but these are tooling issues. Have you created issues for the points you are bringing up? |
atimmer
left a comment
There was a problem hiding this comment.
Looks like the tooling issues are resolved (or worked on). Giving my own 👍 here and then merging.
Description
Also props to @IreneStr.
Usage:
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:
How Has This Been Tested?
Screenshots (jpeg or gifs if applicable):
Types of changes
New feature.
Checklist: