-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7a17585
commit cd0a888
Showing
14 changed files
with
81 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,34 @@ | ||
window.cachedStylesheets = window.cachedStylesheets || new Map(); | ||
|
||
window.initializeShadowDOMWithMultipleStylesheets = (element, stylesheetPaths) => { | ||
if (element && Array.isArray(stylesheetPaths)) { | ||
const shadowRoot = element.attachShadow({ mode: 'open' }); | ||
if (!element || typeof element.attachShadow !== 'function') { | ||
return; | ||
} | ||
|
||
if (!Array.isArray(stylesheetPaths)) { | ||
return; | ||
} | ||
|
||
stylesheetPaths.forEach((stylesheetPath) => { | ||
if (cachedStylesheets.has(stylesheetPath)) { | ||
// Reuse cached style | ||
shadowRoot.appendChild(cachedStylesheets.get(stylesheetPath).cloneNode(true)); | ||
} else { | ||
// Load new stylesheet | ||
const link = document.createElement('link'); | ||
link.rel = 'stylesheet'; | ||
link.href = stylesheetPath; | ||
const shadowRoot = element.attachShadow({ mode: 'open' }); | ||
|
||
// Cache the stylesheet | ||
cachedStylesheets.set(stylesheetPath, link); | ||
shadowRoot.appendChild(link); | ||
} | ||
}); | ||
stylesheetPaths.forEach((stylesheetPath) => { | ||
if (cachedStylesheets.has(stylesheetPath)) { | ||
// Reuse cached style | ||
shadowRoot.appendChild(cachedStylesheets.get(stylesheetPath).cloneNode(true)); | ||
} else { | ||
// Load new stylesheet | ||
const link = document.createElement('link'); | ||
link.rel = 'stylesheet'; | ||
link.href = stylesheetPath; | ||
|
||
// Move the inner content of the component to the shadow root | ||
while (element.firstChild) { | ||
shadowRoot.appendChild(element.firstChild); | ||
// Cache the stylesheet | ||
cachedStylesheets.set(stylesheetPath, link); | ||
shadowRoot.appendChild(link); | ||
} | ||
}); | ||
|
||
// Move the inner content of the component to the shadow root | ||
while (element.firstChild) { | ||
shadowRoot.appendChild(element.firstChild); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters