Back to Grapesjs

Modal Dialog

docs/api/modal_dialog.md

0.22.164.0 KB
Original Source
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->

You can customize the initial state of the module from the editor initialization, by passing the following Configuration Object

js
const editor = grapesjs.init({
 modal: {
   // options
 }
})

Once the editor is instantiated you can use its API. Before using these methods you should get the module from the instance

js
const modal = editor.Modal;

Available Events

  • modal:open Modal is opened
javascript
editor.on('modal:open', () => { ... });
  • modal:close Modal is closed
javascript
editor.on('modal:close', () => { ... });
  • modal Event triggered on any change related to the modal. An object containing all the available data about the triggered event is passed as an argument to the callback.
javascript
editor.on('modal', ({ open, title, content, ... }) => { ... });

Methods

open

Open the modal window

Parameters

  • opts Object Options (optional, default {})

    • opts.title (String | HTMLElement)? Title to set for the modal
    • opts.content (String | HTMLElement)? Content to set for the modal
    • opts.attributes Object? Updates the modal wrapper with custom attributes

Examples

javascript
modal.open({
  title: 'My title',
  content: 'My content',
  attributes: { class: 'my-class' },
});

Returns this

close

Close the modal window

Examples

javascript
modal.close();

Returns this

onceClose

Execute callback when the modal will be closed. The callback will be called one only time

Parameters

Examples

javascript
modal.onceClose(() => {
 console.log('The modal is closed');
});

Returns this

onceOpen

Execute callback when the modal will be opened. The callback will be called one only time

Parameters

Examples

javascript
modal.onceOpen(() => {
 console.log('The modal is opened');
});

Returns this

isOpen

Checks if the modal window is open

Examples

javascript
modal.isOpen(); // true | false

Returns Boolean

setTitle

Set the title to the modal window

Parameters

Examples

javascript
// pass a string
modal.setTitle('Some title');
// or an HTMLElement
const el = document.createElement('div');
el.innerText =  'New title';
modal.setTitle(el);

Returns this

getTitle

Returns the title of the modal window

Examples

javascript
modal.getTitle();

Returns (string | HTMLElement)

setContent

Set the content of the modal window

Parameters

Examples

javascript
// pass a string
modal.setContent('Some content');
// or an HTMLElement
const el = document.createElement('div');
el.innerText =  'New content';
modal.setContent(el);

Returns this

getContent

Get the content of the modal window

Examples

javascript
modal.getContent();

Returns (string | HTMLElement)