GitHub

API

Dialogs API

The Dialogs API shows dialogs with the built-in Modal of bigpipe-util, which has no dependencies. Its markup is that of Bootstrap modals by default, and several dialogs can be displayed at once: each new one is shown above the previous one.

Methods

setController

  • This method sets the JavaScript controller (module) for the dialog, allowing you to register additional event listeners (such as show, shown, hide, hidden) or other logic related to the dialog.

    PHP
    $response->setController('ModalMonitor');

    The arguments given after the name are passed to the controller after the dialog, e.g. setController('PostEditor', [$post]).

setTitle

  • Use this method to set the title of a dialog.

    PHP
    $response->setTitle("dialog title");

setBody

  • This method sets the body content of a dialog.

    PHP
    $response->setBody("dialog body");

setFooter

  • Use this method to set the footer content of a dialog.

    PHP
    $response->setFooter('dialog footer');

setDialog

  • This method sets the entire content of a dialog, including title, body, and footer.

    PHP
    $response->setDialog('dialog content');

closeDialogs

  • This method closes the open dialogs: all of them, or only the $limit most recent ones.

    PHP
    $response->closeDialogs();  // all of them
    $response->closeDialogs(2); // the last two

closeDialog

  • Use this method to close the currently displayed dialog.

    PHP
    $response->closeDialog();

dialog

  • This method renders the defined dialog.

    PHP
    $response->dialog();

Options and behaviors

The dialog behaves the same on every page, and each behavior can be changed per dialog. The setters can be called in any order before dialog():

PHP
$response->setTitle('Delete the post?')
    ->setBody($form)
    ->setBackdrop('static')          // true, false, or 'static': a click on the backdrop doesn't close it
    ->setKeyboard(false)             // Esc doesn't close it
    ->setHideOnSuccess('form')       // close it when the request of a form inside succeeds
    ->setPosition(80)                // the top margin in pixels
    ->dialog();
Setter Default Description
setBackdrop(bool|string) true false for no backdrop, 'static' for one that doesn't close the dialog.
setKeyboard(bool) true Close the dialog with Esc.
setAnimate(bool) false Animate showing and hiding.
setTimeout(int) Show the dialog after this many milliseconds.
setAutoFocus(bool) true Move the focus into the dialog when it is shown.
setTrapFocus(bool) true Keep Tab inside of the dialog.
setRefocus(bool) true Give the focus back to the element that opened the dialog when it closes.
setCausalElement(string $elementId) The element to give the focus back to, by its id. By default the element that sent the request, e.g. the link, or the focused element inside of it, e.g. the button of a form.
setHideOnTransition(bool) true Close the dialog before a page transition.
setHideOnSuccess(bool|string $selector) false Close the dialog when a request sent from inside of it succeeds, from any element or only those that match the selector.
setPosition(?int $top, bool $centered, bool $ignoreTopInShortViewport) Set the top margin: $top pixels, or else a third of the free height of the window (half when centered). Without arguments, the default margin.

setOption($name, $value), setOptions($array) and the array given to dialog($options) set any option of the browser part by its name, e.g. setOption('transition', 300); dialog($options) wins over the setters. The styling is up to your CSS: the markup is that of Bootstrap modals, and the options above add no classes. To style dialogs with Bootstrap, Tailwind or your own CSS, replace the markup, the class names and the selectors with Modal.templates, Modal.options and Modal.selectors, see Styling.

The dialog is also a layer in the browser, whose beforehide event a controller can use to keep it open, see Layer.

Live Example

You can observe this API in action in the demo page provided.

Example

If you want to trigger a dialog from the backend:

PHP
<?php
// dialog.php
$response = new \dobron\BigPipe\DialogResponse();

$response->setTitle('Dialog title')
    ->setController('ModalLogger')
    ->setBody('html <strong>content</strong>')
    ->setFooter('<button>close</button>')
    ->dialog();

$response->send();

To open this dialog from the frontend, you can use the following HTML code:

MARKUP
<a href="#"
   ajaxify="/dialog.php"
   rel="dialog">Open Dialog</a>

For frontend-triggered dialog invocation:

JAVASCRIPT
import Dialog from "bigpipe-util/dist/core/Dialog";

(new Dialog()).showFromModel({
    controller: 'ModalLogger',
    backdrop: 'static',
    title: 'Dialog title',
    body: 'html <strong>content</strong>',
    footer: '<button>close</button>',
});

Here's an example of the implementation of a controller that can be attached to the dialog:

JAVASCRIPT
export default class ModalLogger
{
    constructor(dialog) {
        console.log('dialog data:', dialog);

        dialog.on('show', (event) => {
            console.log('event: show', event)
        });

        dialog.on('shown', (event) => {
            console.log('event: shown', event)
        });

        dialog.on('hide', (event) => {
            console.log('event: hide', event)
        });

        dialog.on('hidden', (event) => {
            console.log('event: hidden', event)
        });
    }
}

Edit this page on GitHub