Any page can be presented as a modal dialog by adding the data-rel="dialog"
attribute to the page anchor link. When the "dialog" attribute is applied, the framework adds styles to add rounded corners, margins around the page and a dark background to make the "dialog" appear to be suspended above the page. If the dialog has a header the framework will also add a close button at the left side of the header.
<a href="foo.html" data-rel="dialog">Open dialog</a>
By default, the dialog will open with a 'pop' transition. Like all pages, you can specify any page transition you want on the dialog by adding the data-transition
attribute to the link. To make it feel more dialog-like, we recommend specifying a transition of "pop", "slideup" or "flip".
<a href="foo.html" data-rel="dialog" data-transition="pop">Open dialog</a>
When any link is clicked within in a dialog, the framework will automatically close the dialog and transition to the requested page, just as if the dialog were a normal page. To create a "cancel" button in a dialog, just link to the page that triggered the dialog to open and add the data-rel="back"
attribute to your link. This pattern of linking to the previous page is also usable in non-JS devices as well.
For JavaScript-generated links, you can simply set the href attribute to "#" and use the data-rel="back"
attribute. You can also call the dialog's close()
method to programmatically close dialogs, for example: $('.ui-dialog').dialog('close')
.
Just like the page plugin, you can set a dialog's close button text through an option or data attribute. The option can be configured for all dialogs by binding to the mobileinit
event and setting the $.mobile.dialog.prototype.options.closeBtnText
property to a string of your choosing, or you can place the data attribute data-close-btn-text
to configure the text from your markup.
Since dialogs are typically used to support actions within a page, the framework does not include dialogs in the hash state history tracking. This means that dialogs will not appear in your browsing history chronology when the Back button is clicked. For example, if you are on a page, click a link to open a dialog, close the dialog, then navigate to another page, if you were to click the browser's Back button at that point you will navigate back to the first page, not the dialog.
Dialogs can be styled with different theme swatches, just like any page by adding data-theme
attributes to the header, content, or footer containers. Here is an example of a different dialog design:
Dialogs appear to be floating above an overlay layer. This overlay adopts the swatch A content color by default, but the data-overlay-theme
attribute can be added to the page wrapper to set the overlay to any swatch letter. Here is an example of a dialog with the overlay set to swatch e:
Dialogs can also be used more like a control sheet to offer multiple buttons if you simply remove the top margin from the dialog's inner container element. For example, if your dialog page had a class of my-dialog
, you could add this CSS to pin that dialog to the top: .ui-dialog.my-dialog .ui-dialog-contain { margin-top: 0 }
, or you could just apply that style to all dialogs with .ui-dialog .ui-dialog-contain { margin-top: 0 }
.
For the sake of readability, dialogs have a default width
of 92.5% and a max-width
of 500 pixels. There is also a 10% top margin
to give dialogs larger top margin on larger screens, but collapse to a small margin on smartphones. The dialog's inner container is shifted towards the top
with 15px to hide the corner styling if a dialog is used as a control sheet (see above). To override these styles, add the following CSS override rule to your stylesheet and tweak as needed:
.ui-dialog-contain {
width: 92.5%;
max-width: 500px;
margin: 10% auto 15px auto;
padding: 0;
position: relative;
top: -15px;
}