Element Framework

Dialog

  • HTML
  • JS
  • CSS
<ef-dialog id="d1" header="System Permissions" draggable opened>
  <p>Grant geolocation permissions?</p>
  <p>This will allow the application to see your real-time location at any point in time.</p>
</ef-dialog>
<div class="container">
  <ef-button onclick="document.querySelector(&apos;#d1&apos;).opened = true;">Open Dialog</ef-button>
</div>

                  
.container {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 300px;
}

ef-dialog is a popup window designed to contain and display any HTML content. It provides modal and dragging functionality, and also allows custom footers and control buttons to be included.

Basic usage

Dialog can easily be launched by adding the opened property. If you want the dialog to open by default, include the opened attribute when you embed ef-dialog.

<ef-dialog id="dlg1" header="System Permissions">
  <p>Grant geolocation permissions?</p>
  <p>This will allow the application to see your real-time location at any point in time.</p>
</ef-dialog>
var dlg = document.getElementById('dlg1');
function showDialog(open) {
  dlg.opened = open;
}

The default behavior of ef-dialog is to display at the center of the viewport and to not be draggable. You can enable dragging by adding the draggable attribute or using the property setting.

  • HTML
  • JS
  • CSS
<ef-dialog id="d1" header="System Permissions">
  <p>Grant geolocation permissions?</p>
  <p>This will allow the application to see your real-time location at any point in time.</p>
</ef-dialog>
<div class="container">
  <ef-button onclick="openDialog(&apos;d1&apos;, true)">Draggable</ef-button>
  <ef-button onclick="openDialog(&apos;d1&apos;, false)">Fixed</ef-button>
</div>
function openDialog(id, dragMode) {
  var dlg = document.getElementById(id);
  dlg.draggable = dragMode;
  dlg.opened = true;
}
.container {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 300px;
}

ef-button {
  margin: 0 5px;
}
<ef-dialog id="dlg1" header="System Permissions" draggable>
  <p>Grant geolocation permissions?</p>
  <p>This will allow the application to see your real-time location at any point in time.</p>
</ef-dialog>

Confirmed vs cancelled

You may want to detect if the user has closed the dialog by clicking the OK or Cancel button. You can listen to the opened-changed event and check the value of the confirmed property, which will be set to true if the user clicked the OK button to close the dialog.

  • HTML
  • JS
  • CSS
<ef-dialog id="d1" header="System Permissions" draggable opened>
  <p>Grant geolocation permissions?</p>
  <p>This will allow the application to see your real-time location at any point in time.</p>
</ef-dialog>
<div class="container">
  <ef-button onclick="document.getElementById(&apos;d1&apos;).opened = true;">Open Dialog</ef-button>
  <p id="close-result"></p>
  <p>
  </p>
</div>
var dlg = document.getElementById('d1');
dlg.addEventListener('opened-changed', function(e) {
  document.getElementById('close-result').innerHTML = dlg.confirmed ? 'User clicked OK' : 'User clicked Cancel';
});
.container {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 300px;
}
<ef-dialog id="d1" header="System Permissions">
  <p>Grant geolocation permissions?</p>
  <p>This will allow the application to see your real-time location at any point in time.</p>
</ef-dialog>
var dlg = document.getElementById('d1');
dlg.addEventListener('opened-changed', function (e) {
  if (dlg.confirmed) {
    // user clicked OK button
  }
  else {
    // user clicked Cancel button
  }
});

The dialog provides default OK and Cancel buttons. To replace those buttons with your own content, assign content to the footer slot.

  • HTML
  • JS
  • CSS
<ef-dialog id="d1" header="System Permissions">
  <p>Grant geolocation permissions?</p>
  <p>This will allow the application to see your real-time location at any point in time.</p>
  <div slot="footer" style="padding: 15px 25px;">
    <ef-button style="width:100%" cta onclick="accept()">Accept</ef-button>
  </div>
</ef-dialog>
<div class="container">
  <ef-button onclick="document.getElementById(&apos;d1&apos;).opened = true;">Custom Dialog</ef-button>
</div>
function accept() {
  document.getElementById('d1').opened = false;
}
.container {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 300px;
}
<ef-dialog id="d1" header="System Permissions">
  <p>Grant geolocation permissions?</p>
  <p>This will allow the application to see your real-time location at any point in time.</p>
  <div slot="footer" style="padding: 15px 25px;">
      <ef-button style="width:100%" cta onclick="accept()">Accept</ef-button>
  </div>
</ef-dialog>
function accept () {
  // user clicked accept button, close dialog.
  document.getElementById('d1').opened = false;
}

Customize close behaviors

By default, ef-dialog will only close when the user clicks the OK or Cancel button, or presses the ESC key. However, you can allow the dialog to close when the user clicks outside of the dialog, or prevent the dialog from closing when the ESC key is pressed.

To close the dialog when the user clicks outside of the dialog, set the noCancelOnOutsideClick property to false.

<ef-dialog id="d1" header="System Permissions">
  <p>Grant geolocation permissions?</p>
  <p>This will allow the application to see your real-time location at any point in time.</p>
</ef-dialog>
document.getElementById('d1').noCancelOnOutsideClick = false;

To prevent the dialog from closing on ESC key press, add the no-cancel-on-esc-key attribute to ef-dialog.

<ef-dialog id="d1" header="System Permissions" no-cancel-on-esc-key>
  <p>Grant geolocation permissions?</p>
  <p>This will allow the application to see your real-time location at any point in time.</p>
</ef-dialog>

Slots

footer
Hide default OK and Cancel button and replace dialog's footer with your custom content.

API Reference

Attributes

string | null
header
Set Header/Title of the dialog
boolean
draggable
Should the dialog be draggable
boolean
opened
Set dialog to open
string
x
Set a specific x coordinate of dialog
string
y
Set a specific y coordinate of dialog
boolean
no-cancel-on-esc-key
Prevents dialog to close when user presses ESC key
boolean
full-screen
Set dialog to full screen
string
position-target
Set position of dialog i.e. `top`, `right`, `left`, `bottom`, `center` or combination of theme e.g. `top right`.

Properties

string | null
header
Set Header/Title of the dialog
boolean
draggable
Should the dialog be draggable
false
boolean
noCancelOnOutsideClick
Prevents dialog to close when user clicks outside the dialog.
boolean
opened
Set dialog to open
false
boolean
noCancelOnEscKey
Prevents dialog to close when user presses ESC key
false
string
x
Set a specific x coordinate of dialog
string
y
Set a specific y coordinate of dialog
boolean
fullScreen
Set dialog to full screen
string
positionTarget
Set position of dialog i.e. `top`, `right`, `left`, `bottom`, `center` or combination of theme e.g. `top right`.

Methods

refit()
Clear all cached values and fit the popup. Use this function only if maxWidth, maxHeight, minWidth, minHeight, height, width are changed

Events

opened-changed
Fired when value of `opened` property is changed. Prevent default to stop default action
confirm
Fired when dialog is closed by user clicked a default OK button. Prevent default to stop default action
cancel
Fired when dialog is closed by user clicked a default Cancel button, clicked outside to close dialog or press ESC to close the dialog. Prevent default to stop default action