ibrows/vanilla-modal

Name: vanilla-modal

Owner: PwC Experience Center

Description: A flexible, dependency-free, CSS-driven JavaScript modal.

Created: 2017-04-06 09:20:02.0

Updated: 2017-04-06 09:20:03.0

Pushed: 2017-04-25 08:46:22.0

Homepage: null

Size: 120

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Vanilla Modal

npm version

See the demo.

A tiny, flexible, completely dependency-free CSS-powered JavaScript modal.

Written in ECMAScript 2015 and transpiled for universal use with Babel 6.

Contributions welcome.

Please be aware of recent breaking changes.
If importing using a <script/> tag

new VanillaModal() will now be new VanillaModal.default(). This is due to the way in which Babel 6 handles default exports.

The semantically-unsound [rel="modal:open"] and [rel="modal:close"] default listeners have been deprecated

They have been replaced by [data-modal-open] and [data-modal-close]. Please update your codebase if you were not supplying open or close parameters to the modal.

License

MIT. Please feel free to offer any assistance - pull requests, bug tracking, suggestions are all welcome. The issue tracker is over here.

Q & A
Why?

I was pretty fed up looking for a modal script that wasn't variously:

a) Bloated. b) Inaccessible. c) Needlessly complicated. d) Riddled with third party dependencies (here's looking at you, jQuery). e) Trying to hijack beautiful hardware-accelerated CSS transitions using JavaScript.

Can I integrate this with a single page app framework?

Since the modal's open and close event listeners are delegated from the document, you can use this script with any client-side routing or DOM-affecting framework.

If you're concerned about garbage collection, you may be pleased to know there's a modal.destroy() method baked in, which removes all internal events and references.

Usage and Examples
1. Install the script. 2. Include the script in your project. 3. Create the modal's container using HTML.

This part is important. Vanilla Modal doesn't use any template strings or DOM building algorithms (although this is on the roadmap for version 2).

As a result, you will need to add your modal's container HTML to your document - by using JavaScript ahead of the modal's instantiation, or by writing HTML into your document.

The payoff is that you can make the modal look any way you wish.

 class="modal">
iv class="modal-inner">
<a data-modal-close>Close</a>
<div class="modal-content"></div>
div>
v>

Following this, create some off-screen containers to house your modal's content. Give each an ID to make them selectable via anchor elements, and accessible using JavaScript-disabled browsers.

The modal will pick up the contents that are inside the container with the ID specified by the triggering anchor's href attribute. It will place them in the modalContent container specified by your settings object. In the example above, the default container class of .modal-content is used.

 id="modal-1" class="modal-hider">Modal 1 content</div>
 id="modal-2" class="modal-hider">Modal 2 content</div>

Note: Vanilla Modal applies the class specified by loadClass to the page element. Both are specified in settings, and default respectively to vanilla-modal and body. This is done in order to make the modal as accessible as possible for all use cases.

le type="text/css">
dy.vanilla-modal .modal-hider {
position: absolute;
left: -99999em;

yle>
4. Create a VanillaModal instance.
t modal = new VanillaModal(options);

(Where options is a configuration hash. The full list of options, as well as their defaults, are listed below under the “Options and Defaults” heading.)

5. Add your own CSS rules.

Here's the demo's stylesheet.

Vanilla Modal handles display logic using CSS. Hardware acceleration via CSS transforms comes highly recommended, for a smooth device-agnostic experience.

Two things to keep in mind:

sition: opacity 0.2s, z-index 0s 0.2s;
6. Delegation and Built-in Methods

Default delegate targets are as follows:

Examples follow:

The following element will open #modal-1 using VanillaModal.

ref="#modal-1" data-modal-open>Modal 1</a>

The element below will close the modal.

ata-modal-close>Close</a>

These defaults can easily be changed at instantiation:

t modal = new VanillaModal({
en: '.my-open-class',
ose: '.my-close-class'

7. Programmatically opening a modal

If you need to open the modal automatically, you can do so by passing a DOM ID string to the open() function.

For example:

t modal = new VanillaModal();
l.open('#foo');

The modal can likewise be closed programmatically using the close() method.

VanillaModal Public Properties
Options and Defaults

The options object contains DOM selector strings and bindings. Defaults are overridden by providing an options object to a new VanillaModal instance.

Note: this API is feature-frozen for the 1.x release, but subject to change at 2.x.

Defaults:

dal: '.modal',
dalInner: '.modal-inner',
dalContent: '.modal-content',
en: '[data-modal-open]',
ose: '[data-modal-close]',
ge: 'body',
adClass: 'vanilla-modal',
ass: 'modal-visible',
ickOutside: false,
oseKeys: [27],
ansitions: true,
BeforeOpen: null,
BeforeClose: null,
Open: null,
Close: null

Compatibility

This script works in the evergreen mobile & desktop browsers, as well as IE 11, 10, and 9 (the last has no support for transitions).

It is not compatible with Opera Mini or the Blackberry browser, and there are currently no plans afoot to support either.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.