@@ -52,7 +52,13 @@ export function createCache() {
5252
5353export type HashPriority = 'low' | 'high' ;
5454
55- export interface StyleContextProps {
55+ export type LayerComposer = ( dependency : ReadonlySet < string > ) => string ;
56+ export type LayerConfig = {
57+ /** Define the hierarchical order here */
58+ composer : LayerComposer ;
59+ } ;
60+
61+ export interface StyleContextValue {
5662 autoClear ?: boolean ;
5763 /** @private Test only. Not work in production. */
5864 mock ?: 'server' | 'client' ;
@@ -77,38 +83,63 @@ export interface StyleContextProps {
7783 * Please note that `linters` do not support dynamic update.
7884 */
7985 linters ?: Linter [ ] ;
80- /** Wrap css in a layer to avoid global style conflict */
81- layer ?: boolean ;
86+ /**
87+ * Wrap css in a layer to avoid global style conflict
88+ * @see [MDN-CSS Layer](https://developer.mozilla.org/en-US/docs/Web/CSS/@layer)
89+ */
90+ layer ?: LayerConfig ;
8291}
8392
84- const StyleContext = React . createContext < StyleContextProps > ( {
93+ export const defaultLayerComposer : LayerComposer = ( dependency ) =>
94+ Array . from ( dependency ) . join ( ) ;
95+
96+ const noop = ( ) => `` ;
97+
98+ const StyleContext = React . createContext < StyleContextValue > ( {
8599 hashPriority : 'low' ,
86100 cache : createCache ( ) ,
87101 defaultCache : true ,
88102} ) ;
89103
90- export type StyleProviderProps = Partial < StyleContextProps > & {
91- children ?: React . ReactNode ;
92- } ;
104+ export interface StyleProviderProps
105+ extends Omit < Partial < StyleContextValue > , 'layer' > {
106+ layer : boolean | LayerConfig ;
107+ }
93108
94- export const StyleProvider : React . FC < StyleProviderProps > = ( props ) => {
109+ export const StyleProvider = (
110+ props : React . PropsWithChildren < StyleProviderProps > ,
111+ ) => {
95112 const { children, ...restProps } = props ;
96113
97114 const parentContext = React . useContext ( StyleContext ) ;
98115
99- const context = useMemo < StyleContextProps > (
116+ const context = useMemo < StyleContextValue > (
100117 ( ) => {
101- const mergedContext : StyleContextProps = {
118+ const mergedContext : StyleContextValue = {
102119 ...parentContext ,
103120 } ;
104121
105- ( Object . keys ( restProps ) as ( keyof StyleContextProps ) [ ] ) . forEach ( ( key ) => {
122+ ( Object . keys ( restProps ) as ( keyof StyleContextValue ) [ ] ) . forEach ( ( key ) => {
106123 const value = restProps [ key ] ;
107124 if ( restProps [ key ] !== undefined ) {
108125 ( mergedContext as any ) [ key ] = value ;
109126 }
110127 } ) ;
111128
129+ // Standardize layer
130+ const { layer } = mergedContext ;
131+ if ( typeof layer === 'boolean' ) {
132+ mergedContext . layer = layer
133+ ? { composer : defaultLayerComposer }
134+ : { composer : noop } ;
135+ } else if ( typeof layer === 'object' ) {
136+ mergedContext . layer = {
137+ ...layer ,
138+ // Ensure composer is always a function
139+ composer : layer . composer ?? defaultLayerComposer ,
140+ } ;
141+ }
142+
112143 const { cache } = restProps ;
113144 mergedContext . cache = mergedContext . cache || createCache ( ) ;
114145 mergedContext . defaultCache = ! cache && parentContext . defaultCache ;
0 commit comments