Assets
Type | File |
---|---|
js | /dist/js/dialog.js |
css | ../dist/css/dialog.css |
less | /dist/less/dialog.less |
scss | /dist/scss/dialog.scss |
Examples
Dropdown Menu
Easily link an anchor with a dialog and create a dropdown menu on hover/focus or click. Use one of the twelve positions inherited from Tooltip to align your dropdown wherever you like or use a position of "none" to bypass all positioning logic.
- About
- Brand
- Categories
- HTML5
Learn HTML 5 like a pro
- CSS3
Learn CSS3 like a pro
- JavaScript
Learn JavaScript like a pro
- Accessibility
Learn Accessibility like a pro
- HTML5
Popover
The number of ways a Dialog can be used is endless. Anytime you have an element, assiciated with another, that is abolustely positioned in a "popover" fashion, it is a suitable case to use the Dialog.
Setup
Although Dialog inherits from tooltip, the main difference between the two is that Dialog is *not* a delegate. Tooltip is more performance for batch dialogs/tooltips, where as Dialog is a better choice for single instances or instances that will require more customization. That said, you set up the markup of a Dialog in the same way you would a tooltip.
As Aria-Label
<button class="mk-dg" aria-label="Some text and even markup!">A Dialog</button>
As Nested Element
<button class="mk-dg" data-position="top-center">
<span>A Dialog</span>
<span class="mk-dg-modal">A Dialog Modal!</span>
</button>
As Sibling Element
<div>
<button class="mk-dg" data-position="bottom-center" data-arrow="true">
<span>A Dialog</span>
</button>
<div class="mk-dg-modal">A Dialog Modal!</div>
</div>
Anywhere You Want
This option requires you to 'connect' the dialog trigger and modal with one another manually with id and aria-describedby attributes.
<!-- some part of the document -->
<button class="mk-dg" aria-describedby="my-dialog-modal-id">
<span>A Dialog</span>
</button>
<!-- some other part of the document -->
<div id="my-dialog-modal-id" class="mk-dg-modal">A Dialog Modal!</div>
Instantiation
Unlike Tooltip (who works on delegation), Dialog is a single instance component like most other Mk components. Instantiation takes two arguments: the first being the root (trigger) node, the second being optional configuration options. Config options are opt-in and can also be added via data-attributes on the root element. See the Examples Section and Tooltip Documentation for more of that. But in case you want to, below are all possible options available to pass in. Additionally, check out the Events Section for adding events to your instance.
var Dialog = Mk.get('Dialog');
//
// instantiate
var myDialog = new Dialog('.mk-dg', {
// set the default position
// see Examples for all 12 positions
position: 'bottom-center',
//tell Dialog whether to add the arrow class (for caret styling)
arrow: true,
// set the delay in milliseconds for mouseover/out tooltips, if any
delay: 100,
templates: {
..change any of the templates for this instance only..
},
events: {
..add events here (optional)..
}
});
Events
All events are part of the Mk EventEmitter and *not* added to the DOM. Events are also called in context of the component instance so it isn't a great idea to use a proxy function to change context, as you'll loose a lot of beneficial object access.
show
Fires when dialog is shown.
instance.on('show', function (dg, modal) {
console.info('Showing for:', dg, modal);
});
hide
Fired when dialog is hidden.
instance.on('show', function (dg, modal) {
console.info('Hiding for:', dg, modal);
});
connect
Fired when a connection is being made between a trigger and it's modal.
instance.on('connect', function (dg, modal) {
console.info('Connection being made for:', dg, modal);
});
position
Fired when positioning is invoked for a dialog modal.
instance.on('position', function (dg, modal, coords) {
console.info('Coors for positioning are:', coords);
});
lock
Fires when dialog is locked or unlocked.
instance.on('lock', function (dg, isLocked) {
console.info('This tooltip is now ', isLocked && 'locked' || 'unlocked');
});
API
Below are the different public properties and methods accessible to you, the end developer. All of these are accessible on your Dialog [dg] instance as well as inside event handlers bound to your instance. Some methods have intentionally been left out due to their difficulty tof use for end developers vs. internal use.
Methods
instance.unmount()
Teardown instance freeing event, data, and reference memory.
- No params required.
instance.link()
Links the root to the modal element.
- No params required.
instance.position()
Positions the modal with the root element.
- No params required.
instance.focus()
Injects the focusable killswitch inside the modal for tab focus functionality.
- No params required.
instance.show([silent])
Shows the modal.
Parameters
- Name
- silent
- Type
- Boolean
- Description
- Set as true to keep show event from triggering.
instance.hide([immediate, silent])
Hides the modal.
Parameters
- Name
- immediate
- Type
- Boolean
- Description
- Set as true to close the modal without delay or animations.
- Name
- silent
- Type
- Boolean
- Description
- Set as true to keep hide event from triggering.
instance.hideAll()
Hides all page dialogs in root context with "immediate" mode on (no delay or animations).
- No params required.
instance.toggle()
Toggles the modal between show() and hide().
- No params required.
instance.lock()
Locks the modal from being shown or hidden.
- No params required.
instance.unlock()
Unlocks the modal, enabling the modal to be shown and hidden.
- No params required.
Properties
instance.locked
Boolean representing if the dialog is locked.
instance.unlocked
Boolean representing if the dialog is unlocked.
instance.isOpen
Boolean representing if the dialog is open.
instance.isHidden
Boolean representing if the dialog is closed.
instance.focusable
Boolean representing if the dialog has focusable content.