react-native-unistyles 1.0.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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
package/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Jacek Pudysz
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ of this software and associated documentation files (the "Software"), to deal
6
+ in the Software without restriction, including without limitation the rights
7
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the Software is
9
+ furnished to do so, subject to the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included in all
12
+ copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,243 @@
1
+ [<img alt="react-native-unistyles" src="assets/banner.png">](https://codemask.com)
2
+
3
+
4
+ <picture>
5
+ <source media="(prefers-color-scheme: dark)" srcset="assets/uni-dark.svg">
6
+ <img alt="react-native-unistyles" src="assets/uni-light.svg">
7
+ </picture>
8
+
9
+ ## Features
10
+ - ⚡ Blazing fast, adds around ~5ms on top of StyleSheet*
11
+ - 🎳 Share up to 100% of your styles across platforms in monorepo
12
+ - 🎯 Doesn't introduce new components
13
+ - 🖥️ Supports custom breakpoints and css-like media queries
14
+ - 🎨 Access theme in your StyleSheets and components
15
+ - 🪄 Supports dynamic functions to access values from JSX
16
+ - 🥳 Compatible with Expo, Expo Go, Bare React Native and React Native Web
17
+ - ⚔️ No 3rd party dependencies
18
+
19
+ *-based on this [benchmark](https://github.com/efstathiosntonas/react-native-style-libraries-benchmark)
20
+
21
+ ## Beta
22
+
23
+ This project is currently in its beta phase. While it hasn't reached version 1.0.0 yet, it's been tested and proven in a large-scale application, performing flawlessly across hundreds screens and components.
24
+
25
+ I'm looking for testers to check the typings, scalability and overall usability for your monorepo projects.
26
+
27
+ Suggestions, ideas, and potential improvements are always welcome!
28
+
29
+ ## Setup
30
+
31
+
32
+ **1. Install library**
33
+
34
+ ```cmd
35
+ yarn add react-native-unistyles
36
+ ```
37
+
38
+ **2. Define your theme**
39
+
40
+ You don't have to follow a specific format. Just make an object and add any keys/values you like.
41
+
42
+ ```ts
43
+ // theme.ts
44
+ export const theme = {
45
+ colors: {
46
+ blood: '#eb4d4b',
47
+ barbie: '#e056fd',
48
+ pumpkin: '#f0932b',
49
+ background: '#ffffff'
50
+ },
51
+ components: {
52
+ typography: {
53
+ bold: {
54
+ fontWeight: 'bold'
55
+ },
56
+ thin: {
57
+ fontWeight: '300'
58
+ }
59
+ }
60
+ },
61
+ margins: {
62
+ sm: 2,
63
+ md: 4,
64
+ lg: 8,
65
+ xl: 12
66
+ },
67
+ utils: {
68
+ hexToRGBA: (hex: string, opacity: number) => {
69
+ const rgb = hex
70
+ .replace('#', '')
71
+ .split(/(?=(?:..)*$)/)
72
+ .map(x => parseInt(x, 16))
73
+ return `rgba(${rgb.at(0)}, ${rgb.at(1)}, ${rgb.at(2)}, ${opacity})`
74
+ }
75
+ }
76
+ }
77
+ ```
78
+ **3. Create breakpoints**
79
+
80
+ There are no predefined breakpoints. You can name them anything. Just make an object with string keys and number values.
81
+
82
+ ```ts
83
+ // breakpoints.ts
84
+ export const breakpoints = {
85
+ xs: 0,
86
+ sm: 576,
87
+ md: 768,
88
+ lg: 992,
89
+ xl: 1200,
90
+ superLarge: 2000,
91
+ tvLike: 4000
92
+ }
93
+ ```
94
+
95
+ **4. Wrap your app with UnistylesTheme to inject theme**
96
+
97
+ ```tsx
98
+ import React from 'react'
99
+ import { UnistylesTheme } from 'react-native-unistyles'
100
+ import { theme } from './theme'
101
+
102
+ export const App: React.FunctionComponent = () => (
103
+ <UnistylesTheme theme={theme}>
104
+ // Your App
105
+ </UnistylesTheme>
106
+ )
107
+ ```
108
+
109
+ **5. Access createStyles and useStyles with a factory**
110
+
111
+ ```ts
112
+ // styles.ts
113
+
114
+ // import library factory
115
+ import { createUnistyles } from 'react-native-unistyles'
116
+ // import your breakpoints, add whatever keys and numeric values you want
117
+ import { breakpoints } from './breakpoints'
118
+ // import your app's theme TypeScript type, or simply use 'typeof theme'
119
+ import { theme } from './theme'
120
+
121
+ export const {
122
+ createStyles,
123
+ useStyles,
124
+ } = createUnistyles<typeof breakpoints, typeof theme>(breakpoints)
125
+ ```
126
+
127
+ ## Basic Usage
128
+
129
+ Library gives you two functions from the factory:
130
+ - `createStyles` which replaces `StyleSheet.create`
131
+ - `useStyles` which parses your styles based on screen height, width and theme
132
+
133
+ ```tsx
134
+ import React from 'react'
135
+ import { View, Text } from 'react-native'
136
+ // access createStyles and useStyles exported from factory
137
+ import { createStyles, useStyles } from 'lib/styles'
138
+
139
+ export const ExampleUnistyles = () => {
140
+ const { styles } = useStyles(stylesheet)
141
+
142
+ return (
143
+ <View style={styles.container}>
144
+ <Text style={styles.text}>
145
+ Unistyles example
146
+ </Text>
147
+ </View>
148
+ )
149
+ }
150
+
151
+ const stylesheet = createStyles(theme => ({
152
+ container: {
153
+ flex: 1,
154
+ justifyContent: 'center',
155
+ alignItems: 'center',
156
+ backgroundColor: theme.colors.background
157
+ },
158
+ text: {
159
+ color: theme.colors.typography
160
+ }
161
+ }))
162
+ ```
163
+
164
+ `createStyles` takes an object like `StyleSheet.create` or function that injects your theme
165
+
166
+ `useStyles` hook takes a `stylesheet` and returns an object with two keys:
167
+ - `styles` - parsed styles that can be used directly in React Native components
168
+ - `theme` - your app's theme that can be used in JSX
169
+
170
+ You can also skip `stylesheet` if you just want to access `theme`:
171
+
172
+ ```tsx
173
+ const { theme } = useStyles()
174
+ ```
175
+
176
+ ## Breakpoints
177
+
178
+ Any style can change based on breakpoints. To do this, change a value to an object:
179
+
180
+ ```ts
181
+ const stylesheet = createStyles(theme => ({
182
+ container: {
183
+ flex: 1,
184
+ justifyContent: 'center',
185
+ alignItems: 'center',
186
+ backgroundColor: {
187
+ // your breakpoints
188
+ xs: theme.colors.background,
189
+ sm: theme.colors.barbie
190
+ }
191
+ },
192
+ text: {
193
+ color: theme.colors.typography
194
+ }
195
+ }))
196
+ ```
197
+
198
+ Library will choose the correct value (based on screen width) in the runtime.
199
+
200
+ ## Media queries
201
+
202
+ For more advanced usage and pixel perfect designs you can also use a custom media queries. Library supports 4 types of media queries (w-width, h-height):
203
+
204
+ ```ts
205
+ :w[200, 500] - with upper and lower bounds, it translates to width from 200-500px
206
+ :w[, 800] - with upper bound only, it's equal to width from 0-800px
207
+ :h[400] - lower bound only, it means height from 400px
208
+ :h[200, 300]:w[500] - combined queries for both width and height
209
+ ```
210
+
211
+ Media queries can be mixed with breakpoints, but have a bigger priority:
212
+
213
+ ```tsx
214
+ const stylesheet = createStyles(theme => ({
215
+ container: {
216
+ justifyContent: 'center',
217
+ alignItems: 'center',
218
+ flexDirection: {
219
+ xs: 'column',
220
+ sm: 'row',
221
+ },
222
+ backgroundColor: {
223
+ xs: theme.colors.background,
224
+ ':w[600]': theme.colors.barbie
225
+ }
226
+ },
227
+ text: {
228
+ color: theme.colors.typography
229
+ }
230
+ }))
231
+ ```
232
+
233
+ ## Example
234
+
235
+ In order to check out working example go to [example/](./example).
236
+
237
+ ## Blog post
238
+
239
+ For more detailed explanation please refer to my blog post [here](https://www.reactnativecrossroads.com/posts/level-up-react-native-styles).
240
+
241
+ ## License
242
+
243
+ MIT
@@ -0,0 +1,21 @@
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(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
9
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
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
@@ -0,0 +1 @@
1
+ {"version":3,"names":["_react","_interopRequireWildcard","require","_getRequireWildcardCache","nodeInterop","WeakMap","cacheBabelInterop","cacheNodeInterop","obj","__esModule","default","cache","has","get","newObj","hasPropertyDescriptor","Object","defineProperty","getOwnPropertyDescriptor","key","prototype","hasOwnProperty","call","desc","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,WAAA,eAAAC,OAAA,kCAAAC,iBAAA,OAAAD,OAAA,QAAAE,gBAAA,OAAAF,OAAA,YAAAF,wBAAA,YAAAA,CAAAC,WAAA,WAAAA,WAAA,GAAAG,gBAAA,GAAAD,iBAAA,KAAAF,WAAA;AAAA,SAAAH,wBAAAO,GAAA,EAAAJ,WAAA,SAAAA,WAAA,IAAAI,GAAA,IAAAA,GAAA,CAAAC,UAAA,WAAAD,GAAA,QAAAA,GAAA,oBAAAA,GAAA,wBAAAA,GAAA,4BAAAE,OAAA,EAAAF,GAAA,UAAAG,KAAA,GAAAR,wBAAA,CAAAC,WAAA,OAAAO,KAAA,IAAAA,KAAA,CAAAC,GAAA,CAAAJ,GAAA,YAAAG,KAAA,CAAAE,GAAA,CAAAL,GAAA,SAAAM,MAAA,WAAAC,qBAAA,GAAAC,MAAA,CAAAC,cAAA,IAAAD,MAAA,CAAAE,wBAAA,WAAAC,GAAA,IAAAX,GAAA,QAAAW,GAAA,kBAAAH,MAAA,CAAAI,SAAA,CAAAC,cAAA,CAAAC,IAAA,CAAAd,GAAA,EAAAW,GAAA,SAAAI,IAAA,GAAAR,qBAAA,GAAAC,MAAA,CAAAE,wBAAA,CAAAV,GAAA,EAAAW,GAAA,cAAAI,IAAA,KAAAA,IAAA,CAAAV,GAAA,IAAAU,IAAA,CAAAC,GAAA,KAAAR,MAAA,CAAAC,cAAA,CAAAH,MAAA,EAAAK,GAAA,EAAAI,IAAA,YAAAT,MAAA,CAAAK,GAAA,IAAAX,GAAA,CAAAW,GAAA,SAAAL,MAAA,CAAAJ,OAAA,GAAAF,GAAA,MAAAG,KAAA,IAAAA,KAAA,CAAAa,GAAA,CAAAhB,GAAA,EAAAM,MAAA,YAAAA,MAAA;AAOrC,MAAMW,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,oBACG7B,MAAA,CAAAU,OAAA,CAAAsB,aAAA,CAACP,gBAAgB,CAACQ,QAAQ;IAACC,KAAK,EAAEJ;EAAM,GACnCC,QACsB,CAAC;AAAA,CAC/B;AAAAL,OAAA,CAAAE,cAAA,GAAAA,cAAA"}
@@ -0,0 +1,52 @@
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 _utils = require("./utils");
11
+ const createUnistyles = breakpoints => {
12
+ const sortedBreakpoints = (0, _utils.sortAndValidateBreakpoints)(breakpoints);
13
+ return {
14
+ createStyles: styles => styles,
15
+ useStyles: stylesheet => {
16
+ const theme = (0, _react.useContext)(_UnistylesTheme.UnistylesContext);
17
+ const dimensions = (0, _reactNative.useWindowDimensions)();
18
+ const breakpoint = (0, _utils.getBreakpointFromScreenWidth)(dimensions.width, sortedBreakpoints);
19
+ const screenSize = {
20
+ width: dimensions.width,
21
+ height: dimensions.height
22
+ };
23
+ if (!stylesheet) {
24
+ return {
25
+ theme,
26
+ styles: {}
27
+ };
28
+ }
29
+ const parsedStyles = typeof stylesheet === 'function' ? stylesheet(theme) : stylesheet;
30
+ const dynamicStyleSheet = Object.entries(parsedStyles).reduce((acc, _ref) => {
31
+ let [key, value] = _ref;
32
+ const x = value;
33
+ if (typeof value === 'function') {
34
+ return {
35
+ ...acc,
36
+ [key]: (0, _utils.proxifyFunction)(value, breakpoint, screenSize, sortedBreakpoints)
37
+ };
38
+ }
39
+ return {
40
+ ...acc,
41
+ [key]: (0, _utils.parseStyle)(x, breakpoint, screenSize, sortedBreakpoints)
42
+ };
43
+ }, {});
44
+ return {
45
+ theme,
46
+ styles: dynamicStyleSheet
47
+ };
48
+ }
49
+ };
50
+ };
51
+ exports.createUnistyles = createUnistyles;
52
+ //# sourceMappingURL=createUnistyles.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["_react","require","_reactNative","_UnistylesTheme","_utils","createUnistyles","breakpoints","sortedBreakpoints","sortAndValidateBreakpoints","createStyles","styles","useStyles","stylesheet","theme","useContext","UnistylesContext","dimensions","useWindowDimensions","breakpoint","getBreakpointFromScreenWidth","width","screenSize","height","parsedStyles","dynamicStyleSheet","Object","entries","reduce","acc","_ref","key","value","x","proxifyFunction","parseStyle","exports"],"sourceRoot":"../../src","sources":["createUnistyles.ts"],"mappings":";;;;;;AAAA,IAAAA,MAAA,GAAAC,OAAA;AACA,IAAAC,YAAA,GAAAD,OAAA;AAEA,IAAAE,eAAA,GAAAF,OAAA;AACA,IAAAG,MAAA,GAAAH,OAAA;AAEO,MAAMI,eAAe,GAA8CC,WAAc,IAAK;EACzF,MAAMC,iBAAiB,GAAG,IAAAC,iCAA0B,EAACF,WAAW,CAAM;EAEtE,OAAO;IACHG,YAAY,EAAsCC,MAAqC,IAAKA,MAAW;IACvGC,SAAS,EAAwCC,UAA4C,IAAK;MAC9F,MAAMC,KAAK,GAAG,IAAAC,iBAAU,EAACC,gCAAgB,CAAM;MAC/C,MAAMC,UAAU,GAAG,IAAAC,gCAAmB,EAAC,CAAC;MACxC,MAAMC,UAAU,GAAG,IAAAC,mCAA4B,EAAIH,UAAU,CAACI,KAAK,EAAEb,iBAAiB,CAAC;MACvF,MAAMc,UAAsB,GAAG;QAC3BD,KAAK,EAAEJ,UAAU,CAACI,KAAK;QACvBE,MAAM,EAAEN,UAAU,CAACM;MACvB,CAAC;MAED,IAAI,CAACV,UAAU,EAAE;QACb,OAAO;UACHC,KAAK;UACLH,MAAM,EAAE,CAAC;QACb,CAAC;MACL;MAEA,MAAMa,YAAY,GAAG,OAAOX,UAAU,KAAK,UAAU,GAC/CA,UAAU,CAACC,KAAK,CAAC,GACjBD,UAAU;MAEhB,MAAMY,iBAAiB,GAAGC,MAAM,CAC3BC,OAAO,CAACH,YAAY,CAAC,CACrBI,MAAM,CAAC,CAACC,GAAG,EAAAC,IAAA,KAAmB;QAAA,IAAjB,CAACC,GAAG,EAAEC,KAAK,CAAC,GAAAF,IAAA;QACtB,MAAMG,CAAC,GAAGD,KAAiC;QAE3C,IAAI,OAAOA,KAAK,KAAK,UAAU,EAAE;UAC7B,OAAO;YACH,GAAGH,GAAG;YACN,CAACE,GAAG,GAAG,IAAAG,sBAAe,EAAIF,KAAK,EAAEb,UAAU,EAAEG,UAAU,EAAEd,iBAAiB;UAC9E,CAAC;QACL;QAEA,OAAO;UACH,GAAGqB,GAAG;UACN,CAACE,GAAG,GAAG,IAAAI,iBAAU,EAAQF,CAAC,EAAEd,UAAU,EAAEG,UAAU,EAAEd,iBAAiB;QACzE,CAAC;MACL,CAAC,EAAE,CAAC,CAAO,CAAC;MAEhB,OAAO;QACHM,KAAK;QACLH,MAAM,EAAEc;MACZ,CAAC;IACL;EACJ,CAAC;AACL,CAAC;AAAAW,OAAA,CAAA9B,eAAA,GAAAA,eAAA"}
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ Object.defineProperty(exports, "UnistylesTheme", {
7
+ enumerable: true,
8
+ get: function () {
9
+ return _UnistylesTheme.UnistylesTheme;
10
+ }
11
+ });
12
+ Object.defineProperty(exports, "createUnistyles", {
13
+ enumerable: true,
14
+ get: function () {
15
+ return _createUnistyles.createUnistyles;
16
+ }
17
+ });
18
+ var _UnistylesTheme = require("./UnistylesTheme");
19
+ var _createUnistyles = require("./createUnistyles");
20
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["_UnistylesTheme","require","_createUnistyles"],"sourceRoot":"../../src","sources":["index.ts"],"mappings":";;;;;;;;;;;;;;;;;AAAA,IAAAA,eAAA,GAAAC,OAAA;AACA,IAAAC,gBAAA,GAAAD,OAAA"}
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":[],"sourceRoot":"../../src","sources":["types.ts"],"mappings":""}
@@ -0,0 +1,139 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.sortAndValidateBreakpoints = exports.getValueForBreakpoint = exports.getBreakpointFromScreenWidth = void 0;
7
+ var _common = require("./common");
8
+ var _mediaQueries = require("./mediaQueries");
9
+ /**
10
+ * Sorts the breakpoints object based on its numeric values in ascending order and validates them.
11
+ *
12
+ * This function takes an object where keys represent breakpoint names and values are numeric.
13
+ * It returns a new object with the same keys but sorted based on their corresponding numeric values.
14
+ * Additionally, it validates that:
15
+ * 1. The first breakpoint starts with a value of 0.
16
+ * 2. No duplicate breakpoint values exist.
17
+ *
18
+ * If the validation fails, appropriate error messages are logged to the console.
19
+ *
20
+ * @template B - An object type where keys are strings and values are numbers.
21
+ * @param {B} breakpoints - The breakpoints object to be sorted and validated.
22
+ * @returns {B} A new object with sorted and validated breakpoints.
23
+ *
24
+ * @example
25
+ * const input = { md: 768, lg: 1024, sm: 0 }
26
+ * sortAndValidateBreakpoints(input) // returns { sm: 0, md: 768, lg: 1024 }
27
+ */
28
+ const sortAndValidateBreakpoints = breakpoints => {
29
+ const sortedPairs = Object.entries(breakpoints).sort((breakpoint1, breakpoint2) => {
30
+ const [, value1] = breakpoint1;
31
+ const [, value2] = breakpoint2;
32
+ return value1 - value2;
33
+ });
34
+ const sortedBreakpoints = Object.freeze(Object.fromEntries(sortedPairs));
35
+ const breakpointValues = Object.values(sortedBreakpoints);
36
+ const [firstBreakpoint] = breakpointValues;
37
+ if (firstBreakpoint !== 0) {
38
+ (0, _common.throwError)('first breakpoint must start with 0');
39
+ }
40
+ if (breakpointValues.length !== new Set(breakpointValues).size) {
41
+ (0, _common.throwError)('breakpoint values are duplicated');
42
+ }
43
+ return sortedBreakpoints;
44
+ };
45
+
46
+ /**
47
+ * Determines the appropriate breakpoint key for a given screen width based on provided breakpoints.
48
+ *
49
+ * This function takes a screen width and an object of breakpoints. It returns the key of the breakpoint
50
+ * that the screen width falls into. The breakpoints are assumed to be sorted in ascending order.
51
+ *
52
+ * @template B - An object type where keys are strings and values are numbers representing screen widths.
53
+ * @param {number} width - The screen width to determine the breakpoint for.
54
+ * @param {B} breakpoints - The breakpoints object to use for determination.
55
+ * @returns {keyof B & string} The key of the breakpoint that the screen width falls into.
56
+ *
57
+ * @example
58
+ * const breakpoints = { sm: 0, md: 768, lg: 1024 }
59
+ * getBreakpointFromScreenWidth(800, breakpoints) // returns 'md'
60
+ */
61
+ exports.sortAndValidateBreakpoints = sortAndValidateBreakpoints;
62
+ const getBreakpointFromScreenWidth = (width, breakpoints) => {
63
+ const [key] = Object.entries(breakpoints).find((_ref, index, otherBreakpoints) => {
64
+ let [, value] = _ref;
65
+ const minVal = value;
66
+ const maxVal = otherBreakpoints[index + 1]?.[1];
67
+ if (!maxVal) {
68
+ return true;
69
+ }
70
+ return width >= minVal && width < maxVal;
71
+ });
72
+ return key;
73
+ };
74
+
75
+ /**
76
+ * Retrieves the value associated with a given breakpoint or custom media query based on the provided screen size.
77
+ *
78
+ * The function first checks for custom media queries. If a matching custom media query is found, its associated value is returned.
79
+ * If no custom media query matches, the function then checks for a direct breakpoint match.
80
+ * If there's no direct breakpoint match, the function simulates CSS cascading to find the closest matching breakpoint.
81
+ *
82
+ * @template B - An object type where keys represent breakpoint names and values represent breakpoint values.
83
+ *
84
+ * @param {Record<keyof B & string, string | number>} value - An object containing values associated with breakpoints or custom media queries.
85
+ * @param {keyof B & string} breakpoint - The breakpoint name to check against.
86
+ * @param {ScreenSize} screenSize - An object representing the screen size to be checked against the media queries.
87
+ * @param {B} breakpoints - An object representing the defined breakpoints.
88
+ *
89
+ * @returns {string | number | undefined} Returns the value associated with the matching breakpoint or custom media query, or `undefined` if no match is found.
90
+ *
91
+ * @example
92
+ *
93
+ * const values = { ':w[200]': 'value1', sm: 'value2', md: 'value3' }
94
+ * const screenSize = { width: 250, height: 400 }
95
+ * const breakpoints = { sm: 300, md: 600, lg: 900 }
96
+ *
97
+ * getValueForBreakpoint(values, 'sm', screenSize, breakpoints); // 'value1'
98
+ */
99
+ exports.getBreakpointFromScreenWidth = getBreakpointFromScreenWidth;
100
+ const getValueForBreakpoint = (value, breakpoint, screenSize, breakpoints) => {
101
+ // the highest priority is for custom media queries
102
+ const customMediaQueries = Object.entries(value).filter(_ref2 => {
103
+ let [key] = _ref2;
104
+ return (0, _mediaQueries.isMediaQuery)(key);
105
+ });
106
+ const customMediaQueryKey = (0, _mediaQueries.getKeyForCustomMediaQuery)(customMediaQueries, screenSize);
107
+ if (customMediaQueryKey && customMediaQueryKey in value) {
108
+ return value[customMediaQueryKey];
109
+ }
110
+
111
+ // if no custom media query, or didn't match, proceed with defined breakpoints
112
+ const unifiedKey = breakpoint.toLowerCase();
113
+ const directBreakpoint = value[unifiedKey];
114
+
115
+ // if there is a direct key like 'sm' or 'md', or value for this key exists but its undefined
116
+ if (directBreakpoint || unifiedKey in value) {
117
+ return directBreakpoint;
118
+ }
119
+
120
+ // there is no direct hit for breakpoint nor media-query, so let's simulate CSS cascading
121
+ const allBreakpoints = Object.entries(breakpoints).map(_ref3 => {
122
+ let [key, bpValue] = _ref3;
123
+ return [key.toLowerCase(), bpValue];
124
+ });
125
+ const currentBreakpoint = allBreakpoints.findIndex(_ref4 => {
126
+ let [key] = _ref4;
127
+ return key === unifiedKey;
128
+ });
129
+ const availableBreakpoints = allBreakpoints.filter((_ref5, index) => {
130
+ let [key] = _ref5;
131
+ return index < currentBreakpoint && key && key in value;
132
+ }).map(_ref6 => {
133
+ let [key] = _ref6;
134
+ return key;
135
+ });
136
+ return allBreakpoints.length > 0 ? value[availableBreakpoints[availableBreakpoints.length - 1]] : undefined;
137
+ };
138
+ exports.getValueForBreakpoint = getValueForBreakpoint;
139
+ //# sourceMappingURL=breakpoints.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["_common","require","_mediaQueries","sortAndValidateBreakpoints","breakpoints","sortedPairs","Object","entries","sort","breakpoint1","breakpoint2","value1","value2","sortedBreakpoints","freeze","fromEntries","breakpointValues","values","firstBreakpoint","throwError","length","Set","size","exports","getBreakpointFromScreenWidth","width","key","find","_ref","index","otherBreakpoints","value","minVal","maxVal","getValueForBreakpoint","breakpoint","screenSize","customMediaQueries","filter","_ref2","isMediaQuery","customMediaQueryKey","getKeyForCustomMediaQuery","unifiedKey","toLowerCase","directBreakpoint","allBreakpoints","map","_ref3","bpValue","currentBreakpoint","findIndex","_ref4","availableBreakpoints","_ref5","_ref6","undefined"],"sourceRoot":"../../../src","sources":["utils/breakpoints.ts"],"mappings":";;;;;;AAAA,IAAAA,OAAA,GAAAC,OAAA;AAEA,IAAAC,aAAA,GAAAD,OAAA;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,MAAME,0BAA0B,GAAsCC,WAAc,IAAQ;EAC/F,MAAMC,WAAW,GAAGC,MAAM,CACrBC,OAAO,CAACH,WAAW,CAAC,CACpBI,IAAI,CAAC,CAACC,WAAW,EAAEC,WAAW,KAAK;IAChC,MAAM,GA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