react-native-unistyles 2.0.0-rc.1 → 2.0.0-rc.2
Sign up to get free protection for your applications and to get access to all the features.
- package/lib/commonjs/core/UnistyleRegistry.js +15 -25
- package/lib/commonjs/core/UnistyleRegistry.js.map +1 -1
- package/lib/commonjs/core/UnistylesModule.js +187 -2
- package/lib/commonjs/core/UnistylesModule.js.map +1 -1
- package/lib/commonjs/core/UnistylesModule.native.js +9 -0
- package/lib/commonjs/core/UnistylesModule.native.js.map +1 -0
- package/lib/commonjs/hooks/useCSS.js +36 -1
- package/lib/commonjs/hooks/useCSS.js.map +1 -1
- package/lib/commonjs/hooks/useCSS.native.js +9 -0
- package/lib/commonjs/hooks/useCSS.native.js.map +1 -0
- package/lib/commonjs/normalizer/normalizer.js +1 -2
- package/lib/commonjs/normalizer/normalizer.js.map +1 -1
- package/lib/commonjs/useStyles.js +1 -2
- package/lib/commonjs/useStyles.js.map +1 -1
- package/lib/commonjs/utils/breakpoints.js +2 -11
- package/lib/commonjs/utils/breakpoints.js.map +1 -1
- package/lib/commonjs/utils/cssMediaQuery.js +3 -9
- package/lib/commonjs/utils/cssMediaQuery.js.map +1 -1
- package/lib/commonjs/utils/mq.js +12 -36
- package/lib/commonjs/utils/mq.js.map +1 -1
- package/lib/commonjs/utils/mqParser.js +1 -2
- package/lib/commonjs/utils/mqParser.js.map +1 -1
- package/lib/commonjs/utils/styles.js +34 -39
- package/lib/commonjs/utils/styles.js.map +1 -1
- package/lib/module/core/UnistyleRegistry.js +15 -25
- package/lib/module/core/UnistyleRegistry.js.map +1 -1
- package/lib/module/core/UnistylesModule.js +186 -2
- package/lib/module/core/UnistylesModule.js.map +1 -1
- package/lib/module/core/UnistylesModule.native.js +3 -0
- package/lib/module/core/UnistylesModule.native.js.map +1 -0
- package/lib/module/hooks/useCSS.js +36 -1
- package/lib/module/hooks/useCSS.js.map +1 -1
- package/lib/module/hooks/useCSS.native.js +2 -0
- package/lib/module/hooks/useCSS.native.js.map +1 -0
- package/lib/module/normalizer/normalizer.js +1 -2
- package/lib/module/normalizer/normalizer.js.map +1 -1
- package/lib/module/useStyles.js +1 -2
- package/lib/module/useStyles.js.map +1 -1
- package/lib/module/utils/breakpoints.js +2 -11
- package/lib/module/utils/breakpoints.js.map +1 -1
- package/lib/module/utils/cssMediaQuery.js +3 -9
- package/lib/module/utils/cssMediaQuery.js.map +1 -1
- package/lib/module/utils/mq.js +12 -36
- package/lib/module/utils/mq.js.map +1 -1
- package/lib/module/utils/mqParser.js +1 -2
- package/lib/module/utils/mqParser.js.map +1 -1
- package/lib/module/utils/styles.js +34 -39
- package/lib/module/utils/styles.js.map +1 -1
- package/lib/typescript/src/core/UnistylesModule.d.ts +17 -4
- package/lib/typescript/src/core/UnistylesModule.d.ts.map +1 -1
- package/lib/typescript/src/core/UnistylesModule.native.d.ts +6 -0
- package/lib/typescript/src/core/UnistylesModule.native.d.ts.map +1 -0
- package/lib/typescript/src/hooks/useCSS.d.ts +1 -1
- package/lib/typescript/src/hooks/useCSS.d.ts.map +1 -1
- package/lib/typescript/src/hooks/useCSS.native.d.ts +3 -0
- package/lib/typescript/src/hooks/useCSS.native.d.ts.map +1 -0
- package/package.json +6 -8
- package/src/core/UnistylesModule.native.ts +7 -0
- package/src/core/UnistylesModule.ts +224 -4
- package/src/hooks/useCSS.native.ts +3 -0
- package/src/hooks/useCSS.ts +49 -1
- package/lib/commonjs/core/UnistylesModule.web.js +0 -199
- package/lib/commonjs/core/UnistylesModule.web.js.map +0 -1
- package/lib/commonjs/hooks/useCSS.web.js +0 -46
- package/lib/commonjs/hooks/useCSS.web.js.map +0 -1
- package/lib/module/core/UnistylesModule.web.js +0 -192
- package/lib/module/core/UnistylesModule.web.js.map +0 -1
- package/lib/module/hooks/useCSS.web.js +0 -39
- package/lib/module/hooks/useCSS.web.js.map +0 -1
- package/lib/typescript/src/core/UnistylesModule.web.d.ts +0 -19
- package/lib/typescript/src/core/UnistylesModule.web.d.ts.map +0 -1
- package/lib/typescript/src/hooks/useCSS.web.d.ts +0 -3
- package/lib/typescript/src/hooks/useCSS.web.d.ts.map +0 -1
- package/src/core/UnistylesModule.web.ts +0 -227
- package/src/hooks/useCSS.web.ts +0 -51
@@ -1,227 +0,0 @@
|
|
1
|
-
import { NativeEventEmitter, NativeModules } from 'react-native'
|
2
|
-
import type { UnistylesThemes, UnistylesBreakpoints } from 'react-native-unistyles'
|
3
|
-
import type { ColorSchemeName } from '../types'
|
4
|
-
import { normalizeWebStylesPlugin } from '../plugins'
|
5
|
-
import { isServer } from '../common'
|
6
|
-
|
7
|
-
export class UnistylesBridgeWeb {
|
8
|
-
#timerRef?: ReturnType<typeof setTimeout> = undefined
|
9
|
-
#hasAdaptiveThemes: boolean = false
|
10
|
-
#supportsAutomaticColorScheme = false
|
11
|
-
#screenWidth = isServer ? undefined : window.innerWidth
|
12
|
-
#screenHeight = isServer ? undefined : window.innerHeight
|
13
|
-
#themes: Array<keyof UnistylesThemes> = []
|
14
|
-
#breakpoints: UnistylesBreakpoints = {} as UnistylesBreakpoints
|
15
|
-
#colorScheme: ColorSchemeName = this.getPreferredColorScheme()
|
16
|
-
#themeName: keyof UnistylesThemes = '' as keyof UnistylesThemes
|
17
|
-
#enabledPlugins: Array<string> = [normalizeWebStylesPlugin.name]
|
18
|
-
#unistylesEvents = new NativeEventEmitter(NativeModules.Unistyles)
|
19
|
-
#sortedBreakpointPairs: Array<[keyof UnistylesBreakpoints, number]> = []
|
20
|
-
#breakpoint: keyof UnistylesBreakpoints = '' as keyof UnistylesBreakpoints
|
21
|
-
|
22
|
-
constructor() {
|
23
|
-
if (!isServer) {
|
24
|
-
this.setupListeners()
|
25
|
-
this.#screenWidth = window.innerWidth
|
26
|
-
this.#screenHeight = window.innerHeight
|
27
|
-
}
|
28
|
-
}
|
29
|
-
|
30
|
-
public install() {
|
31
|
-
// @ts-ignore
|
32
|
-
// eslint-disable-next-line no-undef
|
33
|
-
globalThis.__UNISTYLES__ = new Proxy({}, {
|
34
|
-
get: (_target, prop) => {
|
35
|
-
switch (prop) {
|
36
|
-
case 'themeName':
|
37
|
-
return this.getTheme()
|
38
|
-
case 'screenWidth':
|
39
|
-
return this.#screenWidth
|
40
|
-
case 'screenHeight':
|
41
|
-
return this.#screenHeight
|
42
|
-
case 'breakpoint':
|
43
|
-
return this.#breakpoint || undefined
|
44
|
-
case 'breakpoints':
|
45
|
-
return this.#breakpoints
|
46
|
-
case 'hasAdaptiveThemes':
|
47
|
-
return this.#hasAdaptiveThemes
|
48
|
-
case 'sortedBreakpointPairs':
|
49
|
-
return this.#sortedBreakpointPairs
|
50
|
-
case 'enabledPlugins':
|
51
|
-
return this.#enabledPlugins
|
52
|
-
case 'colorScheme':
|
53
|
-
return this.#colorScheme
|
54
|
-
case 'useTheme':
|
55
|
-
return (themeName: keyof UnistylesThemes) => this.useTheme(themeName)
|
56
|
-
case 'useBreakpoints':
|
57
|
-
return (breakpoints: UnistylesBreakpoints) => this.useBreakpoints(breakpoints)
|
58
|
-
case 'useAdaptiveThemes':
|
59
|
-
return (enable: boolean) => this.useAdaptiveThemes(enable)
|
60
|
-
case 'addPlugin':
|
61
|
-
return (pluginName: string, notify: boolean) => this.addPlugin(pluginName, notify)
|
62
|
-
case 'removePlugin':
|
63
|
-
return (pluginName: string) => this.removePlugin(pluginName)
|
64
|
-
default:
|
65
|
-
return Reflect.get(this, prop)
|
66
|
-
}
|
67
|
-
},
|
68
|
-
set: (target, prop, newValue, receiver) => {
|
69
|
-
switch (prop) {
|
70
|
-
case 'themes': {
|
71
|
-
this.#themes = newValue
|
72
|
-
this.#supportsAutomaticColorScheme = newValue.includes('light') && newValue.includes('dark')
|
73
|
-
|
74
|
-
return true
|
75
|
-
}
|
76
|
-
case 'themeName': {
|
77
|
-
this.#themeName = newValue as keyof UnistylesThemes
|
78
|
-
this.emitThemeChange()
|
79
|
-
|
80
|
-
return true
|
81
|
-
}
|
82
|
-
default:
|
83
|
-
return Reflect.set(target, prop, newValue, receiver)
|
84
|
-
}
|
85
|
-
}
|
86
|
-
})
|
87
|
-
|
88
|
-
return true
|
89
|
-
}
|
90
|
-
|
91
|
-
private useTheme(themeName: keyof UnistylesThemes) {
|
92
|
-
this.#themeName = themeName
|
93
|
-
this.emitThemeChange()
|
94
|
-
}
|
95
|
-
|
96
|
-
private useBreakpoints(breakpoints: UnistylesBreakpoints) {
|
97
|
-
this.#breakpoints = breakpoints
|
98
|
-
this.#sortedBreakpointPairs = Object
|
99
|
-
.entries(breakpoints)
|
100
|
-
.sort(([, a], [, b]) => (a ?? 0) - (b ?? 0)) as Array<[keyof UnistylesBreakpoints, number]>
|
101
|
-
|
102
|
-
if (!isServer) {
|
103
|
-
this.#breakpoint = this.getBreakpointFromScreenWidth(this.#screenWidth as number)
|
104
|
-
}
|
105
|
-
}
|
106
|
-
|
107
|
-
private useAdaptiveThemes(enable: boolean) {
|
108
|
-
this.#hasAdaptiveThemes = enable
|
109
|
-
|
110
|
-
if (!this.#hasAdaptiveThemes || !this.#supportsAutomaticColorScheme) {
|
111
|
-
return
|
112
|
-
}
|
113
|
-
|
114
|
-
if (this.#themeName !== this.#colorScheme) {
|
115
|
-
this.#themeName = this.#colorScheme as keyof UnistylesThemes
|
116
|
-
this.emitThemeChange()
|
117
|
-
}
|
118
|
-
}
|
119
|
-
|
120
|
-
private addPlugin(pluginName: string, notify: boolean) {
|
121
|
-
this.#enabledPlugins = [pluginName].concat(this.#enabledPlugins)
|
122
|
-
|
123
|
-
if (notify) {
|
124
|
-
this.emitPluginChange()
|
125
|
-
}
|
126
|
-
}
|
127
|
-
|
128
|
-
private removePlugin(pluginName: string) {
|
129
|
-
this.#enabledPlugins = this.#enabledPlugins.filter(name => name !== pluginName)
|
130
|
-
this.emitPluginChange()
|
131
|
-
}
|
132
|
-
|
133
|
-
private getTheme(): keyof UnistylesThemes {
|
134
|
-
|
135
|
-
if (this.#themes.length === 1) {
|
136
|
-
return this.#themes.at(0) as keyof UnistylesThemes
|
137
|
-
}
|
138
|
-
|
139
|
-
return this.#themeName
|
140
|
-
}
|
141
|
-
|
142
|
-
private setupListeners() {
|
143
|
-
window.addEventListener('resize', () => {
|
144
|
-
clearTimeout(this.#timerRef)
|
145
|
-
|
146
|
-
this.#timerRef = setTimeout(() => {
|
147
|
-
this.#screenWidth = window.innerWidth
|
148
|
-
this.#screenHeight = window.innerHeight
|
149
|
-
this.#breakpoint = this.getBreakpointFromScreenWidth(this.#screenWidth)
|
150
|
-
|
151
|
-
this.emitLayoutChange()
|
152
|
-
}, 100)
|
153
|
-
})
|
154
|
-
|
155
|
-
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', event => {
|
156
|
-
this.#colorScheme = event.matches
|
157
|
-
? 'dark'
|
158
|
-
: 'light'
|
159
|
-
|
160
|
-
if (!this.#supportsAutomaticColorScheme || !this.#hasAdaptiveThemes) {
|
161
|
-
return
|
162
|
-
}
|
163
|
-
|
164
|
-
if (this.#colorScheme !== this.#themeName) {
|
165
|
-
this.#themeName = this.#colorScheme as keyof UnistylesThemes
|
166
|
-
this.emitThemeChange()
|
167
|
-
}
|
168
|
-
})
|
169
|
-
}
|
170
|
-
|
171
|
-
private getBreakpointFromScreenWidth(width: number): keyof UnistylesBreakpoints {
|
172
|
-
const breakpoint = this.#sortedBreakpointPairs
|
173
|
-
.find(([, value], index, otherBreakpoints) => {
|
174
|
-
const minVal = value
|
175
|
-
const maxVal = otherBreakpoints[index + 1]?.[1]
|
176
|
-
|
177
|
-
if (!maxVal) {
|
178
|
-
return true
|
179
|
-
}
|
180
|
-
|
181
|
-
return width >= minVal && width < maxVal
|
182
|
-
})
|
183
|
-
|
184
|
-
return breakpoint?.at(0) as keyof UnistylesBreakpoints
|
185
|
-
}
|
186
|
-
|
187
|
-
private getPreferredColorScheme() {
|
188
|
-
if (!isServer && window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
|
189
|
-
return 'dark'
|
190
|
-
}
|
191
|
-
|
192
|
-
return 'light'
|
193
|
-
}
|
194
|
-
|
195
|
-
private emitPluginChange() {
|
196
|
-
this.#unistylesEvents.emit('__unistylesOnChange', {
|
197
|
-
type: 'plugin'
|
198
|
-
})
|
199
|
-
}
|
200
|
-
|
201
|
-
private emitThemeChange() {
|
202
|
-
this.#unistylesEvents.emit('__unistylesOnChange', {
|
203
|
-
type: 'theme',
|
204
|
-
payload: {
|
205
|
-
themeName: this.#themeName
|
206
|
-
}
|
207
|
-
})
|
208
|
-
}
|
209
|
-
|
210
|
-
private emitLayoutChange() {
|
211
|
-
this.#unistylesEvents.emit('__unistylesOnChange', {
|
212
|
-
type: 'layout',
|
213
|
-
payload: {
|
214
|
-
breakpoint: this.#breakpoint,
|
215
|
-
orientation: (this.#screenWidth as number) > (this.#screenHeight as number)
|
216
|
-
? 'landscape'
|
217
|
-
: 'portrait',
|
218
|
-
screen: {
|
219
|
-
width: this.#screenWidth,
|
220
|
-
height: this.#screenHeight
|
221
|
-
}
|
222
|
-
}
|
223
|
-
})
|
224
|
-
}
|
225
|
-
}
|
226
|
-
|
227
|
-
export const UnistylesModule = new UnistylesBridgeWeb()
|
package/src/hooks/useCSS.web.ts
DELETED
@@ -1,51 +0,0 @@
|
|
1
|
-
import { useInsertionEffect, useRef } from 'react'
|
2
|
-
import { unistyles } from '../core'
|
3
|
-
import type { ReactNativeStyleSheet } from '../types'
|
4
|
-
import { generateReactNativeWebId } from '../utils'
|
5
|
-
|
6
|
-
export const useCSS = <T>(stylesheet: ReactNativeStyleSheet<T>) => {
|
7
|
-
const insertedIds = useRef<Array<string>>([])
|
8
|
-
|
9
|
-
useInsertionEffect(() => {
|
10
|
-
if (!unistyles.registry.config.experimentalCSSMediaQueries) {
|
11
|
-
return
|
12
|
-
}
|
13
|
-
|
14
|
-
Object
|
15
|
-
.entries(stylesheet)
|
16
|
-
.forEach(([_key, value]) => {
|
17
|
-
Object.entries(value!)
|
18
|
-
.forEach(([prop, val]) => {
|
19
|
-
if (!val.toString().includes('@media')) {
|
20
|
-
return
|
21
|
-
}
|
22
|
-
|
23
|
-
const id = generateReactNativeWebId(prop, '""')
|
24
|
-
|
25
|
-
if (insertedIds.current.includes(id)) {
|
26
|
-
return
|
27
|
-
}
|
28
|
-
|
29
|
-
const style = document.createElement('style')
|
30
|
-
|
31
|
-
style.id = id
|
32
|
-
style.innerHTML = val
|
33
|
-
|
34
|
-
document.head.appendChild(style)
|
35
|
-
insertedIds.current = [...insertedIds.current, id]
|
36
|
-
})
|
37
|
-
})
|
38
|
-
|
39
|
-
return () => {
|
40
|
-
insertedIds.current.forEach(id => {
|
41
|
-
const style = document.getElementById(id)
|
42
|
-
|
43
|
-
if (style) {
|
44
|
-
style.remove()
|
45
|
-
}
|
46
|
-
})
|
47
|
-
|
48
|
-
insertedIds.current = []
|
49
|
-
}
|
50
|
-
}, [stylesheet])
|
51
|
-
}
|