-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Site Editor - add basic plugin support (#34460)
* add sidebar and menu items * add general sidebar actions * remove unnecessary props * Update packages/edit-site/src/components/header/plugin-more-menu-item/index.js
- Loading branch information
1 parent
30744d0
commit 1a1a9a0
Showing
5 changed files
with
247 additions
and
0 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
packages/edit-site/src/components/header/plugin-more-menu-item/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { ActionItem } from '@wordpress/interface'; | ||
import { compose } from '@wordpress/compose'; | ||
import { withPluginContext } from '@wordpress/plugins'; | ||
|
||
/** | ||
* Renders a menu item in `Plugins` group in `More Menu` drop down, and can be used to as a button or link depending on the props provided. | ||
* The text within the component appears as the menu item label. | ||
* | ||
* @param {Object} props Component properties. | ||
* @param {string} [props.href] When `href` is provided then the menu item is represented as an anchor rather than button. It corresponds to the `href` attribute of the anchor. | ||
* @param {WPBlockTypeIconRender} [props.icon=inherits from the plugin] The [Dashicon](https://developer.wordpress.org/resource/dashicons/) icon slug string, or an SVG WP element, to be rendered to the left of the menu item label. | ||
* @param {Function} [props.onClick=noop] The callback function to be executed when the user clicks the menu item. | ||
* @param {...*} [props.other] Any additional props are passed through to the underlying [Button](/packages/components/src/button/README.md) component. | ||
* | ||
* @example | ||
* ```js | ||
* // Using ES5 syntax | ||
* var __ = wp.i18n.__; | ||
* var PluginMoreMenuItem = wp.editSite.PluginMoreMenuItem; | ||
* var moreIcon = wp.element.createElement( 'svg' ); //... svg element. | ||
* | ||
* function onButtonClick() { | ||
* alert( 'Button clicked.' ); | ||
* } | ||
* | ||
* function MyButtonMoreMenuItem() { | ||
* return wp.element.createElement( | ||
* PluginMoreMenuItem, | ||
* { | ||
* icon: moreIcon, | ||
* onClick: onButtonClick, | ||
* }, | ||
* __( 'My button title' ) | ||
* ); | ||
* } | ||
* ``` | ||
* | ||
* @example | ||
* ```jsx | ||
* // Using ESNext syntax | ||
* import { __ } from '@wordpress/i18n'; | ||
* import { PluginMoreMenuItem } from '@wordpress/edit-site'; | ||
* import { more } from '@wordpress/icons'; | ||
* | ||
* function onButtonClick() { | ||
* alert( 'Button clicked.' ); | ||
* } | ||
* | ||
* const MyButtonMoreMenuItem = () => ( | ||
* <PluginMoreMenuItem | ||
* icon={ more } | ||
* onClick={ onButtonClick } | ||
* > | ||
* { __( 'My button title' ) } | ||
* </PluginMoreMenuItem> | ||
* ); | ||
* ``` | ||
* | ||
* @return {WPComponent} The component to be rendered. | ||
*/ | ||
export default compose( | ||
withPluginContext( ( context, ownProps ) => { | ||
return { | ||
icon: ownProps.icon || context.icon, | ||
name: 'core/edit-site/plugin-more-menu', | ||
}; | ||
} ) | ||
)( ActionItem ); |
64 changes: 64 additions & 0 deletions
64
packages/edit-site/src/components/header/plugin-sidebar-more-menu-item/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { ComplementaryAreaMoreMenuItem } from '@wordpress/interface'; | ||
|
||
/** | ||
* Renders a menu item in `Plugins` group in `More Menu` drop down, | ||
* and can be used to activate the corresponding `PluginSidebar` component. | ||
* The text within the component appears as the menu item label. | ||
* | ||
* @param {Object} props Component props. | ||
* @param {string} props.target A string identifying the target sidebar you wish to be activated by this menu item. Must be the same as the `name` prop you have given to that sidebar. | ||
* @param {WPBlockTypeIconRender} [props.icon=inherits from the plugin] The [Dashicon](https://developer.wordpress.org/resource/dashicons/) icon slug string, or an SVG WP element, to be rendered to the left of the menu item label. | ||
* | ||
* @example | ||
* ```js | ||
* // Using ES5 syntax | ||
* var __ = wp.i18n.__; | ||
* var PluginSidebarMoreMenuItem = wp.editSite.PluginSidebarMoreMenuItem; | ||
* var moreIcon = wp.element.createElement( 'svg' ); //... svg element. | ||
* | ||
* function MySidebarMoreMenuItem() { | ||
* return wp.element.createElement( | ||
* PluginSidebarMoreMenuItem, | ||
* { | ||
* target: 'my-sidebar', | ||
* icon: moreIcon, | ||
* }, | ||
* __( 'My sidebar title' ) | ||
* ) | ||
* } | ||
* ``` | ||
* | ||
* @example | ||
* ```jsx | ||
* // Using ESNext syntax | ||
* import { __ } from '@wordpress/i18n'; | ||
* import { PluginSidebarMoreMenuItem } from '@wordpress/edit-site'; | ||
* import { more } from '@wordpress/icons'; | ||
* | ||
* const MySidebarMoreMenuItem = () => ( | ||
* <PluginSidebarMoreMenuItem | ||
* target="my-sidebar" | ||
* icon={ more } | ||
* > | ||
* { __( 'My sidebar title' ) } | ||
* </PluginSidebarMoreMenuItem> | ||
* ); | ||
* ``` | ||
* | ||
* @return {WPComponent} The component to be rendered. | ||
*/ | ||
|
||
export default function PluginSidebarMoreMenuItem( props ) { | ||
return ( | ||
<ComplementaryAreaMoreMenuItem | ||
// Menu item is marked with unstable prop for backward compatibility. | ||
// @see https://github.com/WordPress/gutenberg/issues/14457 | ||
__unstableExplicitMenuItem | ||
scope="core/edit-site" | ||
{ ...props } | ||
/> | ||
); | ||
} |
80 changes: 80 additions & 0 deletions
80
packages/edit-site/src/components/sidebar/plugin-sidebar/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { ComplementaryArea } from '@wordpress/interface'; | ||
|
||
/** | ||
* Renders a sidebar when activated. The contents within the `PluginSidebar` will appear as content within the sidebar. | ||
* It also automatically renders a corresponding `PluginSidebarMenuItem` component when `isPinnable` flag is set to `true`. | ||
* If you wish to display the sidebar, you can with use the `PluginSidebarMoreMenuItem` component or the `wp.data.dispatch` API: | ||
* | ||
* ```js | ||
* wp.data.dispatch( 'core/edit-site' ).openGeneralSidebar( 'plugin-name/sidebar-name' ); | ||
* ``` | ||
* | ||
* @see PluginSidebarMoreMenuItem | ||
* | ||
* @param {Object} props Element props. | ||
* @param {string} props.name A string identifying the sidebar. Must be unique for every sidebar registered within the scope of your plugin. | ||
* @param {string} [props.className] An optional class name added to the sidebar body. | ||
* @param {string} props.title Title displayed at the top of the sidebar. | ||
* @param {boolean} [props.isPinnable=true] Whether to allow to pin sidebar to the toolbar. When set to `true` it also automatically renders a corresponding menu item. | ||
* @param {WPBlockTypeIconRender} [props.icon=inherits from the plugin] The [Dashicon](https://developer.wordpress.org/resource/dashicons/) icon slug string, or an SVG WP element, to be rendered when the sidebar is pinned to toolbar. | ||
* | ||
* @example | ||
* ```js | ||
* // Using ES5 syntax | ||
* var __ = wp.i18n.__; | ||
* var el = wp.element.createElement; | ||
* var PanelBody = wp.components.PanelBody; | ||
* var PluginSidebar = wp.editSite.PluginSidebar; | ||
* var moreIcon = wp.element.createElement( 'svg' ); //... svg element. | ||
* | ||
* function MyPluginSidebar() { | ||
* return el( | ||
* PluginSidebar, | ||
* { | ||
* name: 'my-sidebar', | ||
* title: 'My sidebar title', | ||
* icon: moreIcon, | ||
* }, | ||
* el( | ||
* PanelBody, | ||
* {}, | ||
* __( 'My sidebar content' ) | ||
* ) | ||
* ); | ||
* } | ||
* ``` | ||
* | ||
* @example | ||
* ```jsx | ||
* // Using ESNext syntax | ||
* import { __ } from '@wordpress/i18n'; | ||
* import { PanelBody } from '@wordpress/components'; | ||
* import { PluginSidebar } from '@wordpress/edit-site'; | ||
* import { more } from '@wordpress/icons'; | ||
* | ||
* const MyPluginSidebar = () => ( | ||
* <PluginSidebar | ||
* name="my-sidebar" | ||
* title="My sidebar title" | ||
* icon={ more } | ||
* > | ||
* <PanelBody> | ||
* { __( 'My sidebar content' ) } | ||
* </PanelBody> | ||
* </PluginSidebar> | ||
* ); | ||
* ``` | ||
*/ | ||
export default function PluginSidebarEditSite( { className, ...props } ) { | ||
return ( | ||
<ComplementaryArea | ||
panelClassName={ className } | ||
className="edit-site-sidebar" | ||
scope="core/edit-site" | ||
{ ...props } | ||
/> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters