Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Put components in custom elements #125

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/assets/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
shiny_component_assets/
4 changes: 4 additions & 0 deletions docs/assets/custom-styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* Hide children of shadow-container until they're moved into the shadow DOM. */
shadow-container > * {
visibility: hidden;
}
64 changes: 64 additions & 0 deletions docs/assets/shadow-container.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const cssPaths = [
// "assets/custom-styles.css",
// "assets/shiny/bootstrap/bootstrap.min.css",
];

const styleSheetPromises = {};

/**
* Loads a CSS file and returns a promise that resolves to a CSSStyleSheet. Multiple
* calls to this function with the same path will return the same promise, so this will
* not make multiple requests for the same file.
*
* @param {string} path - The path to the CSS file.
* @returns {Promise<CSSStyleSheet>} A promise that resolves to a CSSStyleSheet.
*/
function loadCSS(path) {
// function implementation goes here
}
function loadStyleSheet(cssPath) {
if (styleSheetPromises[cssPath] === undefined) {
styleSheetPromises[cssPath] = (async () => {
const response = await fetch(cssPath);
const css = await response.text();
const sheet = new CSSStyleSheet();
sheet.replaceSync(css);
return sheet;
})();
}
return styleSheetPromises[cssPath];
}

/**
* `ShadowContainer` is a custom HTML element that encapsulates its content in a Shadow
* DOM. This provides style and markup encapsulation, isolating the styling from the
* containing document and vice versa.
*
* @extends {HTMLElement}
*/
class ShadowContainer extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: "open" });
}

connectedCallback() {
this.moveChildrenToShadowDOM();
this.loadStyles();
}

moveChildrenToShadowDOM() {
// Move existing children to the shadow DOM.
while (this.childNodes.length > 0) {
this.shadowRoot.appendChild(this.firstChild);
}
}

async loadStyles() {
// Load CSS from external files.
const loadedStyleSheets = await Promise.all(cssPaths.map(loadStyleSheet));
this.shadowRoot.adoptedStyleSheets = loadedStyleSheets;
}
}

customElements.define("shadow-container", ShadowContainer);
Loading
Loading