react-native-unistyles 1.0.0-beta.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (84) hide show
  1. package/LICENSE +20 -0
  2. package/README.md +243 -0
  3. package/lib/commonjs/UnistylesTheme.js +21 -0
  4. package/lib/commonjs/UnistylesTheme.js.map +1 -0
  5. package/lib/commonjs/createUnistyles.js +52 -0
  6. package/lib/commonjs/createUnistyles.js.map +1 -0
  7. package/lib/commonjs/index.js +20 -0
  8. package/lib/commonjs/index.js.map +1 -0
  9. package/lib/commonjs/types.js +6 -0
  10. package/lib/commonjs/types.js.map +1 -0
  11. package/lib/commonjs/utils/breakpoints.js +139 -0
  12. package/lib/commonjs/utils/breakpoints.js.map +1 -0
  13. package/lib/commonjs/utils/breakpoints.spec.js +149 -0
  14. package/lib/commonjs/utils/breakpoints.spec.js.map +1 -0
  15. package/lib/commonjs/utils/common.js +11 -0
  16. package/lib/commonjs/utils/common.js.map +1 -0
  17. package/lib/commonjs/utils/index.js +32 -0
  18. package/lib/commonjs/utils/index.js.map +1 -0
  19. package/lib/commonjs/utils/mediaQueries.js +189 -0
  20. package/lib/commonjs/utils/mediaQueries.js.map +1 -0
  21. package/lib/commonjs/utils/mediaQueries.spec.js +220 -0
  22. package/lib/commonjs/utils/mediaQueries.spec.js.map +1 -0
  23. package/lib/commonjs/utils/styles.js +82 -0
  24. package/lib/commonjs/utils/styles.js.map +1 -0
  25. package/lib/commonjs/utils/styles.spec.js +98 -0
  26. package/lib/commonjs/utils/styles.spec.js.map +1 -0
  27. package/lib/module/UnistylesTheme.js +12 -0
  28. package/lib/module/UnistylesTheme.js.map +1 -0
  29. package/lib/module/createUnistyles.js +45 -0
  30. package/lib/module/createUnistyles.js.map +1 -0
  31. package/lib/module/index.js +3 -0
  32. package/lib/module/index.js.map +1 -0
  33. package/lib/module/types.js +2 -0
  34. package/lib/module/types.js.map +1 -0
  35. package/lib/module/utils/breakpoints.js +131 -0
  36. package/lib/module/utils/breakpoints.js.map +1 -0
  37. package/lib/module/utils/breakpoints.spec.js +147 -0
  38. package/lib/module/utils/breakpoints.spec.js.map +1 -0
  39. package/lib/module/utils/common.js +4 -0
  40. package/lib/module/utils/common.js.map +1 -0
  41. package/lib/module/utils/index.js +3 -0
  42. package/lib/module/utils/index.js.map +1 -0
  43. package/lib/module/utils/mediaQueries.js +176 -0
  44. package/lib/module/utils/mediaQueries.js.map +1 -0
  45. package/lib/module/utils/mediaQueries.spec.js +218 -0
  46. package/lib/module/utils/mediaQueries.spec.js.map +1 -0
  47. package/lib/module/utils/styles.js +75 -0
  48. package/lib/module/utils/styles.js.map +1 -0
  49. package/lib/module/utils/styles.spec.js +96 -0
  50. package/lib/module/utils/styles.spec.js.map +1 -0
  51. package/lib/typescript/src/UnistylesTheme.d.ts +9 -0
  52. package/lib/typescript/src/UnistylesTheme.d.ts.map +1 -0
  53. package/lib/typescript/src/createUnistyles.d.ts +9 -0
  54. package/lib/typescript/src/createUnistyles.d.ts.map +1 -0
  55. package/lib/typescript/src/index.d.ts +3 -0
  56. package/lib/typescript/src/index.d.ts.map +1 -0
  57. package/lib/typescript/src/types.d.ts +27 -0
  58. package/lib/typescript/src/types.d.ts.map +1 -0
  59. package/lib/typescript/src/utils/breakpoints.d.ts +63 -0
  60. package/lib/typescript/src/utils/breakpoints.d.ts.map +1 -0
  61. package/lib/typescript/src/utils/breakpoints.spec.d.ts +2 -0
  62. package/lib/typescript/src/utils/breakpoints.spec.d.ts.map +1 -0
  63. package/lib/typescript/src/utils/common.d.ts +2 -0
  64. package/lib/typescript/src/utils/common.d.ts.map +1 -0
  65. package/lib/typescript/src/utils/index.d.ts +3 -0
  66. package/lib/typescript/src/utils/index.d.ts.map +1 -0
  67. package/lib/typescript/src/utils/mediaQueries.d.ts +130 -0
  68. package/lib/typescript/src/utils/mediaQueries.d.ts.map +1 -0
  69. package/lib/typescript/src/utils/mediaQueries.spec.d.ts +2 -0
  70. package/lib/typescript/src/utils/mediaQueries.spec.d.ts.map +1 -0
  71. package/lib/typescript/src/utils/styles.d.ts +56 -0
  72. package/lib/typescript/src/utils/styles.d.ts.map +1 -0
  73. package/lib/typescript/src/utils/styles.spec.d.ts +2 -0
  74. package/lib/typescript/src/utils/styles.spec.d.ts.map +1 -0
  75. package/package.json +132 -0
  76. package/src/UnistylesTheme.tsx +17 -0
  77. package/src/createUnistyles.ts +56 -0
  78. package/src/index.ts +2 -0
  79. package/src/types.ts +52 -0
  80. package/src/utils/breakpoints.ts +140 -0
  81. package/src/utils/common.ts +3 -0
  82. package/src/utils/index.ts +2 -0
  83. package/src/utils/mediaQueries.ts +201 -0
  84. package/src/utils/styles.ts +95 -0
@@ -0,0 +1,27 @@
1
+ import type { ImageStyle, TextStyle, TransformsStyle, ViewStyle } from 'react-native';
2
+ import type { CSSProperties } from 'react';
3
+ export type ScreenSize = {
4
+ width: number;
5
+ height: number;
6
+ };
7
+ export type CreateStylesFactory<T, Theme> = (theme: Theme) => T;
8
+ type StyleProperty<T, B extends Record<string, number>> = {
9
+ [key in keyof T]?: {
10
+ [innerKey in keyof B]?: T[key];
11
+ } | {
12
+ [innerKey in `:w${string}` | `:h${string}`]?: T[key];
13
+ } | T[key];
14
+ };
15
+ export type CustomNamedStyles<T, B extends Record<string, number>> = {
16
+ [P in keyof T]: ViewStyle | TextStyle | ImageStyle | TransformsStyle | CSSProperties | StyleProperty<ViewStyle, B> | StyleProperty<ImageStyle, B> | StyleProperty<TextStyle, B> | ((...args: Array<never>) => ViewStyle | TextStyle | ImageStyle | TransformsStyle | CSSProperties | StyleProperty<ViewStyle, B> | StyleProperty<ImageStyle, B> | StyleProperty<TextStyle, B>);
17
+ };
18
+ 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> : {
19
+ [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];
20
+ };
21
+ 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> ? {
22
+ [K in keyof T as K extends `:w${string}` | `:h${string}` ? keyof B & string : K]: RemoveKeysWithPrefix<T[K], B>;
23
+ } : {
24
+ [K in keyof T]: RemoveKeysWithPrefix<T[K], B>;
25
+ } : T;
26
+ export {};
27
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/types.ts"],"names":[],"mappings":"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"}
@@ -0,0 +1,63 @@
1
+ import type { ScreenSize } from '../types';
2
+ /**
3
+ * Sorts the breakpoints object based on its numeric values in ascending order and validates them.
4
+ *
5
+ * This function takes an object where keys represent breakpoint names and values are numeric.
6
+ * It returns a new object with the same keys but sorted based on their corresponding numeric values.
7
+ * Additionally, it validates that:
8
+ * 1. The first breakpoint starts with a value of 0.
9
+ * 2. No duplicate breakpoint values exist.
10
+ *
11
+ * If the validation fails, appropriate error messages are logged to the console.
12
+ *
13
+ * @template B - An object type where keys are strings and values are numbers.
14
+ * @param {B} breakpoints - The breakpoints object to be sorted and validated.
15
+ * @returns {B} A new object with sorted and validated breakpoints.
16
+ *
17
+ * @example
18
+ * const input = { md: 768, lg: 1024, sm: 0 }
19
+ * sortAndValidateBreakpoints(input) // returns { sm: 0, md: 768, lg: 1024 }
20
+ */
21
+ export declare const sortAndValidateBreakpoints: <B extends Record<string, number>>(breakpoints: B) => B;
22
+ /**
23
+ * Determines the appropriate breakpoint key for a given screen width based on provided breakpoints.
24
+ *
25
+ * This function takes a screen width and an object of breakpoints. It returns the key of the breakpoint
26
+ * that the screen width falls into. The breakpoints are assumed to be sorted in ascending order.
27
+ *
28
+ * @template B - An object type where keys are strings and values are numbers representing screen widths.
29
+ * @param {number} width - The screen width to determine the breakpoint for.
30
+ * @param {B} breakpoints - The breakpoints object to use for determination.
31
+ * @returns {keyof B & string} The key of the breakpoint that the screen width falls into.
32
+ *
33
+ * @example
34
+ * const breakpoints = { sm: 0, md: 768, lg: 1024 }
35
+ * getBreakpointFromScreenWidth(800, breakpoints) // returns 'md'
36
+ */
37
+ export declare const getBreakpointFromScreenWidth: <B extends Record<string, number>>(width: number, breakpoints: B) => keyof B & string;
38
+ /**
39
+ * Retrieves the value associated with a given breakpoint or custom media query based on the provided screen size.
40
+ *
41
+ * The function first checks for custom media queries. If a matching custom media query is found, its associated value is returned.
42
+ * If no custom media query matches, the function then checks for a direct breakpoint match.
43
+ * If there's no direct breakpoint match, the function simulates CSS cascading to find the closest matching breakpoint.
44
+ *
45
+ * @template B - An object type where keys represent breakpoint names and values represent breakpoint values.
46
+ *
47
+ * @param {Record<keyof B & string, string | number>} value - An object containing values associated with breakpoints or custom media queries.
48
+ * @param {keyof B & string} breakpoint - The breakpoint name to check against.
49
+ * @param {ScreenSize} screenSize - An object representing the screen size to be checked against the media queries.
50
+ * @param {B} breakpoints - An object representing the defined breakpoints.
51
+ *
52
+ * @returns {string | number | undefined} Returns the value associated with the matching breakpoint or custom media query, or `undefined` if no match is found.
53
+ *
54
+ * @example
55
+ *
56
+ * const values = { ':w[200]': 'value1', sm: 'value2', md: 'value3' }
57
+ * const screenSize = { width: 250, height: 400 }
58
+ * const breakpoints = { sm: 300, md: 600, lg: 900 }
59
+ *
60
+ * getValueForBreakpoint(values, 'sm', screenSize, breakpoints); // 'value1'
61
+ */
62
+ export declare const getValueForBreakpoint: <B extends Record<string, number>>(value: Record<keyof B & string, string | number | undefined>, breakpoint: keyof B & string, screenSize: ScreenSize, breakpoints: B) => string | number | Record<keyof B & string, string | number | undefined>[keyof B & string] | undefined;
63
+ //# sourceMappingURL=breakpoints.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"breakpoints.d.ts","sourceRoot":"","sources":["../../../../src/utils/breakpoints.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,UAAU,CAAA;AAG1C;;;;;;;;;;;;;;;;;;GAkBG;AACH,eAAO,MAAM,0BAA0B,yDAuBtC,CAAA;AAED;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,4BAA4B,4CAA6C,MAAM,qCAe3F,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,eAAO,MAAM,qBAAqB,6IAA8I,UAAU,0HAmCzL,CAAA"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=breakpoints.spec.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"breakpoints.spec.d.ts","sourceRoot":"","sources":["../../../../src/utils/breakpoints.spec.ts"],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+ export declare const throwError: (message: string) => never;
2
+ //# sourceMappingURL=common.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"common.d.ts","sourceRoot":"","sources":["../../../../src/utils/common.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,UAAU,YAAa,MAAM,UAEzC,CAAA"}
@@ -0,0 +1,3 @@
1
+ export { getBreakpointFromScreenWidth, sortAndValidateBreakpoints } from './breakpoints';
2
+ export { proxifyFunction, parseStyle } from './styles';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/utils/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,4BAA4B,EAAE,0BAA0B,EAAE,MAAM,eAAe,CAAA;AACxF,OAAO,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,UAAU,CAAA"}
@@ -0,0 +1,130 @@
1
+ import type { ScreenSize } from '../types';
2
+ /**
3
+ * Extracts numeric values from a coded string.
4
+ *
5
+ * The function is designed to process strings that have a format like "w[100,200]" or "h[300]".
6
+ * It removes characters 'w', 'h', '[', and ']' from the input string and then extracts the numbers.
7
+ *
8
+ * @param {string} codedValue - The input string to extract values from.
9
+ * @returns {Array<number>} An array of extracted numbers. Can contain one or two numbers based on the input format.
10
+ *
11
+ * @example
12
+ * extractValues("w[100,200]") // returns [100, 200]
13
+ * extractValues("h[300]") // returns [300]
14
+ * extractValues("h[,300]") // returns [0,300]
15
+ * extractValues("h[100,]") // returns [100]
16
+ */
17
+ export declare const extractValues: (codedValue: string) => Array<number>;
18
+ /**
19
+ * Determines if the given screen size matches the specified breakpoint query.
20
+ *
21
+ * The function checks if the screen size (width and/or height) falls within the range
22
+ * specified by the breakpoint query. The query can specify width (using 'w'), height (using 'h'),
23
+ * or both.
24
+ *
25
+ * @param {string} query - The breakpoint query string. Examples: 'w[100,200]', 'h[300]', 'w[100,200]h[300,400]'.
26
+ * @param {ScreenSize} screenSize - The screen size to check against the breakpoint query.
27
+ * @returns {boolean} True if the screen size matches the breakpoint query, false otherwise.
28
+ *
29
+ * @example
30
+ * const screenSize = { width: 150, height: 350 }
31
+ * isWithinBreakpoint('w[100,200]', screenSize) // returns true
32
+ * isWithinBreakpoint('h[400]', screenSize) // returns false
33
+ */
34
+ export declare const isWithinBreakpoint: (query: string, screenSize: ScreenSize) => boolean;
35
+ /**
36
+ * Determines if the given width matches the specified width range in the query.
37
+ *
38
+ * The function checks if the provided width falls within the range specified by the query.
39
+ * The query specifies a width range using a format like 'w[100,200]'. If only one value is provided,
40
+ * it's treated as a minimum width.
41
+ *
42
+ * @param {string} query - The width query string. Examples: 'w[100,200]' or 'w[100]'.
43
+ * @param {number} width - The width to check against the query.
44
+ * @returns {boolean} True if the width matches the query range, false otherwise.
45
+ *
46
+ * @example
47
+ * isWithinTheWidth('w[100,200]', 150) // returns true
48
+ * isWithinTheWidth('w[100]', 50) // returns false
49
+ * isWithinTheWidth('w[100]', 150) // returns true
50
+ */
51
+ export declare const isWithinTheWidth: (query: string, width: number) => boolean;
52
+ /**
53
+ * Determines if the given height matches the specified height range in the query.
54
+ *
55
+ * The function checks if the provided height falls within the range specified by the query.
56
+ * The query specifies a height range using a format like 'h[100,200]'. If only one value is provided,
57
+ * it's treated as a minimum height.
58
+ *
59
+ * @param {string} query - The height query string. Examples: 'h[100,200]' or 'h[100]'.
60
+ * @param {number} height - The height to check against the query.
61
+ * @returns {boolean} True if the height matches the query range, false otherwise.
62
+ *
63
+ * @example
64
+ * isWithinTheHeight('h[100,200]', 150) // returns true
65
+ * isWithinTheHeight('h[100]', 50) // returns false
66
+ * isWithinTheHeight('h[100]', 150) // returns true
67
+ */
68
+ export declare const isWithinTheHeight: (query: string, height: number) => boolean;
69
+ /**
70
+ * Determines if the given screen size matches both the specified width and height ranges in the query.
71
+ *
72
+ * The function checks if the provided screen size (both width and height) falls within the ranges
73
+ * specified by the query. The query can specify both width and height using a format like 'w[100,200]:h[300,400]'.
74
+ *
75
+ * @param {string} query - The combined width and height query string. Example: 'w[100,200]:h[300,400]'.
76
+ * @param {ScreenSize} screenSize - The screen size to check against the query.
77
+ * @returns {boolean} True if the screen size matches both the width and height ranges in the query, false otherwise.
78
+ *
79
+ * @example
80
+ * const screenSize = { width: 150, height: 350 }
81
+ * isWithinTheWidthAndHeight('w[100,200]:h[300,400]', screenSize) // returns true
82
+ * isWithinTheWidthAndHeight('w[100,200]:h[400,500]', screenSize) // returns false
83
+ */
84
+ export declare const isWithinTheWidthAndHeight: (query: string, screenSize: ScreenSize) => boolean;
85
+ /**
86
+ * Checks if the given query string is a valid custom media query.
87
+ *
88
+ * The valid custom media query formats include:
89
+ * - :w[200]
90
+ * - :w[0, 200]
91
+ * - :w[, 300]
92
+ * - :h[200]
93
+ * - :h[0, 500]
94
+ * - :h[,200]
95
+ * - :w[100, 300]:h[200,500]
96
+ * - :h[200,500]:w[100, 300]
97
+ *
98
+ * @param {string} query - The query string to be checked.
99
+ * @returns {boolean} Returns `true` if the query is a valid custom media query, otherwise `false`.
100
+ * @example
101
+ *
102
+ * isMediaQuery(':w[200]') // true
103
+ * isMediaQuery(':w100]') // false
104
+ */
105
+ export declare const isMediaQuery: (query: string) => boolean;
106
+ /**
107
+ * Retrieves the first matching custom media query key based on the provided screen size.
108
+ *
109
+ * The function processes an array of media queries and returns the first query that matches
110
+ * the given screen size. The media queries can be in formats like:
111
+ * - w[200]
112
+ * - w[0, 200]
113
+ * - w[, 300]
114
+ * - h[200]
115
+ * - h[0, 500]
116
+ * - h[,200]
117
+ * - w[100, 300]:h[200,500]
118
+ * - h[200,500]:w[100, 300]
119
+ *
120
+ * @param {Array<[string, string | number]>} mediaQueries - An array of tuples containing media query keys and associated values.
121
+ * @param {ScreenSize} screenSize - An object representing the screen size to be checked against the media queries.
122
+ * @returns {string | undefined} Returns the first matching media query key or `undefined` if no match is found.
123
+ * @example
124
+ *
125
+ * const queries = [[':w[200]', 'value1'], [':h[300,500]', 'value2']]
126
+ * const size = { width: 250, height: 400 }
127
+ * getKeyForCustomMediaQuery(queries, size) // ':w[200]
128
+ */
129
+ export declare const getKeyForCustomMediaQuery: (mediaQueries: Array<[string, string | number | undefined]>, screenSize: ScreenSize) => string | undefined;
130
+ //# sourceMappingURL=mediaQueries.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mediaQueries.d.ts","sourceRoot":"","sources":["../../../../src/utils/mediaQueries.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,UAAU,CAAA;AAE1C;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,aAAa,eAAgB,MAAM,KAAG,MAAM,MAAM,CAQ9D,CAAA;AAED;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,kBAAkB,UAAW,MAAM,cAAc,UAAU,KAAG,OAc1E,CAAA;AAED;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,gBAAgB,UAAW,MAAM,SAAS,MAAM,KAAG,OAQ/D,CAAA;AAED;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,iBAAiB,UAAW,MAAM,UAAU,MAAM,KAAG,OAQjE,CAAA;AAED;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,yBAAyB,UAAW,MAAM,cAAc,UAAU,KAAG,OAQjF,CAAA;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,eAAO,MAAM,YAAY,UAAW,MAAM,KAAG,OAI5C,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,eAAO,MAAM,yBAAyB,iBAAkB,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAAC,CAAC,cAAc,UAAU,KAAG,MAAM,GAAG,SAevI,CAAA"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=mediaQueries.spec.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mediaQueries.spec.d.ts","sourceRoot":"","sources":["../../../../src/utils/mediaQueries.spec.ts"],"names":[],"mappings":""}
@@ -0,0 +1,56 @@
1
+ import type { CustomNamedStyles, ScreenSize } from '../types';
2
+ /**
3
+ * Proxies a function to parse its return value for custom media queries or breakpoints.
4
+ *
5
+ * If the function's string representation contains a custom media query or a defined breakpoint,
6
+ * the returned function will be proxied to parse its return value based on the provided screen size and breakpoints.
7
+ * If neither is found, the original function is returned.
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 {B} breakpoints - An object representing the defined breakpoints.
15
+ *
16
+ * @returns {Function} Returns the proxified function or the original function if no custom media query or breakpoint is found in its string representation.
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 declare const proxifyFunction: <B extends Record<string, number>>(fn: Function, breakpoint: keyof B & string, screenSize: ScreenSize, breakpoints: B) => Function;
28
+ /**
29
+ * Parses a style object to resolve custom media queries or breakpoints based on the provided screen size and breakpoints.
30
+ *
31
+ * 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),
32
+ * it is returned as-is. Otherwise, the function attempts to resolve the value based on the provided breakpoint, screen size, and defined breakpoints.
33
+ *
34
+ * @template T - The type of the style object.
35
+ * @template B - An object type where keys represent breakpoint names and values represent breakpoint values.
36
+ *
37
+ * @param {CustomNamedStyles<T, B>} style - The style object to be parsed.
38
+ * @param {keyof B & string} breakpoint - The breakpoint name to check against.
39
+ * @param {ScreenSize} screenSize - An object representing the screen size to be checked against the media queries.
40
+ * @param {B} breakpoints - An object representing the defined breakpoints.
41
+ *
42
+ * @returns {Record<string, string | number | Function>} Returns the parsed style object with resolved custom media queries or breakpoints.
43
+ *
44
+ * @example
45
+ *
46
+ * const style = { fontSize: { sm: '12px', md: '16px' } }
47
+ * const screenSize = { width: 300, height: 400 }
48
+ * const breakpoints = { xs: 0, sm: 300, md: 600 }
49
+ *
50
+ * const parsedStyle = parseStyle(style, 'sm', screenSize, breakpoints)
51
+ * // { fontSize: '12px' }
52
+ */
53
+ export declare const parseStyle: <T, B extends Record<string, number>>(style: CustomNamedStyles<T, B>, breakpoint: keyof B & string, screenSize: ScreenSize, breakpoints: B) => {
54
+ [k: string]: unknown;
55
+ };
56
+ //# sourceMappingURL=styles.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"styles.d.ts","sourceRoot":"","sources":["../../../../src/utils/styles.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,UAAU,EAAE,MAAM,UAAU,CAAA;AAG7D;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,eAAO,MAAM,eAAe,yCACpB,QAAQ,4CACA,UAAU,qBAEvB,QAeF,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,eAAO,MAAM,UAAU,kHAGP,UAAU;;CAiBrB,CAAA"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=styles.spec.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"styles.spec.d.ts","sourceRoot":"","sources":["../../../../src/utils/styles.spec.ts"],"names":[],"mappings":""}
package/package.json ADDED
@@ -0,0 +1,132 @@
1
+ {
2
+ "name": "react-native-unistyles",
3
+ "version": "1.0.0-beta.1",
4
+ "description": "Level up your React Native StyleSheet",
5
+ "scripts": {
6
+ "test": "jest",
7
+ "tsc": "node_modules/typescript/bin/tsc --noEmit",
8
+ "lint": "eslint . --ext .ts,.tsx",
9
+ "clean": "del-cli lib",
10
+ "prepare": "husky install && bob build",
11
+ "precommit": "concurrently 'yarn tsc' 'yarn lint' 'yarn test'",
12
+ "release": "release-it patch --preRelease=beta"
13
+ },
14
+ "main": "lib/commonjs/index",
15
+ "module": "lib/module/index",
16
+ "types": "lib/typescript/src/index.d.ts",
17
+ "react-native": "src/index",
18
+ "source": "src/index",
19
+ "files": [
20
+ "src",
21
+ "lib",
22
+ "!**/*.spec.ts",
23
+ "!**/.*"
24
+ ],
25
+ "keywords": [
26
+ "react-native",
27
+ "ios",
28
+ "android"
29
+ ],
30
+ "repository": "https://github.com/jpudysz/react-native-unistyles",
31
+ "author": "Jacek Pudysz <jacekpudysz@gmail.com> (https://github.com/jpudysz)",
32
+ "license": "MIT",
33
+ "bugs": {
34
+ "url": "https://github.com/jpudysz/react-native-unistyles/issues"
35
+ },
36
+ "homepage": "https://github.com/jpudysz/react-native-unistyles#readme",
37
+ "publishConfig": {
38
+ "registry": "https://registry.npmjs.org/"
39
+ },
40
+ "devDependencies": {
41
+ "@commitlint/config-conventional": "17.7.0",
42
+ "@evilmartians/lefthook": "1.5.0",
43
+ "@react-native/eslint-config": "0.73.1",
44
+ "@release-it/conventional-changelog": "5.1.1",
45
+ "@types/jest": "29.5.5",
46
+ "@types/react": "18.2.24",
47
+ "@types/react-native": "0.72.3",
48
+ "@typescript-eslint/eslint-plugin": "6.7.4",
49
+ "@typescript-eslint/eslint-plugin-tslint": "6.7.4",
50
+ "@typescript-eslint/parser": "6.7.4",
51
+ "commitlint": "17.7.2",
52
+ "concurrently": "8.2.1",
53
+ "del-cli": "5.1.0",
54
+ "eslint": "8.50.0",
55
+ "eslint-config-codemask": "1.1.7",
56
+ "eslint-plugin-functional": "6.0.0",
57
+ "eslint-plugin-import": "2.28.1",
58
+ "eslint-plugin-jsdoc": "46.8.2",
59
+ "eslint-plugin-jsx-a11y": "6.7.1",
60
+ "eslint-plugin-nested-if": "1.0.0",
61
+ "eslint-plugin-no-else": "0.2.2",
62
+ "eslint-plugin-no-loops": "0.3.0",
63
+ "eslint-plugin-prefer-arrow": "1.2.3",
64
+ "eslint-plugin-react": "7.33.2",
65
+ "eslint-plugin-react-hooks": "4.6.0",
66
+ "husky": "8.0.3",
67
+ "jest": "29.7.0",
68
+ "react": "18.2.0",
69
+ "react-native": "0.72.5",
70
+ "react-native-builder-bob": "0.23.0",
71
+ "release-it": "16.2.1",
72
+ "typescript": "5.2.2"
73
+ },
74
+ "peerDependencies": {
75
+ "react": "*",
76
+ "react-native": "*"
77
+ },
78
+ "workspaces": [
79
+ "example"
80
+ ],
81
+ "packageManager": "yarn@3.6.1",
82
+ "engines": {
83
+ "node": ">= 18.0.0"
84
+ },
85
+ "jest": {
86
+ "preset": "react-native",
87
+ "modulePathIgnorePatterns": [
88
+ "<rootDir>/example/node_modules",
89
+ "<rootDir>/lib/"
90
+ ]
91
+ },
92
+ "commitlint": {
93
+ "extends": [
94
+ "@commitlint/config-conventional"
95
+ ]
96
+ },
97
+ "release-it": {
98
+ "git": {
99
+ "commitMessage": "chore: release ${version}",
100
+ "tagName": "v${version}"
101
+ },
102
+ "npm": {
103
+ "publish": true
104
+ },
105
+ "github": {
106
+ "release": true
107
+ },
108
+ "plugins": {
109
+ "@release-it/conventional-changelog": {
110
+ "preset": "angular"
111
+ }
112
+ }
113
+ },
114
+ "eslintIgnore": [
115
+ "node_modules/",
116
+ "lib/"
117
+ ],
118
+ "react-native-builder-bob": {
119
+ "source": "src",
120
+ "output": "lib",
121
+ "targets": [
122
+ "commonjs",
123
+ "module",
124
+ [
125
+ "typescript",
126
+ {
127
+ "project": "tsconfig.build.json"
128
+ }
129
+ ]
130
+ ]
131
+ }
132
+ }
@@ -0,0 +1,17 @@
1
+ import React, { createContext } from 'react'
2
+ import type { PropsWithChildren } from 'react'
3
+
4
+ interface UnistylesThemeProps extends PropsWithChildren {
5
+ theme: any
6
+ }
7
+
8
+ export const UnistylesContext = createContext({})
9
+
10
+ export const UnistylesTheme: React.FunctionComponent<UnistylesThemeProps> = ({
11
+ theme,
12
+ children
13
+ }) => (
14
+ <UnistylesContext.Provider value={theme}>
15
+ {children}
16
+ </UnistylesContext.Provider>
17
+ )
@@ -0,0 +1,56 @@
1
+ import { useContext } from 'react'
2
+ import { useWindowDimensions } from 'react-native'
3
+ import type { CreateStylesFactory, CustomNamedStyles, ScreenSize, ExtractBreakpoints, RemoveKeysWithPrefix } from './types'
4
+ import { UnistylesContext } from './UnistylesTheme'
5
+ import { getBreakpointFromScreenWidth, proxifyFunction, parseStyle, sortAndValidateBreakpoints } from './utils'
6
+
7
+ export const createUnistyles = <B extends Record<string, number>, T = {}>(breakpoints: B) => {
8
+ const sortedBreakpoints = sortAndValidateBreakpoints(breakpoints) as B
9
+
10
+ return {
11
+ createStyles: <S extends CustomNamedStyles<S, B>>(styles: S | CreateStylesFactory<S, T>) => styles as S,
12
+ useStyles: <ST extends CustomNamedStyles<ST, B>>(stylesheet?: ST | CreateStylesFactory<ST, T>) => {
13
+ const theme = useContext(UnistylesContext) as T
14
+ const dimensions = useWindowDimensions()
15
+ const breakpoint = getBreakpointFromScreenWidth<B>(dimensions.width, sortedBreakpoints)
16
+ const screenSize: ScreenSize = {
17
+ width: dimensions.width,
18
+ height: dimensions.height
19
+ }
20
+
21
+ if (!stylesheet) {
22
+ return {
23
+ theme,
24
+ styles: {} as ExtractBreakpoints<RemoveKeysWithPrefix<ST, B>, B>
25
+ }
26
+ }
27
+
28
+ const parsedStyles = typeof stylesheet === 'function'
29
+ ? stylesheet(theme)
30
+ : stylesheet
31
+
32
+ const dynamicStyleSheet = Object
33
+ .entries(parsedStyles)
34
+ .reduce((acc, [key, value]) => {
35
+ const x = value as CustomNamedStyles<ST, B>
36
+
37
+ if (typeof value === 'function') {
38
+ return {
39
+ ...acc,
40
+ [key]: proxifyFunction<B>(value, breakpoint, screenSize, sortedBreakpoints)
41
+ }
42
+ }
43
+
44
+ return {
45
+ ...acc,
46
+ [key]: parseStyle<ST, B>(x, breakpoint, screenSize, sortedBreakpoints)
47
+ }
48
+ }, {} as ST)
49
+
50
+ return {
51
+ theme,
52
+ styles: dynamicStyleSheet as ExtractBreakpoints<RemoveKeysWithPrefix<ST, B>, B>
53
+ }
54
+ }
55
+ }
56
+ }
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export { UnistylesTheme } from './UnistylesTheme'
2
+ export { createUnistyles } from './createUnistyles'
package/src/types.ts ADDED
@@ -0,0 +1,52 @@
1
+ import type { ImageStyle, TextStyle, TransformsStyle, ViewStyle } from 'react-native'
2
+ import type { CSSProperties } from 'react'
3
+
4
+ export type ScreenSize = {
5
+ width: number,
6
+ height: number
7
+ }
8
+
9
+ export type CreateStylesFactory<T, Theme> = (theme: Theme) => T
10
+
11
+ type StyleProperty<T, B extends Record<string, number>> = {
12
+ [key in keyof T]?: {
13
+ [innerKey in keyof B]?: T[key]
14
+ } | {
15
+ [innerKey in `:w${string}` | `:h${string}`]?: T[key]
16
+ } | T[key]
17
+ }
18
+
19
+ export type CustomNamedStyles<T, B extends Record<string, number>> = {
20
+ [P in keyof T]:
21
+ | ViewStyle
22
+ | TextStyle
23
+ | ImageStyle
24
+ | TransformsStyle
25
+ | CSSProperties
26
+ | StyleProperty<ViewStyle, B>
27
+ | StyleProperty<ImageStyle, B>
28
+ | StyleProperty<TextStyle, B>
29
+ | (
30
+ (...args: Array<never>) => ViewStyle | TextStyle | ImageStyle | TransformsStyle | CSSProperties | StyleProperty<ViewStyle, B> | StyleProperty<ImageStyle, B> | StyleProperty<TextStyle, B>
31
+ )
32
+ }
33
+
34
+ export type ExtractBreakpoints<T, B extends Record<string, number>> = T extends Partial<Record<keyof B & string, infer V>>
35
+ ? V
36
+ : T extends (...args: infer A) => infer R
37
+ ? (...args: A) => ExtractBreakpoints<R, B>
38
+ : {
39
+ [K in keyof T]: T[K] extends (...args: infer A) => infer R
40
+ ? (...args: A) => ExtractBreakpoints<R, B>
41
+ : T[K] extends object
42
+ ? ExtractBreakpoints<T[K], B>
43
+ : T[K]
44
+ }
45
+
46
+ export type RemoveKeysWithPrefix<T, B extends Record<string, number>> = T extends (...args: Array<any>) => infer R
47
+ ? (...args: Parameters<T>) => RemoveKeysWithPrefix<R, B>
48
+ : T extends object
49
+ ? T extends Record<string, infer _V>
50
+ ? { [K in keyof T as K extends `:w${string}` | `:h${string}` ? keyof B & string : K]: RemoveKeysWithPrefix<T[K], B> }
51
+ : { [K in keyof T]: RemoveKeysWithPrefix<T[K], B> }
52
+ : T