@wordpress/edit-post
Version:
Edit Post module for WordPress.
103 lines (94 loc) • 3.18 kB
JavaScript
import { createElement } from "@wordpress/element";
/**
* WordPress dependencies
*/
import { compose } from '@wordpress/compose';
import { withSelect, withDispatch } from '@wordpress/data';
import { withPluginContext } from '@wordpress/plugins';
/**
* Internal dependencies
*/
import PluginMoreMenuItem from '../plugin-more-menu-item';
var PluginSidebarMoreMenuItem = function PluginSidebarMoreMenuItem(_ref) {
var children = _ref.children,
icon = _ref.icon,
isSelected = _ref.isSelected,
onClick = _ref.onClick;
return createElement(PluginMoreMenuItem, {
icon: isSelected ? 'yes' : icon,
isSelected: isSelected,
role: "menuitemcheckbox",
onClick: onClick
}, children);
};
/**
* 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 {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 to the left of the menu item label.
*
* @example <caption>ES5</caption>
* ```js
* // Using ES5 syntax
* var __ = wp.i18n.__;
* var PluginSidebarMoreMenuItem = wp.editPost.PluginSidebarMoreMenuItem;
*
* function MySidebarMoreMenuItem() {
* return wp.element.createElement(
* PluginSidebarMoreMenuItem,
* {
* target: 'my-sidebar',
* icon: 'smiley',
* },
* __( 'My sidebar title' )
* )
* }
* ```
*
* @example <caption>ESNext</caption>
* ```jsx
* // Using ESNext syntax
* const { __ } = wp.i18n;
* const { PluginSidebarMoreMenuItem } = wp.editPost;
*
* const MySidebarMoreMenuItem = () => (
* <PluginSidebarMoreMenuItem
* target="my-sidebar"
* icon="smiley"
* >
* { __( 'My sidebar title' ) }
* </PluginSidebarMoreMenuItem>
* );
* ```
*
* @return {WPElement} The element to be rendered.
*/
export default compose(withPluginContext(function (context, ownProps) {
return {
icon: ownProps.icon || context.icon,
sidebarName: "".concat(context.name, "/").concat(ownProps.target)
};
}), withSelect(function (select, _ref2) {
var sidebarName = _ref2.sidebarName;
var _select = select('core/edit-post'),
getActiveGeneralSidebarName = _select.getActiveGeneralSidebarName;
return {
isSelected: getActiveGeneralSidebarName() === sidebarName
};
}), withDispatch(function (dispatch, _ref3) {
var isSelected = _ref3.isSelected,
sidebarName = _ref3.sidebarName;
var _dispatch = dispatch('core/edit-post'),
closeGeneralSidebar = _dispatch.closeGeneralSidebar,
openGeneralSidebar = _dispatch.openGeneralSidebar;
var onClick = isSelected ? closeGeneralSidebar : function () {
return openGeneralSidebar(sidebarName);
};
return {
onClick: onClick
};
}))(PluginSidebarMoreMenuItem);
//# sourceMappingURL=index.js.map