MCPcopy
hub / github.com/styletron/styletron / StyletronClient

Class StyletronClient

packages/styletron-engine-atomic/src/client/client.ts:76–233  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

74};
75
76class StyletronClient implements StandardEngine {
77 container: Element;
78 styleElements: {
79 [x: string]: HTMLStyleElement;
80 };
81 fontFaceSheet: HTMLStyleElement;
82 keyframesSheet: HTMLStyleElement;
83
84 styleCache: MultiCache<{
85 pseudo: string;
86 block: string;
87 }>;
88 keyframesCache: Cache<KeyframesObject>;
89 fontFaceCache: Cache<FontFaceObject>;
90
91 constructor(opts: optionsT = {}) {
92 this.styleElements = {};
93
94 const styleIdGenerator = new SequentialIDGenerator(opts.prefix);
95 const onNewStyle = (cache, id, value) => {
96 const {pseudo, block} = value;
97 const sheet: CSSStyleSheet = this.styleElements[cache.key].sheet as any;
98 const selector = atomicSelector(id, pseudo);
99 const rule = styleBlockToRule(selector, block);
100 try {
101 sheet.insertRule(rule, sheet.cssRules.length);
102 if (__BROWSER__ && __DEV__ && window.__STYLETRON_DEVTOOLS__) {
103 insertRuleIntoDevtools(selector, block);
104 }
105 } catch (e) {
106 if (__DEV__) {
107 // eslint-disable-next-line no-console
108 console.warn(
109 `Failed to inject CSS: "${rule}". Perhaps this has invalid or un-prefixed properties?`,
110 );
111 }
112 }
113 };
114
115 // Setup style cache
116 this.styleCache = new MultiCache(
117 styleIdGenerator,
118 (media, _cache, insertBeforeMedia) => {
119 const styleElement = document.createElement("style");
120 styleElement.media = media;
121 if (insertBeforeMedia === void 0) {
122 this.container.appendChild(styleElement);
123 } else {
124 const insertBeforeIndex = findSheetIndexWithMedia(
125 this.container.children,
126 insertBeforeMedia,
127 );
128 this.container.insertBefore(
129 styleElement,
130 this.container.children[insertBeforeIndex],
131 );
132 }
133

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected