react-native-unistyles 1.0.0-beta.4 → 1.0.0-beta.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (96) hide show
  1. package/lib/commonjs/createUnistyles.js +10 -12
  2. package/lib/commonjs/createUnistyles.js.map +1 -1
  3. package/lib/commonjs/types/breakpoints.js +6 -0
  4. package/lib/commonjs/types/breakpoints.js.map +1 -0
  5. package/lib/commonjs/{types.js → types/core.js} +1 -1
  6. package/lib/commonjs/types/core.js.map +1 -0
  7. package/lib/commonjs/types/index.js +2 -0
  8. package/lib/commonjs/types/index.js.map +1 -0
  9. package/lib/commonjs/types/mediaQueries.js +2 -0
  10. package/lib/commonjs/types/mediaQueries.js.map +1 -0
  11. package/lib/commonjs/utils/breakpoints.js +12 -16
  12. package/lib/commonjs/utils/breakpoints.js.map +1 -1
  13. package/lib/commonjs/utils/breakpoints.spec.js +20 -15
  14. package/lib/commonjs/utils/breakpoints.spec.js.map +1 -1
  15. package/lib/commonjs/utils/common.js +8 -1
  16. package/lib/commonjs/utils/common.js.map +1 -1
  17. package/lib/commonjs/utils/index.js +7 -0
  18. package/lib/commonjs/utils/index.js.map +1 -1
  19. package/lib/commonjs/utils/mediaQueries.spec.js +1 -1
  20. package/lib/commonjs/utils/mediaQueries.spec.js.map +1 -1
  21. package/lib/commonjs/utils/normalizeStyles.js +10 -0
  22. package/lib/commonjs/utils/normalizeStyles.js.map +1 -0
  23. package/lib/commonjs/utils/normalizeStyles.web.js +59 -0
  24. package/lib/commonjs/utils/normalizeStyles.web.js.map +1 -0
  25. package/lib/commonjs/utils/styles.js +14 -11
  26. package/lib/commonjs/utils/styles.js.map +1 -1
  27. package/lib/commonjs/utils/styles.spec.js +17 -7
  28. package/lib/commonjs/utils/styles.spec.js.map +1 -1
  29. package/lib/module/createUnistyles.js +10 -12
  30. package/lib/module/createUnistyles.js.map +1 -1
  31. package/lib/module/types/breakpoints.js +2 -0
  32. package/lib/module/types/breakpoints.js.map +1 -0
  33. package/lib/module/types/core.js +2 -0
  34. package/lib/module/types/core.js.map +1 -0
  35. package/lib/module/types/index.js +2 -0
  36. package/lib/module/types/index.js.map +1 -0
  37. package/lib/module/types/mediaQueries.js +2 -0
  38. package/lib/module/types/mediaQueries.js.map +1 -0
  39. package/lib/module/utils/breakpoints.js +12 -16
  40. package/lib/module/utils/breakpoints.js.map +1 -1
  41. package/lib/module/utils/breakpoints.spec.js +20 -15
  42. package/lib/module/utils/breakpoints.spec.js.map +1 -1
  43. package/lib/module/utils/common.js +5 -0
  44. package/lib/module/utils/common.js.map +1 -1
  45. package/lib/module/utils/index.js +1 -0
  46. package/lib/module/utils/index.js.map +1 -1
  47. package/lib/module/utils/mediaQueries.spec.js +1 -1
  48. package/lib/module/utils/mediaQueries.spec.js.map +1 -1
  49. package/lib/module/utils/normalizeStyles.js +3 -0
  50. package/lib/module/utils/normalizeStyles.js.map +1 -0
  51. package/lib/module/utils/normalizeStyles.web.js +52 -0
  52. package/lib/module/utils/normalizeStyles.web.js.map +1 -0
  53. package/lib/module/utils/styles.js +14 -11
  54. package/lib/module/utils/styles.js.map +1 -1
  55. package/lib/module/utils/styles.spec.js +17 -7
  56. package/lib/module/utils/styles.spec.js.map +1 -1
  57. package/lib/typescript/src/createUnistyles.d.ts +2 -2
  58. package/lib/typescript/src/createUnistyles.d.ts.map +1 -1
  59. package/lib/typescript/src/types/breakpoints.d.ts +19 -0
  60. package/lib/typescript/src/types/breakpoints.d.ts.map +1 -0
  61. package/lib/typescript/src/types/core.d.ts +32 -0
  62. package/lib/typescript/src/types/core.d.ts.map +1 -0
  63. package/lib/typescript/src/types/index.d.ts +3 -0
  64. package/lib/typescript/src/types/index.d.ts.map +1 -0
  65. package/lib/typescript/src/types/mediaQueries.d.ts +2 -0
  66. package/lib/typescript/src/types/mediaQueries.d.ts.map +1 -0
  67. package/lib/typescript/src/utils/breakpoints.d.ts +6 -6
  68. package/lib/typescript/src/utils/breakpoints.d.ts.map +1 -1
  69. package/lib/typescript/src/utils/common.d.ts +2 -0
  70. package/lib/typescript/src/utils/common.d.ts.map +1 -1
  71. package/lib/typescript/src/utils/index.d.ts +1 -0
  72. package/lib/typescript/src/utils/index.d.ts.map +1 -1
  73. package/lib/typescript/src/utils/normalizeStyles.d.ts +2 -0
  74. package/lib/typescript/src/utils/normalizeStyles.d.ts.map +1 -0
  75. package/lib/typescript/src/utils/normalizeStyles.web.d.ts +2 -0
  76. package/lib/typescript/src/utils/normalizeStyles.web.d.ts.map +1 -0
  77. package/lib/typescript/src/utils/styles.d.ts +5 -5
  78. package/lib/typescript/src/utils/styles.d.ts.map +1 -1
  79. package/package.json +9 -2
  80. package/src/createUnistyles.ts +20 -14
  81. package/src/types/breakpoints.ts +33 -0
  82. package/src/types/core.ts +73 -0
  83. package/src/types/index.ts +9 -0
  84. package/src/types/mediaQueries.ts +69 -0
  85. package/src/utils/breakpoints.ts +15 -15
  86. package/src/utils/common.ts +8 -0
  87. package/src/utils/index.ts +1 -0
  88. package/src/utils/normalizeStyles.ts +2 -0
  89. package/src/utils/normalizeStyles.web.ts +103 -0
  90. package/src/utils/styles.ts +20 -14
  91. package/lib/commonjs/types.js.map +0 -1
  92. package/lib/module/types.js +0 -2
  93. package/lib/module/types.js.map +0 -1
  94. package/lib/typescript/src/types.d.ts +0 -45
  95. package/lib/typescript/src/types.d.ts.map +0 -1
  96. package/src/types.ts +0 -96
@@ -0,0 +1,73 @@
1
+ import type {
2
+ MatrixTransform,
3
+ PerpectiveTransform,
4
+ RotateTransform,
5
+ RotateXTransform,
6
+ RotateYTransform,
7
+ RotateZTransform,
8
+ ScaleTransform,
9
+ ScaleXTransform,
10
+ ScaleYTransform,
11
+ SkewXTransform,
12
+ SkewYTransform,
13
+ TranslateXTransform,
14
+ TranslateYTransform
15
+ } from 'react-native/Libraries/StyleSheet/StyleSheetTypes'
16
+ import type { ImageStyle, TextStyle, ViewStyle } from 'react-native'
17
+ import type { Breakpoints } from './breakpoints'
18
+ import type { MediaQueries } from './mediaQueries'
19
+
20
+ type ShadowOffset = {
21
+ width: number,
22
+ height: number
23
+ }
24
+
25
+ type TransformStyles =
26
+ & PerpectiveTransform
27
+ & RotateTransform
28
+ & RotateXTransform
29
+ & RotateYTransform
30
+ & RotateZTransform
31
+ & ScaleTransform
32
+ & ScaleXTransform
33
+ & ScaleYTransform
34
+ & TranslateXTransform
35
+ & TranslateYTransform
36
+ & SkewXTransform
37
+ & SkewYTransform
38
+ & MatrixTransform
39
+
40
+ type UnistyleNested<B> = {
41
+ shadowOffset?: DeepUniStyle<ShadowOffset, B>,
42
+ textShadowOffset?: DeepUniStyle<ShadowOffset, B>,
43
+ transform?: Array<DeepUniStyle<TransformStyles, B>>
44
+ }
45
+
46
+ type UniStyle<V, B> = {
47
+ [innerKey in keyof B]?: V
48
+ } | {
49
+ [innerKey in MediaQueries]?: V
50
+ } | V
51
+
52
+ type DeepUniStyle<T, B> = {
53
+ [K in keyof T]?: UniStyle<T[K], B>
54
+ }
55
+
56
+ // these props are treated differently to nest breakpoints and media queries
57
+ type NestedTypes = 'shadowOffset' | 'transform' | 'textShadowOffset'
58
+
59
+ type UnistyleView<B> = DeepUniStyle<Omit<ViewStyle, NestedTypes>, B>
60
+ type UnistyleText<B> = DeepUniStyle<Omit<TextStyle, NestedTypes>, B>
61
+ type UnistyleImage<B> = DeepUniStyle<Omit<ImageStyle, NestedTypes>, B>
62
+
63
+ export type StaticStyles<B extends Breakpoints> =
64
+ | UnistyleView<B>
65
+ | UnistyleText<B>
66
+ | UnistyleImage<B>
67
+ & UnistyleNested<B>
68
+
69
+ export type CustomNamedStyles<T, B extends Breakpoints> = {
70
+ [K in keyof T]: T[K] extends (...args: infer A) => StaticStyles<B>
71
+ ? (...args: A) => StaticStyles<B>
72
+ : StaticStyles<B>
73
+ }
@@ -0,0 +1,9 @@
1
+ export type { CustomNamedStyles } from './core'
2
+ export type {
3
+ ScreenSize,
4
+ Breakpoints,
5
+ CreateStylesFactory,
6
+ ExtractBreakpoints,
7
+ RemoveKeysWithPrefix,
8
+ SortedBreakpointEntries
9
+ } from './breakpoints'
@@ -0,0 +1,69 @@
1
+ // this is super weird, but number passes empty string and bigint does not
2
+ export type MediaQueries =
3
+ // For :w
4
+ `:w[${bigint}]` |
5
+ `:w[,${bigint}]` |
6
+ `:w[, ${bigint}]` |
7
+ `:w[${bigint},${bigint}]` |
8
+ `:w[${bigint}, ${bigint}]` |
9
+
10
+ // For :h
11
+ `:h[${bigint}]` |
12
+ `:h[,${bigint}]` |
13
+ `:h[, ${bigint}]` |
14
+ `:h[${bigint},${bigint}]` |
15
+ `:h[${bigint}, ${bigint}]` |
16
+
17
+ // Combinations of :w and :h
18
+ `:w[${bigint}]:h[${bigint}]` |
19
+ `:w[${bigint},${bigint}]:h[${bigint}]` |
20
+ `:w[${bigint}, ${bigint}]:h[${bigint}]` |
21
+ `:w[${bigint}]:h[${bigint},${bigint}]` |
22
+ `:w[${bigint}]:h[${bigint}, ${bigint}]` |
23
+ `:w[${bigint},${bigint}]:h[${bigint},${bigint}]` |
24
+ `:w[${bigint}, ${bigint}]:h[${bigint},${bigint}]` |
25
+ `:w[${bigint},${bigint}]:h[${bigint}, ${bigint}]` |
26
+ `:w[${bigint}, ${bigint}]:h[${bigint}, ${bigint}]` |
27
+ `:w[,${bigint}]:h[,${bigint}]` |
28
+ `:w[, ${bigint}]:h[,${bigint}]` |
29
+ `:w[,${bigint}]:h[, ${bigint}]` |
30
+ `:w[, ${bigint}]:h[, ${bigint}]` |
31
+ `:w[,${bigint}]:h[${bigint}]` |
32
+ `:w[, ${bigint}]:h[${bigint}]` |
33
+ `:w[${bigint}]:h[,${bigint}]` |
34
+ `:w[${bigint}]:h[, ${bigint}]` |
35
+ `:w[,${bigint}]:h[${bigint},${bigint}]` |
36
+ `:w[, ${bigint}]:h[${bigint},${bigint}]` |
37
+ `:w[,${bigint}]:h[${bigint}, ${bigint}]` |
38
+ `:w[, ${bigint}]:h[${bigint}, ${bigint}]` |
39
+ `:w[${bigint},${bigint}]:h[,${bigint}]` |
40
+ `:w[${bigint}, ${bigint}]:h[,${bigint}]` |
41
+ `:w[${bigint},${bigint}]:h[, ${bigint}]` |
42
+ `:w[${bigint}, ${bigint}]:h[, ${bigint}]` |
43
+
44
+ // Combinations of :h and :w
45
+ `:h[${bigint}]:w[${bigint}]` |
46
+ `:h[${bigint},${bigint}]:w[${bigint}]` |
47
+ `:h[${bigint}, ${bigint}]:w[${bigint}]` |
48
+ `:h[${bigint}]:w[${bigint},${bigint}]` |
49
+ `:h[${bigint}]:w[${bigint}, ${bigint}]` |
50
+ `:h[${bigint},${bigint}]:w[${bigint},${bigint}]` |
51
+ `:h[${bigint}, ${bigint}]:w[${bigint},${bigint}]` |
52
+ `:h[${bigint},${bigint}]:w[${bigint}, ${bigint}]` |
53
+ `:h[${bigint}, ${bigint}]:w[${bigint}, ${bigint}]` |
54
+ `:h[,${bigint}]:w[,${bigint}]` |
55
+ `:h[, ${bigint}]:w[,${bigint}]` |
56
+ `:h[,${bigint}]:w[, ${bigint}]` |
57
+ `:h[, ${bigint}]:w[, ${bigint}]` |
58
+ `:h[,${bigint}]:w[${bigint}]` |
59
+ `:h[, ${bigint}]:w[${bigint}]` |
60
+ `:h[${bigint}]:w[,${bigint}]` |
61
+ `:h[${bigint}]:w[, ${bigint}]` |
62
+ `:h[,${bigint}]:w[${bigint},${bigint}]` |
63
+ `:h[, ${bigint}]:w[${bigint},${bigint}]` |
64
+ `:h[,${bigint}]:w[${bigint}, ${bigint}]` |
65
+ `:h[, ${bigint}]:w[${bigint}, ${bigint}]` |
66
+ `:h[${bigint},${bigint}]:w[,${bigint}]` |
67
+ `:h[${bigint}, ${bigint}]:w[,${bigint}]` |
68
+ `:h[${bigint},${bigint}]:w[, ${bigint}]` |
69
+ `:h[${bigint}, ${bigint}]:w[, ${bigint}]`
@@ -1,5 +1,5 @@
1
1
  import { throwError } from './common'
2
- import type { ScreenSize } from '../types'
2
+ import type { Breakpoints, ScreenSize, SortedBreakpointEntries } from '../types'
3
3
  import { getKeyForCustomMediaQuery, isMediaQuery } from './mediaQueries'
4
4
 
5
5
  /**
@@ -21,7 +21,7 @@ import { getKeyForCustomMediaQuery, isMediaQuery } from './mediaQueries'
21
21
  * const input = { md: 768, lg: 1024, sm: 0 }
22
22
  * sortAndValidateBreakpoints(input) // returns { sm: 0, md: 768, lg: 1024 }
23
23
  */
24
- export const sortAndValidateBreakpoints = <B extends Record<string, number>>(breakpoints: B): B => {
24
+ export const sortAndValidateBreakpoints = <B extends Breakpoints>(breakpoints: B): B => {
25
25
  const sortedPairs = Object
26
26
  .entries(breakpoints)
27
27
  .sort((breakpoint1, breakpoint2) => {
@@ -54,16 +54,15 @@ export const sortAndValidateBreakpoints = <B extends Record<string, number>>(bre
54
54
  *
55
55
  * @template B - An object type where keys are strings and values are numbers representing screen widths.
56
56
  * @param {number} width - The screen width to determine the breakpoint for.
57
- * @param {B} breakpoints - The breakpoints object to use for determination.
57
+ * @param breakpointEntries - sorted pairs of breakpoints
58
58
  * @returns {keyof B & string} The key of the breakpoint that the screen width falls into.
59
59
  *
60
60
  * @example
61
61
  * const breakpoints = { sm: 0, md: 768, lg: 1024 }
62
62
  * getBreakpointFromScreenWidth(800, breakpoints) // returns 'md'
63
63
  */
64
- export const getBreakpointFromScreenWidth = <B extends Record<string, number>>(width: number, breakpoints: B): keyof B & string => {
65
- const [key] = Object
66
- .entries(breakpoints)
64
+ export const getBreakpointFromScreenWidth = <B extends Breakpoints>(width: number, breakpointEntries: SortedBreakpointEntries<B>): keyof B & string => {
65
+ const [key] = breakpointEntries
67
66
  .find(([, value], index, otherBreakpoints) => {
68
67
  const minVal = value
69
68
  const maxVal = otherBreakpoints[index + 1]?.[1]
@@ -90,7 +89,7 @@ export const getBreakpointFromScreenWidth = <B extends Record<string, number>>(w
90
89
  * @param {Record<keyof B & string, string | number>} value - An object containing values associated with breakpoints or custom media queries.
91
90
  * @param {keyof B & string} breakpoint - The breakpoint name to check against.
92
91
  * @param {ScreenSize} screenSize - An object representing the screen size to be checked against the media queries.
93
- * @param {B} breakpoints - An object representing the defined breakpoints.
92
+ * @param breakpointPairs - sorted pairs of breakpoints
94
93
  *
95
94
  * @returns {string | number | undefined} Returns the value associated with the matching breakpoint or custom media query, or `undefined` if no match is found.
96
95
  *
@@ -102,7 +101,12 @@ export const getBreakpointFromScreenWidth = <B extends Record<string, number>>(w
102
101
  *
103
102
  * getValueForBreakpoint(values, 'sm', screenSize, breakpoints); // 'value1'
104
103
  */
105
- export const getValueForBreakpoint = <B extends Record<string, number>>(value: Record<keyof B & string, string | number | undefined>, breakpoint: keyof B & string, screenSize: ScreenSize, breakpoints: B) => {
104
+ export const getValueForBreakpoint = <B extends Breakpoints>(
105
+ value: Record<keyof B & string, string | number | undefined>,
106
+ breakpoint: keyof B & string,
107
+ screenSize: ScreenSize,
108
+ breakpointPairs: SortedBreakpointEntries<B>
109
+ ): string | number | undefined => {
106
110
  // the highest priority is for custom media queries
107
111
  const customMediaQueries = Object
108
112
  .entries(value)
@@ -123,18 +127,14 @@ export const getValueForBreakpoint = <B extends Record<string, number>>(value: R
123
127
  }
124
128
 
125
129
  // there is no direct hit for breakpoint nor media-query, so let's simulate CSS cascading
126
- const allBreakpoints = Object
127
- .entries(breakpoints)
128
- .map(([key, bpValue]) => [key.toLowerCase(), bpValue])
129
-
130
- const currentBreakpoint = allBreakpoints
130
+ const currentBreakpoint = breakpointPairs
131
131
  .findIndex(([key]) => key === unifiedKey)
132
132
 
133
- const availableBreakpoints = allBreakpoints
133
+ const availableBreakpoints = breakpointPairs
134
134
  .filter(([key], index) => index < currentBreakpoint && key && key in value)
135
135
  .map(([key]) => key)
136
136
 
137
- return allBreakpoints.length > 0
137
+ return breakpointPairs.length > 0
138
138
  ? value[availableBreakpoints[availableBreakpoints.length - 1] as keyof B & string]
139
139
  : undefined
140
140
  }
@@ -1,3 +1,11 @@
1
+ import { Platform } from 'react-native'
2
+
1
3
  export const throwError = (message: string) => {
2
4
  throw new Error(`🦄 [react-native-unistyles]: ${message}`)
3
5
  }
6
+
7
+ export const warn = (message: string) => {
8
+ console.warn(`🦄 [react-native-unistyles]: ${message}`)
9
+ }
10
+
11
+ export const isWeb = () => Platform.OS === 'web'
@@ -1,2 +1,3 @@
1
+ export { normalizeStyles } from './normalizeStyles'
1
2
  export { getBreakpointFromScreenWidth, sortAndValidateBreakpoints } from './breakpoints'
2
3
  export { proxifyFunction, parseStyle } from './styles'
@@ -0,0 +1,2 @@
1
+ // nothing to do here
2
+ export const normalizeStyles = <T>(styles: T) => styles as T
@@ -0,0 +1,103 @@
1
+ import { warn } from './common'
2
+
3
+ const preprocessor: Preprocessor = require('react-native-web/src/exports/StyleSheet/preprocess.js')
4
+
5
+ type Preprocessor = {
6
+ createTextShadowValue<T>(styles: any): T,
7
+ createBoxShadowValue<T>(styles: any): T,
8
+ createTransformValue<T>(transforms: any): T,
9
+ }
10
+
11
+ type NormalizedBoxShadow = {
12
+ shadowColor: undefined,
13
+ shadowOffset: undefined,
14
+ shadowOpacity: undefined,
15
+ shadowRadius: undefined,
16
+ boxShadow?: string
17
+ }
18
+
19
+ type NormalizedTextShadow = {
20
+ textShadowColor: undefined
21
+ textShadowOffset: undefined
22
+ textShadowRadius: undefined,
23
+ textShadow?: string
24
+ }
25
+
26
+ const normalizeBoxShadow = <T extends {}>(styles: T): NormalizedBoxShadow => {
27
+ const requiredBoxShadowProperties = [
28
+ 'shadowColor',
29
+ 'shadowOffset',
30
+ 'shadowOpacity',
31
+ 'shadowRadius'
32
+ ]
33
+
34
+ if (!requiredBoxShadowProperties.every(prop => prop in styles)) {
35
+ warn(`can't apply box shadow as you miss at least one of these properties: ${requiredBoxShadowProperties.join(', ')}`)
36
+
37
+ return {
38
+ shadowColor: undefined,
39
+ shadowOffset: undefined,
40
+ shadowOpacity: undefined,
41
+ shadowRadius: undefined
42
+ }
43
+ }
44
+
45
+ return {
46
+ boxShadow: preprocessor.createBoxShadowValue(styles),
47
+ shadowColor: undefined,
48
+ shadowOffset: undefined,
49
+ shadowOpacity: undefined,
50
+ shadowRadius: undefined
51
+ }
52
+ }
53
+
54
+ const normalizeTextShadow = <T extends {}>(styles: T): NormalizedTextShadow => {
55
+ const requiredTextShadowProperties = [
56
+ 'textShadowColor',
57
+ 'textShadowOffset',
58
+ 'textShadowRadius'
59
+ ]
60
+
61
+ if (!requiredTextShadowProperties.every(prop => prop in styles)) {
62
+ warn(`can't apply text shadow as you miss at least one of these properties: ${requiredTextShadowProperties.join(', ')}`)
63
+
64
+ return {
65
+ textShadowColor: undefined,
66
+ textShadowOffset: undefined,
67
+ textShadowRadius: undefined
68
+ }
69
+ }
70
+
71
+ return {
72
+ textShadow: preprocessor.createTextShadowValue(styles),
73
+ textShadowColor: undefined,
74
+ textShadowOffset: undefined,
75
+ textShadowRadius: undefined
76
+ }
77
+ }
78
+
79
+ export const normalizeStyles = <T extends {}>(styles: T): T => {
80
+ const normalizedTransform = ('transform' in styles && Array.isArray(styles.transform))
81
+ ? { transform: preprocessor.createTransformValue(styles.transform) }
82
+ : {}
83
+
84
+ const normalizedBoxShadow = (
85
+ 'shadowColor' in styles ||
86
+ 'shadowOffset' in styles ||
87
+ 'shadowOpacity' in styles ||
88
+ 'shadowRadius' in styles
89
+ ) ? normalizeBoxShadow(styles) : {}
90
+
91
+ const normalizedTextShadow = (
92
+ 'textShadowColor' in styles ||
93
+ 'textShadowOffset' in styles ||
94
+ 'textShadowRadius' in styles
95
+ ) ? normalizeTextShadow(styles) : {}
96
+
97
+ return {
98
+ ...styles,
99
+ ...normalizedTransform,
100
+ ...normalizedBoxShadow,
101
+ ...normalizedTextShadow
102
+ }
103
+ }
@@ -1,5 +1,7 @@
1
- import type { CustomNamedStyles, ScreenSize } from '../types'
1
+ import type { Breakpoints, CustomNamedStyles, ScreenSize, SortedBreakpointEntries } from '../types'
2
2
  import { getValueForBreakpoint } from './breakpoints'
3
+ import { normalizeStyles } from './normalizeStyles'
4
+ import { isWeb } from './common'
3
5
 
4
6
  /**
5
7
  * Proxies a function to parse its return value for custom media queries or breakpoints.
@@ -9,7 +11,7 @@ import { getValueForBreakpoint } from './breakpoints'
9
11
  * @param {Function} fn - The function to be proxified.
10
12
  * @param {keyof B & string} breakpoint - The breakpoint name to check against.
11
13
  * @param {ScreenSize} screenSize - An object representing the screen size to be checked against the media queries.
12
- * @param {B} breakpoints - An object representing the defined breakpoints.
14
+ * @param breakpointPairs - sorted pairs of breakpoints
13
15
  *
14
16
  * @returns {Function} Returns the proxified function
15
17
  *
@@ -22,13 +24,13 @@ import { getValueForBreakpoint } from './breakpoints'
22
24
  * const proxifiedFunction = proxifyFunction(myFunction, 'sm', screenSize, breakpoints)
23
25
  * proxifiedFunction() // parsed style based on screenSize and breakpoints
24
26
  */
25
- export const proxifyFunction = <B extends Record<string, number>>(
27
+ export const proxifyFunction = <B extends Breakpoints>(
26
28
  fn: Function, breakpoint: keyof B & string,
27
29
  screenSize: ScreenSize,
28
- breakpoints: B
30
+ breakpointPairs: SortedBreakpointEntries<B>
29
31
  ): Function => new Proxy(fn, {
30
32
  apply: (target, thisArg, argumentsList) =>
31
- parseStyle(target.apply(thisArg, argumentsList), breakpoint, screenSize, breakpoints)
33
+ parseStyle(target.apply(thisArg, argumentsList), breakpoint, screenSize, breakpointPairs)
32
34
  })
33
35
 
34
36
  /**
@@ -43,7 +45,7 @@ export const proxifyFunction = <B extends Record<string, number>>(
43
45
  * @param {CustomNamedStyles<T, B>} style - The style object to be parsed.
44
46
  * @param {keyof B & string} breakpoint - The breakpoint name to check against.
45
47
  * @param {ScreenSize} screenSize - An object representing the screen size to be checked against the media queries.
46
- * @param {B} breakpoints - An object representing the defined breakpoints.
48
+ * @param breakpointPairs - sorted pairs of breakpoints
47
49
  *
48
50
  * @returns {Record<string, string | number | Function>} Returns the parsed style object with resolved custom media queries or breakpoints.
49
51
  *
@@ -56,26 +58,26 @@ export const proxifyFunction = <B extends Record<string, number>>(
56
58
  * const parsedStyle = parseStyle(style, 'sm', screenSize, breakpoints)
57
59
  * // { fontSize: '12px' }
58
60
  */
59
- export const parseStyle = <T, B extends Record<string, number>>(
61
+ export const parseStyle = <T, B extends Breakpoints>(
60
62
  style: CustomNamedStyles<T, B>,
61
63
  breakpoint: keyof B & string,
62
64
  screenSize: ScreenSize,
63
- breakpoints: B
65
+ breakpointPairs: SortedBreakpointEntries<B>
64
66
  ): T => {
65
67
  const entries = Object.entries(style) as [[
66
68
  keyof T,
67
69
  CustomNamedStyles<T, B> | Record<keyof B & string, string | number | undefined>]
68
70
  ]
69
71
 
70
- return Object
72
+ const parsedStyles = Object
71
73
  .fromEntries(entries
72
74
  .map(([key, value]) => {
73
- const isNestedStyle = key === 'shadowOffset'
75
+ const hasNestedProperties = key === 'shadowOffset' || key === 'textShadowOffset'
74
76
 
75
- if (isNestedStyle) {
77
+ if (hasNestedProperties) {
76
78
  return [
77
79
  key,
78
- parseStyle(value as CustomNamedStyles<T, B>, breakpoint, screenSize, breakpoints)
80
+ parseStyle(value as CustomNamedStyles<T, B>, breakpoint, screenSize, breakpointPairs)
79
81
  ]
80
82
  }
81
83
 
@@ -84,7 +86,7 @@ export const parseStyle = <T, B extends Record<string, number>>(
84
86
  if (isTransform && Array.isArray(value)) {
85
87
  return [
86
88
  key,
87
- value.map(value => parseStyle(value, breakpoint, screenSize, breakpoints))
89
+ value.map(value => parseStyle(value, breakpoint, screenSize, breakpointPairs))
88
90
  ]
89
91
  }
90
92
 
@@ -101,9 +103,13 @@ export const parseStyle = <T, B extends Record<string, number>>(
101
103
  value as Record<keyof B & string, string | number | undefined>,
102
104
  breakpoint,
103
105
  screenSize,
104
- breakpoints
106
+ breakpointPairs
105
107
  )
106
108
  ]
107
109
  })
108
110
  )
111
+
112
+ return isWeb()
113
+ ? normalizeStyles(parsedStyles)
114
+ : parsedStyles
109
115
  }
@@ -1 +0,0 @@
1
- {"version":3,"names":[],"sourceRoot":"../../src","sources":["types.ts"],"mappings":""}
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=types.js.map
@@ -1 +0,0 @@
1
- {"version":3,"names":[],"sourceRoot":"../../src","sources":["types.ts"],"mappings":""}
@@ -1,45 +0,0 @@
1
- import type { ImageStyle, TextStyle, ViewStyle } from 'react-native';
2
- import type { MatrixTransform, PerpectiveTransform, RotateTransform, RotateXTransform, RotateYTransform, RotateZTransform, ScaleTransform, ScaleXTransform, ScaleYTransform, SkewXTransform, SkewYTransform, TranslateXTransform, TranslateYTransform } from 'react-native/Libraries/StyleSheet/StyleSheetTypes';
3
- export type ScreenSize = {
4
- width: number;
5
- height: number;
6
- };
7
- export type CreateStylesFactory<ST, Theme> = (theme: Theme) => ST;
8
- type StyleProperty<T, B extends Record<string, number>> = {
9
- [K in keyof T]: {
10
- [innerKey in keyof B]?: T[K];
11
- } | {
12
- [innerKey in string]?: T[K];
13
- } | T[K];
14
- };
15
- type ShadowOffsetProps<B extends Record<string, number>> = {
16
- shadowOffset: {
17
- width: number | {
18
- [innerKey in keyof B]?: number;
19
- };
20
- height: number | {
21
- [innerKey in keyof B]?: number;
22
- };
23
- };
24
- };
25
- type TransformStyles<B extends Record<string, number>> = PerpectiveTransform | StyleProperty<PerpectiveTransform, B> | RotateTransform | StyleProperty<RotateTransform, B> | RotateXTransform | StyleProperty<RotateXTransform, B> | RotateYTransform | StyleProperty<RotateYTransform, B> | RotateZTransform | StyleProperty<RotateZTransform, B> | ScaleTransform | StyleProperty<ScaleTransform, B> | ScaleXTransform | StyleProperty<ScaleXTransform, B> | ScaleYTransform | StyleProperty<ScaleYTransform, B> | TranslateXTransform | StyleProperty<TranslateXTransform, B> | TranslateYTransform | StyleProperty<TranslateYTransform, B> | SkewXTransform | StyleProperty<SkewXTransform, B> | SkewYTransform | StyleProperty<SkewYTransform, B> | MatrixTransform | StyleProperty<MatrixTransform, B>;
26
- type TransformProps<B extends Record<string, number>> = {
27
- transform: Array<TransformStyles<B>>;
28
- };
29
- type UnistyleView = Omit<Omit<ViewStyle, 'shadowOffset'>, 'transform'>;
30
- type UnistyleText = Omit<Omit<TextStyle, 'shadowOffset'>, 'transform'>;
31
- type UnistyleImage = Omit<Omit<ImageStyle, 'shadowOffset'>, 'transform'>;
32
- export type StaticStyles<B extends Record<string, number>> = (UnistyleView | StyleProperty<UnistyleView, B>) | (UnistyleText | StyleProperty<UnistyleText, B>) | (UnistyleImage | StyleProperty<UnistyleImage, B>) & TransformProps<B> & ShadowOffsetProps<B>;
33
- export type CustomNamedStyles<T, B extends Record<string, number>> = {
34
- [K in keyof T]: T[K] extends (...args: infer A) => unknown ? (...args: A) => StaticStyles<B> : StaticStyles<B>;
35
- };
36
- export type ExtractBreakpoints<T, B extends Record<string, number>> = T extends Partial<Record<keyof B & string, infer V>> ? V : T extends (...args: infer A) => infer R ? (...args: A) => ExtractBreakpoints<R, B> : {
37
- [K in keyof T]: T[K] extends (...args: infer A) => infer R ? (...args: A) => ExtractBreakpoints<R, B> : T[K] extends object ? ExtractBreakpoints<T[K], B> : T[K];
38
- };
39
- export type RemoveKeysWithPrefix<T, B extends Record<string, number>> = T extends (...args: Array<any>) => infer R ? (...args: Parameters<T>) => RemoveKeysWithPrefix<R, B> : T extends object ? T extends Record<string, infer _V> ? {
40
- [K in keyof T as K extends `:w${string}` | `:h${string}` ? keyof B & string : K]: RemoveKeysWithPrefix<T[K], B>;
41
- } : {
42
- [K in keyof T]: RemoveKeysWithPrefix<T[K], B>;
43
- } : T;
44
- export {};
45
- //# sourceMappingURL=types.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,cAAc,CAAA;AACpE,OAAO,KAAK,EACR,eAAe,EACf,mBAAmB,EACnB,eAAe,EACf,gBAAgB,EAChB,gBAAgB,EAChB,gBAAgB,EAChB,cAAc,EACd,eAAe,EACf,eAAe,EACf,cAAc,EACd,cAAc,EACd,mBAAmB,EACnB,mBAAmB,EACtB,MAAM,mDAAmD,CAAA;AAE1D,MAAM,MAAM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oB,CAAC,CAAC,EAAE,CAAC,CAAC,GACtD,CAAC,SAAS,MAAM,GACZ,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC,GAC9B;KAAG,CAAC,IAAI,MAAM,CAAC,IAAI,CAAC,SAAS,KAAK,MAAM,EAAE,GAAG,KAAK,MAAM,EAAE,GAAG,MAAM,CAAC,GAAG,MAAM,GAAG,CAAC,GAAG,oBAAoB,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;CAAE,GACnH;KAAG,CAAC,IAAI,MAAM,CAAC,GAAG,oBAAoB,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;CAAE,GACrD,CAAC,CAAA"}
package/src/types.ts DELETED
@@ -1,96 +0,0 @@
1
- import type { ImageStyle, TextStyle, ViewStyle } from 'react-native'
2
- import type {
3
- MatrixTransform,
4
- PerpectiveTransform,
5
- RotateTransform,
6
- RotateXTransform,
7
- RotateYTransform,
8
- RotateZTransform,
9
- ScaleTransform,
10
- ScaleXTransform,
11
- ScaleYTransform,
12
- SkewXTransform,
13
- SkewYTransform,
14
- TranslateXTransform,
15
- TranslateYTransform
16
- } from 'react-native/Libraries/StyleSheet/StyleSheetTypes'
17
-
18
- export type ScreenSize = {
19
- width: number,
20
- height: number
21
- }
22
-
23
- export type CreateStylesFactory<ST, Theme> = (theme: Theme) => ST
24
-
25
- type StyleProperty<T, B extends Record<string, number>> = {
26
- [K in keyof T]: {
27
- [innerKey in keyof B]?: T[K]
28
- } | {
29
- [innerKey in string]?: T[K]
30
- } | T[K]
31
- }
32
-
33
- type ShadowOffsetProps<B extends Record<string, number>> = {
34
- shadowOffset: {
35
- width: number | {
36
- [innerKey in keyof B]?: number
37
- },
38
- height: number | {
39
- [innerKey in keyof B]?: number
40
- }
41
- }
42
- }
43
-
44
- type TransformStyles<B extends Record<string, number>> =
45
- PerpectiveTransform | StyleProperty<PerpectiveTransform, B>
46
- | RotateTransform | StyleProperty<RotateTransform, B>
47
- | RotateXTransform | StyleProperty<RotateXTransform, B>
48
- | RotateYTransform | StyleProperty<RotateYTransform, B>
49
- | RotateZTransform | StyleProperty<RotateZTransform, B>
50
- | ScaleTransform | StyleProperty<ScaleTransform, B>
51
- | ScaleXTransform | StyleProperty<ScaleXTransform, B>
52
- | ScaleYTransform | StyleProperty<ScaleYTransform, B>
53
- | TranslateXTransform | StyleProperty<TranslateXTransform, B>
54
- | TranslateYTransform | StyleProperty<TranslateYTransform, B>
55
- | SkewXTransform | StyleProperty<SkewXTransform, B>
56
- | SkewYTransform | StyleProperty<SkewYTransform, B>
57
- | MatrixTransform | StyleProperty<MatrixTransform, B>
58
-
59
- type TransformProps<B extends Record<string, number>> = {
60
- transform: Array<TransformStyles<B>>
61
- }
62
-
63
- type UnistyleView = Omit<Omit<ViewStyle, 'shadowOffset'>, 'transform'>
64
- type UnistyleText = Omit<Omit<TextStyle, 'shadowOffset'>, 'transform'>
65
- type UnistyleImage = Omit<Omit<ImageStyle, 'shadowOffset'>, 'transform'>
66
-
67
- export type StaticStyles<B extends Record<string, number>> =
68
- | (UnistyleView | StyleProperty<UnistyleView, B>)
69
- | (UnistyleText | StyleProperty<UnistyleText, B>)
70
- | (UnistyleImage | StyleProperty<UnistyleImage, B>)
71
- & TransformProps<B> & ShadowOffsetProps<B>
72
-
73
- export type CustomNamedStyles<T, B extends Record<string, number>> = {
74
- [K in keyof T]: T[K] extends (...args: infer A) => unknown
75
- ? (...args: A) => StaticStyles<B>
76
- : StaticStyles<B>
77
- }
78
- export type ExtractBreakpoints<T, B extends Record<string, number>> = T extends Partial<Record<keyof B & string, infer V>>
79
- ? V
80
- : T extends (...args: infer A) => infer R
81
- ? (...args: A) => ExtractBreakpoints<R, B>
82
- : {
83
- [K in keyof T]: T[K] extends (...args: infer A) => infer R
84
- ? (...args: A) => ExtractBreakpoints<R, B>
85
- : T[K] extends object
86
- ? ExtractBreakpoints<T[K], B>
87
- : T[K]
88
- }
89
-
90
- export type RemoveKeysWithPrefix<T, B extends Record<string, number>> = T extends (...args: Array<any>) => infer R
91
- ? (...args: Parameters<T>) => RemoveKeysWithPrefix<R, B>
92
- : T extends object
93
- ? T extends Record<string, infer _V>
94
- ? { [K in keyof T as K extends `:w${string}` | `:h${string}` ? keyof B & string : K]: RemoveKeysWithPrefix<T[K], B> }
95
- : { [K in keyof T]: RemoveKeysWithPrefix<T[K], B> }
96
- : T