Modal
Create modal dialogs with different styles and transitions.
Table of contents
-
Usage -
Close button -
Center modal -
Header and footer -
Container modifier -
Full modifier -
Overflow -
Media -
Groups -
Component options -
JavaScript -
Modal dialogs -
Accessibility
Usage
The Modal component consists of an overlay, a dialog and an optional close button. You can use any element to toggle a modal dialog. To enable the necessary JavaScript, add the data-uk-toggle
attribute. An <a>
element needs to be linked to the modal’s id. If you are using another element, like a button, just add the data-uk-toggle="target: #ID"
attribute to target the id of the modal container.
Add the data-uk-modal
attribute to a <div>
element to create the modal container and an overlay that blanks out the page. It is important to add an id
to indicate the element for toggling. Use the following classes to define the modal’s sections.
Class | Description |
---|---|
.uk-modal-dialog | Add this class to a child <div> element to create the dialog |
.uk-modal-body | Add this class to create padding between the modal and its content. |
.uk-modal-title | Add this class to a heading element to create the modal title. |
.uk-modal-close | Add this class to an <a> or <button> element to create a close button and enable its functionality. |
Close button
To create a close button, enable its functionality and add proper styling and positioning, add the .uk-modal-close
class to an <a>
or <button>
element.
Add the data-uk-close
attribute from the Close component, to apply a close icon.
Center modal
To vertically center the modal dialog, you can use the .uk-margin-auto-vertical
class.
Note .uk-flex-top
on the modal
container is needed to support IE 11.
Header and footer
To divide the modal into different content sections, use the following classes.
Class | Description |
---|---|
.uk-modal-header | Add this class to a <div> element to create the modal header. |
.uk-modal-footer | Add this class to a <div> element to create the modal footer. |
Container modifier
Add the .uk-modal-container
class to expand the modal dialog to the default Container width.
Full modifier
To create a modal, that fills the entire page, add the .uk-modal-full
class.
Overflow
By default, the page will scroll with the modal, if its content exceeds the window height. To apply a scrollbar inside the modal, add the data-uk-overflow-auto
attribute from the Utility component to the modal body.
Media
If you want to display media, you should first check, if the Lightbox component doesn’t already offer everything you need. However, you can also use the modal to have more control over the markup to wrap your media in.
Note Use the data-uk-video
attribute from the Video component to make sure videos are
stopped when the modal is closed.
Groups
You can group multiple modals by linking from one to the other and back. Use this to create multistep wizards inside your modals.
Component options
Any of these options can be applied to the component attribute. Separate multiple options with a semicolon. Learn more
Option | Value | Default | Description |
---|---|---|---|
esc-close | Boolean | true | Close the modal when the Esc key is pressed. |
bg-close | Boolean | true | Close the modal when the background is clicked. |
stack | Boolean | false | Stack modals, when more than one is open. By default, the previous modal will be hidden. |
container | String | true | Define a target container via a selector to specify where the modal should be appended in the DOM. Setting it to false will prevent this behavior. |
cls-page | String | uk-modal-page | Class to add to <html> when modal is active |
cls-panel | String | uk-modal-dialog | Class of the element to be considered the panel of the modal |
sel-close | String | .uk-modal-close | CSS selector for all elements that should trigger the closing of the modal |
JavaScript
Learn more about JavaScript components.
Initialization
Events
The following events will be triggered on elements with this component attached:
Name | Description |
---|---|
beforeshow | Fires before an item is shown. |
show | Fires after an item is shown. |
shown | Fires after the item’s show animation has completed. |
beforehide | Fires before an item is hidden. |
hide | Fires after an item’s hide animation has started. |
hidden | Fires after an item is hidden. |
Methods
The following methods are available for the component:
Show
Shows the Modal.
Hide
Hides the Modal.
Modal dialogs
Note While the modal dialogs work perfectly, customizing padding requires a custom-compiled UIkit JavaScript. If you’re unfamiliar with compiling your JavaScript, it’s advisable to build the modal using HTML.
The component comes with a number of prepared modal dialogs that you can use for user interaction. You can call the dialog directly from JavaScript and use callback functions to process the user input.
Code | Description |
---|---|
UIkit.modal.alert('UIkit alert!') | Show an alert box with one button. |
UIkit.modal.confirm('UIkit confirm!') | Show a confirm dialog with your message and two buttons. |
UIkit.modal.prompt('Name:', 'Your name') | Show a dialog asking for a text input. |
UIkit.modal.dialog('<p>UIkit dialog!</p>'); | Show dialog with any HTML content. |
To process the user input, the modal uses a promise based interface which provides a then()
function to register your callback functions. The UIkit.modal.dialog
function however will return the modal itself.
The returned promise has a property dialog
, which holds a reference to the modal itself. This lets you manipulate e.g. the markup of the modal’s element.
To translate the button labels, the dialog functions accepts an optional options
object parameter. This has a key i18n
and two properties ok
and cancel
.
Accessibility
The Modal component adheres to the Dialog (Modal) WAI-ARIA design pattern and automatically sets the appropriate WAI-ARIA roles and properties.
- The modal has the
dialog
role and thearia-modal
property.
The Close component automatically sets the appropriate WAI-ARIA roles and properties.
- The close icon has the
aria-label
property, and if an<a>
element is used, thebutton
role.
Keyboard interaction
The Modal component can be accessed through keyboard using the following keys.
- The esc key closes the modal. This behaviour is disabled if the
bg-close: false
option is set.