Hooks

Docs Hooks Editor filters · JavaScript

modalKit.styleEditor

filter

Free

JavaScript

Supply the style editor on Modal Kit → Settings → Styles, the component behind New style, Edit and Edit a copy. Modal Kit Pro provides one.

Runs each time the Styles tab of Modal Kit → Settings renders, in Free’s modal-kit-settings script. Without a component, the tab shows a dismissible note about Modal Kit Pro’s Style editor. With one, it adds New style, Edit on your own and imported styles, and Edit a copy on built-in styles and those added by plugins, and renders the component while one of them is open.

Modal Kit Pro’s Style editor is a full-screen dialog with grouped controls, a live preview and a contrast check.

Parameters

Parameter

Description

StyleEditor

Component|null

The editor so far; null by default.

Returns

Component|null The editor. It receives style (the style’s JSON as an object, with an empty slug for a new style or a copy), slug (empty for a new style or a copy, the style’s slug when editing your own), usedBy (how many modals use it), styles (every style in the library), onClose() and onSaved( item ), where item is the saved style as POST /modal-kit/v1/styles returns it.

Example

A small editor for a style’s name and description

It saves through Free’s endpoint: replace updates one of your own styles, copy adds a new one.

JavaScript

import { __ } from '@wordpress/i18n';
import apiFetch from '@wordpress/api-fetch';
import { addFilter } from '@wordpress/hooks';
import { Button, Modal, Notice, TextControl } from '@wordpress/components';
import { useState } from '@wordpress/element';

function NameEditor( { style, slug, onClose, onSaved } ) {
  const [ label, setLabel ] = useState( style.label ?? '' );
  const [ text, setText ] = useState( style.description ?? '' );
  const [ error, setError ] = useState( '' );
  const [ busy, setBusy ] = useState( false );

  const save = async () => {
    setBusy( true );
    try {
      const result = await apiFetch( {
        path: '/modal-kit/v1/styles',
        method: 'POST',
        data: {
          style: { ...style, slug, label, description: text },
          mode: slug ? 'replace' : 'copy',
          source: 'yours',
          label,
          description: text,
        },
      } );
      onSaved( result.item );
    } catch ( failure ) {
      setError( failure.message );
      setBusy( false );
    }
  };

  return (
    <Modal
      title={ __( 'Edit style', 'my-plugin' ) }
      onRequestClose={ onClose }
    >
      { error && (
        <Notice status="error" isDismissible={ false }>
          { error }
        </Notice>
      ) }
      <TextControl
        __next40pxDefaultSize
        __nextHasNoMarginBottom
        label={ __( 'Name', 'my-plugin' ) }
        value={ label }
        onChange={ setLabel }
      />
      <TextControl
        __next40pxDefaultSize
        __nextHasNoMarginBottom
        label={ __( 'Description', 'my-plugin' ) }
        value={ text }
        onChange={ setText }
      />
      <Button
        variant="primary"
        onClick={ save }
        isBusy={ busy }
        disabled={ busy || ! label.trim() }
        __next40pxDefaultSize
      >
        { __( 'Save', 'my-plugin' ) }
      </Button>
    </Modal>
  );
}

addFilter(
  'modalKit.styleEditor',
  'my-plugin/name-editor',
  ( editor ) => editor ?? NameEditor
);

Good to know

One editor is used. Pro’s callback returns its own editor whatever it receives. To leave Pro’s in place, return the value you receive when it isn’t null, as above; to replace it, add your filter with a priority above 10.

The endpoint checks the style’s values and contrast as it does for an imported file, and refuses a style without a name. replace works only on styles that are yours or imported.

Applied in modal-kit/src/admin/settings/styles.js.

Need a hook that isn’t here?