Skip to content

PDF Generation Template Editor

Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.

View on GitHub

Custom Modals ​

You can register custom modal dialogs and control them with the standard element APIs (openElement, closeElements, disableElements).

UI API ​

UI.addCustomModal(options) ​

Register a custom modal dialog.

javascript
/**
 * @method UI.addCustomModal
 * @param {object} options
 * @param {string} options.dataElement                 Unique name of the custom modal.
 * @param {boolean} [options.disableBackdropClick=false] Disable closing on outside click.
 * @param {boolean} [options.disableEscapeKeyDown=false] Disable closing on Escape.
 * @param {function} [options.render]                   Function returning the modal contents.
 * @param {object} [options.header]                     { title, className, style, innerHTML, children }
 * @param {object} [options.body]                       { title, className, style, innerHTML, children }
 * @param {object} [options.footer]                     { title, className, style, innerHTML, children }
 */
UI.addCustomModal({
  dataElement: 'myModal',
  disableBackdropClick: false,
  disableEscapeKeyDown: false,
  header: { title: 'My Modal' },
  body:   { title: 'Modal content' },
  footer: { children: [{ title: 'Close', button: true, onClick: () => UI.closeElements(['myModal']) }] },
});

Registering a modal with the same dataElement as an existing one replaces it.

Controlling a modal ​

Once registered, the modal is controlled like any other element using its dataElement:

javascript
UI.openElement('myModal');           // show
UI.closeElements(['myModal']);       // hide
UI.isElementOpen('myModal');         // => boolean
UI.disableElements(['myModal']);     // remove entirely

Structure ​

A modal is divided into three optional sections — header, body, footer. You can also supply a render function that returns an HTMLElement or a string for full custom content; it is called each time the modal opens.

Section fieldPurpose
titleSection title text.
classNameCustom class(es) applied to the section.
styleInline style object.
innerHTMLHTML content of the section (used instead of title and children).
childrenArray of child items { title, button, className, style, onClick }. With button: true the child is rendered as a button.

Examples ​

A confirmation modal ​

javascript
UI.addCustomModal({
  dataElement: 'confirmModal',
  disableBackdropClick: true,
  header: { title: 'Please confirm' },
  body:   { title: 'Are you sure you want to continue?' },
  footer: {
    children: [
      { title: 'Cancel', button: true, onClick: () => UI.closeElements(['confirmModal']) },
      { title: 'OK', button: true, onClick: () => { /* … */ UI.closeElements(['confirmModal']); } },
    ],
  },
});

UI.openElement('confirmModal');

A modal with a custom render function ​

javascript
UI.addCustomModal({
  dataElement: 'customRenderModal',
  render: () => {
    const el = document.createElement('div');
    el.innerHTML = '<h3>Custom</h3><p>Rendered by a function.</p>';
    return el;
  },
});
UI.openElement('customRenderModal');

Internal plumbing ​

  • The viewer store holds the list of custom modals; addCustomModal appends or replaces by dataElement.
  • Disabled modals are not rendered.
  • Clicking the backdrop and pressing Escape close the modal unless disableBackdropClick / disableEscapeKeyDown is set.
  • Visibility is driven by the same open / disabled state as the built-in dialogs.