Hooks
Docs Hooks Editor filters · JavaScript
modalKit.canvasChrome
filter
Free
JavaScript
Add editor-only controls inside the Modal block on the canvas, like Modal Kit Pro’s step tabs and A/B variant bar.
Runs each time the Modal block renders in the editor, in the block’s own editor script (modal-kit-modal-editor-script). Whatever the filter returns is rendered inside the modal on the canvas, before its content. It is never saved and never reaches the front end.
Modal Kit Pro adds its step tabs and, for A/B tests, the variant bar. Each wraps the value it receives, so all of them show.
Parameters
Parameter
Description
chrome
Element|null
What other plugins added; null by default.
context
Object
clientId (the Modal block’s client ID), attributes (its attributes) and setAttributes.
Returns
Element|null The chrome to render. Return chrome together with your own element so other plugins’ chrome stays.
Example
Show a reminder while the modal is a draft
import { __ } from '@wordpress/i18n';
import { addFilter } from '@wordpress/hooks';
import { useSelect } from '@wordpress/data';
import { store as blockEditorStore } from '@wordpress/block-editor';
import { store as editorStore } from '@wordpress/editor';
function DraftBadge() {
const { status, isPreview } = useSelect(
( select ) => ( {
status: select( editorStore ).getEditedPostAttribute( 'status' ),
isPreview: !! select( blockEditorStore ).getSettings().isPreviewMode,
} ),
[]
);
if ( isPreview || status === 'publish' ) {
return null;
}
return (
<p className="my-plugin-draft-badge">
{ __( 'Draft: visitors don’t see this modal yet.', 'my-plugin' ) }
</p>
);
}
addFilter(
'modalKit.canvasChrome',
'my-plugin/draft-badge',
( chrome ) => (
<>
{ chrome }
<DraftBadge />
</>
)
);
Good to know
The callback runs during render. Put React hooks such as useSelect in a component and return that component, as above, rather than calling them in the callback.
It also renders in block previews, such as the template picker’s. Pro hides its chrome there by checking the block editor setting isPreviewMode.
The canvas may be an iframe, so load CSS for your chrome on enqueue_block_assets, which reaches the iframe (check is_admin() to keep it off the front end).
Applied in modal-kit/src/blocks/modal/edit.js.