Hooks

Docs Hooks Editor filters · JavaScript

modalKit.triggerMenuGroups

filter

Free

JavaScript

Add your own sections to the end of the Add trigger menu in the Triggers panel, as components that render a menu group.

Runs each time the Triggers panel renders, in Free’s modal-kit-editor script on the block editor for a modal. Each component in the returned array is rendered at the end of the Add trigger menu, after the trigger types and their groups.

Free uses it for its dismissible Modal Kit Pro listing of WooCommerce triggers. To add a trigger type, use modalKit.triggerTypes instead: it builds the menu items for you.

Parameters

Parameter

Description

groups

Array

Components, empty by default. Free adds its Pro listing here when there is one to show.

Returns

Array Components. Each is rendered with { onClose }, which closes the menu, and should return a MenuGroup from @wordpress/components, or null.

Example

Add a preset set of triggers

The panel reads the modal’s _modal_kit_triggers meta, so triggers written there show at once.

JavaScript

import { __ } from '@wordpress/i18n';
import { addFilter } from '@wordpress/hooks';
import { MenuGroup, MenuItem } from '@wordpress/components';
import { useEntityProp } from '@wordpress/core-data';

const PRESET = [
  { type: 'exit', touch: 'scroll-up' },
  { type: 'scroll', percent: 60 },
];

function PresetGroup( { onClose } ) {
  const [ meta, setMeta ] = useEntityProp( 'postType', 'mk_modal', 'meta' );
  const triggers = meta?._modal_kit_triggers ?? [];
  const used = triggers.map( ( trigger ) => trigger.type );
  const missing = PRESET.filter(
    ( trigger ) => ! used.includes( trigger.type )
  );

  return (
    <MenuGroup label={ __( 'Presets', 'my-plugin' ) }>
      <MenuItem
        info={ __( 'Leaving the page, or 60% scrolled', 'my-plugin' ) }
        disabled={ ! missing.length }
        onClick={ () => {
          setMeta( {
            ...meta,
            _modal_kit_triggers: [ ...triggers, ...missing ],
          } );
          onClose();
        } }
      >
        { __( 'Exit or scroll', 'my-plugin' ) }
      </MenuItem>
    </MenuGroup>
  );
}

addFilter(
  'modalKit.triggerMenuGroups',
  'my-plugin/presets',
  ( groups ) => [ ...groups, PresetGroup ]
);

Good to know

The menu mounts the groups each time it opens, so any state inside a group starts again the next time.

Append to the array you receive, so Free’s listing and other plugins’ groups stay.

Applied in modal-kit/src/editor/panels/triggers.js.

Need a hook that isn’t here?