Hooks

Docs Hooks Editor filters · JavaScript

modalKit.layouts

filter

Free

JavaScript

Add layouts to the Modal block in the editor, with their labels, position names, mobile help and a note for the Triggers panel.

The editor twin of modal_kit_layouts. The Modal block’s Layout panel and toolbar, the Triggers panel and the template picker all read the layouts through this filter, so a layout added here can be chosen for a modal in the block editor.

Each layout is keyed by name. The name is stored in the block’s modalLayout attribute and becomes the class modal-kit--{name} on the modal. The filter is applied by Free’s editor scripts on the block editor for a modal (mk_modal): the Modal block’s editor script and modal-kit-editor.

Parameters

Parameter

Description

layouts

Object

Layouts by name, from Free’s schema/layouts.json (centered, corner, bar, full, drawer, split, sheet, inline, video) plus any added by other plugins. Each has overlay (bool), modal (bool: opens with showModal() and makes the page behind inert), positions (string[]) and mobile (the layout used on phones when On mobile is Automatic).

Returns

Object Layouts by name. Besides the keys above, a layout may set base (a built-in layout whose class and styles it adds), closable (false: no close button, Esc or click outside, and no Closing panel), and for the editor label, positionLabels (labels by position), mobileHelp (help under On mobile) and triggerNote (shown in the Triggers panel instead of the automatic triggers).

Examples

Add a side panel layout

A drawer with its own name, so your CSS can target .modal-kit--side-panel while it keeps the drawer’s styles. Add the same layout with modal_kit_layouts so the front end knows it too.

JavaScript

import { __ } from '@wordpress/i18n';
import { addFilter } from '@wordpress/hooks';

addFilter( 'modalKit.layouts', 'my-plugin/side-panel', ( layouts ) => ( {
  ...layouts,
  'side-panel': {
    overlay: true,
    modal: true,
    positions: [ 'right', 'left' ],
    mobile: 'sheet',
    base: 'drawer',
    label: __( 'Side panel', 'my-plugin' ),
    mobileHelp: __( 'Slides up from the bottom on phones.', 'my-plugin' ),
  },
} ) );

Load your editor script on the modal screen

Enqueue on enqueue_block_editor_assets for mk_modal screens, after Free’s modal-kit-editor script (priority 20, as Modal Kit Pro does). The asset file built by @wordpress/scripts already lists wp-hooks. The same script can hold every editor filter on this page.

PHP

add_action(
  'enqueue_block_editor_assets',
  function () {
    $screen = get_current_screen();

    if ( ! $screen || 'mk_modal' !== $screen->post_type ) {
      return;
    }

    $asset = require plugin_dir_path( __FILE__ ) . 'build/editor.asset.php';

    wp_enqueue_script(
      'my-plugin-modal-editor',
      plugins_url( 'build/editor.js', __FILE__ ),
      array_merge( $asset['dependencies'], array( 'modal-kit-editor' ) ),
      $asset['version'],
      true
    );
    wp_set_script_translations( 'my-plugin-modal-editor', 'my-plugin' );
  },
  20
);

Good to know

If the returned object has no centered layout, the result is thrown away and the built-in layouts are used, so add to the object rather than building a new one.

Register the same layout with modal_kit_layouts in PHP. The server resolves the layout again when it renders the modal, and a name it doesn’t know falls back to centered.

label and positionLabels are used only where Free has no label of its own: built-in layouts and positions (right, left, top, bottom-right and so on) keep their names.

Free styles only its own layouts. Use base to inherit one, and load CSS for .modal-kit--{name} on the front end and in the editor canvas for anything else.

The layouts are read many times per render, so keep the callback a plain, fast function.

Applied in modal-kit/src/blocks/modal/layouts.js.

Need a hook that isn’t here?