Skip to content

Commit

Permalink
feat(html): Add example of <dialog> (#2212)
Browse files Browse the repository at this point in the history
* dialog

* Update live-examples/html-examples/interactive-elements/dialog.html

---------

Co-authored-by: Queen Vinyl Da.i'gyu-Kazotetsu <[email protected]>
Co-authored-by: Brian Thomas Smith <[email protected]>
  • Loading branch information
3 people authored Aug 18, 2023
1 parent 900b899 commit e97aa1d
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
18 changes: 18 additions & 0 deletions live-examples/html-examples/interactive-elements/css/dialog.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
.card {
text-align: center;
width: 80%;
background-color: bisque;
padding: 1em;
font-weight: bold;
}

dialog::backdrop {
background-color: grey;
}

.won {
font-weight: bold;
color: deeppink;
font-size: 1.3em;
text-align: center;
}
13 changes: 13 additions & 0 deletions live-examples/html-examples/interactive-elements/dialog.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<div class="card">
<p class="question">Would you like to try your luck in a throw of the dice?</p>
<p>Everyone has a chance to win.</p>
<button id="showDialogBtn">Yes, let's give it a try</button>
</div>

<dialog id="favDialog">
<form method="dialog">
<p class="won">You have won! Congratulations!</p>
<p class="lost">I am sorry to say, but you have lost...</p>
<button id="closeBtn">Close dialog</button>
</form>
</dialog>
24 changes: 24 additions & 0 deletions live-examples/html-examples/interactive-elements/js/dialog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const showDialogBtn = document.getElementById('showDialogBtn');
const favDialog = document.getElementById('favDialog');

function rollDice() {
return Math.floor(Math.random() * 3) === 0;
}

function changeHidden(className, hidden) {
const elements = document.getElementsByClassName(className);
for (const e of elements) {
if (hidden) {
e.classList.add('hidden');
} else {
e.classList.remove('hidden');
}
}
}

showDialogBtn.addEventListener('click', () => {
const won = rollDice();
changeHidden('won', !won);
changeHidden('lost', won);
favDialog.showModal();
});
11 changes: 11 additions & 0 deletions live-examples/html-examples/interactive-elements/meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@
"type": "tabbed",
"height": "tabbed-shorter"
},
"dialog": {
"exampleCode": "./live-examples/html-examples/interactive-elements/dialog.html",
"cssExampleSrc": "./live-examples/html-examples/interactive-elements/css/dialog.css",
"jsExampleSrc": "./live-examples/html-examples/interactive-elements/js/dialog.js",
"fileName": "dialog.html",
"title": "HTML Demo: <dialog>",
"type": "tabbed",
"defaultTab": "html",
"tabs": "html,css,js",
"height": "tabbed-standard"
},
"menu": {
"exampleCode": "./live-examples/html-examples/interactive-elements/menu.html",
"cssExampleSrc": "./live-examples/html-examples/interactive-elements/css/menu.css",
Expand Down

0 comments on commit e97aa1d

Please sign in to comment.