Use a modal for dialog boxes, confirmation messages, or other content that can be
called up. In order for the modal to work you have to add the Modal ID to the link of the trigger.
To add a close button, just add the class
.modal-close
to your button. To make the overlay blurry, add blur-overlay
class to the modal element. If you just want the
modal itself to have the blur effect,
just add blur-bg
to the modal element and blur-bg acrylic
if you want the acrylic effect also.
For outlined primary
modals, the outline color follows the
semantic colors just like in buttons. You can
change the primary to success, info, warning or danger. If you just use the outlined
class, the
border color will fallback to secondary color. Please note that outline on bottom-sheet is a bit
different.
<!-- Modal Trigger -->
<a class="waves-effect waves-light btn modal-trigger" href="#modal1">Modal</a>
<!-- Modal Structure -->
<div id="modal1" class="modal">
<div class="modal-content">
<h4>Modal Header</h4>
<p>A bunch of text</p>
</div>
<div class="modal-footer">
<a href="#!" class="modal-close waves-effect waves-green btn-flat">Agree</a>
</div>
</div>
<!-- Modal Trigger -->
<a class="waves-effect waves-light btn modal-trigger" href="#modal1-1">Modal (Blurred Overlay)</a>
<!-- Modal Structure -->
<div id="modal1-1" class="modal blur-overlay">
<div class="modal-content">
<h4>Modal Header</h4>
<p>A bunch of text</p>
</div>
<div class="modal-footer">
<a href="#!" class="modal-close waves-effect waves-green btn-flat">Agree</a>
</div>
</div>
<!-- Modal Trigger -->
<a class="waves-effect waves-light btn modal-trigger" href="#modal1-2">Modal (Blurred Background)</a>
<!-- Modal Structure -->
<div id="modal1-2" class="modal blur-bg">
<div class="modal-content">
<h4>Modal Header</h4>
<p>A bunch of text</p>
</div>
<div class="modal-footer">
<a href="#!" class="modal-close waves-effect waves-green btn-flat">Agree</a>
</div>
</div>
<!-- Modal Trigger -->
<a class="waves-effect waves-light btn modal-trigger" href="#modal1-3">Modal (Blurred Background, Acrylic)</a>
<!-- Modal Structure -->
<div id="modal1-3" class="modal blur-bg acrylic">
<div class="modal-content">
<h4>Modal Header</h4>
<p>A bunch of text</p>
</div>
<div class="modal-footer">
<a href="#!" class="modal-close waves-effect waves-green btn-flat">Agree</a>
</div>
</div>
Initialization
To open a modal using a trigger:
document.addEventListener('DOMContentLoaded', function() {
var elems = document.querySelectorAll('.modal');
var instances = M.Modal.init(elems, {
// specify options here
});
});
// Or with jQuery
$(document).ready(function(){
$('.modal').modal({
// specify options here
});
});
Options
You can customize the behavior of each modal using these options. For example, you can call a custom function to run when a modal is dismissed. To do this, just place your function in the initialization code as shown below.
Name | Type | Default | Description |
---|---|---|---|
opacity | Number | 0.5 | Opacity of the modal overlay. |
inDuration | Number | 250 | Transition in duration in milliseconds. |
outDuration | Number | 250 | Transition out duration in milliseconds. |
onOpenStart | Function | null | Callback function called before modal is opened. |
onOpenEnd | Function | null | Callback function called after modal is opened. |
onCloseStart | Function | null | Callback function called before modal is closed. |
onCloseEnd | Function | null | Callback function called after modal is closed. |
preventScrolling | Boolean | true | Prevent page from scrolling while modal is open. |
dismissible | Boolean | true | Allow modal to be dismissed by keyboard or overlay click. |
startingTop | String | '4%' | Starting top offset |
endingTop | String | '10%' | Ending top offset |
Methods
Because jQuery is no longer a dependency, all the methods are called on the plugin instance. You can get the plugin instance like this:
var instance = M.Modal.getInstance(elem); /* jQuery Method Calls You can still use the old jQuery plugin method calls. But you won't be able to access instance properties. $('.modal').modal('methodName'); $('.modal').modal('methodName', paramName); */
.open();
Open modal
instance.open();
.close();
Close modal
instance.close();
.destroy();
Destroy plugin instance and teardown
instance.destroy();
Properties
Name | Type | Description |
---|---|---|
el | Element | The DOM element the plugin was initialized with. |
options | Object | The options the instance was initialized with. |
isOpen | Boolean | If the modal is open. |
id | string | ID of the modal element |
Modals with Fixed Header
Modal With Fixed Header Modal With Fixed Header (Blurred Background)If you want to keep the header always visible, add
modal-fixed-header
class to the modal and also add a <div class="modal-header">
element inside the modal container
for your header.
<!-- Modal Trigger -->
<a class="waves-effect waves-light btn modal-trigger" href="#modal1">Modal</a>
<!-- Modal Structure -->
<div id="modal1" class="modal modal-fixed-header">
<div class="modal-header">Modal Header</div>
<div class="modal-content">
<p>A bunch of text</p>
</div>
</div>
Bottom Sheet Modals
Modal Bottom Sheet Style Modal Bottom Sheet Style (Blurred Overlay) Modal Bottom Sheet Style (Blurred Background)Bottom Sheet Modals are good for displaying actions to the user on the bottom of a screen. They still act the same as regular modals.
<!-- Modal Trigger -->
<a class="waves-effect waves-light btn modal-trigger" href="#modal1">Modal</a>
<!-- Modal Structure -->
<div id="modal1" class="modal bottom-sheet">
<div class="modal-content">
<h4>Modal Header</h4>
<p>A bunch of text</p>
</div>
<div class="modal-footer">
<a href="#!" class="modal-close waves-effect waves-green btn-flat">Agree</a>
</div>
</div>
<!-- Modal Trigger -->
<a class="waves-effect waves-light btn modal-trigger" href="#modal1-1">Modal (Blurred Overlay)</a>
<!-- Modal Structure -->
<div id="modal1-1" class="modal bottom-sheet blur-overlay">
<div class="modal-content">
<h4>Modal Header</h4>
<p>A bunch of text</p>
</div>
<div class="modal-footer">
<a href="#!" class="modal-close waves-effect waves-green btn-flat">Agree</a>
</div>
</div>
<!-- Modal Trigger -->
<a class="waves-effect waves-light btn modal-trigger" href="#modal1-2">Modal (Blurred Background)</a>
<!-- Modal Structure -->
<div id="modal1-2" class="modal bottom-sheet blur-bg">
<div class="modal-content">
<h4>Modal Header</h4>
<p>A bunch of text</p>
</div>
<div class="modal-footer">
<a href="#!" class="modal-close waves-effect waves-green btn-flat">Agree</a>
</div>
</div>