Extension Examples

This page is generated from HOOKS.md in the Typography Stylist source. Edit it there — edits made here are kept, but they stop this page from being updated until the conflict is resolved.

Vanilla DOM Example

A minimal extension that adds a message to the top of the inline editor:

// my-extension.php
add_action('typost_editor_assets', function() {
    wp_enqueue_script('my-ext', plugins_url('editor.js', __FILE__), array('typost-block-editor'), '1.0.0', true);
});
// editor.js
(function() {
    function waitForHooks(cb) {
        if (window.typostHooks) { cb(); return; }
        var i = setInterval(function() {
            if (window.typostHooks) { clearInterval(i); cb(); }
        }, 100);
    }

    waitForHooks(function() {
        window.typostHooks.addAction('typost_inline_modal_top', function(containerEl, state) {
            // Pure vanilla DOM — no build step required
            var div = document.createElement('div');
            div.style.cssText = 'padding: 8px 12px; background: #f0f6fc; border: 1px solid #c8d8e8; border-radius: 4px; margin-bottom: 12px; font-size: 13px;';
            div.textContent = 'Hello from My Extension! Current font: ' + (state.selectedFont || 'Default');
            containerEl.appendChild(div);
        }, 10);
    });
})();

React (wp.element) Example

A more polished extension using WordPress React components:

// my-extension.php
add_action('typost_editor_assets', function() {
    wp_enqueue_script('my-ext', plugins_url('editor.js', __FILE__),
        array('typost-block-editor', 'wp-element', 'wp-components', 'wp-i18n'), '1.0.0', true);
});
// editor.js
(function() {
    var el = wp.element.createElement;
    var useState = wp.element.useState;
    var Button = wp.components.Button;
    var Notice = wp.components.Notice;
    var __ = wp.i18n.__;

    function MyPanel(props) {
        var state = useState(false);
        var isActive = state[0];
        var setIsActive = state[1];

        return el('div', { style: { marginBottom: '12px' } },
            el(Notice, { status: 'info', isDismissible: false },
                __('My Extension is active!', 'my-text-domain')
            ),
            el(Button, {
                variant: isActive ? 'primary' : 'secondary',
                onClick: function() { setIsActive(!isActive); },
                size: 'small',
            }, isActive ? __('Deactivate', 'my-text-domain') : __('Activate', 'my-text-domain'))
        );
    }

    function waitForHooks(cb) {
        if (window.typostHooks) { cb(); return; }
        var i = setInterval(function() {
            if (window.typostHooks) { clearInterval(i); cb(); }
        }, 100);
    }

    waitForHooks(function() {
        window.typostHooks.addAction('typost_inline_modal_top', function(containerEl) {
            wp.element.render(el(MyPanel), containerEl);
        }, 10);

        window.typostHooks.addAction('typost_qft_modal_top', function(containerEl) {
            wp.element.render(el(MyPanel), containerEl);
        }, 10);
    });
})();