<my-element></my-element>
class MyElement extends HTMLElement {
constructor() {
super();
this.shadow = this.attachShadow({ mode: "open" });
}
connectedCallback() {
const div = document.createElement("div");
div.textContent = "I'm in the shadow DOM, my font-family is Bungee Tint, but you cannot see it";
const button = document.createElement("button")
button.textContent = "Attach the @font-face rule to the document"
button.addEventListener("click", () => {
document.adoptedStyleSheets = [fontFace];
div.textContent = "I'm in the shadow DOM, my font-family is Bungee Tint, now you see it!";
button.hidden = true;
})
const fontFace = new CSSStyleSheet();
fontFace.replaceSync(`@font-face {
font-family: 'Bungee Tint';
font-style: normal;
font-weight: 400;
font-display: swap;
src: url(https://fonts.gstatic.com/s/bungeetint/v3/J7abnpl_EGtUEuAJwN9WmotNOj0xpikA.woff2) format('woff2');
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+0304, U+0308, U+0329, U+2000-206F, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}`);
const fontFamily = new CSSStyleSheet();
fontFamily.replaceSync(`* {font-family: 'Bungee Tint'}`);
this.shadow.adoptedStyleSheets = [fontFace, fontFamily];
this.shadow.appendChild(div);
this.shadow.appendChild(button);
}
}
customElements.define("my-element", MyElement);
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.