solid-modals
Version:
A container for handling modals and hamburger menus in SolidJS
225 lines (218 loc) • 6.53 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var web = require('solid-js/web');
var router = require('@solidjs/router');
var solidJs = require('solid-js');
var store$1 = require('solid-js/store');
var _tmpl$ = /*#__PURE__*/web.template(`<div style=z-index:50;overscroll-behavior:none><div><div><div style=align-items:center;justify-content:center;max-width:100%>`),
_tmpl$2 = /*#__PURE__*/web.template(`<div style=z-index:50;overscroll-behavior:none><div><div><div>`);
const [store, setStore] = store$1.createStore({});
function useModal() {
function showModal(modal) {
setStore("modal", modal);
}
function clearModal() {
setStore("modal", undefined);
}
const location = router.useLocation();
solidJs.createEffect(() => {
location.pathname;
clearModal();
});
return {
showModal,
clearModal
};
}
function ModalRoot() {
return web.memo(() => store.modal);
}
// =====================================================
function ModalBase({
children,
onClear,
outsideClose = true,
withAnimation = true
}) {
const baseWrapperStyle = {
width: "100%",
"min-height": "100.1%",
height: "100%",
"overflow-y": "auto",
"background-attachment": "fixed",
transition: "all 200ms",
"background-color": "black"
};
const wrapperCloseStyle = {
...baseWrapperStyle,
"background-color": "rgba(0, 0, 0, 0)"
};
const wrapperOpenStyle = {
...baseWrapperStyle,
"background-color": "rgba(0, 0, 0, 0.7)"
};
const baseBackStyle = {
display: "flex",
"align-items": "center",
"justify-content": "center",
padding: "48px 16px",
width: "100%",
height: "max-content",
"min-height": "100%",
transition: "all 200ms"
};
const backCloseStyle = {
...baseBackStyle,
opacity: "0",
transform: "scale(0.9)"
};
const backOpenStyle = {
...baseBackStyle,
opacity: "1",
transform: "scale(1)"
};
const [backgroundStyle, setBackgroundStyle] = solidJs.createSignal(withAnimation ? backCloseStyle : backOpenStyle);
const [wrapperStyle, setWrapperStyle] = solidJs.createSignal(withAnimation ? wrapperCloseStyle : wrapperOpenStyle);
const [onClose, setOnClose] = solidJs.createSignal(false);
solidJs.onMount(() => {
if (withAnimation) {
setBackgroundStyle(backOpenStyle);
setWrapperStyle(wrapperOpenStyle);
}
});
const handleWrapperClick = () => {
if (outsideClose) {
setOnClose(true);
if (withAnimation) {
setBackgroundStyle(backCloseStyle);
setWrapperStyle(wrapperCloseStyle);
} else {
onClear();
}
}
};
const handleTransitionEnd = e => {
if (onClose() && e.propertyName === "opacity") {
onClear();
}
};
return (() => {
var _el$ = _tmpl$(),
_el$2 = _el$.firstChild,
_el$3 = _el$2.firstChild,
_el$4 = _el$3.firstChild;
web.setStyleProperty(_el$, "position", "fixed");
web.setStyleProperty(_el$, "top", "0");
web.setStyleProperty(_el$, "right", "0");
web.setStyleProperty(_el$, "bottom", "0");
web.setStyleProperty(_el$, "left", "0");
web.setStyleProperty(_el$, "background-attachment", "fixed");
web.setStyleProperty(_el$, "overflow-y", "auto");
web.setStyleProperty(_el$, "width", "100%");
_el$2.$$click = handleWrapperClick;
_el$3.addEventListener("transitionend", handleTransitionEnd);
_el$4.$$click = e => e.stopPropagation();
web.setStyleProperty(_el$4, "display", "flex");
web.insert(_el$4, children);
web.effect(_p$ => {
var _v$ = {
...wrapperStyle()
},
_v$2 = backgroundStyle();
_p$.e = web.style(_el$2, _v$, _p$.e);
_p$.t = web.style(_el$3, _v$2, _p$.t);
return _p$;
}, {
e: undefined,
t: undefined
});
return _el$;
})();
}
function ModalHamburger({
onClear,
children
}) {
const baseWrapperStyle = {
width: "100%",
height: "100.1%",
"overflow-y": "auto",
"background-attachment": "fixed",
transition: "all 300ms",
"background-color": "black"
};
const wrapperCloseStyle = {
...baseWrapperStyle,
"background-color": "rgba(0, 0, 0, 0)"
};
const wrapperOpenStyle = {
...baseWrapperStyle,
"background-color": "rgba(0, 0, 0, 0.7)"
};
const baseBackStyle = {
position: "absolute",
display: "flex",
"flex-direction": "column",
height: "100%",
width: "21rem",
"max-width": "75%",
transition: "all 300ms"
};
const backCloseStyle = {
...baseBackStyle,
right: "-24rem"
};
const backOpenStyle = {
...baseBackStyle,
right: "0"
};
const [backgroundStyle, setBackgroundStyle] = solidJs.createSignal(backCloseStyle);
const [wrapperStyle, setWrapperStyle] = solidJs.createSignal(wrapperCloseStyle);
const [onClose, setOnClose] = solidJs.createSignal(false);
solidJs.onMount(() => {
setBackgroundStyle(backOpenStyle);
setWrapperStyle(wrapperOpenStyle);
});
return (() => {
var _el$5 = _tmpl$2(),
_el$6 = _el$5.firstChild,
_el$7 = _el$6.firstChild,
_el$8 = _el$7.firstChild;
web.setStyleProperty(_el$5, "position", "fixed");
web.setStyleProperty(_el$5, "top", "0");
web.setStyleProperty(_el$5, "right", "0");
web.setStyleProperty(_el$5, "bottom", "0");
web.setStyleProperty(_el$5, "left", "0");
web.setStyleProperty(_el$5, "background-attachment", "fixed");
web.setStyleProperty(_el$5, "overflow-y", "auto");
_el$6.$$click = () => {
setOnClose(true);
setBackgroundStyle(backCloseStyle);
setWrapperStyle(wrapperCloseStyle);
};
_el$7.addEventListener("transitionend", e => onClose() && e.propertyName === "right" && onClear());
_el$8.$$click = e => e.stopPropagation();
web.setStyleProperty(_el$8, "display", "flex");
web.setStyleProperty(_el$8, "flex-direction", "column");
web.setStyleProperty(_el$8, "width", "100%");
web.setStyleProperty(_el$8, "height", "100%");
web.insert(_el$8, children);
web.effect(_p$ => {
var _v$3 = wrapperStyle(),
_v$4 = backgroundStyle();
_p$.e = web.style(_el$6, _v$3, _p$.e);
_p$.t = web.style(_el$7, _v$4, _p$.t);
return _p$;
}, {
e: undefined,
t: undefined
});
return _el$5;
})();
}
web.delegateEvents(["click"]);
exports.ModalBase = ModalBase;
exports.ModalHamburger = ModalHamburger;
exports.ModalRoot = ModalRoot;
exports.useModal = useModal;
//# sourceMappingURL=index.js.map