@wordpress/edit-post
Version:
Edit Post module for WordPress.
157 lines (148 loc) • 5.06 kB
JavaScript
import { createElement } from "@wordpress/element";
/**
* WordPress dependencies
*/
import { IconButton, Panel } from '@wordpress/components';
import { withDispatch, withSelect } from '@wordpress/data';
import { Fragment } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { withPluginContext } from '@wordpress/plugins';
import { compose } from '@wordpress/compose';
/**
* Internal dependencies
*/
import PinnedPlugins from '../../header/pinned-plugins';
import Sidebar from '../';
import SidebarHeader from '../sidebar-header';
function PluginSidebar(props) {
var children = props.children,
className = props.className,
icon = props.icon,
isActive = props.isActive,
_props$isPinnable = props.isPinnable,
isPinnable = _props$isPinnable === void 0 ? true : _props$isPinnable,
isPinned = props.isPinned,
sidebarName = props.sidebarName,
title = props.title,
togglePin = props.togglePin,
toggleSidebar = props.toggleSidebar;
return createElement(Fragment, null, isPinnable && createElement(PinnedPlugins, null, isPinned && createElement(IconButton, {
icon: icon,
label: title,
onClick: toggleSidebar,
isToggled: isActive,
"aria-expanded": isActive
})), createElement(Sidebar, {
name: sidebarName,
label: __('Editor plugins')
}, createElement(SidebarHeader, {
closeLabel: __('Close plugin')
}, createElement("strong", null, title), isPinnable && createElement(IconButton, {
icon: isPinned ? 'star-filled' : 'star-empty',
label: isPinned ? __('Unpin from toolbar') : __('Pin to toolbar'),
onClick: togglePin,
isToggled: isPinned,
"aria-expanded": isPinned
})), createElement(Panel, {
className: className
}, children)));
}
/**
* Renders a sidebar when activated. The contents within the `PluginSidebar` will appear as content within the sidebar.
* 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-post' ).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 toolbar.
* @param {string|Element} [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 <caption>ES5</caption>
* ```js
* // Using ES5 syntax
* var __ = wp.i18n.__;
* var el = wp.element.createElement;
* var PanelBody = wp.components.PanelBody;
* var PluginSidebar = wp.editPost.PluginSidebar;
*
* function MyPluginSidebar() {
* return el(
* PluginSidebar,
* {
* name: 'my-sidebar',
* title: 'My sidebar title',
* icon: 'smiley',
* },
* el(
* PanelBody,
* {},
* __( 'My sidebar content' )
* )
* );
* }
* ```
*
* @example <caption>ESNext</caption>
* ```jsx
* // Using ESNext syntax
* const { __ } = wp.i18n;
* const { PanelBody } = wp.components;
* const { PluginSidebar } = wp.editPost;
*
* const MyPluginSidebar = () => (
* <PluginSidebar
* name="my-sidebar"
* title="My sidebar title"
* icon="smiley"
* >
* <PanelBody>
* { __( 'My sidebar content' ) }
* </PanelBody>
* </PluginSidebar>
* );
* ```
*
* @return {WPElement} Plugin sidebar component.
*/
export default compose(withPluginContext(function (context, ownProps) {
return {
icon: ownProps.icon || context.icon,
sidebarName: "".concat(context.name, "/").concat(ownProps.name)
};
}), withSelect(function (select, _ref) {
var sidebarName = _ref.sidebarName;
var _select = select('core/edit-post'),
getActiveGeneralSidebarName = _select.getActiveGeneralSidebarName,
isPluginItemPinned = _select.isPluginItemPinned;
return {
isActive: getActiveGeneralSidebarName() === sidebarName,
isPinned: isPluginItemPinned(sidebarName)
};
}), withDispatch(function (dispatch, _ref2) {
var isActive = _ref2.isActive,
sidebarName = _ref2.sidebarName;
var _dispatch = dispatch('core/edit-post'),
closeGeneralSidebar = _dispatch.closeGeneralSidebar,
openGeneralSidebar = _dispatch.openGeneralSidebar,
togglePinnedPluginItem = _dispatch.togglePinnedPluginItem;
return {
togglePin: function togglePin() {
togglePinnedPluginItem(sidebarName);
},
toggleSidebar: function toggleSidebar() {
if (isActive) {
closeGeneralSidebar();
} else {
openGeneralSidebar(sidebarName);
}
}
};
}))(PluginSidebar);
//# sourceMappingURL=index.js.map