Setup
User interaction may lead to one of several components being asynchronously injected into a document.
Each of these components has an associated Constructable Stylesheet.
But - and this is the key point - any of the user interactions may be repeated and the markup of each component injected into the document any number of times.
Regardless of how many times the markup is injected, it stands to reason that the associated constructable stylesheet need only be constructed and adopted by the document once.
After that first time it will continue to determine the presentation of the associated markup, no matter how many times the latter is injected into the document.
Question
I would like to understand how I might go about uniquely identifying each constructable stylesheet (via e.g. a unique title
property), such that once it has been adopted by the document the first time I can check against the existing document.adoptedStyleSheets
and ensure that the same stylesheet will not be constructed and adopted again.
After I run:
const componentAStylesheet = new CSSStyleSheet();
then, I have tried to...
Write to an existing property
:
componentAStylesheet.title == 'Component A'; // no effect
Add a new property
:
componentAStylesheet.componentTitle == 'Component A'; // no effect
Edit the constructor prototype
:
CSSStyleSheet.prototype.title = 'Component A'; // Error: setting getter-only property "title"
Add a new property to the constructor prototype
:
CSSStyleSheet.prototype.componentTitle = 'Component A'; // no effect
Question: Is the CSSStyleSheet
object frozen in some way (or read-only?) and this is why I can't add to or edit its properties?
Attempt at an alternative solution
It occurred to me that instead of applying a unique label to the myComponentAStylesheet
object, I might create a parent object:
const myComponents = {}
and, after running:
const componentAStylesheet = new CSSStyleSheet();
I could manually add the following entry:
myComponents['componentA'] = componentAStylesheet;
That would still give the browser a way to discern whether a given component-associated stylesheet had already been constructed and adopted by the current document.
But is there really no way to label the constructed stylesheet object, itself?