33
loading...
This website collects cookies to deliver better user experience
<details role='dialog'>
<summary>click me to open a dialog</summary>
<div>I'm in a dialog. click outside to close me.</div>
</details>
:root {
--border-color: #ccc;
--spacing: 1rem;
--primary: #fff;
}
details[role='dialog'] {
display: inline-block;
summary {
cursor: pointer;
border: 1px solid var(--border-color);
padding: 10px;
}
summary + div {
position: fixed;
top: 0;
left: 50%;
transform: translateX(-50%);
margin: calc(var(--spacing) * 2) auto;
width: 500px;
background-color: var(--primary);
z-index: 2;
padding: var(--spacing);
}
}
click outside to close me
. heck but where is the overlay?? 😕&[open] summary {
&::before {
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
z-index: 1;
display: block;
cursor: default;
content: ' ';
background-color: rgba(0, 0, 0, 0.5);
}
}
background-color
of summary
tag is transparent but here in order to give user a sense of overlay we slapped color to background. :root {
--border-color: #ccc;
--spacing: 1rem;
--primary: #fff;
}
details[role='dialog'] {
display: inline-block;
summary {
cursor: pointer;
border: 1px solid var(--border-color);
padding: 10px;
}
&[open] summary {
&::before {
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
z-index: 1;
display: block;
cursor: default;
content: ' ';
background-color: rgba(0, 0, 0, 0.5);
}
}
summary + div {
position: fixed;
top: 0;
left: 50%;
transform: translateX(-50%);
margin: calc(var(--spacing) * 2) auto;
width: 500px;
background-color: var(--primary);
z-index: 2;
padding: var(--spacing);
}
}
Details
tag has ontoggle
event which helps us in such scenerios.detailsTag.addEventListener('toggle', () => {
if(detailsTag.open) {
// do operation when dialog opens
} else {
// do operation when dialog closed
}
})