Skip to content

Commit

Permalink
Allow extending preview menu
Browse files Browse the repository at this point in the history
  • Loading branch information
simison committed Aug 9, 2023
1 parent 38ad72f commit 5efcca7
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/block-editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"@wordpress/html-entities": "file:../html-entities",
"@wordpress/i18n": "file:../i18n",
"@wordpress/icons": "file:../icons",
"@wordpress/interface": "file:../interface",
"@wordpress/is-shallow-equal": "file:../is-shallow-equal",
"@wordpress/keyboard-shortcuts": "file:../keyboard-shortcuts",
"@wordpress/keycodes": "file:../keycodes",
Expand Down
4 changes: 4 additions & 0 deletions packages/block-editor/src/components/preview-options/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { useViewportMatch } from '@wordpress/compose';
import { DropdownMenu, MenuGroup, MenuItem } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { check, desktop, mobile, tablet } from '@wordpress/icons';
import { PluginPreviewMenu } from '@wordpress/interface';

export default function PreviewOptions( {
children,
Expand Down Expand Up @@ -79,6 +80,9 @@ export default function PreviewOptions( {
{ __( 'Mobile' ) }
</MenuItem>
</MenuGroup>
<PluginPreviewMenu.Slot>
{ ( fills ) => <MenuGroup>{ fills }</MenuGroup> }
</PluginPreviewMenu.Slot>
{ children( renderProps ) }
</>
) }
Expand Down
1 change: 1 addition & 0 deletions packages/interface/src/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ export { default as PreferencesModalTabs } from './preferences-modal-tabs';
export { default as PreferencesModalSection } from './preferences-modal-section';
export { default as ___unstablePreferencesModalBaseOption } from './preferences-modal-base-option';
export { default as NavigableRegion } from './navigable-region';
export { default as PluginPreviewMenu } from './plugin-preview-menu';
58 changes: 58 additions & 0 deletions packages/interface/src/components/plugin-preview-menu/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* WordPress dependencies
*/
import { createSlotFill, MenuItem } from '@wordpress/components';

export const { Fill, Slot } = createSlotFill( 'PluginPreviewMenu' );

/**
* Defines an extensibility slot for the device preview dropdown.
*
* The `title` and `icon` are used to populate the Preview menu item.
*
* @param {Object} props Component properties.
* @param {string} props.name A unique name of the custom preview. Prefix with plugin name.
* @param {Function} props.onClick Menu item click handler
* @param {string} props.title Menu item title.
*/
const PluginPreviewMenu = ( { name, onClick, title, ...props } ) => (
<Fill>
<MenuItem
className="block-editor-post-preview__button-resize"
onClick={ ( ...args ) => {
if ( onClick ) {
onClick( ...args );
}
} }
{ ...props }
>
{ title }
</MenuItem>
</Fill>
);

PluginPreviewMenu.Slot = Slot;

/**
* Renders items in the devide preview dropdown within the editor header.
*
* @example
* ```jsx
* // Using ESNext syntax
* import { PluginPreviewMenu } from '@wordpress/interface';
* import { registerPlugin } from '@wordpress/plugins';
*
* const MyPreviewMenu = () => (
* <PluginPreviewMenu
* name="MyPreview"
* onClick={ () => {} }
* title="My preview"
* />
* );
*
* registerPlugin( 'my-preview-menu', { render: MyPreviewMenu } );
* ```
*
* @return {WPComponent} The component to be rendered.
*/
export default PluginPreviewMenu;

0 comments on commit 5efcca7

Please sign in to comment.