Skip to content

Commit fda0aea

Browse files
committed
feat: support light-dark css function
1 parent b264c89 commit fda0aea

File tree

6 files changed

+18
-2
lines changed

6 files changed

+18
-2
lines changed

packages/uniwind/src/core/config/themeChange.native.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ import { UniwindRuntime } from '../native/runtime'
44

55
export const themeChange = (theme: string) => {
66
UniwindRuntime.currentThemeName = theme
7+
UniwindStore.reload()
78
UniwindStore.notifyListeners([StyleDependency.Theme])
89
}

packages/uniwind/src/core/native/native-utils.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { formatHex, formatHex8, interpolate, parse } from 'culori'
2+
import type { UniwindRuntime } from '../types'
23

34
export const colorMix = (color: string, mixColor: string, weight: number) => {
45
// Change alpha
@@ -17,3 +18,11 @@ export const colorMix = (color: string, mixColor: string, weight: number) => {
1718

1819
return formatHex(interpolate([mixColor, color])(weight))
1920
}
21+
22+
export function lightDark(this: UniwindRuntime, light: string, dark: string) {
23+
if (this.currentThemeName === 'dark') {
24+
return dark
25+
}
26+
27+
return light
28+
}

packages/uniwind/src/core/native/runtime.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Appearance, Dimensions, I18nManager, PixelRatio } from 'react-native'
22
import { ColorScheme, Orientation } from '../../types'
33
import type { UniwindRuntime as UniwindRuntimeType } from '../types'
4-
import { colorMix } from './native-utils'
4+
import { colorMix, lightDark } from './native-utils'
55

66
const window = Dimensions.get('window')
77
const initialColorScheme = Appearance.getColorScheme() ?? ColorScheme.Light
@@ -24,4 +24,7 @@ export const UniwindRuntime = {
2424
},
2525
colorMix,
2626
cubicBezier: () => '',
27+
lightDark: () => '',
2728
} as UniwindRuntimeType
29+
30+
UniwindRuntime.lightDark = lightDark.bind(UniwindRuntime)

packages/uniwind/src/core/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export type UniwindRuntime = {
4343
}
4444
colorMix: (color: string, mixColor: string, weight: number) => string
4545
cubicBezier: (x1: number, y1: number, x2: number, y2: number) => string
46+
lightDark: (light: string, dark: string) => string
4647
}
4748

4849
export type RNStyle = ViewStyle & TextStyle & ImageStyle & {

packages/uniwind/src/metro/processor/css.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,8 @@ export class CSS {
224224
return '('
225225
case 'close-parenthesis':
226226
return ')'
227+
case 'light-dark':
228+
return `rt.lightDark( ${this.processValue(declarationValue.light)}, ${this.processValue(declarationValue.dark)} )`
227229
case 'weight':
228230
case 'horizontal':
229231
case 'vertical':

packages/uniwind/src/metro/stylesheet/addMetaToStylesTemplate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export const addMetaToStylesTemplate = (Processor: ProcessorBuilder, currentPlat
5151

5252
const stringifiedEntries = JSON.stringify(filteredEntries)
5353

54-
if (theme !== null || stringifiedEntries.includes('--color')) {
54+
if (theme !== null || stringifiedEntries.includes('--color') || stringifiedEntries.includes('rt.lightDark')) {
5555
dependencies.push(StyleDependency.Theme)
5656
}
5757

0 commit comments

Comments
 (0)