Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/uniwind/src/core/config/config.common.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Appearance, Insets, Platform } from 'react-native'
import { GetCSSVariable, getCSSVariable } from '../../hooks/useCSSVariable/useCSSVariable'
import { ColorScheme, StyleDependency } from '../../types'
import { UniwindListener } from '../listener'
import { CSSVariables, GenerateStyleSheetsCallback, ThemeName } from '../types'
Expand Down Expand Up @@ -106,6 +107,10 @@ export class UniwindConfigBuilder {
// noop
}

getCSSVariable = ((variableName: string | Array<string>) => {
return getCSSVariable(variableName, { scopedTheme: null })
}) as GetCSSVariable

protected __reinit(_: GenerateStyleSheetsCallback, themes: Array<string>) {
this._themes = themes
}
Expand Down
2 changes: 1 addition & 1 deletion packages/uniwind/src/hooks/useCSSVariable/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './useCSSVariable'
export { useCSSVariable } from './useCSSVariable'
49 changes: 26 additions & 23 deletions packages/uniwind/src/hooks/useCSSVariable/useCSSVariable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ import { UniwindContextType } from '../../core/types'
import { StyleDependency } from '../../types'
import { getVariableValue } from './getVariableValue'

const getValue = (name: string | Array<string>, uniwindContext: UniwindContextType) =>
Array.isArray(name)
? name.map(name => getVariableValue(name, uniwindContext))
: getVariableValue(name, uniwindContext)

let warned = false

const logDevError = (name: string) => {
Expand All @@ -21,10 +16,30 @@ const logDevError = (name: string) => {
)
}

export const getCSSVariable = (name: string | Array<string>, uniwindContext: UniwindContextType) => {
const value = Array.isArray(name)
? name.map(name => getVariableValue(name, uniwindContext))
: getVariableValue(name, uniwindContext)

if (Array.isArray(value)) {
value.forEach((val, index) => {
if (val === undefined && __DEV__ && !warned) {
logDevError(name[index] ?? '')
}
})
}

if (value === undefined && __DEV__ && !warned) {
logDevError(name as string)
}

return value
}

type IsGenericNumber<T> = T & 0 extends -1 ? false : true
type CreateArray<N extends number, Value, TAcc extends Array<Value> = []> = TAcc['length'] extends N ? TAcc : CreateArray<N, Value, [...TAcc, Value]>

type UseCSSVariable = {
export type GetCSSVariable = {
(name: string): string | number | undefined
<const T extends Array<string>>(
names: T,
Expand All @@ -36,9 +51,9 @@ type UseCSSVariable = {
* @param name Name / Array of names of the CSS variable.
* @returns Value / Values of the CSS variable. On web it is always a string (1rem, #ff0000, etc.), but on native it can be a string or a number (16px, #ff0000)
*/
export const useCSSVariable: UseCSSVariable = (name: string | Array<string>) => {
export const useCSSVariable: GetCSSVariable = (name: string | Array<string>) => {
const uniwindContext = useUniwindContext()
const [value, setValue] = useState(getValue(name, uniwindContext))
const [value, setValue] = useState(getCSSVariable(name, uniwindContext))
const nameRef = useRef(name)

useLayoutEffect(() => {
Expand All @@ -47,20 +62,20 @@ export const useCSSVariable: UseCSSVariable = (name: string | Array<string>) =>
return
}

setValue(getValue(name, uniwindContext))
setValue(getCSSVariable(name, uniwindContext))
nameRef.current = name

return
}

if (name !== nameRef.current) {
setValue(getValue(name, uniwindContext))
setValue(getCSSVariable(name, uniwindContext))
nameRef.current = name
}
}, [name])

useLayoutEffect(() => {
const updateValue = () => setValue(getValue(nameRef.current, uniwindContext))
const updateValue = () => setValue(getCSSVariable(nameRef.current, uniwindContext))
const dispose = UniwindListener.subscribe(
updateValue,
[StyleDependency.Theme, StyleDependency.Variables],
Expand All @@ -69,17 +84,5 @@ export const useCSSVariable: UseCSSVariable = (name: string | Array<string>) =>
return dispose
}, [uniwindContext])

if (Array.isArray(value)) {
value.forEach((val, index) => {
if (val === undefined && __DEV__ && !warned) {
logDevError(name[index] ?? '')
}
})
}

if (value === undefined && __DEV__ && !warned) {
logDevError(name as string)
}

return value as never
}
29 changes: 29 additions & 0 deletions packages/uniwind/tests/type-test/getCSSVariable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Uniwind, useCSSVariable } from 'uniwind'
import type { Equal, Expect } from './checks'

type CSSVariable = string | number | undefined

const singleCSSVariableHook = useCSSVariable('--color-red-500')

type SingleCSSVariableHookTest = Expect<Equal<CSSVariable, typeof singleCSSVariableHook>>

const doubleCSSVariableHook = useCSSVariable(['--color-red-500', '--color-red-500'])

type DoubleCSSVariableHookTest = Expect<Equal<[CSSVariable, CSSVariable], typeof doubleCSSVariableHook>>

const variablesProp = ['--color-red-500', '--color-red-500']
const multipleCSSVariableHook = useCSSVariable(variablesProp)

type MultipleCSSVariableHookTest = Expect<Equal<Array<CSSVariable>, typeof multipleCSSVariableHook>>

const singleCSSVariable = Uniwind.getCSSVariable('--color-red-500')

type SingleCSSVariableTest = Expect<Equal<CSSVariable, typeof singleCSSVariable>>

const doubleCSSVariable = Uniwind.getCSSVariable(['--color-red-500', '--color-red-500'])

type DoubleCSSVariableTest = Expect<Equal<[CSSVariable, CSSVariable], typeof doubleCSSVariable>>

const multipleCSSVariable = Uniwind.getCSSVariable(variablesProp)

type MultipleCSSVariableTest = Expect<Equal<Array<CSSVariable>, typeof multipleCSSVariable>>