Modal
A progressively enhanced modal window that supports multiple toggles, multiple close buttons, and works with pure CSS thanks to :target
pseudo selector.
yarn add @threespot/modal
Usage
Javascript
import Modal from "@threespot/modal";
const container = document.getElementById("modal");
new Modal(container, opts);
const containers = document.querySelectorAll(".Modal");
containers.forEach(container => new Modal(container));
for (var i = 0, len = containers.length; i < len; i+=1) {
new Modal(containers[i]);
}
Styles needed
.Modal {
$selector: &;
$open-speed: 250ms;
$close-speed: 500ms;
$z-index: 900;
$bg-default: #fff;
background-color: rgba(#000, 0.5);
bottom: 0;
left: 0;
margin: 0 !important;
max-height: 0;
opacity: 0;
overflow: auto;
-webkit-overflow-scrolling: touch;
position: fixed;
right: 0;
top: 0;
transition: max-height 0s linear $close-speed,
visibility 0s linear $close-speed,
opacity $close-speed;
visibility: hidden;
z-index: $z-index;
&:not([aria-hidden]):target,
&[aria-hidden="false"] {
max-height: 100vh;
opacity: 1;
transition: max-height 0s linear 0s, visibility 0s linear 0s, opacity $open-speed;
visibility: visible;
}
&-content {
@include fs-print-hide;
background-color: $bg-default;
opacity: 0;
position: relative;
transform: scale(0.95);
transition: all $close-speed fs-easing('easeOutCubic');
visibility: hidden;
width: 100%;
z-index: $z-index + 1;
#{$selector}:not([aria-hidden]):target &,
#{$selector}[aria-hidden="false"] & {
opacity: 1;
transform: scale(1);
transition: all $open-speed fs-easing('easeOutCubic');
visibility: visible;
}
}
&-close {
padding: fs-rem(15px);
position: fixed;
right: 0;
top: 0;
transition: all 150ms ease-in-out;
z-index: $z-index + 2;
}
&--fullWidth {
#{$selector}-content {
min-height: 100%;
}
}
}
Minimum markup needed
<a data-modal="foo">Open</a>
<div class="Modal" id="foo">
<a class="Modal-close" href="#nav-modal-toggle" data-modal-close>Close</a>
<div class="Modal-content">
</div>
</div>
API
Required Data Attributes for markup
data-modal-close
tells Modal what button(s) can close the modal window. This can be anywhere inside of the .Modal
data-modal="{YOUR_MODAL_ID_GOES_HERE}"
- toggle button(s) for the modal button must have this attribute with the .Modal
corresponding ID as its value. This allow us to toggle the .Modal
on and off.
new Modal(el, [opts])
const el = document.querySelector('.Modal');
{
transitionSpeed: "100",
activeClasses: "is-active is-privacy-modal",
modalContentClass: "Modal-content",
onReady: function() {
console.log('Success!')
}
}
License
This is free software and may be redistributed under the terms of the MIT license.
About Threespot
Threespot is an independent digital agency hell-bent on helping those, and only those, who are committed to helping others. Find out more at https://www.threespot.com.