Skip to content

Instantly share code, notes, and snippets.

@jaredcwhite
Last active April 28, 2023 06:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaredcwhite/0979a2c09763da7a8fb01910806fd67b to your computer and use it in GitHub Desktop.
Save jaredcwhite/0979a2c09763da7a8fb01910806fd67b to your computer and use it in GitHub Desktop.
customElements.define("paywalled-content", class extends HTMLElement {
static observedAttributes = ["open"]
attributeChangedCallback(name, oldValue, newValue) {
if (name === "open" && newValue !== null) {
this.shadowRoot.querySelector("slot").removeAttribute("name")
} else {
this.shadowRoot.querySelector("slot").name = "public"
}
}
})
<p>Intro content.</p>
<paywalled-content>
<template shadowrootmode="open">
<slot name="public"></slot>
</template>
<aside slot="public">
<p>And that's all! Sign up for the full thing.</p>
</aside>
<!-- a server fetch can just append paywalled content here and toggle the element attribute `open` -->
</paywalled-content>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment