-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Restore WIP code * Restore dialog partial * feat: add dialog (unstyled) * fix: add inert polyfill in a Chromium-compatible way * feat: add title/body support to dialog * feat: responsive sizes
- Loading branch information
Ned Zimmerman
authored
Feb 10, 2020
1 parent
d32a330
commit 1b90b88
Showing
9 changed files
with
196 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/** | ||
* Dialog Handler. | ||
*/ | ||
|
||
/** | ||
* Dialog class. | ||
*/ | ||
class Dialog { | ||
/** | ||
* Constructor. | ||
* | ||
* @param {DomNode} btn | ||
* @param {Object} options | ||
*/ | ||
constructor( btn, options ) { | ||
this.btn = btn; | ||
this.config = { | ||
...{ | ||
callback: | ||
/** | ||
* Callback for when one was not provided. | ||
*/ | ||
() => { | ||
console.error( 'No callback provided.' ); // eslint-disable-line | ||
} | ||
}, | ||
...options | ||
}; | ||
|
||
this.invokeDialog = this.invokeDialog.bind( this ); | ||
this.handleClick = this.handleClick.bind( this ); | ||
this.addEventListeners(); | ||
} | ||
|
||
/** | ||
* | ||
* | ||
*/ | ||
handleClick() { | ||
this.invokeDialog( this.config.callback ); | ||
} | ||
|
||
/** | ||
* Invoke dialog. | ||
* | ||
* @param {Function} callback | ||
*/ | ||
invokeDialog( callback ) { | ||
const elems = document.querySelectorAll( 'body > *' ); | ||
Array.prototype.forEach.call( elems, ( elem ) => { | ||
elem.setAttribute( 'inert', 'inert' ); | ||
} ); | ||
|
||
const unique = +new Date(); | ||
|
||
const dialog = document.createElement( 'div' ); | ||
dialog.setAttribute( 'role', 'dialog' ); | ||
dialog.setAttribute( 'aria-labelledby', `q-${unique}` ); | ||
dialog.setAttribute( 'aria-describedby', `q-${unique + 1}` ); | ||
let innerHtml = `<h2 id="q-${unique}">${this.config.title}</h2>`; | ||
if ( this.config.question ) { | ||
innerHtml += `<p id="q-${unique + 1}">${this.config.question}</p>`; | ||
} | ||
innerHtml += ` | ||
<div class="buttons"> | ||
<button class="button button--secondary dismiss">${this.config.dismiss}</button> | ||
<button class="button confirm">${this.config.confirm}</button> | ||
</div> | ||
`; | ||
dialog.innerHTML = innerHtml; | ||
const overlay = document.createElement( 'div' ); | ||
overlay.setAttribute( 'inert', 'inert' ); | ||
overlay.classList.add( 'overlay' ); | ||
|
||
/** | ||
* Handle close event. | ||
*/ | ||
const close = () => { | ||
Array.prototype.forEach.call( elems, elem => { | ||
if ( elem !== dialog ) { | ||
elem.removeAttribute( 'inert' ); | ||
} | ||
} ); | ||
dialog.parentNode.removeChild( dialog ); | ||
overlay.parentNode.removeChild( overlay ); | ||
trigger.focus(); | ||
}; | ||
|
||
const confirm = dialog.querySelector( '.confirm' ); | ||
const dismiss = dialog.querySelector( '.dismiss' ); | ||
const trigger = this.btn; | ||
|
||
document.body.appendChild( overlay ); | ||
document.body.appendChild( dialog ); | ||
|
||
dismiss.focus(); | ||
|
||
confirm.onclick = () => { | ||
close(); | ||
callback(); | ||
}; | ||
dismiss.onclick = () => close(); | ||
dialog.addEventListener( 'keydown', e => { | ||
if ( 27 == e.keyCode ) { | ||
e.preventDefault(); | ||
close(); | ||
} | ||
} ); | ||
} | ||
|
||
/** | ||
* Add event listeners. | ||
*/ | ||
addEventListeners() { | ||
this.btn.addEventListener( 'click', this.handleClick, false ); | ||
} | ||
} | ||
|
||
export default Dialog; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
.button--invoke-dialog + * { | ||
display: none; | ||
} | ||
|
||
[role="dialog"] { | ||
background-color: $white; | ||
border-radius: rem(3); | ||
box-shadow: 0 rem(3) rem(6) rgba(0, 0, 0, 0.16); | ||
height: auto; | ||
left: $gutter; | ||
padding: $gutter; | ||
position: fixed; | ||
top: 20vh; | ||
width: calc(100% - 2 * #{$gutter}); | ||
|
||
.buttons { | ||
margin-top: rem(16); | ||
} | ||
|
||
.buttons button { | ||
max-width: 100%; | ||
width: 100%; | ||
} | ||
|
||
.buttons button + button { | ||
margin-top: rem(16); | ||
} | ||
} | ||
|
||
.overlay { | ||
background: $black; | ||
height: 100%; | ||
left: 0; | ||
opacity: 0.7; | ||
position: fixed; | ||
top: 0; | ||
width: 100%; | ||
} | ||
|
||
@include breakpoint-up(md) { | ||
[role="dialog"] { | ||
left: calc((100% - 32rem) / 2); | ||
padding: rem(68) rem(92); | ||
width: 32rem; | ||
|
||
.buttons { | ||
margin-top: $gutter; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
module.exports = { | ||
title: 'Dialog', | ||
status: 'wip', | ||
context: { | ||
dialogTitle: 'Remove favorite', | ||
dialogContent: 'Are your sure you want to remove this resource from your favorites?', | ||
dialogConfirm: 'Yes, remove', | ||
dialogCancel: 'No, don’t remove' | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
{% if not standAlone %}<div class="spacer"></div>{% endif %} | ||
{% render '@button--borderless', {label: 'Remove favorite', id: 'invoke-dialog', icon: 'delete', iconPosition: 'start', standAlone: true}, true %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters