Has anyone made a disclosure/content-warning/spoilers web component?
10 replies
@zachleat `<a href=spoiler>Spoiler</a>`
Progressively enhance that.
@zachleat maybe this helps?
@stvfrnzl *excellent* thank you! (and @scottohara too)
@zachleat
Also not in web component form but this is my take on a spoiler pattern.`visibility: hidden` keeps the content hidden from everyone until it's used.
@cwilcox808 @stvfrnzl @scottohara oh my this code would be excellent for a web component (would have the extra benefit of for-free init for any later injected content too)
@zachleat
Here's a web component version. I fumbled my way through making this custom element by cribbing from from others.Using :has() with :host didn't seem to work so I'm glad I had already included a fallback for browsers that didn't support it.
Because of the click listener, I wouldn't use it for interactive content though links should be fine.
https://codepen.io/ccwilcox/pen/ZEgKvea
@stvfrnzl @scottohara@cwilcox808 @stvfrnzl @scottohara oh my—you went much deeper than I expected! nice work! I was just expecting a super simple custom element with light dom 😅
@zachleat
Yeah, I took it as an opportunity to try making one that used Shadow DOM.Now I've made a lowest effort Light DOM version, it only replaces the querySelectorAll() and forEach() calls with creating the class and defining the element.
I also made a collection for the 'pens (including a fork of Scott's). I want to do at least one more variant, a Light DOM version that at least adds the button for you.
https://codepen.io/ccwilcox/pen/QWegKEy
@cwilcox808 @stvfrnzl @scottohara excellent work!
@zachleat
I made a fancier light DOM version. Like the Shadow DOM version, you only need to use <spoil-er>, the rest of what is needed is automatically added.I think how it adds the button is okay but I'm a little unsure about how it wraps the content in a span.