-
Notifications
You must be signed in to change notification settings - Fork 232
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make custom elements resilient to being moved between hosts
Co-authored-by: fyorl <kim.mantas@gmail.com>
- Loading branch information
Showing
5 changed files
with
214 additions
and
182 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
54 changes: 54 additions & 0 deletions
54
module/applications/components/adopted-stylesheet-mixin.mjs
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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/** | ||
* Adds functionality to a custom HTML element for caching its stylesheet and adopting it into its Shadow DOM, rather | ||
* than having each stylesheet duplicated per element. | ||
* @param {typeof HTMLElement} Base The base class being mixed. | ||
* @returns {typeof AdoptedStyleSheetElement} | ||
*/ | ||
export default function AdoptedStyleSheetMixin(Base) { | ||
return class AdoptedStyleSheetElement extends Base { | ||
/** | ||
* A map of cached stylesheets per Document root. | ||
* @type {WeakMap<WeakKey<Document>, CSSStyleSheet>} | ||
* @protected | ||
*/ | ||
static _stylesheets = new WeakMap(); | ||
|
||
/** | ||
* The CSS content for this element. | ||
* @type {string} | ||
*/ | ||
static CSS = ""; | ||
|
||
/* -------------------------------------------- */ | ||
|
||
/** @inheritDoc */ | ||
adoptedCallback() { | ||
this._adoptStyleSheet(this._getStyleSheet()); | ||
} | ||
|
||
/* -------------------------------------------- */ | ||
|
||
/** | ||
* Retrieves the cached stylesheet, or generates a new one. | ||
* @protected | ||
*/ | ||
_getStyleSheet() { | ||
let sheet = this.constructor._stylesheets.get(this.ownerDocument); | ||
if ( !sheet ) { | ||
sheet = new this.ownerDocument.defaultView.CSSStyleSheet(); | ||
sheet.replaceSync(this.constructor.CSS); | ||
this.constructor._stylesheets.set(this.ownerDocument, sheet); | ||
} | ||
return sheet; | ||
} | ||
|
||
/* -------------------------------------------- */ | ||
|
||
/** | ||
* Adopt the stylesheet into the Shadow DOM. | ||
* @param {CSSStyleSheet} sheet The sheet to adopt. | ||
* @abstract | ||
*/ | ||
_adoptStyleSheet(sheet) {} | ||
} | ||
} |
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
Oops, something went wrong.