html/confirm
< >

return function () {
    $self = url('/@object/@action/' . urlencode('html/confirm'));
    if (isset($_GET['frag']) && ! empty($_SERVER['HTTP_HX_REQUEST'])) {
        no_graphics(true);
        echo '<span class="docs-confirm-htmx-done">Loaded by htmx after the confirm.</span>';
        exit;
    }
    if (isset($_GET['del']) && main()->is_post() && is_ajax()) {
        no_graphics(true);
        echo (int) $_GET['del'];
        exit;
    }
    asset('jquery');
    asset('htmx');
    asset('yf_js_admin');
    _class('admin_ui')->confirm_assets();
    $ui = _class('admin_ui');
    $h = fn ($s) => _prepare_html((string) $s, 0);

    $state = [];
    foreach (['hit', 'go', 'q'] as $k) {
        if (isset($_GET[$k])) {
            $state[] = $k . '=' . $h($_GET[$k]);
        }
    }
    $intro = '<p class="docs-demo-intro">Confirm and prompt dialogs on a Bootstrap modal (asset <code>yf_confirm</code>, no jQuery, no
        Bootstrap JS needed): <code>yf.confirm(opts)</code> resolves to <code>true</code> / <code>false</code>, <code>yf.prompt(opts)</code>
        to the text or <code>null</code>. A link, a button or a form with <code>data-yf-confirm="..."</code> asks first, through one delegated
        listener, and htmx <code>hx-confirm</code> uses the same dialog. Esc cancels, Enter confirms, Tab stays in the dialog; a danger dialog
        starts on Cancel. The admin asks with it everywhere (<code>_class(\'admin_ui\')->confirm_attrs()</code>).</p>';
    $btn = fn ($id, $label, $type = 'default') => '<button type="button" id="' . $id . '" class="' . $ui->btn_class($type) . '">' . $label . '</button>';
    $api = '<h4>JavaScript API</h4><div class="yf-ui-toolbar">'
        . $btn('docs-confirm-basic', 'yf.confirm()') . $btn('docs-confirm-danger', 'Danger', 'danger')
        . $btn('docs-confirm-typed', 'Typed word', 'danger') . $btn('docs-confirm-prompt', 'yf.prompt()', 'primary')
        . $btn('docs-confirm-two', 'Two in a row') . '</div>'
        . '<p>Answer: <output id="docs-confirm-out">none yet</output></p>';
    $decl = '<h4>Declarative</h4><div class="yf-ui-toolbar">'
        . '<a id="docs-confirm-link" class="' . $ui->btn_class() . '" href="' . $self . '?hit=link"' . $ui->confirm_html('Open the link?') . '>Link</a>'
        . '<form id="docs-confirm-form" method="get" action="' . $self . '"' . $ui->confirm_html('Send the form?', ['on' => 'submit', 'danger' => true, 'ok' => 'Send']) . '>'
        . '<input type="text" name="q" value="typed" class="' . $ui->input_class() . '" style="width:8em;display:inline-block">'
        . '<button type="submit" name="go" value="b1" class="' . $ui->btn_class() . '">Send 1</button>'
        . '<button type="submit" name="go" value="b2" class="' . $ui->btn_class('primary') . '">Send 2</button></form>'
        . '<button type="button" id="docs-confirm-htmx" class="' . $ui->btn_class() . '" hx-get="' . $self . '?frag=1" hx-target="#docs-confirm-htmx-out"'
        . ' hx-confirm="Load the fragment?">htmx</button></div>'
        . '<p>Page state: <span id="docs-confirm-state">' . ($state ? implode(' ', $state) : 'none') . '</span>;'
        . ' htmx: <span id="docs-confirm-htmx-out">nothing yet</span></p>';
    $rows = '';
    foreach ([1 => 'Apples', 2 => 'Pears'] as $id => $name) {
        $rows .= '<tr data-id="' . $id . '"><td>' . $id . '</td><td>' . $name . '</td><td>' . $ui->btn('', $self . '?del=' . $id, ['kind' => 'delete', 'class' => 'ajax_delete']) . '</td></tr>';
    }
    $table = '<h4>Row delete (table2 btn_delete, yf_js_admin)</h4><table class="' . $ui->table_class() . ' docs-confirm-table"><tbody>' . $rows . '</tbody></table>';
    js(<<<'JS'
(function () {
    var out = document.getElementById('docs-confirm-out');
    var show = function (v) { out.textContent = JSON.stringify(v); };
    var on = function (id, fn) { document.getElementById(id).addEventListener('click', fn); };
    on('docs-confirm-basic', function () { yf.confirm('Go on?').then(show); });
    on('docs-confirm-danger', function () { yf.confirm({ title: 'Delete', message: 'Delete the item?\nIt cannot be undone.', danger: true, ok: 'Delete' }).then(show); });
    on('docs-confirm-typed', function () { yf.confirm({ message: 'Purge every cache?', danger: true, type: 'purge', ok: 'Purge' }).then(show); });
    on('docs-confirm-prompt', function () {
        yf.prompt({ message: 'Number of days', value: '7', required: true, validate: function (v) { return /^\d+$/.test(v) || 'Digits only'; } }).then(show);
    });
    on('docs-confirm-two', function () {
        Promise.all([yf.confirm('First?'), yf.confirm('Second?')]).then(show);
    });
})();
JS);
    return $intro . $api . $decl . $table;
};

Confirm and prompt dialogs on a Bootstrap modal (asset yf_confirm, no jQuery, no Bootstrap JS needed): yf.confirm(opts) resolves to true / false, yf.prompt(opts) to the text or null. A link, a button or a form with data-yf-confirm="..." asks first, through one delegated listener, and htmx hx-confirm uses the same dialog. Esc cancels, Enter confirms, Tab stays in the dialog; a danger dialog starts on Cancel. The admin asks with it everywhere (_class('admin_ui')->confirm_attrs()).

JavaScript API

Answer: none yet

Declarative

Link

Page state: none; htmx: nothing yet

Row delete (table2 btn_delete, yf_js_admin)

1Apples
2Pears