Hooks
Docs Hooks Editor filters · JavaScript
modalKit.buttonActions
filter
Free
JavaScript
Add actions to the Action panel of Button blocks inside a modal, each with its own settings.
Runs each time the Action panel renders, in Free’s modal-kit-editor script on the block editor for a modal. The panel appears in the sidebar of every Button block in a modal and lists the actions as When clicked choices. The chosen one is saved in the button’s modalKitAction attribute (left unset for Open link).
Free’s actions are Open link, Close modal and Copy text. Modal Kit Pro adds Next / previous step, Go to step, Submit form, Play the game and, with WooCommerce, Add to cart and Apply coupon / go to checkout. It also wraps Copy text’s settings to add Send the code from the site.
Parameters
Parameter
Description
actions
Object[]
Actions in order. Each has value (the action name), label, and optionally Settings, a component shown under the choices while that action is selected. Free’s are link, close and copy (with a Text to copy field).
Returns
Object[] Actions. Settings receives the button’s { attributes, setAttributes, clientId }.
Examples
Add a Share action
The editor side. The setting is a block attribute, so it’s registered on core/button too.
import { __ } from '@wordpress/i18n';
import { addFilter } from '@wordpress/hooks';
import { TextControl } from '@wordpress/components';
function ShareSettings( { attributes, setAttributes } ) {
return (
<TextControl
__next40pxDefaultSize
__nextHasNoMarginBottom
label={ __( 'Text to share', 'my-plugin' ) }
value={ attributes.myShareText ?? '' }
onChange={ ( myShareText ) => setAttributes( { myShareText } ) }
/>
);
}
addFilter( 'modalKit.buttonActions', 'my-plugin/share', ( actions ) => [
...actions,
{
value: 'my-share',
label: __( 'Share this page', 'my-plugin' ),
Settings: ShareSettings,
},
] );
addFilter(
'blocks.registerBlockType',
'my-plugin/share-attribute',
( settings, name ) =>
name === 'core/button'
? {
...settings,
attributes: {
...settings.attributes,
myShareText: { type: 'string' },
},
}
: settings
);
Tell the front end about the action
Free adds its click handler only to actions listed in modal_kit_button_actions. modal_kit_render_button_action then adds your data to the button.
add_filter( 'modal_kit_button_actions', function ( $actions ) {
$actions[] = 'my-share';
return $actions;
} );
add_action(
'modal_kit_render_button_action',
function ( $processor, $action, $block ) {
if ( 'my-share' !== $action ) {
return;
}
$text = isset( $block['attrs']['myShareText'] )
? (string) $block['attrs']['myShareText']
: '';
$processor->set_attribute( 'data-my-share-text', $text );
},
10,
3
);
Handle the click
For an action it doesn’t handle itself, Free’s click handler prevents the default and dispatches a bubbling modal-kit:action event from the button. Load this with wp_enqueue_scripts.
document.addEventListener( 'modal-kit:action', ( event ) => {
const { action, button } = event.detail;
if ( action !== 'my-share' ) {
return;
}
const data = {
title: document.title,
text: button.dataset.myShareText || '',
url: window.location.href,
};
if ( window.navigator.share ) {
window.navigator.share( data ).catch( () => {} );
} else {
window.navigator.clipboard?.writeText( data.url );
}
} );
Good to know
Choosing an action only sets block attributes, so the button’s saved markup doesn’t change. Register every attribute your settings use on core/button with the blocks.registerBlockType filter, or it isn’t saved.
To change one of Free’s actions, map over the array and replace that entry, as Pro does for copy: it renders the old Settings inside its own.
A Pro listing in the panel’s More with Pro list is hidden when an action with the same value exists.
Applied in modal-kit/src/extensions/button-action/index.js.