11import hash from '@emotion/hash' ;
22
33const elementId = 'rcx-styles' ;
4- let element : HTMLStyleElement ;
5- const getStyleTag = ( ) : HTMLStyleElement => {
4+ const documentStyleElementMap = new Map < Document , HTMLStyleElement > ( ) ;
5+
6+ const getStyleTag = ( document : Document ) : HTMLStyleElement => {
7+ let element = documentStyleElementMap . get ( document ) ;
8+ console . trace ( 'getStyleTag' , document , element ) ;
69 if ( ! element ) {
710 const el = document . getElementById ( elementId ) as HTMLStyleElement ;
811 if ( el ) {
9- element = el ;
10- return element ;
12+ documentStyleElementMap . set ( document , el ) ;
13+ return el ;
1114 }
1215 }
1316
1417 if ( ! element ) {
1518 element = document . createElement ( 'style' ) ;
19+
20+ documentStyleElementMap . set ( document , element ) ;
21+
1622 element . id = elementId ;
1723 element . appendChild ( document . createTextNode ( '' ) ) ;
24+
1825 if ( document . head ) {
1926 document . head . appendChild ( element ) ;
2027 }
@@ -23,10 +30,13 @@ const getStyleTag = (): HTMLStyleElement => {
2330 return element ;
2431} ;
2532
26- let styleSheet : CSSStyleSheet ;
27- const getStyleSheet = ( ) : CSSStyleSheet => {
33+ // let styleSheet: CSSStyleSheet;
34+ const documentStyleSheetMap = new Map < Document , CSSStyleSheet > ( ) ;
35+ const getStyleSheet = ( document : Document ) : CSSStyleSheet => {
36+ let styleSheet = documentStyleSheetMap . get ( document ) ;
37+
2838 if ( ! styleSheet ) {
29- const styleTag = getStyleTag ( ) ;
39+ const styleTag = getStyleTag ( document ) ;
3040 const _styleSheet =
3141 styleTag . sheet ||
3242 Array . from ( document . styleSheets ) . find (
@@ -40,24 +50,29 @@ const getStyleSheet = (): CSSStyleSheet => {
4050 styleSheet = _styleSheet ;
4151 }
4252
53+ documentStyleSheetMap . set ( document , styleSheet ) ;
54+
4355 return styleSheet ;
4456} ;
4557
46- type RuleAttacher = ( rules : string ) => ( ) => void ;
58+ type RuleAttacher = (
59+ rules : string ,
60+ options : { document : Document } ,
61+ ) => ( ) => void ;
4762
4863const discardRules : RuleAttacher = ( ) => ( ) => undefined ;
4964
50- const attachRulesIntoElement : RuleAttacher = ( rules ) => {
51- const element = getStyleTag ( ) ;
65+ const attachRulesIntoElement : RuleAttacher = ( rules , { document } ) => {
66+ const element = getStyleTag ( document ) ;
5267
5368 const textNode = document . createTextNode ( rules ) ;
5469 element . appendChild ( textNode ) ;
5570
5671 return ( ) => textNode . remove ( ) ;
5772} ;
5873
59- const attachRulesIntoStyleSheet : RuleAttacher = ( rules ) => {
60- const styleSheet = getStyleSheet ( ) ;
74+ const attachRulesIntoStyleSheet : RuleAttacher = ( rules , { document } ) => {
75+ const styleSheet = getStyleSheet ( document ) ;
6176 const index = styleSheet . insertRule (
6277 `@media all{${ rules } }` ,
6378 styleSheet . cssRules . length ,
@@ -88,11 +103,14 @@ const wrapReferenceCounting = (attacher: RuleAttacher): RuleAttacher => {
88103 window . queueMicrotask ( fn ) ;
89104 } ;
90105
91- const enhancedAttacher : RuleAttacher = ( content : string ) => {
106+ const enhancedAttacher : RuleAttacher = ( content , options ) => {
92107 const id = hash ( content ) ;
93108
94109 if ( ! refs [ id ] ) {
95- const detach = attacher ( content ) ;
110+ const detach = attacher ( content , {
111+ ...options ,
112+ document : options . document ? options . document : window . document ,
113+ } ) ;
96114 let count = 0 ;
97115
98116 const ref = ( ) : void => {
0 commit comments