-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Conversation
@@ -44,7 +45,7 @@ function PluginsPanel( { onClose, plugin } ) { | |||
/> | |||
</div> | |||
<div className="edit-post-plugins-panel__content"> |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
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.
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?
There was a problem hiding this comment.
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).
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.
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.
edit-post/api/components/context.js
Outdated
/** | ||
* External dependencies | ||
*/ | ||
import PropTypes from 'prop-types'; |
There was a problem hiding this comment.
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
.
edit-post/api/index.js
Outdated
registerSidebar as __experimentalRegisterSidebar, | ||
activateSidebar as __experimentalActivateSidebar, | ||
} from './sidebar'; | ||
registerPlugin, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice 👍
There was a problem hiding this comment.
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' ); |
There was a problem hiding this comment.
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.
…separate sidebar layout component
edit-post/components/layout/index.js
Outdated
|
||
function GeneralSidebar( { openedGeneralSidebar } ) { | ||
switch ( openedGeneralSidebar ) { | ||
case 'editor': | ||
return <Sidebar />; | ||
case 'plugin': | ||
return <PluginsPanel />; | ||
return <PluginSidebarSlot />; |
There was a problem hiding this comment.
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.
edit-post/components/layout/index.js
Outdated
@@ -111,6 +107,7 @@ function Layout( { | |||
openedGeneralSidebar={ openedGeneralSidebar } /> | |||
} | |||
<Popover.Slot /> | |||
<PluginFills /> |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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' ) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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)
There was a problem hiding this 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.
edit-post/store/selectors.js
Outdated
|
||
return activeGeneralSidebar.startsWith( 'plugin-sidebar/' ); | ||
const activeGeneralSidebar = getActiveGeneralSidebarName( state ); | ||
return !! activeGeneralSidebar && ! isEditorSidebarOpened( state ); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
plugins/api/index.js
Outdated
|
||
const registry = PluginRegistry.getInstance(); | ||
settings.name = name; | ||
settings.sidebar = {}; |
There was a problem hiding this comment.
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
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 😉 |
There was a problem hiding this 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.
The className for the plugin sidebar was changed in 644b7e8. But this was missed in one location. This commit changes the class there too.
There was a problem hiding this 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'; |
There was a problem hiding this comment.
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
?
Lines 77 to 80 in 10df23c
{ | |
selector: 'ImportDeclaration[source.value=/^viewport$/]', | |
message: 'Use @wordpress/viewport as import path instead.', | |
}, |
There was a problem hiding this comment.
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.
plugins/README.md
Outdated
</Fragment> | ||
); | ||
|
||
wp.plugins.__experimental.registerPlugin( 'plugin-names', { |
There was a problem hiding this comment.
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.)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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} \ |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
--> #5664, for starters
@mcsf Completely agree, but these are tooling issues. Have you created issues for the points you are bringing up? |
There was a problem hiding this 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.
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: