From 2749fe1ee90a18101e73fe10229266da036ff66a Mon Sep 17 00:00:00 2001 From: Jiri Zraly Date: Wed, 30 Nov 2022 16:54:40 +0100 Subject: [PATCH] modal: possibility to open more modals in one component --- components/modal/modal.js | 208 +++++++++--------- components/modal/variations/default/demo.html | 35 ++- components/modal/variations/default/html.html | 11 +- dist/cdn.min.js | 2 +- package.json | 2 +- 5 files changed, 151 insertions(+), 107 deletions(-) diff --git a/components/modal/modal.js b/components/modal/modal.js index 533c6fa..504c13a 100644 --- a/components/modal/modal.js +++ b/components/modal/modal.js @@ -1,99 +1,111 @@ export default () => ({ - open: false, - originalBodyPaddingRight: null, - originalBodyOverflow: null, - style: { - background: "fixed top-0 left-0 flex w-screen h-screen max-w-full bg-neutral-500/50 z-50 overflow-y-auto p-3", - dialog: "h-auto p-6 m-auto relative w-full max-w-md bg-white rounded-lg shadow-xl", - }, - init() { - const style = this.style; - const templateContent = this.$el.querySelector("template").content; - const backgroundElement = templateContent.querySelector("[x-bind=background]"); - const dialogElement = templateContent.querySelector("[x-bind=dialog]"); - backgroundElement.className = style.background; - dialogElement.className = style.dialog; - - this.$watch('open', value => { - if (value === true) { - this.originalBodyOverfow = window.getComputedStyle(document.body).overflow; - this.originalPaddingRight = document.body.style.paddingRight; - const originalComputedPaddingRight = window.getComputedStyle(document.body).paddingRight; - const scrollBarWidth = window.innerWidth - document.documentElement.clientWidth; - document.body.classList.add('overflow-hidden') - document.body.style.paddingRight = `${scrollBarWidth + parseFloat(originalComputedPaddingRight || 0)}px`; - - } else { - setTimeout(() => { - document.body.classList.remove('overflow-hidden') - document.body.style.paddingRight = this.originalBodyPaddingRight || null; - document.body.style.overflow = this.originalBodyOverflow; - }, 300); - } - }); - }, - trigger: { - ['@click']() { - this.open = ! this.open - }, - }, - background: { - ['x-show']() { - return this.open - }, - ['x-transition:enter']() { - return 'transition duration-300'; - }, - ['x-transition:enter-start']() { - return 'opacity-0'; - }, - ['x-transition:enter-end']() { - return 'opacity-1'; - }, - ['x-transition:leave']() { - return 'transition duration-300'; - }, - ['x-transition:leave-start']() { - return 'opacity-1'; - }, - ['x-transition:leave-end']() { - return 'opacity-0'; - } - }, - dialog: { - ['@mistral-close-modals.window']() { - return this.open = false; - }, - ['x-show']() { - return this.open - }, - ["x-trap"]() { - return this.open; - }, - ['@keydown.escape.window']() { - this.open = false; - }, - ['@click.away']() { - this.open = false; - }, - ['x-transition:enter']() { - return 'transition ease-[cubic-bezier(.46,1.56,.8,1)] duration-300'; - }, - ['x-transition:enter-start']() { - return 'opacity-0 -translate-y-12'; - }, - ['x-transition:enter-end']() { - return 'opacity-1 translate-y-0'; - }, - ['x-transition:leave']() { - return 'transition ease-out duration-300'; - }, - ['x-transition:leave-start']() { - return 'opacity-1 translate-y-0'; - }, - ['x-transition:leave-end']() { - return 'opacity-0 -translate-y-12'; - } - - } -}) \ No newline at end of file + openedModalId: null, + originalBodyPaddingRight: null, + originalBodyOverflow: null, + style: { + background: + 'fixed top-0 left-0 flex w-screen h-screen max-w-full bg-neutral-500/50 z-50 overflow-y-auto p-3', + dialog: + 'h-auto p-6 m-auto relative w-full max-w-md bg-white rounded-lg shadow-xl', + }, + init() { + const style = this.style; + const templateContent = this.$el.querySelector('template').content; + const backgroundElement = templateContent.querySelector( + '[x-bind=background]' + ); + const dialogElements = templateContent.querySelectorAll('[x-bind=dialog]'); + dialogElements.forEach(dialogElement => { + backgroundElement.className = style.background; + dialogElement.className = style.dialog; + }); + + this.$watch('open', (value) => { + if (value !== null) { + this.originalBodyOverfow = window.getComputedStyle( + document.body + ).overflow; + this.originalPaddingRight = document.body.style.paddingRight; + const originalComputedPaddingRight = window.getComputedStyle( + document.body + ).paddingRight; + const scrollBarWidth = + window.innerWidth - document.documentElement.clientWidth; + document.body.classList.add('overflow-hidden'); + document.body.style.paddingRight = `${ + scrollBarWidth + parseFloat(originalComputedPaddingRight || 0) + }px`; + } else { + setTimeout(() => { + document.body.classList.remove('overflow-hidden'); + document.body.style.paddingRight = + this.originalBodyPaddingRight || null; + document.body.style.overflow = this.originalBodyOverflow; + }, 300); + } + }); + }, + trigger: { + ['@click']() { + this.openedModalId = this.$el.dataset.mistralModalId; + }, + }, + background: { + ['x-show']() { + return this.openedModalId !== null; + }, + ['x-transition:enter']() { + return 'transition duration-300'; + }, + ['x-transition:enter-start']() { + return 'opacity-0'; + }, + ['x-transition:enter-end']() { + return 'opacity-1'; + }, + ['x-transition:leave']() { + return 'transition duration-300'; + }, + ['x-transition:leave-start']() { + return 'opacity-1'; + }, + ['x-transition:leave-end']() { + return 'opacity-0'; + }, + }, + dialog: { + ['@mistral-close-modals.window']() { + return (this.openedModalId === null); + }, + ['x-show']() { + return this.openedModalId === this.$el.id; + }, + ['x-trap']() { + return this.openedModalId === this.$el.id; + }, + ['@keydown.escape.window']() { + this.openedModalId = null; + }, + ['@click.away']() { + this.openedModalId = null; + }, + ['x-transition:enter']() { + return 'transition ease-[cubic-bezier(.46,1.56,.8,1)] duration-300'; + }, + ['x-transition:enter-start']() { + return 'opacity-0 -translate-y-12'; + }, + ['x-transition:enter-end']() { + return 'opacity-1 translate-y-0'; + }, + ['x-transition:leave']() { + return 'transition ease-out duration-300'; + }, + ['x-transition:leave-start']() { + return 'opacity-1 translate-y-0'; + }, + ['x-transition:leave-end']() { + return 'opacity-0 -translate-y-12'; + }, + }, +}); diff --git a/components/modal/variations/default/demo.html b/components/modal/variations/default/demo.html index 5fa0421..0cf8999 100644 --- a/components/modal/variations/default/demo.html +++ b/components/modal/variations/default/demo.html @@ -1,14 +1,20 @@
+
diff --git a/components/modal/variations/default/html.html b/components/modal/variations/default/html.html index 5fa16e2..c5486cb 100644 --- a/components/modal/variations/default/html.html +++ b/components/modal/variations/default/html.html @@ -1,9 +1,14 @@
- + +