Skip to content

Commit

Permalink
Add option to close modal when is clicked outsite
Browse files Browse the repository at this point in the history
  • Loading branch information
vinimarcili committed Nov 12, 2021
1 parent 7c10684 commit 95afe71
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 44 deletions.
17 changes: 17 additions & 0 deletions dist/js/squid.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,25 @@ function createModal (event, trigger) {
event.preventDefault()
const modal = document.getElementById(trigger.dataset.modal)
const backdrop = document.createElement('div')
const dataBackdrop = modal.getAttribute('data-backdrop')
backdrop.setAttribute('id', 'modal-backdrop')
backdrop.setAttribute('class', 'modal-backdrop show')
if (dataBackdrop !== 'static') {
modal.addEventListener('click', (event) => {
event.preventDefault()
const modalDialog = modal.firstElementChild
if (!modalDialog?.contains(event.target)) {
if (backdrop.parentNode) {
backdrop.parentNode.removeChild(backdrop)
}
modal.classList.remove('open')
setTimeout(() => {
modal.style.display = 'none'
body.classList.remove('block')
}, 500)
}
})
}
document.body.appendChild(backdrop)
const body = document.getElementsByTagName('body')[0]
body.classList.add('block')
Expand Down
2 changes: 1 addition & 1 deletion dist/js/squid.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/js/squid.min.js.map

Large diffs are not rendered by default.

42 changes: 42 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2539,6 +2539,48 @@ <h5 class='m-0'>Modal Title</h5>
</div>
</div>
</div>
<div class='col-24 col-md-6'>
<h6 class='text-medium'>
Example with backdrop exit
</h6>
<button
type='button'
class='button button-pink mb-3'
data-modal="modal-backdrop"
data-modal-backdrop-exit="true"
>
Open
</button>
<div class="modal align-items-center" id="modal-backdrop" data-backdrop="none">
<div class="modal-dialog modal-md">
<div class='modal-content'>
<div class='modal-header'>
<h5 class='m-0'>Modal Title</h5>
<button class="button-close modal-exit">
<i class="far fa-times"></i>
</button>
</div>
<div class='modal-body'>
<p>
Content Modal
</p>
</div>
<div class='modal-footer'>
<button
class='button button-white black mr-3 modal-exit'
>
Close
</button>
<button
class='button button-pink'
>
Button
</button>
</div>
</div>
</div>
</div>
</div>
</div>
<div class='row mb-3'>
<div class='col-24 col-md-6'>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@squidit/css",
"version": "1.2.52",
"version": "1.2.53",
"scripts": {
"start": "gulp watch",
"css": "sass src/scss/squid.scss:dist/css/squid.min.css --style compressed",
Expand Down
17 changes: 17 additions & 0 deletions src/docs/public/js/squid.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,25 @@ function createModal (event, trigger) {
event.preventDefault()
const modal = document.getElementById(trigger.dataset.modal)
const backdrop = document.createElement('div')
const dataBackdrop = modal.getAttribute('data-backdrop')
backdrop.setAttribute('id', 'modal-backdrop')
backdrop.setAttribute('class', 'modal-backdrop show')
if (dataBackdrop !== 'static') {
modal.addEventListener('click', (event) => {
event.preventDefault()
const modalDialog = modal.firstElementChild
if (!modalDialog?.contains(event.target)) {
if (backdrop.parentNode) {
backdrop.parentNode.removeChild(backdrop)
}
modal.classList.remove('open')
setTimeout(() => {
modal.style.display = 'none'
body.classList.remove('block')
}, 500)
}
})
}
document.body.appendChild(backdrop)
const body = document.getElementsByTagName('body')[0]
body.classList.add('block')
Expand Down
77 changes: 36 additions & 41 deletions src/docs/src/components/modal.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,90 +8,85 @@ route: /components/modal

JavaScript modal plugin to add dialogs to your site for lightboxes, user notifications, or completely custom content.

<div class='box black'>
<strong>Attention</strong>: the use of the components needs the <span class='pink'>squid.js</span> included in the project
<div class="box black">
<strong>Attention</strong>: the use of the components needs the{" "}
<span class="pink">squid.js</span> included in the project
</div>

<div class='box black'>
<strong>Attention + 1</strong>: Probably the interaction via Javascript should not work well in SPA applications (independent of the framework React, Angular, Vuejs, etc). For now we have no implementation but we will create in the future, for now just use the styles and create your own implementation
<div class="box black">
<strong>Attention + 1</strong>: Probably the interaction via Javascript should
not work well in SPA applications (independent of the framework React,
Angular, Vuejs, etc). For now we have no implementation but we will create in
the future, for now just use the styles and create your own implementation
</div>

<button
type='button'
class='button button-pink mb-3'
type="button"
class="button button-pink mb-3"
data-modal="modal-exemple"
>
Open
</button>
<div class="modal align-items-center" id="modal-exemple" data-backdrop="static">
<div class="modal-dialog modal-md">
<div class='modal-content'>
<div class='modal-header'>
<h5 class='m-0'>Modal Title</h5>
<div class="modal-content">
<div class="modal-header">
<h5 class="m-0">Modal Title</h5>
<button class="button-close modal-exit">
<i class="far fa-times"></i>
</button>
</div>
<div class='modal-body'>
<p>
Content Modal
</p>
<div class="modal-body">
<p>Content Modal</p>
</div>
<div class='modal-footer'>
<button
class='button button-white black mr-3 modal-exit'
>
Close
</button>
<button
class='button button-pink'
>
Button
</button>
<div class="modal-footer">
<button class="button button-white black mr-3 modal-exit">Close</button>
<button class="button button-pink">Button</button>
</div>
</div>
</div>
</div>

```html
<button
type='button'
data-modal="modal-id"
>
<button type="button" data-modal="modal-id">
<!-- Button content, data-modal must reference the `id` attribute of the modal -->
</button>
<div
class="modal"
id="modal-id"
data-backdrop="static"
>
<div class="modal" id="modal-id" data-backdrop="static">
<div class="modal-dialog">
<div class='modal-content'>
<div class='modal-header'>
<div class="modal-content">
<div class="modal-header">
<!-- Modal Title -->
<button class="button-close modal-exit">
<i class="far fa-times"></i>
</button>
</div>
<div class='modal-body'>
<div class="modal-body">
<!-- Modal Content -->
</div>
<div class='modal-footer'>
<div class="modal-footer">
<!-- Modal Footer -->
</div>
</div>
</div>
</div>
```

## Modal backdrop Close

To close modal when the backdrop is clicked, just remove the `data-backdrop="static"` attribute to the modal.

```html
<div class="modal" id="modal-id">
<!-- Content -->
</div>
```

## Modal Sizes

Just add one of the classes below to `.modal-dialog`.
Just add one of the classes below to `.modal-dialog`.
If no size class is passed, the modal will be the full size of the screen.
Where value is one of:

```css
.modal-sm,
.modal-md,
.modal-lg
```
```
17 changes: 17 additions & 0 deletions src/js/components/modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,25 @@ function createModal (event, trigger) {
event.preventDefault()
const modal = document.getElementById(trigger.dataset.modal)
const backdrop = document.createElement('div')
const dataBackdrop = modal.getAttribute('data-backdrop')
backdrop.setAttribute('id', 'modal-backdrop')
backdrop.setAttribute('class', 'modal-backdrop show')
if (dataBackdrop !== 'static') {
modal.addEventListener('click', (event) => {
event.preventDefault()
const modalDialog = modal.firstElementChild
if (!modalDialog?.contains(event.target)) {
if (backdrop.parentNode) {
backdrop.parentNode.removeChild(backdrop)
}
modal.classList.remove('open')
setTimeout(() => {
modal.style.display = 'none'
body.classList.remove('block')
}, 500)
}
})
}
document.body.appendChild(backdrop)
const body = document.getElementsByTagName('body')[0]
body.classList.add('block')
Expand Down

0 comments on commit 95afe71

Please sign in to comment.