react-native-unistyles 2.0.0-alpha.1 → 2.0.0-alpha.11
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +4 -4
- package/cxx/UnistylesRuntime.cpp +255 -0
- package/cxx/UnistylesRuntime.h +60 -0
- package/ios/UnistylesHelpers.h +4 -0
- package/ios/UnistylesHelpers.mm +20 -0
- package/ios/UnistylesModule.h +10 -0
- package/ios/UnistylesModule.mm +158 -0
- package/lib/commonjs/common.js +26 -0
- package/lib/commonjs/common.js.map +1 -0
- package/lib/commonjs/core/UnistyleRegistry.js +38 -0
- package/lib/commonjs/core/UnistyleRegistry.js.map +1 -0
- package/lib/commonjs/core/Unistyles.js +31 -0
- package/lib/commonjs/core/Unistyles.js.map +1 -0
- package/lib/commonjs/core/UnistylesModule.js +9 -0
- package/lib/commonjs/core/UnistylesModule.js.map +1 -0
- package/lib/commonjs/core/UnistylesRuntime.js +66 -0
- package/lib/commonjs/core/UnistylesRuntime.js.map +1 -0
- package/lib/commonjs/core/index.js +13 -0
- package/lib/commonjs/core/index.js.map +1 -0
- package/lib/commonjs/createStyleSheet.js +14 -0
- package/lib/commonjs/createStyleSheet.js.map +1 -0
- package/lib/commonjs/global.js +2 -0
- package/lib/commonjs/global.js.map +1 -0
- package/lib/commonjs/hooks/index.js +10 -3
- package/lib/commonjs/hooks/index.js.map +1 -1
- package/lib/commonjs/hooks/useInitialTheme.js +17 -0
- package/lib/commonjs/hooks/useInitialTheme.js.map +1 -0
- package/lib/commonjs/hooks/useUnistyles.js +54 -0
- package/lib/commonjs/hooks/useUnistyles.js.map +1 -0
- package/lib/commonjs/index.js +35 -6
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/types/common.js +2 -0
- package/lib/commonjs/types/{mediaQueries.js.map → common.js.map} +1 -1
- package/lib/commonjs/types/index.js +26 -0
- package/lib/commonjs/types/index.js.map +1 -1
- package/lib/commonjs/types/mq.js +6 -0
- package/lib/{module/types/mediaQueries.js.map → commonjs/types/mq.js.map} +1 -1
- package/lib/commonjs/types/normalizer.js +6 -0
- package/lib/commonjs/types/normalizer.js.map +1 -0
- package/lib/commonjs/types/unistyles.js +6 -0
- package/lib/commonjs/types/unistyles.js.map +1 -0
- package/lib/commonjs/types/variants.js +2 -0
- package/lib/commonjs/types/variants.js.map +1 -0
- package/lib/commonjs/useStyles.js +49 -0
- package/lib/commonjs/useStyles.js.map +1 -0
- package/lib/commonjs/utils/breakpoints.js +33 -80
- package/lib/commonjs/utils/breakpoints.js.map +1 -1
- package/lib/commonjs/utils/common.js +10 -3
- package/lib/commonjs/utils/common.js.map +1 -1
- package/lib/commonjs/utils/index.js +46 -17
- package/lib/commonjs/utils/index.js.map +1 -1
- package/lib/commonjs/utils/module.d.js +2 -0
- package/lib/commonjs/utils/module.d.js.map +1 -0
- package/lib/commonjs/utils/mq.js +89 -0
- package/lib/commonjs/utils/mq.js.map +1 -0
- package/lib/commonjs/utils/mqParser.js +86 -0
- package/lib/commonjs/utils/mqParser.js.map +1 -0
- package/lib/commonjs/utils/normalizeStyles.web.js +13 -13
- package/lib/commonjs/utils/normalizeStyles.web.js.map +1 -1
- package/lib/commonjs/utils/normalizer.js +89 -0
- package/lib/commonjs/utils/normalizer.js.map +1 -0
- package/lib/commonjs/utils/styles.js +29 -58
- package/lib/commonjs/utils/styles.js.map +1 -1
- package/lib/commonjs/utils/variants.js +17 -0
- package/lib/commonjs/utils/variants.js.map +1 -0
- package/lib/module/common.js +22 -0
- package/lib/module/common.js.map +1 -0
- package/lib/module/core/UnistyleRegistry.js +31 -0
- package/lib/module/core/UnistyleRegistry.js.map +1 -0
- package/lib/module/core/Unistyles.js +25 -0
- package/lib/module/core/Unistyles.js.map +1 -0
- package/lib/module/core/UnistylesModule.js +3 -0
- package/lib/module/core/UnistylesModule.js.map +1 -0
- package/lib/module/core/UnistylesRuntime.js +59 -0
- package/lib/module/core/UnistylesRuntime.js.map +1 -0
- package/lib/module/core/index.js +2 -0
- package/lib/module/core/index.js.map +1 -0
- package/lib/module/createStyleSheet.js +7 -0
- package/lib/module/createStyleSheet.js.map +1 -0
- package/lib/module/global.js +2 -0
- package/lib/module/global.js.map +1 -0
- package/lib/module/hooks/index.js +2 -1
- package/lib/module/hooks/index.js.map +1 -1
- package/lib/module/hooks/useInitialTheme.js +10 -0
- package/lib/module/hooks/useInitialTheme.js.map +1 -0
- package/lib/module/hooks/useUnistyles.js +47 -0
- package/lib/module/hooks/useUnistyles.js.map +1 -0
- package/lib/module/index.js +13 -2
- package/lib/module/index.js.map +1 -1
- package/lib/module/types/common.js +2 -0
- package/lib/module/types/common.js.map +1 -0
- package/lib/module/types/index.js +2 -1
- package/lib/module/types/index.js.map +1 -1
- package/lib/module/types/mq.js +2 -0
- package/lib/module/types/mq.js.map +1 -0
- package/lib/module/types/normalizer.js +2 -0
- package/lib/module/types/normalizer.js.map +1 -0
- package/lib/module/types/unistyles.js +2 -0
- package/lib/module/types/unistyles.js.map +1 -0
- package/lib/module/types/variants.js +2 -0
- package/lib/module/types/variants.js.map +1 -0
- package/lib/module/useStyles.js +42 -0
- package/lib/module/useStyles.js.map +1 -0
- package/lib/module/utils/breakpoints.js +34 -82
- package/lib/module/utils/breakpoints.js.map +1 -1
- package/lib/module/utils/common.js +9 -1
- package/lib/module/utils/common.js.map +1 -1
- package/lib/module/utils/index.js +5 -1
- package/lib/module/utils/index.js.map +1 -1
- package/lib/module/utils/module.d.js +2 -0
- package/lib/module/utils/module.d.js.map +1 -0
- package/lib/module/utils/mq.js +83 -0
- package/lib/module/utils/mq.js.map +1 -0
- package/lib/module/utils/mqParser.js +79 -0
- package/lib/module/utils/mqParser.js.map +1 -0
- package/lib/module/utils/normalizeStyles.web.js +13 -13
- package/lib/module/utils/normalizeStyles.web.js.map +1 -1
- package/lib/module/utils/normalizer.js +79 -0
- package/lib/module/utils/normalizer.js.map +1 -0
- package/lib/module/utils/styles.js +28 -59
- package/lib/module/utils/styles.js.map +1 -1
- package/lib/module/utils/variants.js +10 -0
- package/lib/module/utils/variants.js.map +1 -0
- package/lib/typescript/src/common.d.ts +17 -0
- package/lib/typescript/src/common.d.ts.map +1 -0
- package/lib/typescript/src/core/UnistyleRegistry.d.ts +15 -0
- package/lib/typescript/src/core/UnistyleRegistry.d.ts.map +1 -0
- package/lib/typescript/src/core/Unistyles.d.ts +13 -0
- package/lib/typescript/src/core/Unistyles.d.ts.map +1 -0
- package/lib/typescript/src/core/UnistylesModule.d.ts +6 -0
- package/lib/typescript/src/core/UnistylesModule.d.ts.map +1 -0
- package/lib/typescript/src/core/UnistylesRuntime.d.ts +24 -0
- package/lib/typescript/src/core/UnistylesRuntime.d.ts.map +1 -0
- package/lib/typescript/src/core/index.d.ts +2 -0
- package/lib/typescript/src/core/index.d.ts.map +1 -0
- package/lib/typescript/src/createStyleSheet.d.ts +3 -0
- package/lib/typescript/src/createStyleSheet.d.ts.map +1 -0
- package/lib/typescript/src/global.d.ts +7 -0
- package/lib/typescript/src/global.d.ts.map +1 -0
- package/lib/typescript/src/hooks/index.d.ts +2 -1
- package/lib/typescript/src/hooks/index.d.ts.map +1 -1
- package/lib/typescript/src/hooks/useInitialTheme.d.ts +3 -0
- package/lib/typescript/src/hooks/useInitialTheme.d.ts.map +1 -0
- package/lib/typescript/src/hooks/useUnistyles.d.ts +12 -0
- package/lib/typescript/src/hooks/useUnistyles.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +14 -2
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/lib/typescript/src/types/breakpoints.d.ts +12 -13
- package/lib/typescript/src/types/breakpoints.d.ts.map +1 -1
- package/lib/typescript/src/types/common.d.ts +3 -0
- package/lib/typescript/src/types/common.d.ts.map +1 -0
- package/lib/typescript/src/types/core.d.ts +28 -18
- package/lib/typescript/src/types/core.d.ts.map +1 -1
- package/lib/typescript/src/types/index.d.ts +7 -2
- package/lib/typescript/src/types/index.d.ts.map +1 -1
- package/lib/typescript/src/types/mq.d.ts +3 -0
- package/lib/typescript/src/types/mq.d.ts.map +1 -0
- package/lib/typescript/src/types/normalizer.d.ts +20 -0
- package/lib/typescript/src/types/normalizer.d.ts.map +1 -0
- package/lib/typescript/src/types/unistyles.d.ts +37 -0
- package/lib/typescript/src/types/unistyles.d.ts.map +1 -0
- package/lib/typescript/src/types/variants.d.ts +8 -0
- package/lib/typescript/src/types/variants.d.ts.map +1 -0
- package/lib/typescript/src/useStyles.d.ts +10 -0
- package/lib/typescript/src/useStyles.d.ts.map +1 -0
- package/lib/typescript/src/utils/breakpoints.d.ts +5 -62
- package/lib/typescript/src/utils/breakpoints.d.ts.map +1 -1
- package/lib/typescript/src/utils/common.d.ts +9 -1
- package/lib/typescript/src/utils/common.d.ts.map +1 -1
- package/lib/typescript/src/utils/index.d.ts +5 -1
- package/lib/typescript/src/utils/index.d.ts.map +1 -1
- package/lib/typescript/src/utils/mq.d.ts +21 -0
- package/lib/typescript/src/utils/mq.d.ts.map +1 -0
- package/lib/typescript/src/utils/mqParser.d.ts +3 -0
- package/lib/typescript/src/utils/mqParser.d.ts.map +1 -0
- package/lib/typescript/src/utils/normalizeStyles.web.d.ts +4 -1
- package/lib/typescript/src/utils/normalizeStyles.web.d.ts.map +1 -1
- package/lib/typescript/src/utils/normalizer.d.ts +11 -0
- package/lib/typescript/src/utils/normalizer.d.ts.map +1 -0
- package/lib/typescript/src/utils/styles.d.ts +5 -49
- package/lib/typescript/src/utils/styles.d.ts.map +1 -1
- package/lib/typescript/src/utils/variants.d.ts +3 -0
- package/lib/typescript/src/utils/variants.d.ts.map +1 -0
- package/package.json +33 -15
- package/react-native-unistyles.podspec +22 -0
- package/src/common.ts +19 -0
- package/src/core/UnistyleRegistry.ts +41 -0
- package/src/core/Unistyles.ts +34 -0
- package/src/core/UnistylesModule.ts +7 -0
- package/src/core/UnistylesRuntime.ts +73 -0
- package/src/core/index.ts +1 -0
- package/src/createStyleSheet.ts +9 -0
- package/src/global.ts +5 -0
- package/src/hooks/index.ts +2 -1
- package/src/hooks/useInitialTheme.ts +11 -0
- package/src/hooks/useUnistyles.ts +55 -0
- package/src/index.ts +30 -2
- package/src/types/breakpoints.ts +21 -20
- package/src/types/common.ts +2 -0
- package/src/types/core.ts +36 -24
- package/src/types/index.ts +7 -9
- package/src/types/mq.ts +3 -0
- package/src/types/normalizer.ts +29 -0
- package/src/types/unistyles.ts +45 -0
- package/src/types/variants.ts +11 -0
- package/src/useStyles.ts +62 -0
- package/src/utils/breakpoints.ts +46 -86
- package/src/utils/common.ts +10 -1
- package/src/utils/index.ts +5 -8
- package/src/utils/module.d.ts +3 -0
- package/src/utils/mq.ts +106 -0
- package/src/utils/mqParser.ts +99 -0
- package/src/utils/normalizeStyles.web.ts +21 -42
- package/src/utils/normalizer.ts +99 -0
- package/src/utils/styles.ts +46 -70
- package/src/utils/variants.ts +13 -0
- package/lib/commonjs/UnistylesTheme.js +0 -21
- package/lib/commonjs/UnistylesTheme.js.map +0 -1
- package/lib/commonjs/createUnistyles.js +0 -57
- package/lib/commonjs/createUnistyles.js.map +0 -1
- package/lib/commonjs/hooks/useDimensions.js +0 -10
- package/lib/commonjs/hooks/useDimensions.js.map +0 -1
- package/lib/commonjs/hooks/useDimensions.web.js +0 -31
- package/lib/commonjs/hooks/useDimensions.web.js.map +0 -1
- package/lib/commonjs/types/mediaQueries.js +0 -2
- package/lib/commonjs/utils/mediaQueries.js +0 -189
- package/lib/commonjs/utils/mediaQueries.js.map +0 -1
- package/lib/module/UnistylesTheme.js +0 -12
- package/lib/module/UnistylesTheme.js.map +0 -1
- package/lib/module/createUnistyles.js +0 -50
- package/lib/module/createUnistyles.js.map +0 -1
- package/lib/module/hooks/useDimensions.js +0 -3
- package/lib/module/hooks/useDimensions.js.map +0 -1
- package/lib/module/hooks/useDimensions.web.js +0 -24
- package/lib/module/hooks/useDimensions.web.js.map +0 -1
- package/lib/module/types/mediaQueries.js +0 -2
- package/lib/module/utils/mediaQueries.js +0 -176
- package/lib/module/utils/mediaQueries.js.map +0 -1
- package/lib/typescript/src/UnistylesTheme.d.ts +0 -9
- package/lib/typescript/src/UnistylesTheme.d.ts.map +0 -1
- package/lib/typescript/src/createUnistyles.d.ts +0 -10
- package/lib/typescript/src/createUnistyles.d.ts.map +0 -1
- package/lib/typescript/src/hooks/useDimensions.d.ts +0 -3
- package/lib/typescript/src/hooks/useDimensions.d.ts.map +0 -1
- package/lib/typescript/src/hooks/useDimensions.web.d.ts +0 -3
- package/lib/typescript/src/hooks/useDimensions.web.d.ts.map +0 -1
- package/lib/typescript/src/types/mediaQueries.d.ts +0 -8
- package/lib/typescript/src/types/mediaQueries.d.ts.map +0 -1
- package/lib/typescript/src/utils/mediaQueries.d.ts +0 -130
- package/lib/typescript/src/utils/mediaQueries.d.ts.map +0 -1
- package/src/UnistylesTheme.tsx +0 -17
- package/src/__tests__/createUnistyles.spec.tsx +0 -192
- package/src/createUnistyles.ts +0 -70
- package/src/hooks/useDimensions.ts +0 -4
- package/src/hooks/useDimensions.web.ts +0 -30
- package/src/types/mediaQueries.ts +0 -10
- package/src/utils/mediaQueries.ts +0 -201
@@ -1,192 +0,0 @@
|
|
1
|
-
import React from 'react'
|
2
|
-
import { StyleSheet } from 'react-native'
|
3
|
-
import type { ViewStyle } from 'react-native'
|
4
|
-
import { renderHook } from '@testing-library/react-hooks'
|
5
|
-
import { createUnistyles } from '../createUnistyles'
|
6
|
-
import { UnistylesTheme } from '../UnistylesTheme'
|
7
|
-
import type { CustomNamedStyles } from '../types'
|
8
|
-
|
9
|
-
jest.mock('../hooks', () => ({
|
10
|
-
useDimensions: jest.fn(() => ({
|
11
|
-
width: 500,
|
12
|
-
height: 1000
|
13
|
-
}))
|
14
|
-
}))
|
15
|
-
|
16
|
-
describe('createUnistyles', () => {
|
17
|
-
describe('createStyleSheet', () => {
|
18
|
-
it('should work exactly the same like StyleSheet.create', () => {
|
19
|
-
const breakpoints = {
|
20
|
-
xs: 0
|
21
|
-
}
|
22
|
-
const { createStyleSheet } = createUnistyles(breakpoints)
|
23
|
-
|
24
|
-
const styles = {
|
25
|
-
container: {
|
26
|
-
flex: 1,
|
27
|
-
justifyContent: 'center',
|
28
|
-
alignItems: 'center'
|
29
|
-
},
|
30
|
-
text: {
|
31
|
-
fontWeight: 'bold',
|
32
|
-
fontSize: 32
|
33
|
-
}
|
34
|
-
} as const
|
35
|
-
|
36
|
-
expect(createStyleSheet(styles)).toEqual(StyleSheet.create(styles))
|
37
|
-
})
|
38
|
-
|
39
|
-
it('should inject the theme to the createStyleSheet', () => {
|
40
|
-
const theme = {
|
41
|
-
colors: {
|
42
|
-
barbie: '#ff9ff3',
|
43
|
-
oak: '#1dd1a1',
|
44
|
-
sky: '#48dbfb',
|
45
|
-
fog: '#c8d6e5',
|
46
|
-
aloes: '#00d2d3'
|
47
|
-
}
|
48
|
-
}
|
49
|
-
const breakpoints = {
|
50
|
-
xs: 0
|
51
|
-
}
|
52
|
-
const { useStyles, createStyleSheet } = createUnistyles<typeof breakpoints, typeof theme>(breakpoints)
|
53
|
-
const stylesheet = createStyleSheet(theme => ({
|
54
|
-
container: {
|
55
|
-
backgroundColor: theme.colors.barbie
|
56
|
-
}
|
57
|
-
}))
|
58
|
-
const { result } = renderHook(() => useStyles(stylesheet), {
|
59
|
-
// @ts-ignore
|
60
|
-
wrapper: ({ children }) => (
|
61
|
-
<UnistylesTheme theme={theme}>
|
62
|
-
{children}
|
63
|
-
</UnistylesTheme>
|
64
|
-
)
|
65
|
-
})
|
66
|
-
|
67
|
-
expect(result.current.theme).toEqual(theme)
|
68
|
-
expect(result.current.styles).not.toBe(Function)
|
69
|
-
expect(result.current.styles.container.backgroundColor).toEqual(theme.colors.barbie)
|
70
|
-
})
|
71
|
-
})
|
72
|
-
|
73
|
-
describe('useStyles', () => {
|
74
|
-
it('should return empty object for optional stylesheet', () => {
|
75
|
-
const breakpoints = {
|
76
|
-
xs: 0
|
77
|
-
}
|
78
|
-
const { useStyles } = createUnistyles(breakpoints)
|
79
|
-
const { result } = renderHook(() => useStyles())
|
80
|
-
|
81
|
-
expect(result.current.styles).toEqual({})
|
82
|
-
expect(result.current.theme).toEqual({})
|
83
|
-
})
|
84
|
-
|
85
|
-
it ('should choose single value from breakpoints', () => {
|
86
|
-
const breakpoints = {
|
87
|
-
xs: 0,
|
88
|
-
sm: 200,
|
89
|
-
md: 500,
|
90
|
-
lg: 1000
|
91
|
-
}
|
92
|
-
|
93
|
-
const { useStyles, createStyleSheet } = createUnistyles(breakpoints)
|
94
|
-
const stylesheet = createStyleSheet({
|
95
|
-
container: {
|
96
|
-
flex: 1,
|
97
|
-
justifyContent: 'center',
|
98
|
-
alignItems: {
|
99
|
-
xs: 'row',
|
100
|
-
md: 'column'
|
101
|
-
}
|
102
|
-
}
|
103
|
-
})
|
104
|
-
const { result } = renderHook(() => useStyles(stylesheet))
|
105
|
-
|
106
|
-
expect(result.current.styles.container.alignItems).toEqual('column')
|
107
|
-
})
|
108
|
-
|
109
|
-
it ('should choose single value from media queries', () => {
|
110
|
-
const breakpoints = {
|
111
|
-
xs: 0
|
112
|
-
}
|
113
|
-
|
114
|
-
const { useStyles, createStyleSheet } = createUnistyles(breakpoints)
|
115
|
-
const stylesheet = createStyleSheet({
|
116
|
-
container: {
|
117
|
-
flex: 1,
|
118
|
-
justifyContent: 'center',
|
119
|
-
alignItems: {
|
120
|
-
xs: 'row',
|
121
|
-
':w[300, 490]': 'column',
|
122
|
-
':w[491]': 'row'
|
123
|
-
}
|
124
|
-
}
|
125
|
-
})
|
126
|
-
const { result } = renderHook(() => useStyles(stylesheet as CustomNamedStyles<typeof stylesheet, typeof breakpoints>))
|
127
|
-
|
128
|
-
expect(result.current.styles.container.alignItems).toEqual('row')
|
129
|
-
})
|
130
|
-
|
131
|
-
it ('should choose wrap dynamic functions in Proxy', () => {
|
132
|
-
const breakpoints = {
|
133
|
-
xs: 0
|
134
|
-
}
|
135
|
-
|
136
|
-
const { useStyles, createStyleSheet } = createUnistyles<typeof breakpoints, {}>(breakpoints)
|
137
|
-
const stylesheet = createStyleSheet({
|
138
|
-
container: (flex: number) => ({
|
139
|
-
flex,
|
140
|
-
justifyContent: 'center',
|
141
|
-
alignItems: {
|
142
|
-
xs: 'row',
|
143
|
-
':w[300, 490]': 'column',
|
144
|
-
':w[491]': 'row'
|
145
|
-
}
|
146
|
-
})
|
147
|
-
})
|
148
|
-
const { result } = renderHook(() => useStyles(stylesheet as CustomNamedStyles<typeof stylesheet, typeof breakpoints>))
|
149
|
-
|
150
|
-
expect(result.current.styles.container).toBeInstanceOf(Function)
|
151
|
-
expect((result.current.styles.container as (flex: number) => ViewStyle)(2)).toEqual({
|
152
|
-
flex: 2,
|
153
|
-
justifyContent: 'center',
|
154
|
-
alignItems: 'row'
|
155
|
-
})
|
156
|
-
})
|
157
|
-
|
158
|
-
it ('should return breakpoint even if no stylesheet has been provided', () => {
|
159
|
-
const breakpoints = {
|
160
|
-
xs: 0,
|
161
|
-
sm: 200,
|
162
|
-
md: 500,
|
163
|
-
lg: 1000
|
164
|
-
}
|
165
|
-
|
166
|
-
const { useStyles } = createUnistyles<typeof breakpoints, {}>(breakpoints)
|
167
|
-
const { result } = renderHook(() => useStyles())
|
168
|
-
|
169
|
-
expect(result.current.breakpoint).toEqual('md')
|
170
|
-
})
|
171
|
-
|
172
|
-
it ('should return breakpoint for stylesheet', () => {
|
173
|
-
const breakpoints = {
|
174
|
-
xs: 0,
|
175
|
-
sm: 200,
|
176
|
-
lg: 1000
|
177
|
-
}
|
178
|
-
|
179
|
-
const { useStyles, createStyleSheet } = createUnistyles<typeof breakpoints, {}>(breakpoints)
|
180
|
-
const stylesheet = createStyleSheet({
|
181
|
-
container: {
|
182
|
-
flex: 1,
|
183
|
-
justifyContent: 'center',
|
184
|
-
alignItems: 'center'
|
185
|
-
}
|
186
|
-
})
|
187
|
-
const { result } = renderHook(() => useStyles(stylesheet as CustomNamedStyles<typeof stylesheet, typeof breakpoints>))
|
188
|
-
|
189
|
-
expect(result.current.breakpoint).toEqual('sm')
|
190
|
-
})
|
191
|
-
})
|
192
|
-
})
|
package/src/createUnistyles.ts
DELETED
@@ -1,70 +0,0 @@
|
|
1
|
-
import { useContext, useMemo } from 'react'
|
2
|
-
import { StyleSheet } from 'react-native'
|
3
|
-
import type {
|
4
|
-
Breakpoints,
|
5
|
-
CreateStylesFactory,
|
6
|
-
CustomNamedStyles,
|
7
|
-
ExtractBreakpoints,
|
8
|
-
RemoveKeysWithPrefix,
|
9
|
-
SortedBreakpointEntries
|
10
|
-
} from './types'
|
11
|
-
import { UnistylesContext } from './UnistylesTheme'
|
12
|
-
import { useDimensions } from './hooks'
|
13
|
-
import { getBreakpointFromScreenWidth, proxifyFunction, parseStyle, sortAndValidateBreakpoints } from './utils'
|
14
|
-
|
15
|
-
export const createUnistyles = <B extends Breakpoints, T = {}>(breakpoints: B) => {
|
16
|
-
const sortedBreakpoints = sortAndValidateBreakpoints(breakpoints)
|
17
|
-
const sortedBreakpointEntries = Object
|
18
|
-
.entries(sortedBreakpoints) as SortedBreakpointEntries<B>
|
19
|
-
|
20
|
-
return {
|
21
|
-
createStyleSheet: <S extends CustomNamedStyles<S, B>, X>(styles: S | CustomNamedStyles<S, B> | X | ((theme: T) => X | CustomNamedStyles<X, B>)): S | X => {
|
22
|
-
if (typeof styles === 'function') {
|
23
|
-
return styles as X
|
24
|
-
}
|
25
|
-
|
26
|
-
return styles as S
|
27
|
-
},
|
28
|
-
useStyles: <ST extends CustomNamedStyles<ST, B>>(stylesheet?: ST | CreateStylesFactory<ST, T>) => {
|
29
|
-
const theme = useContext(UnistylesContext) as T
|
30
|
-
const screenSize = useDimensions()
|
31
|
-
const breakpoint = getBreakpointFromScreenWidth<B>(screenSize.width, sortedBreakpointEntries)
|
32
|
-
|
33
|
-
if (!stylesheet) {
|
34
|
-
return {
|
35
|
-
theme,
|
36
|
-
breakpoint,
|
37
|
-
styles: {} as ExtractBreakpoints<RemoveKeysWithPrefix<ST, B>, B>
|
38
|
-
}
|
39
|
-
}
|
40
|
-
|
41
|
-
const parsedStyles = useMemo(() => typeof stylesheet === 'function'
|
42
|
-
? stylesheet(theme)
|
43
|
-
: stylesheet, [theme, stylesheet])
|
44
|
-
|
45
|
-
const dynamicStyleSheet = useMemo(() => Object
|
46
|
-
.entries(parsedStyles)
|
47
|
-
.reduce((acc, [key, value]) => {
|
48
|
-
const style = value as CustomNamedStyles<ST, B>
|
49
|
-
|
50
|
-
if (typeof value === 'function') {
|
51
|
-
return {
|
52
|
-
...acc,
|
53
|
-
[key]: proxifyFunction<B>(value, breakpoint, screenSize, sortedBreakpointEntries)
|
54
|
-
}
|
55
|
-
}
|
56
|
-
|
57
|
-
return StyleSheet.create({
|
58
|
-
...acc,
|
59
|
-
[key]: parseStyle<ST, B>(style, breakpoint, screenSize, sortedBreakpointEntries)
|
60
|
-
})
|
61
|
-
}, {} as ST), [breakpoint, screenSize, parsedStyles])
|
62
|
-
|
63
|
-
return {
|
64
|
-
theme,
|
65
|
-
breakpoint,
|
66
|
-
styles: dynamicStyleSheet as ExtractBreakpoints<RemoveKeysWithPrefix<ST, B>, B>
|
67
|
-
}
|
68
|
-
}
|
69
|
-
}
|
70
|
-
}
|
@@ -1,30 +0,0 @@
|
|
1
|
-
import { useEffect, useRef, useState } from 'react'
|
2
|
-
import type { ScreenSize } from '../types'
|
3
|
-
|
4
|
-
export const useDimensions = (): ScreenSize => {
|
5
|
-
const timerRef = useRef<ReturnType<typeof setTimeout>>()
|
6
|
-
const [screenSize, setScreenSize] = useState<ScreenSize>({
|
7
|
-
width: window.innerWidth,
|
8
|
-
height: window.innerHeight
|
9
|
-
})
|
10
|
-
|
11
|
-
useEffect(() => {
|
12
|
-
const handleResize = () => {
|
13
|
-
clearTimeout(timerRef.current)
|
14
|
-
|
15
|
-
timerRef.current = setTimeout(() => setScreenSize({
|
16
|
-
width: window.innerWidth,
|
17
|
-
height: window.innerHeight
|
18
|
-
}), 100)
|
19
|
-
}
|
20
|
-
|
21
|
-
window.addEventListener('resize', handleResize)
|
22
|
-
|
23
|
-
return () => {
|
24
|
-
window.removeEventListener('resize', handleResize)
|
25
|
-
clearTimeout(timerRef.current)
|
26
|
-
}
|
27
|
-
}, [])
|
28
|
-
|
29
|
-
return screenSize
|
30
|
-
}
|
@@ -1,10 +0,0 @@
|
|
1
|
-
type Optional<TOptional extends string> = TOptional | ''
|
2
|
-
type Separator = ',' | ', '
|
3
|
-
// this is super weird, but number passes empty string and bigint does not
|
4
|
-
type MediaQuery = `[${Separator}${bigint}]` | `[${bigint}${Optional<`${Separator}${bigint}`>}]`
|
5
|
-
type WidthMediaQuery = `:w${MediaQuery}`
|
6
|
-
type HeightMediaQuery = `:h${MediaQuery}`
|
7
|
-
|
8
|
-
export type MediaQueries =
|
9
|
-
| `${WidthMediaQuery}${Optional<HeightMediaQuery>}`
|
10
|
-
| `${HeightMediaQuery}${Optional<WidthMediaQuery>}`
|
@@ -1,201 +0,0 @@
|
|
1
|
-
import type { ScreenSize } from '../types'
|
2
|
-
|
3
|
-
/**
|
4
|
-
* Extracts numeric values from a coded string.
|
5
|
-
*
|
6
|
-
* The function is designed to process strings that have a format like "w[100,200]" or "h[300]".
|
7
|
-
* It removes characters 'w', 'h', '[', and ']' from the input string and then extracts the numbers.
|
8
|
-
*
|
9
|
-
* @param {string} codedValue - The input string to extract values from.
|
10
|
-
* @returns {Array<number>} An array of extracted numbers. Can contain one or two numbers based on the input format.
|
11
|
-
*
|
12
|
-
* @example
|
13
|
-
* extractValues("w[100,200]") // returns [100, 200]
|
14
|
-
* extractValues("h[300]") // returns [300]
|
15
|
-
* extractValues("h[,300]") // returns [0,300]
|
16
|
-
* extractValues("h[100,]") // returns [100]
|
17
|
-
*/
|
18
|
-
export const extractValues = (codedValue: string): Array<number> => {
|
19
|
-
const [lh, rh] = codedValue
|
20
|
-
.replace(/[wh[\]]/g, '')
|
21
|
-
.split(',')
|
22
|
-
|
23
|
-
return rh
|
24
|
-
? [Number(lh), Number(rh)]
|
25
|
-
: [Number(lh)]
|
26
|
-
}
|
27
|
-
|
28
|
-
/**
|
29
|
-
* Determines if the given screen size matches the specified breakpoint query.
|
30
|
-
*
|
31
|
-
* The function checks if the screen size (width and/or height) falls within the range
|
32
|
-
* specified by the breakpoint query. The query can specify width (using 'w'), height (using 'h'),
|
33
|
-
* or both.
|
34
|
-
*
|
35
|
-
* @param {string} query - The breakpoint query string. Examples: 'w[100,200]', 'h[300]', 'w[100,200]h[300,400]'.
|
36
|
-
* @param {ScreenSize} screenSize - The screen size to check against the breakpoint query.
|
37
|
-
* @returns {boolean} True if the screen size matches the breakpoint query, false otherwise.
|
38
|
-
*
|
39
|
-
* @example
|
40
|
-
* const screenSize = { width: 150, height: 350 }
|
41
|
-
* isWithinBreakpoint('w[100,200]', screenSize) // returns true
|
42
|
-
* isWithinBreakpoint('h[400]', screenSize) // returns false
|
43
|
-
*/
|
44
|
-
export const isWithinBreakpoint = (query: string, screenSize: ScreenSize): boolean => {
|
45
|
-
if (query.includes('w') && query.includes('h')) {
|
46
|
-
return isWithinTheWidthAndHeight(query, screenSize)
|
47
|
-
}
|
48
|
-
|
49
|
-
if (query.charAt(0) === 'w') {
|
50
|
-
return isWithinTheWidth(query, screenSize.width)
|
51
|
-
}
|
52
|
-
|
53
|
-
if (query.charAt(0) === 'h') {
|
54
|
-
return isWithinTheHeight(query, screenSize.height)
|
55
|
-
}
|
56
|
-
|
57
|
-
return false
|
58
|
-
}
|
59
|
-
|
60
|
-
/**
|
61
|
-
* Determines if the given width matches the specified width range in the query.
|
62
|
-
*
|
63
|
-
* The function checks if the provided width falls within the range specified by the query.
|
64
|
-
* The query specifies a width range using a format like 'w[100,200]'. If only one value is provided,
|
65
|
-
* it's treated as a minimum width.
|
66
|
-
*
|
67
|
-
* @param {string} query - The width query string. Examples: 'w[100,200]' or 'w[100]'.
|
68
|
-
* @param {number} width - The width to check against the query.
|
69
|
-
* @returns {boolean} True if the width matches the query range, false otherwise.
|
70
|
-
*
|
71
|
-
* @example
|
72
|
-
* isWithinTheWidth('w[100,200]', 150) // returns true
|
73
|
-
* isWithinTheWidth('w[100]', 50) // returns false
|
74
|
-
* isWithinTheWidth('w[100]', 150) // returns true
|
75
|
-
*/
|
76
|
-
export const isWithinTheWidth = (query: string, width: number): boolean => {
|
77
|
-
const [minWidth, maxWidth] = extractValues(query) as [number, number | undefined]
|
78
|
-
|
79
|
-
if (maxWidth && width >= minWidth && width <= maxWidth) {
|
80
|
-
return true
|
81
|
-
}
|
82
|
-
|
83
|
-
return !maxWidth && width >= minWidth
|
84
|
-
}
|
85
|
-
|
86
|
-
/**
|
87
|
-
* Determines if the given height matches the specified height range in the query.
|
88
|
-
*
|
89
|
-
* The function checks if the provided height falls within the range specified by the query.
|
90
|
-
* The query specifies a height range using a format like 'h[100,200]'. If only one value is provided,
|
91
|
-
* it's treated as a minimum height.
|
92
|
-
*
|
93
|
-
* @param {string} query - The height query string. Examples: 'h[100,200]' or 'h[100]'.
|
94
|
-
* @param {number} height - The height to check against the query.
|
95
|
-
* @returns {boolean} True if the height matches the query range, false otherwise.
|
96
|
-
*
|
97
|
-
* @example
|
98
|
-
* isWithinTheHeight('h[100,200]', 150) // returns true
|
99
|
-
* isWithinTheHeight('h[100]', 50) // returns false
|
100
|
-
* isWithinTheHeight('h[100]', 150) // returns true
|
101
|
-
*/
|
102
|
-
export const isWithinTheHeight = (query: string, height: number): boolean => {
|
103
|
-
const [minHeight, maxHeight] = extractValues(query) as [number, number | undefined]
|
104
|
-
|
105
|
-
if (maxHeight && height >= minHeight && height <= maxHeight) {
|
106
|
-
return true
|
107
|
-
}
|
108
|
-
|
109
|
-
return !maxHeight && height >= minHeight
|
110
|
-
}
|
111
|
-
|
112
|
-
/**
|
113
|
-
* Determines if the given screen size matches both the specified width and height ranges in the query.
|
114
|
-
*
|
115
|
-
* The function checks if the provided screen size (both width and height) falls within the ranges
|
116
|
-
* specified by the query. The query can specify both width and height using a format like 'w[100,200]:h[300,400]'.
|
117
|
-
*
|
118
|
-
* @param {string} query - The combined width and height query string. Example: 'w[100,200]:h[300,400]'.
|
119
|
-
* @param {ScreenSize} screenSize - The screen size to check against the query.
|
120
|
-
* @returns {boolean} True if the screen size matches both the width and height ranges in the query, false otherwise.
|
121
|
-
*
|
122
|
-
* @example
|
123
|
-
* const screenSize = { width: 150, height: 350 }
|
124
|
-
* isWithinTheWidthAndHeight('w[100,200]:h[300,400]', screenSize) // returns true
|
125
|
-
* isWithinTheWidthAndHeight('w[100,200]:h[400,500]', screenSize) // returns false
|
126
|
-
*/
|
127
|
-
export const isWithinTheWidthAndHeight = (query: string, screenSize: ScreenSize): boolean => {
|
128
|
-
const result = query
|
129
|
-
.split(':')
|
130
|
-
.filter(Boolean)
|
131
|
-
.map(q => isWithinBreakpoint(q, screenSize))
|
132
|
-
.filter(Boolean)
|
133
|
-
|
134
|
-
return result.length === 2
|
135
|
-
}
|
136
|
-
|
137
|
-
/**
|
138
|
-
* Checks if the given query string is a valid custom media query.
|
139
|
-
*
|
140
|
-
* The valid custom media query formats include:
|
141
|
-
* - :w[200]
|
142
|
-
* - :w[0, 200]
|
143
|
-
* - :w[, 300]
|
144
|
-
* - :h[200]
|
145
|
-
* - :h[0, 500]
|
146
|
-
* - :h[,200]
|
147
|
-
* - :w[100, 300]:h[200,500]
|
148
|
-
* - :h[200,500]:w[100, 300]
|
149
|
-
*
|
150
|
-
* @param {string} query - The query string to be checked.
|
151
|
-
* @returns {boolean} Returns `true` if the query is a valid custom media query, otherwise `false`.
|
152
|
-
* @example
|
153
|
-
*
|
154
|
-
* isMediaQuery(':w[200]') // true
|
155
|
-
* isMediaQuery(':w100]') // false
|
156
|
-
*/
|
157
|
-
export const isMediaQuery = (query: string): boolean => {
|
158
|
-
const regex = /^(?:(:w\[\d*(?:,\s?\d+)?])?(:h\[\d*(?:,\s?\d+)?])?|(:h\[\d*(?:,\s?\d+)?])?(:w\[\d*(?:,\s?\d+)?])?)$/
|
159
|
-
|
160
|
-
return query.length > 0 && regex.test(query)
|
161
|
-
}
|
162
|
-
|
163
|
-
/**
|
164
|
-
* Retrieves the first matching custom media query key based on the provided screen size.
|
165
|
-
*
|
166
|
-
* The function processes an array of media queries and returns the first query that matches
|
167
|
-
* the given screen size. The media queries can be in formats like:
|
168
|
-
* - w[200]
|
169
|
-
* - w[0, 200]
|
170
|
-
* - w[, 300]
|
171
|
-
* - h[200]
|
172
|
-
* - h[0, 500]
|
173
|
-
* - h[,200]
|
174
|
-
* - w[100, 300]:h[200,500]
|
175
|
-
* - h[200,500]:w[100, 300]
|
176
|
-
*
|
177
|
-
* @param {Array<[string, string | number]>} mediaQueries - An array of tuples containing media query keys and associated values.
|
178
|
-
* @param {ScreenSize} screenSize - An object representing the screen size to be checked against the media queries.
|
179
|
-
* @returns {string | undefined} Returns the first matching media query key or `undefined` if no match is found.
|
180
|
-
* @example
|
181
|
-
*
|
182
|
-
* const queries = [[':w[200]', 'value1'], [':h[300,500]', 'value2']]
|
183
|
-
* const size = { width: 250, height: 400 }
|
184
|
-
* getKeyForCustomMediaQuery(queries, size) // ':w[200]
|
185
|
-
*/
|
186
|
-
export const getKeyForCustomMediaQuery = (mediaQueries: Array<[string, string | number | undefined]>, screenSize: ScreenSize): string | undefined => {
|
187
|
-
const [matchedQuery] = mediaQueries
|
188
|
-
.flatMap(([key]) => {
|
189
|
-
if (key.includes('w') && key.includes('h')) {
|
190
|
-
return isWithinBreakpoint(key, screenSize) ? key : undefined
|
191
|
-
}
|
192
|
-
|
193
|
-
return key
|
194
|
-
.split(':')
|
195
|
-
.filter(Boolean)
|
196
|
-
.map(query => isWithinBreakpoint(query, screenSize) ? key : undefined)
|
197
|
-
})
|
198
|
-
.filter(Boolean)
|
199
|
-
|
200
|
-
return matchedQuery
|
201
|
-
}
|