-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
|
@@ -79,6 +80,9 @@ export default function PreviewOptions( { | |
{ __( 'Mobile' ) } | ||
</MenuItem> | ||
</MenuGroup> | ||
<PluginPreviewMenu.Slot> | ||
{ ( fills ) => <MenuGroup>{ fills }</MenuGroup> } | ||
</PluginPreviewMenu.Slot> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ) } | ||
</> | ||
) } | ||
|
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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; |
There was a problem hiding this comment.
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.