0.5.2 • Published 5 years ago

modaly.js v0.5.2

Weekly downloads
3
License
MIT
Repository
github
Last release
5 years ago

modaly.js Build Status Coverage Status Bundlephobia Size

Tiny and easy javascript library for creating web modals.

  • Easy to use and customize
  • Lightweight 3.5kB (1.41 kB gzip)
  • Automatic accessibility features
  • Single file with styles injection
  • Highly compatible (browser list)

Install

NPM

npm install modaly.js

CDN

<script src="https://unpkg.com/modaly.js@0.5.0/dist/modaly.min.js"></script>

Alternatively, you can download distribution files directly.

Usage

The basic setup for one modal with default options is the following.

<div id="modal-1">
    <!-- the modal content goes here -->
</div>
new Modaly("#modal-1");

To open the modal add the data-modaly-open attribute to any DOM element.

<button data-modaly-open="#modal-1">
    Open Modal
</button>

To close the modal add the data-modaly-close attribute to any element inside the modal.

<div id="modal-1">
    <!-- the modal content goes here -->

    <img src="close-icon.svg" data-modaly-close />
</div>

You can open/close the modal programmatically.

const modal = new Modaly("#offer-modal");

if (offersAvailable) {
    modal.show();
}
const modal = new Modaly("#timeout-modal");

setTimeout(() => modal.hide(), 5000);

Options

You can further customize the behaviour and looks of your modals.

new Modaly("#modal-custom", {
    // Style
    background: "black",
    opacity: 0.75,
    duration: 250,
    animation: "ease-in",

    // Navigation
    escape: true,
    overlay: true,
    accesible: true,

    // Callbacks
    onShow: (modal, trigger) => {},
    onHide: (modal, trigger) => {}
});
OptionDefaultDescription
background"black"Modal overlay color
opacity0.75Modal overlay opacity
duration250Milliseconds that the transition lasts
animation"ease-in"Timing function for the transition
escapetrueIf the modal can be closed using the ESC key
overlaytrueIf the modal can be closed clicking the overaly
accessibletrueSetup the modal and triggers to be used by assistive technologies
lazyLoadingtrueLazy load the resources inside the modal with data-src attribute

If the initial microsecond where the modal flashes is a problem, you can always add the display none attribute.

<div id="modal-1" style="display: none">
    <!-- the modal content goes here -->

    <img src="close-icon.svg" data-modaly-close />
</div>

Callbacks

onShow(modal, trigger)

Fired when the modal it is about to open.

  • modal: the modal DOM element.
  • trigger: which DOM element openned the modal. null if no DOM element triggered it.

onHide(modal, trigger)

Fired when the modal it is about to close.

  • modal: the modal DOM element.
  • trigger: which DOM element closed the modal. null if no DOM element triggered it.

Lazy Loading

Since version 0.5.0 you can lazy load the resources inside the modal when it opens. This comes in handy when dealing with image slideshows libraries that do not support this feature.

To use it, as in many other libraries, simple add the data-src attribute to the resource you want to lazy load.

If you use your own lazy loading, you can disable it by setting to false the lazyLoading options parameter.

Accessibility

If the option accesible is true, accessibility attributes will be added to the modal and the close triggers automatically.

It will change from this.

<div id="modal-1">
    <!-- modal content -->

    <img src="close-icon.svg" data-modaly-close />
</div>

To this. The aria-hidden attribute will toggle accordingly to the modal.

<div id="modal-1" role="dialog" aria-modal="true" aria-hidden="true">
    <!-- modal content -->

    <img src="close-icon.svg" data-modaly-close aria-label="close this dialog"/>
</div>

Disclaimer: I am by no means an expert on accessibility technologies, but if you are one, feel free to reach out for improving the library.

Licensing

Created by Victor Navarro and licensed under MIT license.

0.5.2

5 years ago

0.5.1

5 years ago

0.5.0

5 years ago

0.4.2

5 years ago

0.4.1

5 years ago

0.4.0

5 years ago

0.3.3

5 years ago

0.3.2

5 years ago

0.3.1

5 years ago

0.3.0

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.0

5 years ago