11/* eslint-disable */
2- import { useContext , useState } from "react" ;
2+ import { useContext , useMemo , useState , type PropsWithChildren } from "react" ;
33import { Appearance } from "react-native" ;
44
5- import type { InlineVariable , StyleDescriptor } from "../../compiler" ;
5+ import type { StyleDescriptor } from "../../compiler" ;
66import type {
77 ColorScheme ,
88 Props ,
@@ -18,6 +18,7 @@ import {
1818 VariableContext ,
1919 type Effect ,
2020 type Getter ,
21+ type VariableContextValue ,
2122} from "./reactivity" ;
2223import { resolveValue } from "./styles/resolve" ;
2324
@@ -102,7 +103,33 @@ export function useNativeVariable(name: string) {
102103 return resolveValue ( [ { } , "var" , [ name ] ] , effect . get , { inheritedVariables } ) ;
103104}
104105
106+ /**
107+ * @deprecated Use `<VariableContextProvider />` instead.
108+ */
105109export function vars ( variables : Record < string , StyleDescriptor > ) {
106- ( variables as InlineVariable ) [ VAR_SYMBOL ] = "inline" ;
107- return variables ;
110+ return Object . assign (
111+ { [ VAR_SYMBOL ] : "inline" } ,
112+ Object . fromEntries (
113+ Object . entries ( variables ) . map ( ( [ k , v ] ) => [ k . replace ( / ^ - - / , "" ) , v ] ) ,
114+ ) ,
115+ ) ;
116+ }
117+
118+ export function VariableContextProvider (
119+ props : PropsWithChildren < { value : Record < `--${string } `, StyleDescriptor > } > ,
120+ ) {
121+ const inheritedVariables = useContext ( VariableContext ) ;
122+
123+ const value : VariableContextValue = useMemo (
124+ ( ) => ( {
125+ ...inheritedVariables ,
126+ ...Object . fromEntries (
127+ Object . entries ( props . value ) . map ( ( [ k , v ] ) => [ k . replace ( / ^ - - / , "" ) , v ] ) ,
128+ ) ,
129+ [ VAR_SYMBOL ] : true ,
130+ } ) ,
131+ [ inheritedVariables , props . value ] ,
132+ ) ;
133+
134+ return < VariableContext value = { value } > { props . children } </ VariableContext > ;
108135}
0 commit comments