@@ -11,13 +11,6 @@ import {
1111} from 'solid-js' ;
1212import { isServer , useAssets } from 'solid-js/web' ;
1313
14- interface StyleRegistryContextValue {
15- insert ( id : string , sheet : string ) : void ;
16- remove ( id : string ) : void ;
17- }
18-
19- const StyleRegistryContext = createContext < StyleRegistryContextValue > ( ) ;
20-
2114const SOLID_SHEET_ATTR = 's:id' ;
2215const SOLID_SHEET_ATTR_ESCAPED = 's\\:id' ;
2316
@@ -39,12 +32,10 @@ function insert(id: string, sheet: string): void {
3932 if ( ! tracked . has ( id ) ) {
4033 tracked . add ( id ) ;
4134
42- if ( ! isServer ) {
43- const node = document . createElement ( 'style' ) ;
44- node . setAttribute ( SOLID_SHEET_ATTR , id ) ;
45- node . innerHTML = sheet ;
46- document . head . appendChild ( node ) ;
47- }
35+ const node = document . createElement ( 'style' ) ;
36+ node . setAttribute ( SOLID_SHEET_ATTR , id ) ;
37+ node . innerHTML = sheet ;
38+ document . head . appendChild ( node ) ;
4839 }
4940 references . set ( id , ( references . get ( id ) ?? 0 ) + 1 ) ;
5041}
@@ -55,64 +46,107 @@ function remove(id: string): void {
5546 references . set ( id , count - 1 ) ;
5647 } else {
5748 references . set ( id , 0 ) ;
58- if ( ! isServer ) {
59- const node = document . head . querySelector (
60- `style[${ SOLID_SHEET_ATTR_ESCAPED } ="${ id } "]` ,
61- ) ;
62- if ( node ) {
63- document . head . removeChild ( node ) ;
64- }
49+ const node = document . head . querySelector (
50+ `style[${ SOLID_SHEET_ATTR_ESCAPED } ="${ id } "]` ,
51+ ) ;
52+ if ( node ) {
53+ document . head . removeChild ( node ) ;
6554 }
6655 tracked . delete ( id ) ;
6756 }
6857}
6958
59+ interface StyleRegistryContextValue {
60+ insert ( id : string , sheet : string ) : void ;
61+ remove ( id : string ) : void ;
62+ }
63+
64+ const StyleRegistryContext = createContext < StyleRegistryContextValue > ( ) ;
65+
7066export interface StyleData {
7167 id : string ;
7268 sheet : string ;
7369}
7470
7571export interface StyleRegistryProps {
72+ auto ?: boolean ;
7673 styles ?: StyleData [ ] ;
7774 children ?: JSX . Element ;
7875}
7976
80- export function StyleRegistry ( props : StyleRegistryProps ) : JSX . Element {
77+ function noopRemove ( _id : string ) : void {
78+ // no-op
79+ }
80+
81+ function ServerStyleRegistry ( props : StyleRegistryProps ) : JSX . Element {
82+ let styles = props . styles ;
83+
84+ if ( props . auto ) {
85+ const current = styles || [ ] ;
86+ styles = current ;
87+ useAssets (
88+ ( ) =>
89+ ( {
90+ t : renderSheets ( current ) ,
91+ } ) as unknown as JSX . Element ,
92+ ) ;
93+ }
94+
8195 const sheets = new Set < string > ( ) ;
8296
83- function wrappedInsert ( id : string , sheet : string ) : void {
97+ function serverInsert ( id : string , sheet : string ) : void {
8498 if ( ! sheets . has ( id ) ) {
8599 sheets . add ( id ) ;
86- if ( isServer && props . styles ) {
87- props . styles . push ( { id, sheet } ) ;
100+
101+ if ( styles ) {
102+ styles . push ( { id, sheet } ) ;
88103 }
89104 }
90- insert ( id , sheet ) ;
91105 }
92106
93107 return createComponent ( StyleRegistryContext . Provider , {
94- value : { insert : wrappedInsert , remove } ,
108+ value : { insert : serverInsert , remove : noopRemove } ,
109+ get children ( ) {
110+ return props . children ;
111+ } ,
112+ } ) ;
113+ }
114+
115+ function ClientStyleRegistry ( props : StyleRegistryProps ) : JSX . Element {
116+ return createComponent ( StyleRegistryContext . Provider , {
117+ value : { insert, remove } ,
95118 get children ( ) {
96119 return props . children ;
97120 } ,
98121 } ) ;
99122}
100123
124+ export const StyleRegistry = isServer
125+ ? ServerStyleRegistry
126+ : ClientStyleRegistry ;
127+
101128export type SolidStyledVariables = Record < string , string > ;
102129
103- export function useSolidStyled (
104- id : string ,
105- offset : string ,
106- sheet : string ,
107- ) : void {
108- const index = `${ id } -${ offset } ` ;
130+ function serverUseSolidStyled ( id : string , offset : string , sheet : string ) : void {
131+ const ctx = useContext ( StyleRegistryContext ) ;
132+ if ( ctx ) {
133+ ctx . insert ( `${ id } -${ offset } ` , sheet ) ;
134+ }
135+ }
136+
137+ function clientUseSolidStyled ( id : string , offset : string , sheet : string ) : void {
109138 const ctx = useContext ( StyleRegistryContext ) ?? { insert, remove } ;
139+ const index = `${ id } -${ offset } ` ;
110140 ctx . insert ( index , sheet ) ;
111141 onCleanup ( ( ) => {
112142 ctx . remove ( index ) ;
113143 } ) ;
114144}
115145
146+ export const useSolidStyled = isServer
147+ ? serverUseSolidStyled
148+ : clientUseSolidStyled ;
149+
116150function serializeStyle ( source : JSX . CSSProperties ) : string {
117151 let result = '' ;
118152 for ( const key in source ) {
@@ -127,7 +161,19 @@ function serializeRootStyle(vars?: () => Record<string, string>): string {
127161 return vars ? ':root{' + serializeStyle ( untrack ( vars ) ) + '}' : '' ;
128162}
129163
130- export function useSolidStyledGlobal (
164+ function serverUseSolidStyledGlobal (
165+ id : string ,
166+ offset : string ,
167+ sheet : string ,
168+ vars ?: ( ) => Record < string , string > ,
169+ ) : void {
170+ const ctx = useContext ( StyleRegistryContext ) ;
171+ if ( ctx ) {
172+ ctx . insert ( `${ id } -${ offset } ` , serializeRootStyle ( vars ) + sheet ) ;
173+ }
174+ }
175+
176+ function clientUseSolidStyledGlobal (
131177 id : string ,
132178 offset : string ,
133179 sheet : string ,
@@ -149,6 +195,10 @@ export function useSolidStyledGlobal(
149195 } ) ;
150196}
151197
198+ export const useSolidStyledGlobal = isServer
199+ ? serverUseSolidStyledGlobal
200+ : clientUseSolidStyledGlobal ;
201+
152202type CSSVarsMerge = ( ) => Record < string , string > ;
153203
154204interface CSSVars {
@@ -215,15 +265,6 @@ export function renderSheets(sheets: StyleData[]): string {
215265 return sheet ;
216266}
217267
218- export function useSheets ( sheets : StyleData [ ] ) : void {
219- useAssets (
220- ( ) =>
221- ( {
222- t : renderSheets ( sheets ) ,
223- } ) as unknown as JSX . Element ,
224- ) ;
225- }
226-
227268export interface CSSConstructor {
228269 ( template : TemplateStringsArray , ...spans : ( string | boolean ) [ ] ) : void ;
229270}
0 commit comments