Hooks

Docs Hooks Editor filters · JavaScript

modalKit.triggerTypes

filter

Free

JavaScript

Add automatic trigger types to the Triggers panel’s Add trigger menu, each with a label, description, defaults and its own settings.

Runs each time the Triggers panel renders, in Free’s modal-kit-editor script on the block editor for a modal. The panel lists the modal’s automatic triggers, and the Add trigger menu offers every type. Adding one stores { type, ...defaults } in the modal’s _modal_kit_triggers meta.

Modal Kit Pro adds its WooCommerce triggers (Quick view button, Added to cart, Removed from cart, Cart total over…, Product or category in cart) in a Shop group, on sites with WooCommerce.

Parameters

Parameter

Description

types

Object

Trigger types by name: load, scroll, exit, time, inactivity, url and element, plus any added by other plugins. Each has label, description (shown in the menu) and defaults (the settings a new trigger starts with).

Returns

Object Types by name. A type may also set Settings, a component that receives { trigger, set }, where set( changes ) merges changes into the trigger, and group, a heading that puts the type in its own section of the menu.

Examples

Open when the visitor comes back to the tab

The editor side: a type with one setting.

JavaScript

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

function TabReturnSettings( { trigger, set } ) {
  return (
    <TextControl
      __next40pxDefaultSize
      __nextHasNoMarginBottom
      type="number"
      min={ 0 }
      label={ __( 'Seconds away, at least', 'my-plugin' ) }
      value={ trigger.away ?? 10 }
      onChange={ ( away ) =>
        set( { away: Math.max( 0, parseInt( away, 10 ) || 0 ) } )
      }
    />
  );
}

addFilter( 'modalKit.triggerTypes', 'my-plugin/tab-return', ( types ) => ( {
  ...types,
  'my-tab-return': {
    label: __( 'Back to the tab', 'my-plugin' ),
    description: __( 'The visitor returns from another tab', 'my-plugin' ),
    defaults: { away: 10 },
    Settings: TabReturnSettings,
  },
} ) );

Allow the type when the modal saves

The triggers meta is checked against a JSON schema that lists the allowed types and fields. Add this when your plugin loads: Free registers the meta on init.

PHP

add_filter(
  'modal_kit_meta_schema',
  function ( $schema, $key ) {
    if ( 'triggers' !== $key ) {
      return $schema;
    }

    $schema['items']['properties']['type']['enum'][] = 'my-tab-return';
    $schema['items']['properties']['away']           = array(
      'type'    => 'integer',
      'minimum' => 0,
      'maximum' => 3600,
    );

    return $schema;
  },
  10,
  2
);

Open the modal on the front end

Free’s runtime skips trigger types it doesn’t know, so a front-end script of yours does the opening. Load it in the footer with wp_enqueue_scripts: Free prints the modals early in wp_footer. It runs in the browser, so it works on a static export.

JavaScript

document.querySelectorAll( 'dialog.modal-kit' ).forEach( ( dialog ) => {
  let context = {};

  try {
    context = JSON.parse( dialog.dataset.wpContext || '{}' );
  } catch ( error ) {
    return;
  }

  const trigger = ( context.triggers || [] ).find(
    ( item ) => item.type === 'my-tab-return'
  );

  if ( ! trigger ) {
    return;
  }

  let leftAt = 0;
  let fired = false;

  document.addEventListener( 'visibilitychange', () => {
    if ( document.hidden ) {
      leftAt = Date.now();
      return;
    }

    const away = ( Date.now() - leftAt ) / 1000;

    if ( fired || ! leftAt || away < trigger.away ) {
      return;
    }

    if ( ! dialog.hasAttribute( 'data-modal-kit-ready' ) ) {
      return;
    }

    fired = true;
    dialog.dispatchEvent(
      new window.CustomEvent( 'modal-kit:request-open', {
        detail: { source: 'my-tab-return' },
      } )
    );
  } );
} );

Good to know

Each type can be added once per modal; the menu shows Already added for types the modal has.

The schema doesn’t allow unknown types or fields (additionalProperties is false), so without modal_kit_meta_schema the save is rejected.

A modal opened through modal-kit:request-open still goes through its display rules, such as frequency and devices.

Without Settings, a type of your own shows no settings under its name.

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

Need a hook that isn’t here?