Zach created a very handy Web Component that augments and wraps around a <details>
element. He’s named it <details-utils>
.
This web component adds five new responsive-friendly enhancements to one or more
<details>
elements nestled inside:
- Force open/closed
- Click outside to close
- Close on esc
- Animate open/closed
- Toggle root element class
Using attributes you can control the behavior. Additionally, you can pass in media queries into (most of) the attribute values to make them respond to viewport sizes. For example, to have a <details>
be automatically opened below a certain viewport size, you can use this:
<details-utils force-open="(max-width: 767px)">
<details>
<summary>Force open at small viewport</summary>
<div class="demo-content">
<p>This is the content.</p>
</div>
</details>
</details-utils>
Handy!