Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add extensibility to PreviewOptions #50605

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the block editor should depend on "interface", interface is a package to build wp-admin pages (post editor, site editor...) and block editor is a reusable package that can be seen as "TinyMCE" for block editors.

"@wordpress/is-shallow-equal": "file:../is-shallow-equal",
"@wordpress/keyboard-shortcuts": "file:../keyboard-shortcuts",
"@wordpress/keycodes": "file:../keycodes",
Expand Down
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>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm guessing the reason you depend on interface is because the "PreviewOptions" is placed within the block editor. That was a mistake IMO, I've tried to fix it a long time ago (sometimes duplicating code is better than sharing code) #30906 but I closed that PR because the added value for that refactoring was a bit small. Potentially today, we could revive that PR (aka remove the shared PreviewOptions component and move it to edit-site and edit-post instead)

{ 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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This classname doesn't follow our guidelines, the component is within "interface" (which can be acceptable) but the classname is like a "block editor" one.

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;