|
| 1 | +/** |
| 2 | + * WordPress dependencies |
| 3 | + */ |
| 4 | +import { compose } from '@wordpress/compose'; |
| 5 | +import { MenuItem } from '@wordpress/components'; |
| 6 | +import { withPluginContext } from '@wordpress/plugins'; |
| 7 | +import { ActionItem } from '@wordpress/interface'; |
| 8 | + |
| 9 | +/** |
| 10 | + * Renders a menu item in the Preview dropdown, which can be used as a button or link depending on the props provided. |
| 11 | + * The text within the component appears as the menu item label. |
| 12 | + * |
| 13 | + * @param {Object} props Component properties. |
| 14 | + * @param {string} [props.href] When `href` is provided, the menu item is rendered as an anchor instead of a button. It corresponds to the `href` attribute of the anchor. |
| 15 | + * @param {WPBlockTypeIconRender} [props.icon=inherits from the plugin] The icon to be rendered to the left of the menu item label. Can be a Dashicon slug or an SVG WP element. |
| 16 | + * @param {Function} [props.onClick] The callback function to be executed when the user clicks the menu item. |
| 17 | + * @param {...*} [props.other] Any additional props are passed through to the underlying MenuItem component. |
| 18 | + * |
| 19 | + * @example |
| 20 | + * ```jsx |
| 21 | + * import { __ } from '@wordpress/i18n'; |
| 22 | + * import { PluginPreviewMenuItem } from '@wordpress/editor'; |
| 23 | + * import { external } from '@wordpress/icons'; |
| 24 | + * |
| 25 | + * function onPreviewClick() { |
| 26 | + * // Handle preview action |
| 27 | + * } |
| 28 | + * |
| 29 | + * const ExternalPreviewMenuItem = () => ( |
| 30 | + * <PreviewDropdownMenuItem |
| 31 | + * icon={ external } |
| 32 | + * onClick={ onPreviewClick } |
| 33 | + * > |
| 34 | + * { __( 'Preview in new tab' ) } |
| 35 | + * </PreviewDropdownMenuItem> |
| 36 | + * ); |
| 37 | + * registerPlugin( 'external-preview-menu-item', { |
| 38 | + * render: ExternalPreviewMenuItem, |
| 39 | + * } ); |
| 40 | + * ``` |
| 41 | + * |
| 42 | + * @return {Component} The rendered menu item component. |
| 43 | + */ |
| 44 | +export default compose( |
| 45 | + withPluginContext( ( context, ownProps ) => { |
| 46 | + return { |
| 47 | + as: ownProps.as ?? MenuItem, |
| 48 | + icon: ownProps.icon || context.icon, |
| 49 | + name: 'core/plugin-preview-menu', |
| 50 | + }; |
| 51 | + } ) |
| 52 | +)( ActionItem ); |
0 commit comments