react-native-unistyles 1.2.0 → 2.0.0-alpha.10
Sign up to get free protection for your applications and to get access to all the features.
- 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 +11 -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/unistyles.js +6 -0
- package/lib/commonjs/types/unistyles.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 +44 -82
- package/lib/commonjs/utils/breakpoints.js.map +1 -1
- package/lib/commonjs/utils/common.js +6 -1
- package/lib/commonjs/utils/common.js.map +1 -1
- package/lib/commonjs/utils/index.js +25 -30
- package/lib/commonjs/utils/index.js.map +1 -1
- 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/styles.js +6 -53
- package/lib/commonjs/utils/styles.js.map +1 -1
- package/lib/commonjs/utils/variants.js +20 -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 +1 -0
- 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/unistyles.js +2 -0
- package/lib/module/types/unistyles.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 +43 -82
- package/lib/module/utils/breakpoints.js.map +1 -1
- package/lib/module/utils/common.js +5 -0
- package/lib/module/utils/common.js.map +1 -1
- package/lib/module/utils/index.js +5 -3
- package/lib/module/utils/index.js.map +1 -1
- 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/styles.js +7 -55
- package/lib/module/utils/styles.js.map +1 -1
- package/lib/module/utils/variants.js +13 -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 +34 -18
- package/lib/typescript/src/types/core.d.ts.map +1 -1
- package/lib/typescript/src/types/index.d.ts +5 -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/unistyles.d.ts +37 -0
- package/lib/typescript/src/types/unistyles.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 +5 -0
- package/lib/typescript/src/utils/common.d.ts.map +1 -1
- package/lib/typescript/src/utils/index.d.ts +5 -3
- 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/styles.d.ts +4 -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 +30 -19
- 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 +16 -19
- package/src/types/common.ts +2 -0
- package/src/types/core.ts +42 -24
- package/src/types/index.ts +5 -9
- package/src/types/mq.ts +3 -0
- package/src/types/unistyles.ts +45 -0
- package/src/useStyles.ts +56 -0
- package/src/utils/breakpoints.ts +57 -88
- package/src/utils/common.ts +6 -0
- package/src/utils/index.ts +5 -10
- package/src/utils/mq.ts +106 -0
- package/src/utils/mqParser.ts +99 -0
- package/src/utils/styles.ts +17 -64
- package/src/utils/variants.ts +17 -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 -34
- 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 -27
- 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/examples/expo/src/App.d.ts +0 -3
- package/lib/typescript/examples/expo/src/App.d.ts.map +0 -1
- package/lib/typescript/examples/expo/src/examples/Breakpoints.d.ts +0 -3
- package/lib/typescript/examples/expo/src/examples/Breakpoints.d.ts.map +0 -1
- package/lib/typescript/examples/expo/src/examples/EmptyStyles.d.ts +0 -3
- package/lib/typescript/examples/expo/src/examples/EmptyStyles.d.ts.map +0 -1
- package/lib/typescript/examples/expo/src/examples/Extreme.d.ts +0 -7
- package/lib/typescript/examples/expo/src/examples/Extreme.d.ts.map +0 -1
- package/lib/typescript/examples/expo/src/examples/MediaQueries.d.ts +0 -3
- package/lib/typescript/examples/expo/src/examples/MediaQueries.d.ts.map +0 -1
- package/lib/typescript/examples/expo/src/examples/Memoization.d.ts +0 -3
- package/lib/typescript/examples/expo/src/examples/Memoization.d.ts.map +0 -1
- package/lib/typescript/examples/expo/src/examples/Minimal.d.ts +0 -3
- package/lib/typescript/examples/expo/src/examples/Minimal.d.ts.map +0 -1
- package/lib/typescript/examples/expo/src/examples/MinimalWithCreateStyleSheet.d.ts +0 -3
- package/lib/typescript/examples/expo/src/examples/MinimalWithCreateStyleSheet.d.ts.map +0 -1
- package/lib/typescript/examples/expo/src/examples/PlatformColors.d.ts +0 -3
- package/lib/typescript/examples/expo/src/examples/PlatformColors.d.ts.map +0 -1
- package/lib/typescript/examples/expo/src/examples/Theme.d.ts +0 -3
- package/lib/typescript/examples/expo/src/examples/Theme.d.ts.map +0 -1
- package/lib/typescript/examples/expo/src/examples/index.d.ts +0 -9
- package/lib/typescript/examples/expo/src/examples/index.d.ts.map +0 -1
- package/lib/typescript/examples/expo/src/index.d.ts +0 -2
- package/lib/typescript/examples/expo/src/index.d.ts.map +0 -1
- package/lib/typescript/examples/expo/src/styles/breakpoints.d.ts +0 -8
- package/lib/typescript/examples/expo/src/styles/breakpoints.d.ts.map +0 -1
- package/lib/typescript/examples/expo/src/styles/index.d.ts +0 -46
- package/lib/typescript/examples/expo/src/styles/index.d.ts.map +0 -1
- package/lib/typescript/examples/expo/src/styles/theme.d.ts +0 -24
- package/lib/typescript/examples/expo/src/styles/theme.d.ts.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 -34
- package/src/types/mediaQueries.ts +0 -10
- package/src/utils/mediaQueries.ts +0 -201
@@ -0,0 +1,99 @@
|
|
1
|
+
import type { NestedStylePairs, Optional, ScreenSize } from '../types'
|
2
|
+
|
3
|
+
const IS_UNISTYLES_REGEX = /:([hw])\[(\d+)(?:,\s*(\d+|Infinity))?]/
|
4
|
+
const UNISTYLES_WIDTH_REGEX = /:(w)\[(\d+)(?:,\s*(\d+|Infinity))?]/
|
5
|
+
const UNISTYLES_HEIGHT_REGEX = /:(h)\[(\d+)(?:,\s*(\d+|Infinity))?]/
|
6
|
+
|
7
|
+
type ParsedMqDimension = {
|
8
|
+
from: number,
|
9
|
+
to: number
|
10
|
+
}
|
11
|
+
|
12
|
+
type UnistylesParsedMq = {
|
13
|
+
width?: ParsedMqDimension,
|
14
|
+
height?: ParsedMqDimension
|
15
|
+
}
|
16
|
+
|
17
|
+
const parseMq = (mq: string): UnistylesParsedMq => {
|
18
|
+
const [, width, fromW, toW] = UNISTYLES_WIDTH_REGEX.exec(mq) || []
|
19
|
+
const [, height, fromH, toH] = UNISTYLES_HEIGHT_REGEX.exec(mq) || []
|
20
|
+
|
21
|
+
return {
|
22
|
+
width: width ? {
|
23
|
+
from: Number(fromW),
|
24
|
+
to: Number(toW)
|
25
|
+
} : undefined,
|
26
|
+
height: height ? {
|
27
|
+
from: Number(fromH),
|
28
|
+
to: Number(toH)
|
29
|
+
} : undefined
|
30
|
+
}
|
31
|
+
}
|
32
|
+
|
33
|
+
const isUnistylesMq = (mq: string) => IS_UNISTYLES_REGEX.test(mq)
|
34
|
+
|
35
|
+
const isValidMq = (parsedMq: UnistylesParsedMq) => {
|
36
|
+
const { width, height } = parsedMq
|
37
|
+
|
38
|
+
if (width && height) {
|
39
|
+
return width.from <= width.to && height.from <= height.to
|
40
|
+
}
|
41
|
+
|
42
|
+
if (width) {
|
43
|
+
return width.from <= width.to
|
44
|
+
}
|
45
|
+
|
46
|
+
if (height) {
|
47
|
+
return height.from <= height.to
|
48
|
+
}
|
49
|
+
|
50
|
+
return false
|
51
|
+
}
|
52
|
+
|
53
|
+
const isWithinTheWidthAndHeight = (parsedMq: UnistylesParsedMq, screenSize: ScreenSize): boolean => {
|
54
|
+
const { width, height } = parsedMq
|
55
|
+
|
56
|
+
if (width && height) {
|
57
|
+
return isWithinTheWidth(width, screenSize.width) && isWithinTheHeight(height, screenSize.height)
|
58
|
+
}
|
59
|
+
|
60
|
+
if (width) {
|
61
|
+
return isWithinTheWidth(width, screenSize.width)
|
62
|
+
}
|
63
|
+
|
64
|
+
if (height) {
|
65
|
+
return isWithinTheHeight(height, screenSize.height)
|
66
|
+
}
|
67
|
+
|
68
|
+
return false
|
69
|
+
}
|
70
|
+
|
71
|
+
const isWithinTheWidth = (width: UnistylesParsedMq['width'], screenWidth: number): boolean => {
|
72
|
+
const { from, to } = width as ParsedMqDimension
|
73
|
+
|
74
|
+
return screenWidth >= from && screenWidth <= to
|
75
|
+
}
|
76
|
+
|
77
|
+
const isWithinTheHeight = (height: UnistylesParsedMq['height'], screenHeight: number): boolean => {
|
78
|
+
const { from, to } = height as ParsedMqDimension
|
79
|
+
|
80
|
+
return screenHeight >= from && screenHeight <= to
|
81
|
+
}
|
82
|
+
|
83
|
+
export const getKeyForUnistylesMediaQuery = (mediaQueries: NestedStylePairs, screenSize: ScreenSize) => {
|
84
|
+
const mq = mediaQueries.find(([key]) => {
|
85
|
+
if (!isUnistylesMq(key as string)) {
|
86
|
+
return false
|
87
|
+
}
|
88
|
+
|
89
|
+
const parsedMq = parseMq(key as string)
|
90
|
+
|
91
|
+
if (!isValidMq(parsedMq)) {
|
92
|
+
return false
|
93
|
+
}
|
94
|
+
|
95
|
+
return isWithinTheWidthAndHeight(parsedMq, screenSize)
|
96
|
+
})
|
97
|
+
|
98
|
+
return mq?.at(0) as Optional<string>
|
99
|
+
}
|
package/src/utils/styles.ts
CHANGED
@@ -1,36 +1,16 @@
|
|
1
|
-
import type {
|
2
|
-
import {
|
1
|
+
import type { CustomNamedStyles, ScreenSize, NestedStyle } from '../types'
|
2
|
+
import { getValueForNestedStyle } from './breakpoints'
|
3
3
|
import { normalizeStyles } from './normalizeStyles'
|
4
|
+
import type { UnistylesBreakpoints } from '../global'
|
4
5
|
import { isAndroid, isIOS, isWeb } from './common'
|
5
6
|
|
6
|
-
|
7
|
-
|
8
|
-
*
|
9
|
-
* @template B - An object type where keys represent breakpoint names and values represent breakpoint values.
|
10
|
-
*
|
11
|
-
* @param {Function} fn - The function to be proxified.
|
12
|
-
* @param {keyof B & string} breakpoint - The breakpoint name to check against.
|
13
|
-
* @param {ScreenSize} screenSize - An object representing the screen size to be checked against the media queries.
|
14
|
-
* @param breakpointPairs - sorted pairs of breakpoints
|
15
|
-
*
|
16
|
-
* @returns {Function} Returns the proxified function
|
17
|
-
*
|
18
|
-
* @example
|
19
|
-
*
|
20
|
-
* const myFunction = () => ({ ':w[200]': 'value1', sm: 'value2' })
|
21
|
-
* const screenSize = { width: 250, height: 400 }
|
22
|
-
* const breakpoints = { sm: 300, md: 600 }
|
23
|
-
*
|
24
|
-
* const proxifiedFunction = proxifyFunction(myFunction, 'sm', screenSize, breakpoints)
|
25
|
-
* proxifiedFunction() // parsed style based on screenSize and breakpoints
|
26
|
-
*/
|
27
|
-
export const proxifyFunction = <B extends Breakpoints>(
|
28
|
-
fn: Function, breakpoint: keyof B & string,
|
7
|
+
export const proxifyFunction = (
|
8
|
+
fn: Function, breakpoint: keyof UnistylesBreakpoints & string,
|
29
9
|
screenSize: ScreenSize,
|
30
|
-
|
10
|
+
variant?: string
|
31
11
|
): Function => new Proxy(fn, {
|
32
12
|
apply: (target, thisArg, argumentsList) =>
|
33
|
-
parseStyle(target.apply(thisArg, argumentsList), breakpoint, screenSize,
|
13
|
+
parseStyle(target.apply(thisArg, argumentsList), breakpoint, screenSize, variant)
|
34
14
|
})
|
35
15
|
|
36
16
|
export const isPlatformColor = <T extends {}>(value: T): boolean => {
|
@@ -41,40 +21,15 @@ export const isPlatformColor = <T extends {}>(value: T): boolean => {
|
|
41
21
|
return isAndroid && 'resource_paths' in value && typeof value.resource_paths === 'object'
|
42
22
|
}
|
43
23
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
* The function processes each key-value pair in the style object. If the value is a function or a valid style (not an object or a 'transform' key),
|
48
|
-
* it is returned as-is. Otherwise, the function attempts to resolve the value based on the provided breakpoint, screen size, and defined breakpoints.
|
49
|
-
*
|
50
|
-
* @template T - The type of the style object.
|
51
|
-
* @template B - An object type where keys represent breakpoint names and values represent breakpoint values.
|
52
|
-
*
|
53
|
-
* @param {CustomNamedStyles<T, B>} style - The style object to be parsed.
|
54
|
-
* @param {keyof B & string} breakpoint - The breakpoint name to check against.
|
55
|
-
* @param {ScreenSize} screenSize - An object representing the screen size to be checked against the media queries.
|
56
|
-
* @param breakpointPairs - sorted pairs of breakpoints
|
57
|
-
*
|
58
|
-
* @returns {Record<string, string | number | Function>} Returns the parsed style object with resolved custom media queries or breakpoints.
|
59
|
-
*
|
60
|
-
* @example
|
61
|
-
*
|
62
|
-
* const style = { fontSize: { sm: '12px', md: '16px' } }
|
63
|
-
* const screenSize = { width: 300, height: 400 }
|
64
|
-
* const breakpoints = { xs: 0, sm: 300, md: 600 }
|
65
|
-
*
|
66
|
-
* const parsedStyle = parseStyle(style, 'sm', screenSize, breakpoints)
|
67
|
-
* // { fontSize: '12px' }
|
68
|
-
*/
|
69
|
-
export const parseStyle = <T, B extends Breakpoints>(
|
70
|
-
style: CustomNamedStyles<T, B>,
|
71
|
-
breakpoint: keyof B & string,
|
24
|
+
export const parseStyle = <T>(
|
25
|
+
style: CustomNamedStyles<T>,
|
26
|
+
breakpoint: keyof UnistylesBreakpoints & string,
|
72
27
|
screenSize: ScreenSize,
|
73
|
-
|
28
|
+
variant?: string
|
74
29
|
): T => {
|
75
30
|
const entries = Object.entries(style || {}) as [[
|
76
31
|
keyof T,
|
77
|
-
CustomNamedStyles<T
|
32
|
+
CustomNamedStyles<T> | NestedStyle]
|
78
33
|
]
|
79
34
|
|
80
35
|
const parsedStyles = Object
|
@@ -85,7 +40,7 @@ export const parseStyle = <T, B extends Breakpoints>(
|
|
85
40
|
if (hasNestedProperties) {
|
86
41
|
return [
|
87
42
|
key,
|
88
|
-
parseStyle(value as CustomNamedStyles<T
|
43
|
+
parseStyle(value as CustomNamedStyles<T>, breakpoint, screenSize, variant)
|
89
44
|
]
|
90
45
|
}
|
91
46
|
|
@@ -94,7 +49,7 @@ export const parseStyle = <T, B extends Breakpoints>(
|
|
94
49
|
if (isTransform && Array.isArray(value)) {
|
95
50
|
return [
|
96
51
|
key,
|
97
|
-
value.map(value => parseStyle(value, breakpoint, screenSize,
|
52
|
+
value.map(value => parseStyle(value, breakpoint, screenSize, variant))
|
98
53
|
]
|
99
54
|
}
|
100
55
|
|
@@ -107,11 +62,9 @@ export const parseStyle = <T, B extends Breakpoints>(
|
|
107
62
|
|
108
63
|
return [
|
109
64
|
key,
|
110
|
-
|
111
|
-
value as
|
112
|
-
|
113
|
-
screenSize,
|
114
|
-
breakpointPairs
|
65
|
+
getValueForNestedStyle(
|
66
|
+
value as NestedStyle,
|
67
|
+
variant
|
115
68
|
)
|
116
69
|
]
|
117
70
|
})
|
@@ -0,0 +1,17 @@
|
|
1
|
+
import type { Optional, NestedStyle } from '../types'
|
2
|
+
|
3
|
+
export const getKeyForVariant = (value: NestedStyle, variant?: string): Optional<keyof typeof value> => {
|
4
|
+
if (!value.variants) {
|
5
|
+
return undefined
|
6
|
+
}
|
7
|
+
|
8
|
+
if (variant && variant in value.variants) {
|
9
|
+
return variant as keyof typeof value
|
10
|
+
}
|
11
|
+
|
12
|
+
if ('default' in value.variants) {
|
13
|
+
return 'default' as keyof typeof value
|
14
|
+
}
|
15
|
+
|
16
|
+
return undefined
|
17
|
+
}
|
@@ -1,21 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
4
|
-
value: true
|
5
|
-
});
|
6
|
-
exports.UnistylesTheme = exports.UnistylesContext = void 0;
|
7
|
-
var _react = _interopRequireWildcard(require("react"));
|
8
|
-
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
9
|
-
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
10
|
-
const UnistylesContext = exports.UnistylesContext = /*#__PURE__*/(0, _react.createContext)({});
|
11
|
-
const UnistylesTheme = _ref => {
|
12
|
-
let {
|
13
|
-
theme,
|
14
|
-
children
|
15
|
-
} = _ref;
|
16
|
-
return /*#__PURE__*/_react.default.createElement(UnistylesContext.Provider, {
|
17
|
-
value: theme
|
18
|
-
}, children);
|
19
|
-
};
|
20
|
-
exports.UnistylesTheme = UnistylesTheme;
|
21
|
-
//# sourceMappingURL=UnistylesTheme.js.map
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"names":["_react","_interopRequireWildcard","require","_getRequireWildcardCache","e","WeakMap","r","t","__esModule","default","has","get","n","__proto__","a","Object","defineProperty","getOwnPropertyDescriptor","u","prototype","hasOwnProperty","call","i","set","UnistylesContext","exports","createContext","UnistylesTheme","_ref","theme","children","createElement","Provider","value"],"sourceRoot":"../../src","sources":["UnistylesTheme.tsx"],"mappings":";;;;;;AAAA,IAAAA,MAAA,GAAAC,uBAAA,CAAAC,OAAA;AAA4C,SAAAC,yBAAAC,CAAA,6BAAAC,OAAA,mBAAAC,CAAA,OAAAD,OAAA,IAAAE,CAAA,OAAAF,OAAA,YAAAF,wBAAA,YAAAA,CAAAC,CAAA,WAAAA,CAAA,GAAAG,CAAA,GAAAD,CAAA,KAAAF,CAAA;AAAA,SAAAH,wBAAAG,CAAA,EAAAE,CAAA,SAAAA,CAAA,IAAAF,CAAA,IAAAA,CAAA,CAAAI,UAAA,SAAAJ,CAAA,eAAAA,CAAA,uBAAAA,CAAA,yBAAAA,CAAA,WAAAK,OAAA,EAAAL,CAAA,QAAAG,CAAA,GAAAJ,wBAAA,CAAAG,CAAA,OAAAC,CAAA,IAAAA,CAAA,CAAAG,GAAA,CAAAN,CAAA,UAAAG,CAAA,CAAAI,GAAA,CAAAP,CAAA,OAAAQ,CAAA,KAAAC,SAAA,UAAAC,CAAA,GAAAC,MAAA,CAAAC,cAAA,IAAAD,MAAA,CAAAE,wBAAA,WAAAC,CAAA,IAAAd,CAAA,oBAAAc,CAAA,IAAAH,MAAA,CAAAI,SAAA,CAAAC,cAAA,CAAAC,IAAA,CAAAjB,CAAA,EAAAc,CAAA,SAAAI,CAAA,GAAAR,CAAA,GAAAC,MAAA,CAAAE,wBAAA,CAAAb,CAAA,EAAAc,CAAA,UAAAI,CAAA,KAAAA,CAAA,CAAAX,GAAA,IAAAW,CAAA,CAAAC,GAAA,IAAAR,MAAA,CAAAC,cAAA,CAAAJ,CAAA,EAAAM,CAAA,EAAAI,CAAA,IAAAV,CAAA,CAAAM,CAAA,IAAAd,CAAA,CAAAc,CAAA,YAAAN,CAAA,CAAAH,OAAA,GAAAL,CAAA,EAAAG,CAAA,IAAAA,CAAA,CAAAgB,GAAA,CAAAnB,CAAA,EAAAQ,CAAA,GAAAA,CAAA;AAOrC,MAAMY,gBAAgB,GAAAC,OAAA,CAAAD,gBAAA,gBAAG,IAAAE,oBAAa,EAAC,CAAC,CAAC,CAAC;AAE1C,MAAMC,cAA4D,GAAGC,IAAA;EAAA,IAAC;IACzEC,KAAK;IACLC;EACJ,CAAC,GAAAF,IAAA;EAAA,oBACG5B,MAAA,CAAAS,OAAA,CAAAsB,aAAA,CAACP,gBAAgB,CAACQ,QAAQ;IAACC,KAAK,EAAEJ;EAAM,GACnCC,QACsB,CAAC;AAAA,CAC/B;AAAAL,OAAA,CAAAE,cAAA,GAAAA,cAAA"}
|
@@ -1,57 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
4
|
-
value: true
|
5
|
-
});
|
6
|
-
exports.createUnistyles = void 0;
|
7
|
-
var _react = require("react");
|
8
|
-
var _reactNative = require("react-native");
|
9
|
-
var _UnistylesTheme = require("./UnistylesTheme");
|
10
|
-
var _hooks = require("./hooks");
|
11
|
-
var _utils = require("./utils");
|
12
|
-
const createUnistyles = breakpoints => {
|
13
|
-
const sortedBreakpoints = (0, _utils.sortAndValidateBreakpoints)(breakpoints);
|
14
|
-
const sortedBreakpointEntries = Object.entries(sortedBreakpoints);
|
15
|
-
return {
|
16
|
-
createStyleSheet: styles => {
|
17
|
-
if (typeof styles === 'function') {
|
18
|
-
return styles;
|
19
|
-
}
|
20
|
-
return styles;
|
21
|
-
},
|
22
|
-
useStyles: stylesheet => {
|
23
|
-
const theme = (0, _react.useContext)(_UnistylesTheme.UnistylesContext);
|
24
|
-
const screenSize = (0, _hooks.useDimensions)();
|
25
|
-
const breakpoint = (0, _utils.getBreakpointFromScreenWidth)(screenSize.width, sortedBreakpointEntries);
|
26
|
-
if (!stylesheet) {
|
27
|
-
return {
|
28
|
-
theme,
|
29
|
-
breakpoint,
|
30
|
-
styles: {}
|
31
|
-
};
|
32
|
-
}
|
33
|
-
const parsedStyles = (0, _react.useMemo)(() => typeof stylesheet === 'function' ? stylesheet(theme) : stylesheet, [theme, stylesheet]);
|
34
|
-
const dynamicStyleSheet = (0, _react.useMemo)(() => Object.entries(parsedStyles).reduce((acc, _ref) => {
|
35
|
-
let [key, value] = _ref;
|
36
|
-
const style = value;
|
37
|
-
if (typeof value === 'function') {
|
38
|
-
return {
|
39
|
-
...acc,
|
40
|
-
[key]: (0, _utils.proxifyFunction)(value, breakpoint, screenSize, sortedBreakpointEntries)
|
41
|
-
};
|
42
|
-
}
|
43
|
-
return _reactNative.StyleSheet.create({
|
44
|
-
...acc,
|
45
|
-
[key]: (0, _utils.parseStyle)(style, breakpoint, screenSize, sortedBreakpointEntries)
|
46
|
-
});
|
47
|
-
}, {}), [breakpoint, screenSize, parsedStyles]);
|
48
|
-
return {
|
49
|
-
theme,
|
50
|
-
breakpoint,
|
51
|
-
styles: dynamicStyleSheet
|
52
|
-
};
|
53
|
-
}
|
54
|
-
};
|
55
|
-
};
|
56
|
-
exports.createUnistyles = createUnistyles;
|
57
|
-
//# sourceMappingURL=createUnistyles.js.map
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"names":["_react","require","_reactNative","_UnistylesTheme","_hooks","_utils","createUnistyles","breakpoints","sortedBreakpoints","sortAndValidateBreakpoints","sortedBreakpointEntries","Object","entries","createStyleSheet","styles","useStyles","stylesheet","theme","useContext","UnistylesContext","screenSize","useDimensions","breakpoint","getBreakpointFromScreenWidth","width","parsedStyles","useMemo","dynamicStyleSheet","reduce","acc","_ref","key","value","style","proxifyFunction","StyleSheet","create","parseStyle","exports"],"sourceRoot":"../../src","sources":["createUnistyles.ts"],"mappings":";;;;;;AAAA,IAAAA,MAAA,GAAAC,OAAA;AACA,IAAAC,YAAA,GAAAD,OAAA;AASA,IAAAE,eAAA,GAAAF,OAAA;AACA,IAAAG,MAAA,GAAAH,OAAA;AACA,IAAAI,MAAA,GAAAJ,OAAA;AAEO,MAAMK,eAAe,GAAmCC,WAAc,IAAK;EAC9E,MAAMC,iBAAiB,GAAG,IAAAC,iCAA0B,EAACF,WAAW,CAAC;EACjE,MAAMG,uBAAuB,GAAGC,MAAM,CACjCC,OAAO,CAACJ,iBAAiB,CAA+B;EAE7D,OAAO;IACHK,gBAAgB,EAAyCC,MAAqF,IAAY;MACtJ,IAAI,OAAOA,MAAM,KAAK,UAAU,EAAE;QAC9B,OAAOA,MAAM;MACjB;MAEA,OAAOA,MAAM;IACjB,CAAC;IACDC,SAAS,EAAwCC,UAA4C,IAAK;MAC9F,MAAMC,KAAK,GAAG,IAAAC,iBAAU,EAACC,gCAAgB,CAAM;MAC/C,MAAMC,UAAU,GAAG,IAAAC,oBAAa,EAAC,CAAC;MAClC,MAAMC,UAAU,GAAG,IAAAC,mCAA4B,EAAIH,UAAU,CAACI,KAAK,EAAEd,uBAAuB,CAAC;MAE7F,IAAI,CAACM,UAAU,EAAE;QACb,OAAO;UACHC,KAAK;UACLK,UAAU;UACVR,MAAM,EAAE,CAAC;QACb,CAAC;MACL;MAEA,MAAMW,YAAY,GAAG,IAAAC,cAAO,EAAC,MAAM,OAAOV,UAAU,KAAK,UAAU,GAC7DA,UAAU,CAACC,KAAK,CAAC,GACjBD,UAAU,EAAE,CAACC,KAAK,EAAED,UAAU,CAAC,CAAC;MAEtC,MAAMW,iBAAiB,GAAG,IAAAD,cAAO,EAAC,MAAMf,MAAM,CACzCC,OAAO,CAACa,YAAY,CAAC,CACrBG,MAAM,CAAC,CAACC,GAAG,EAAAC,IAAA,KAAmB;QAAA,IAAjB,CAACC,GAAG,EAAEC,KAAK,CAAC,GAAAF,IAAA;QACtB,MAAMG,KAAK,GAAGD,KAAiC;QAE/C,IAAI,OAAOA,KAAK,KAAK,UAAU,EAAE;UAC7B,OAAO;YACH,GAAGH,GAAG;YACN,CAACE,GAAG,GAAG,IAAAG,sBAAe,EAAIF,KAAK,EAAEV,UAAU,EAAEF,UAAU,EAAEV,uBAAuB;UACpF,CAAC;QACL;QAEA,OAAOyB,uBAAU,CAACC,MAAM,CAAC;UACrB,GAAGP,GAAG;UACN,CAACE,GAAG,GAAG,IAAAM,iBAAU,EAAQJ,KAAK,EAAEX,UAAU,EAAEF,UAAU,EAAEV,uBAAuB;QACnF,CAAC,CAAC;MACN,CAAC,EAAE,CAAC,CAAO,CAAC,EAAE,CAACY,UAAU,EAAEF,UAAU,EAAEK,YAAY,CAAC,CAAC;MAEzD,OAAO;QACHR,KAAK;QACLK,UAAU;QACVR,MAAM,EAAEa;MACZ,CAAC;IACL;EACJ,CAAC;AACL,CAAC;AAAAW,OAAA,CAAAhC,eAAA,GAAAA,eAAA"}
|
@@ -1,10 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
4
|
-
value: true
|
5
|
-
});
|
6
|
-
exports.useDimensions = void 0;
|
7
|
-
var _reactNative = require("react-native");
|
8
|
-
const useDimensions = () => (0, _reactNative.useWindowDimensions)();
|
9
|
-
exports.useDimensions = useDimensions;
|
10
|
-
//# sourceMappingURL=useDimensions.js.map
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"names":["_reactNative","require","useDimensions","useWindowDimensions","exports"],"sourceRoot":"../../../src","sources":["hooks/useDimensions.ts"],"mappings":";;;;;;AAAA,IAAAA,YAAA,GAAAC,OAAA;AAGO,MAAMC,aAAa,GAAGA,CAAA,KAAkB,IAAAC,gCAAmB,EAAC,CAAC;AAAAC,OAAA,CAAAF,aAAA,GAAAA,aAAA"}
|
@@ -1,34 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
4
|
-
value: true
|
5
|
-
});
|
6
|
-
exports.useDimensions = void 0;
|
7
|
-
var _react = require("react");
|
8
|
-
const useDimensions = () => {
|
9
|
-
const timerRef = (0, _react.useRef)();
|
10
|
-
const [screenSize, setScreenSize] = (0, _react.useState)({});
|
11
|
-
(0, _react.useLayoutEffect)(() => {
|
12
|
-
setScreenSize({
|
13
|
-
width: window.innerWidth,
|
14
|
-
height: window.innerHeight
|
15
|
-
});
|
16
|
-
}, []);
|
17
|
-
(0, _react.useEffect)(() => {
|
18
|
-
const handleResize = () => {
|
19
|
-
clearTimeout(timerRef.current);
|
20
|
-
timerRef.current = setTimeout(() => setScreenSize({
|
21
|
-
width: window.innerWidth,
|
22
|
-
height: window.innerHeight
|
23
|
-
}), 100);
|
24
|
-
};
|
25
|
-
window.addEventListener('resize', handleResize);
|
26
|
-
return () => {
|
27
|
-
window.removeEventListener('resize', handleResize);
|
28
|
-
clearTimeout(timerRef.current);
|
29
|
-
};
|
30
|
-
}, []);
|
31
|
-
return screenSize;
|
32
|
-
};
|
33
|
-
exports.useDimensions = useDimensions;
|
34
|
-
//# sourceMappingURL=useDimensions.web.js.map
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"names":["_react","require","useDimensions","timerRef","useRef","screenSize","setScreenSize","useState","useLayoutEffect","width","window","innerWidth","height","innerHeight","useEffect","handleResize","clearTimeout","current","setTimeout","addEventListener","removeEventListener","exports"],"sourceRoot":"../../../src","sources":["hooks/useDimensions.web.ts"],"mappings":";;;;;;AAAA,IAAAA,MAAA,GAAAC,OAAA;AAGO,MAAMC,aAAa,GAAGA,CAAA,KAAkB;EAC3C,MAAMC,QAAQ,GAAG,IAAAC,aAAM,EAAgC,CAAC;EACxD,MAAM,CAACC,UAAU,EAAEC,aAAa,CAAC,GAAG,IAAAC,eAAQ,EAAa,CAAC,CAAe,CAAC;EAE1E,IAAAC,sBAAe,EAAC,MAAM;IAClBF,aAAa,CAAC;MACVG,KAAK,EAAEC,MAAM,CAACC,UAAU;MACxBC,MAAM,EAAEF,MAAM,CAACG;IACnB,CAAC,CAAC;EACN,CAAC,EAAE,EAAE,CAAC;EAEN,IAAAC,gBAAS,EAAC,MAAM;IACZ,MAAMC,YAAY,GAAGA,CAAA,KAAM;MACvBC,YAAY,CAACb,QAAQ,CAACc,OAAO,CAAC;MAE9Bd,QAAQ,CAACc,OAAO,GAAGC,UAAU,CAAC,MAAMZ,aAAa,CAAC;QAC9CG,KAAK,EAAEC,MAAM,CAACC,UAAU;QACxBC,MAAM,EAAEF,MAAM,CAACG;MACnB,CAAC,CAAC,EAAE,GAAG,CAAC;IACZ,CAAC;IAEDH,MAAM,CAACS,gBAAgB,CAAC,QAAQ,EAAEJ,YAAY,CAAC;IAE/C,OAAO,MAAM;MACTL,MAAM,CAACU,mBAAmB,CAAC,QAAQ,EAAEL,YAAY,CAAC;MAClDC,YAAY,CAACb,QAAQ,CAACc,OAAO,CAAC;IAClC,CAAC;EACL,CAAC,EAAE,EAAE,CAAC;EAEN,OAAOZ,UAAU;AACrB,CAAC;AAAAgB,OAAA,CAAAnB,aAAA,GAAAA,aAAA"}
|
@@ -1,189 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
4
|
-
value: true
|
5
|
-
});
|
6
|
-
exports.isWithinTheWidthAndHeight = exports.isWithinTheWidth = exports.isWithinTheHeight = exports.isWithinBreakpoint = exports.isMediaQuery = exports.getKeyForCustomMediaQuery = exports.extractValues = void 0;
|
7
|
-
/**
|
8
|
-
* Extracts numeric values from a coded string.
|
9
|
-
*
|
10
|
-
* The function is designed to process strings that have a format like "w[100,200]" or "h[300]".
|
11
|
-
* It removes characters 'w', 'h', '[', and ']' from the input string and then extracts the numbers.
|
12
|
-
*
|
13
|
-
* @param {string} codedValue - The input string to extract values from.
|
14
|
-
* @returns {Array<number>} An array of extracted numbers. Can contain one or two numbers based on the input format.
|
15
|
-
*
|
16
|
-
* @example
|
17
|
-
* extractValues("w[100,200]") // returns [100, 200]
|
18
|
-
* extractValues("h[300]") // returns [300]
|
19
|
-
* extractValues("h[,300]") // returns [0,300]
|
20
|
-
* extractValues("h[100,]") // returns [100]
|
21
|
-
*/
|
22
|
-
const extractValues = codedValue => {
|
23
|
-
const [lh, rh] = codedValue.replace(/[wh[\]]/g, '').split(',');
|
24
|
-
return rh ? [Number(lh), Number(rh)] : [Number(lh)];
|
25
|
-
};
|
26
|
-
|
27
|
-
/**
|
28
|
-
* Determines if the given screen size matches the specified breakpoint query.
|
29
|
-
*
|
30
|
-
* The function checks if the screen size (width and/or height) falls within the range
|
31
|
-
* specified by the breakpoint query. The query can specify width (using 'w'), height (using 'h'),
|
32
|
-
* or both.
|
33
|
-
*
|
34
|
-
* @param {string} query - The breakpoint query string. Examples: 'w[100,200]', 'h[300]', 'w[100,200]h[300,400]'.
|
35
|
-
* @param {ScreenSize} screenSize - The screen size to check against the breakpoint query.
|
36
|
-
* @returns {boolean} True if the screen size matches the breakpoint query, false otherwise.
|
37
|
-
*
|
38
|
-
* @example
|
39
|
-
* const screenSize = { width: 150, height: 350 }
|
40
|
-
* isWithinBreakpoint('w[100,200]', screenSize) // returns true
|
41
|
-
* isWithinBreakpoint('h[400]', screenSize) // returns false
|
42
|
-
*/
|
43
|
-
exports.extractValues = extractValues;
|
44
|
-
const isWithinBreakpoint = (query, screenSize) => {
|
45
|
-
if (query.includes('w') && query.includes('h')) {
|
46
|
-
return isWithinTheWidthAndHeight(query, screenSize);
|
47
|
-
}
|
48
|
-
if (query.charAt(0) === 'w') {
|
49
|
-
return isWithinTheWidth(query, screenSize.width);
|
50
|
-
}
|
51
|
-
if (query.charAt(0) === 'h') {
|
52
|
-
return isWithinTheHeight(query, screenSize.height);
|
53
|
-
}
|
54
|
-
return false;
|
55
|
-
};
|
56
|
-
|
57
|
-
/**
|
58
|
-
* Determines if the given width matches the specified width range in the query.
|
59
|
-
*
|
60
|
-
* The function checks if the provided width falls within the range specified by the query.
|
61
|
-
* The query specifies a width range using a format like 'w[100,200]'. If only one value is provided,
|
62
|
-
* it's treated as a minimum width.
|
63
|
-
*
|
64
|
-
* @param {string} query - The width query string. Examples: 'w[100,200]' or 'w[100]'.
|
65
|
-
* @param {number} width - The width to check against the query.
|
66
|
-
* @returns {boolean} True if the width matches the query range, false otherwise.
|
67
|
-
*
|
68
|
-
* @example
|
69
|
-
* isWithinTheWidth('w[100,200]', 150) // returns true
|
70
|
-
* isWithinTheWidth('w[100]', 50) // returns false
|
71
|
-
* isWithinTheWidth('w[100]', 150) // returns true
|
72
|
-
*/
|
73
|
-
exports.isWithinBreakpoint = isWithinBreakpoint;
|
74
|
-
const isWithinTheWidth = (query, width) => {
|
75
|
-
const [minWidth, maxWidth] = extractValues(query);
|
76
|
-
if (maxWidth && width >= minWidth && width <= maxWidth) {
|
77
|
-
return true;
|
78
|
-
}
|
79
|
-
return !maxWidth && width >= minWidth;
|
80
|
-
};
|
81
|
-
|
82
|
-
/**
|
83
|
-
* Determines if the given height matches the specified height range in the query.
|
84
|
-
*
|
85
|
-
* The function checks if the provided height falls within the range specified by the query.
|
86
|
-
* The query specifies a height range using a format like 'h[100,200]'. If only one value is provided,
|
87
|
-
* it's treated as a minimum height.
|
88
|
-
*
|
89
|
-
* @param {string} query - The height query string. Examples: 'h[100,200]' or 'h[100]'.
|
90
|
-
* @param {number} height - The height to check against the query.
|
91
|
-
* @returns {boolean} True if the height matches the query range, false otherwise.
|
92
|
-
*
|
93
|
-
* @example
|
94
|
-
* isWithinTheHeight('h[100,200]', 150) // returns true
|
95
|
-
* isWithinTheHeight('h[100]', 50) // returns false
|
96
|
-
* isWithinTheHeight('h[100]', 150) // returns true
|
97
|
-
*/
|
98
|
-
exports.isWithinTheWidth = isWithinTheWidth;
|
99
|
-
const isWithinTheHeight = (query, height) => {
|
100
|
-
const [minHeight, maxHeight] = extractValues(query);
|
101
|
-
if (maxHeight && height >= minHeight && height <= maxHeight) {
|
102
|
-
return true;
|
103
|
-
}
|
104
|
-
return !maxHeight && height >= minHeight;
|
105
|
-
};
|
106
|
-
|
107
|
-
/**
|
108
|
-
* Determines if the given screen size matches both the specified width and height ranges in the query.
|
109
|
-
*
|
110
|
-
* The function checks if the provided screen size (both width and height) falls within the ranges
|
111
|
-
* specified by the query. The query can specify both width and height using a format like 'w[100,200]:h[300,400]'.
|
112
|
-
*
|
113
|
-
* @param {string} query - The combined width and height query string. Example: 'w[100,200]:h[300,400]'.
|
114
|
-
* @param {ScreenSize} screenSize - The screen size to check against the query.
|
115
|
-
* @returns {boolean} True if the screen size matches both the width and height ranges in the query, false otherwise.
|
116
|
-
*
|
117
|
-
* @example
|
118
|
-
* const screenSize = { width: 150, height: 350 }
|
119
|
-
* isWithinTheWidthAndHeight('w[100,200]:h[300,400]', screenSize) // returns true
|
120
|
-
* isWithinTheWidthAndHeight('w[100,200]:h[400,500]', screenSize) // returns false
|
121
|
-
*/
|
122
|
-
exports.isWithinTheHeight = isWithinTheHeight;
|
123
|
-
const isWithinTheWidthAndHeight = (query, screenSize) => {
|
124
|
-
const result = query.split(':').filter(Boolean).map(q => isWithinBreakpoint(q, screenSize)).filter(Boolean);
|
125
|
-
return result.length === 2;
|
126
|
-
};
|
127
|
-
|
128
|
-
/**
|
129
|
-
* Checks if the given query string is a valid custom media query.
|
130
|
-
*
|
131
|
-
* The valid custom media query formats include:
|
132
|
-
* - :w[200]
|
133
|
-
* - :w[0, 200]
|
134
|
-
* - :w[, 300]
|
135
|
-
* - :h[200]
|
136
|
-
* - :h[0, 500]
|
137
|
-
* - :h[,200]
|
138
|
-
* - :w[100, 300]:h[200,500]
|
139
|
-
* - :h[200,500]:w[100, 300]
|
140
|
-
*
|
141
|
-
* @param {string} query - The query string to be checked.
|
142
|
-
* @returns {boolean} Returns `true` if the query is a valid custom media query, otherwise `false`.
|
143
|
-
* @example
|
144
|
-
*
|
145
|
-
* isMediaQuery(':w[200]') // true
|
146
|
-
* isMediaQuery(':w100]') // false
|
147
|
-
*/
|
148
|
-
exports.isWithinTheWidthAndHeight = isWithinTheWidthAndHeight;
|
149
|
-
const isMediaQuery = query => {
|
150
|
-
const regex = /^(?:(:w\[\d*(?:,\s?\d+)?])?(:h\[\d*(?:,\s?\d+)?])?|(:h\[\d*(?:,\s?\d+)?])?(:w\[\d*(?:,\s?\d+)?])?)$/;
|
151
|
-
return query.length > 0 && regex.test(query);
|
152
|
-
};
|
153
|
-
|
154
|
-
/**
|
155
|
-
* Retrieves the first matching custom media query key based on the provided screen size.
|
156
|
-
*
|
157
|
-
* The function processes an array of media queries and returns the first query that matches
|
158
|
-
* the given screen size. The media queries can be in formats like:
|
159
|
-
* - w[200]
|
160
|
-
* - w[0, 200]
|
161
|
-
* - w[, 300]
|
162
|
-
* - h[200]
|
163
|
-
* - h[0, 500]
|
164
|
-
* - h[,200]
|
165
|
-
* - w[100, 300]:h[200,500]
|
166
|
-
* - h[200,500]:w[100, 300]
|
167
|
-
*
|
168
|
-
* @param {Array<[string, string | number]>} mediaQueries - An array of tuples containing media query keys and associated values.
|
169
|
-
* @param {ScreenSize} screenSize - An object representing the screen size to be checked against the media queries.
|
170
|
-
* @returns {string | undefined} Returns the first matching media query key or `undefined` if no match is found.
|
171
|
-
* @example
|
172
|
-
*
|
173
|
-
* const queries = [[':w[200]', 'value1'], [':h[300,500]', 'value2']]
|
174
|
-
* const size = { width: 250, height: 400 }
|
175
|
-
* getKeyForCustomMediaQuery(queries, size) // ':w[200]
|
176
|
-
*/
|
177
|
-
exports.isMediaQuery = isMediaQuery;
|
178
|
-
const getKeyForCustomMediaQuery = (mediaQueries, screenSize) => {
|
179
|
-
const [matchedQuery] = mediaQueries.flatMap(_ref => {
|
180
|
-
let [key] = _ref;
|
181
|
-
if (key.includes('w') && key.includes('h')) {
|
182
|
-
return isWithinBreakpoint(key, screenSize) ? key : undefined;
|
183
|
-
}
|
184
|
-
return key.split(':').filter(Boolean).map(query => isWithinBreakpoint(query, screenSize) ? key : undefined);
|
185
|
-
}).filter(Boolean);
|
186
|
-
return matchedQuery;
|
187
|
-
};
|
188
|
-
exports.getKeyForCustomMediaQuery = getKeyForCustomMediaQuery;
|
189
|
-
//# sourceMappingURL=mediaQueries.js.map
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"names":["extractValues","codedValue","lh","rh","replace","split","Number","exports","isWithinBreakpoint","query","screenSize","includes","isWithinTheWidthAndHeight","charAt","isWithinTheWidth","width","isWithinTheHeight","height","minWidth","maxWidth","minHeight","maxHeight","result","filter","Boolean","map","q","length","isMediaQuery","regex","test","getKeyForCustomMediaQuery","mediaQueries","matchedQuery","flatMap","_ref","key","undefined"],"sourceRoot":"../../../src","sources":["utils/mediaQueries.ts"],"mappings":"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"}
|
@@ -1,12 +0,0 @@
|
|
1
|
-
import React, { createContext } from 'react';
|
2
|
-
export const UnistylesContext = /*#__PURE__*/createContext({});
|
3
|
-
export const UnistylesTheme = _ref => {
|
4
|
-
let {
|
5
|
-
theme,
|
6
|
-
children
|
7
|
-
} = _ref;
|
8
|
-
return /*#__PURE__*/React.createElement(UnistylesContext.Provider, {
|
9
|
-
value: theme
|
10
|
-
}, children);
|
11
|
-
};
|
12
|
-
//# sourceMappingURL=UnistylesTheme.js.map
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"names":["React","createContext","UnistylesContext","UnistylesTheme","_ref","theme","children","createElement","Provider","value"],"sourceRoot":"../../src","sources":["UnistylesTheme.tsx"],"mappings":"AAAA,OAAOA,KAAK,IAAIC,aAAa,QAAQ,OAAO;AAO5C,OAAO,MAAMC,gBAAgB,gBAAGD,aAAa,CAAC,CAAC,CAAC,CAAC;AAEjD,OAAO,MAAME,cAA4D,GAAGC,IAAA;EAAA,IAAC;IACzEC,KAAK;IACLC;EACJ,CAAC,GAAAF,IAAA;EAAA,oBACGJ,KAAA,CAAAO,aAAA,CAACL,gBAAgB,CAACM,QAAQ;IAACC,KAAK,EAAEJ;EAAM,GACnCC,QACsB,CAAC;AAAA,CAC/B"}
|
@@ -1,50 +0,0 @@
|
|
1
|
-
import { useContext, useMemo } from 'react';
|
2
|
-
import { StyleSheet } from 'react-native';
|
3
|
-
import { UnistylesContext } from './UnistylesTheme';
|
4
|
-
import { useDimensions } from './hooks';
|
5
|
-
import { getBreakpointFromScreenWidth, proxifyFunction, parseStyle, sortAndValidateBreakpoints } from './utils';
|
6
|
-
export const createUnistyles = breakpoints => {
|
7
|
-
const sortedBreakpoints = sortAndValidateBreakpoints(breakpoints);
|
8
|
-
const sortedBreakpointEntries = Object.entries(sortedBreakpoints);
|
9
|
-
return {
|
10
|
-
createStyleSheet: styles => {
|
11
|
-
if (typeof styles === 'function') {
|
12
|
-
return styles;
|
13
|
-
}
|
14
|
-
return styles;
|
15
|
-
},
|
16
|
-
useStyles: stylesheet => {
|
17
|
-
const theme = useContext(UnistylesContext);
|
18
|
-
const screenSize = useDimensions();
|
19
|
-
const breakpoint = getBreakpointFromScreenWidth(screenSize.width, sortedBreakpointEntries);
|
20
|
-
if (!stylesheet) {
|
21
|
-
return {
|
22
|
-
theme,
|
23
|
-
breakpoint,
|
24
|
-
styles: {}
|
25
|
-
};
|
26
|
-
}
|
27
|
-
const parsedStyles = useMemo(() => typeof stylesheet === 'function' ? stylesheet(theme) : stylesheet, [theme, stylesheet]);
|
28
|
-
const dynamicStyleSheet = useMemo(() => Object.entries(parsedStyles).reduce((acc, _ref) => {
|
29
|
-
let [key, value] = _ref;
|
30
|
-
const style = value;
|
31
|
-
if (typeof value === 'function') {
|
32
|
-
return {
|
33
|
-
...acc,
|
34
|
-
[key]: proxifyFunction(value, breakpoint, screenSize, sortedBreakpointEntries)
|
35
|
-
};
|
36
|
-
}
|
37
|
-
return StyleSheet.create({
|
38
|
-
...acc,
|
39
|
-
[key]: parseStyle(style, breakpoint, screenSize, sortedBreakpointEntries)
|
40
|
-
});
|
41
|
-
}, {}), [breakpoint, screenSize, parsedStyles]);
|
42
|
-
return {
|
43
|
-
theme,
|
44
|
-
breakpoint,
|
45
|
-
styles: dynamicStyleSheet
|
46
|
-
};
|
47
|
-
}
|
48
|
-
};
|
49
|
-
};
|
50
|
-
//# sourceMappingURL=createUnistyles.js.map
|