react-native-unistyles 1.0.0-beta.3 → 1.0.0-beta.5
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +342 -35
- package/lib/commonjs/createUnistyles.js +25 -13
- package/lib/commonjs/createUnistyles.js.map +1 -1
- package/lib/commonjs/mediaQueries.js +2 -0
- package/lib/commonjs/mediaQueries.js.map +1 -0
- package/lib/commonjs/utils/breakpoints.js +12 -16
- package/lib/commonjs/utils/breakpoints.js.map +1 -1
- package/lib/commonjs/utils/breakpoints.spec.js +20 -15
- package/lib/commonjs/utils/breakpoints.spec.js.map +1 -1
- package/lib/commonjs/utils/common.js +8 -1
- package/lib/commonjs/utils/common.js.map +1 -1
- package/lib/commonjs/utils/index.js +7 -0
- package/lib/commonjs/utils/index.js.map +1 -1
- package/lib/commonjs/utils/mediaQueries.spec.js +1 -1
- package/lib/commonjs/utils/mediaQueries.spec.js.map +1 -1
- package/lib/commonjs/utils/normalizeStyles.js +10 -0
- package/lib/commonjs/utils/normalizeStyles.js.map +1 -0
- package/lib/commonjs/utils/normalizeStyles.web.js +59 -0
- package/lib/commonjs/utils/normalizeStyles.web.js.map +1 -0
- package/lib/commonjs/utils/styles.js +14 -12
- package/lib/commonjs/utils/styles.js.map +1 -1
- package/lib/commonjs/utils/styles.spec.js +17 -7
- package/lib/commonjs/utils/styles.spec.js.map +1 -1
- package/lib/module/createUnistyles.js +25 -13
- package/lib/module/createUnistyles.js.map +1 -1
- package/lib/module/mediaQueries.js +2 -0
- package/lib/module/mediaQueries.js.map +1 -0
- package/lib/module/utils/breakpoints.js +12 -16
- package/lib/module/utils/breakpoints.js.map +1 -1
- package/lib/module/utils/breakpoints.spec.js +20 -15
- package/lib/module/utils/breakpoints.spec.js.map +1 -1
- package/lib/module/utils/common.js +5 -0
- package/lib/module/utils/common.js.map +1 -1
- package/lib/module/utils/index.js +1 -0
- package/lib/module/utils/index.js.map +1 -1
- package/lib/module/utils/mediaQueries.spec.js +1 -1
- package/lib/module/utils/mediaQueries.spec.js.map +1 -1
- package/lib/module/utils/normalizeStyles.js +3 -0
- package/lib/module/utils/normalizeStyles.js.map +1 -0
- package/lib/module/utils/normalizeStyles.web.js +52 -0
- package/lib/module/utils/normalizeStyles.web.js.map +1 -0
- package/lib/module/utils/styles.js +14 -12
- package/lib/module/utils/styles.js.map +1 -1
- package/lib/module/utils/styles.spec.js +17 -7
- package/lib/module/utils/styles.spec.js.map +1 -1
- package/lib/typescript/src/createUnistyles.d.ts +7 -3
- package/lib/typescript/src/createUnistyles.d.ts.map +1 -1
- package/lib/typescript/src/mediaQueries.d.ts +2 -0
- package/lib/typescript/src/mediaQueries.d.ts.map +1 -0
- package/lib/typescript/src/types.d.ts +53 -13
- package/lib/typescript/src/types.d.ts.map +1 -1
- package/lib/typescript/src/utils/breakpoints.d.ts +6 -6
- package/lib/typescript/src/utils/breakpoints.d.ts.map +1 -1
- package/lib/typescript/src/utils/common.d.ts +2 -0
- package/lib/typescript/src/utils/common.d.ts.map +1 -1
- package/lib/typescript/src/utils/index.d.ts +1 -0
- package/lib/typescript/src/utils/index.d.ts.map +1 -1
- package/lib/typescript/src/utils/normalizeStyles.d.ts +2 -0
- package/lib/typescript/src/utils/normalizeStyles.d.ts.map +1 -0
- package/lib/typescript/src/utils/normalizeStyles.web.d.ts +2 -0
- package/lib/typescript/src/utils/normalizeStyles.web.d.ts.map +1 -0
- package/lib/typescript/src/utils/styles.d.ts +5 -5
- package/lib/typescript/src/utils/styles.d.ts.map +1 -1
- package/package.json +9 -2
- package/src/createUnistyles.ts +38 -16
- package/src/mediaQueries.ts +33 -0
- package/src/types.ts +96 -25
- package/src/utils/breakpoints.ts +15 -15
- package/src/utils/common.ts +8 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/normalizeStyles.ts +2 -0
- package/src/utils/normalizeStyles.web.ts +103 -0
- package/src/utils/styles.ts +32 -17
package/README.md
CHANGED
@@ -28,7 +28,6 @@ Suggestions, ideas, and potential improvements are always welcome!
|
|
28
28
|
|
29
29
|
## Setup
|
30
30
|
|
31
|
-
|
32
31
|
**1. Install library**
|
33
32
|
|
34
33
|
```cmd
|
@@ -39,6 +38,26 @@ yarn add react-native-unistyles
|
|
39
38
|
|
40
39
|
You don't have to follow a specific format. Just make an object and add any keys/values you like.
|
41
40
|
|
41
|
+
```ts
|
42
|
+
// theme.ts
|
43
|
+
export const theme = {
|
44
|
+
colors: {
|
45
|
+
blood: '#eb4d4b',
|
46
|
+
barbie: '#e056fd',
|
47
|
+
pumpkin: '#f0932b',
|
48
|
+
background: '#ffffff'
|
49
|
+
},
|
50
|
+
margins: {
|
51
|
+
sm: 2,
|
52
|
+
md: 4,
|
53
|
+
lg: 8,
|
54
|
+
xl: 12
|
55
|
+
}
|
56
|
+
}
|
57
|
+
```
|
58
|
+
|
59
|
+
or something more advanced with nested objects / functions:
|
60
|
+
|
42
61
|
```ts
|
43
62
|
// theme.ts
|
44
63
|
export const theme = {
|
@@ -106,7 +125,7 @@ export const App: React.FunctionComponent = () => (
|
|
106
125
|
)
|
107
126
|
```
|
108
127
|
|
109
|
-
**5. Access
|
128
|
+
**5. Access createStyleSheet and useStyles with a factory**
|
110
129
|
|
111
130
|
```ts
|
112
131
|
// styles.ts
|
@@ -119,22 +138,22 @@ import { breakpoints } from './breakpoints'
|
|
119
138
|
import { theme } from './theme'
|
120
139
|
|
121
140
|
export const {
|
122
|
-
|
141
|
+
createStyleSheet,
|
123
142
|
useStyles,
|
124
143
|
} = createUnistyles<typeof breakpoints, typeof theme>(breakpoints)
|
125
144
|
```
|
126
145
|
|
127
146
|
## Basic Usage
|
128
147
|
|
129
|
-
|
130
|
-
- `
|
131
|
-
- `useStyles` which parses your styles
|
148
|
+
After the initial setup, you only need to focus on two functions responsible for your styles:
|
149
|
+
- `createStyleSheet` which replaces `StyleSheet.create`
|
150
|
+
- `useStyles` which parses your styles and ensures TypeScript compatibility with media queries and breakpoints
|
132
151
|
|
133
152
|
```tsx
|
134
153
|
import React from 'react'
|
135
154
|
import { View, Text } from 'react-native'
|
136
|
-
// access
|
137
|
-
import {
|
155
|
+
// access createStyleSheet and useStyles exported from factory
|
156
|
+
import { createStyleSheet, useStyles } from 'lib/styles'
|
138
157
|
|
139
158
|
export const ExampleUnistyles = () => {
|
140
159
|
const { styles } = useStyles(stylesheet)
|
@@ -148,7 +167,7 @@ export const ExampleUnistyles = () => {
|
|
148
167
|
)
|
149
168
|
}
|
150
169
|
|
151
|
-
const stylesheet =
|
170
|
+
const stylesheet = createStyleSheet(theme => ({
|
152
171
|
container: {
|
153
172
|
flex: 1,
|
154
173
|
justifyContent: 'center',
|
@@ -161,24 +180,82 @@ const stylesheet = createStyles(theme => ({
|
|
161
180
|
}))
|
162
181
|
```
|
163
182
|
|
164
|
-
|
183
|
+
## createStyleSheet
|
184
|
+
|
185
|
+
`createStyleSheet` is interchangeable with `StyleSheet.create`. You can use objects, and it will function identically to its React Native counterpart.
|
186
|
+
|
187
|
+
```ts
|
188
|
+
const stylesheet = createStyleSheet({
|
189
|
+
container: {
|
190
|
+
flex: 1,
|
191
|
+
justifyContent: 'center',
|
192
|
+
alignItems: 'center',
|
193
|
+
},
|
194
|
+
})
|
195
|
+
```
|
196
|
+
The difference is that you can now use breakpoints and media queries:
|
197
|
+
|
198
|
+
```ts
|
199
|
+
const stylesheet = createStyleSheet({
|
200
|
+
container: {
|
201
|
+
flex: 1,
|
202
|
+
justifyContent: 'center',
|
203
|
+
alignItems: 'center',
|
204
|
+
flexDirection: {
|
205
|
+
xs: 'row',
|
206
|
+
sm: 'column',
|
207
|
+
':w[800]': 'row'
|
208
|
+
}
|
209
|
+
},
|
210
|
+
})
|
211
|
+
```
|
212
|
+
|
213
|
+
`createStyleSheet` also accepts a function, to which the library will inject your theme:
|
214
|
+
|
215
|
+
```ts
|
216
|
+
const stylesheet = createStyleSheet(theme => ({
|
217
|
+
container: {
|
218
|
+
flex: 1,
|
219
|
+
justifyContent: 'center',
|
220
|
+
alignItems: 'center',
|
221
|
+
flexDirection: {
|
222
|
+
xs: 'row',
|
223
|
+
sm: 'column',
|
224
|
+
':w[800]': 'row'
|
225
|
+
},
|
226
|
+
backgroundColor: theme.colors.background
|
227
|
+
},
|
228
|
+
}))
|
229
|
+
```
|
230
|
+
|
231
|
+
Importantly, you'll receive the same TypeScript hints as with `StyleSheet.create`!
|
165
232
|
|
166
|
-
|
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
|
233
|
+
## useStyles
|
169
234
|
|
170
|
-
|
235
|
+
`useStyle` ties everything together and handles the heavy lifting. Without `useStyles`, you can't utilize features like:
|
236
|
+
- breakpoints
|
237
|
+
- media queries
|
238
|
+
- themes
|
239
|
+
|
240
|
+
_useStyles_ allows you to skip the `stylesheet` if you only want to access the `theme`:
|
171
241
|
|
172
242
|
```tsx
|
173
243
|
const { theme } = useStyles()
|
174
244
|
```
|
175
245
|
|
246
|
+
For more advanced usage, pass your `stylesheet` generated with `createStyleSheet`:
|
247
|
+
|
248
|
+
```tsx
|
249
|
+
// you can still access theme
|
250
|
+
const { styles, theme } = useStyles(stylesheet)
|
251
|
+
```
|
252
|
+
|
176
253
|
## Breakpoints
|
177
254
|
|
178
255
|
Any style can change based on breakpoints. To do this, change a value to an object:
|
179
256
|
|
180
257
|
```ts
|
181
|
-
const stylesheet =
|
258
|
+
const stylesheet = createStyleSheet(theme => ({
|
182
259
|
container: {
|
183
260
|
flex: 1,
|
184
261
|
justifyContent: 'center',
|
@@ -195,6 +272,33 @@ const stylesheet = createStyles(theme => ({
|
|
195
272
|
}))
|
196
273
|
```
|
197
274
|
|
275
|
+
You can even use it with nested objects like `transform` or `shadowOffset`:
|
276
|
+
|
277
|
+
```ts
|
278
|
+
const stylesheet = createStyleSheet(theme => ({
|
279
|
+
container: {
|
280
|
+
flex: 1,
|
281
|
+
justifyContent: 'center',
|
282
|
+
alignItems: 'center',
|
283
|
+
backgroundColor: {
|
284
|
+
xs: theme.colors.background,
|
285
|
+
sm: theme.colors.barbie
|
286
|
+
},
|
287
|
+
transform: [
|
288
|
+
{
|
289
|
+
translateX: 100
|
290
|
+
},
|
291
|
+
{
|
292
|
+
scale: {
|
293
|
+
xs: 1.5,
|
294
|
+
':w[500]': 1
|
295
|
+
}
|
296
|
+
}
|
297
|
+
]
|
298
|
+
}
|
299
|
+
}))
|
300
|
+
```
|
301
|
+
|
198
302
|
Library will choose the correct value (based on screen width) in the runtime.
|
199
303
|
|
200
304
|
## Media queries
|
@@ -211,7 +315,7 @@ For more advanced usage and pixel perfect designs you can also use a custom medi
|
|
211
315
|
Media queries can be mixed with breakpoints, but have a bigger priority:
|
212
316
|
|
213
317
|
```tsx
|
214
|
-
const stylesheet =
|
318
|
+
const stylesheet = createStyleSheet(theme => ({
|
215
319
|
container: {
|
216
320
|
justifyContent: 'center',
|
217
321
|
alignItems: 'center',
|
@@ -220,7 +324,8 @@ const stylesheet = createStyles(theme => ({
|
|
220
324
|
sm: 'row',
|
221
325
|
},
|
222
326
|
backgroundColor: {
|
223
|
-
|
327
|
+
md: theme.colors.background,
|
328
|
+
// even though md might overlap with >600px, lib will use 'barbie'
|
224
329
|
':w[600]': theme.colors.barbie
|
225
330
|
}
|
226
331
|
},
|
@@ -255,7 +360,7 @@ export const ExampleUnistyles = () => {
|
|
255
360
|
)
|
256
361
|
}
|
257
362
|
|
258
|
-
const stylesheet =
|
363
|
+
const stylesheet = createStyleSheet({
|
259
364
|
scrollContainer: {
|
260
365
|
flex: 1,
|
261
366
|
justifyContent: 'center',
|
@@ -267,16 +372,221 @@ const stylesheet = createStyles({
|
|
267
372
|
})
|
268
373
|
})
|
269
374
|
```
|
375
|
+
If you use a dynamic function, library will wrap it in a `Proxy` to make sure the correct values of breakpoints will be used:
|
376
|
+
|
377
|
+
```ts
|
378
|
+
const stylesheet = createStyleSheet(theme => ({
|
379
|
+
scrollContainer: {
|
380
|
+
flex: 1,
|
381
|
+
justifyContent: 'center',
|
382
|
+
alignItems: 'center',
|
383
|
+
},
|
384
|
+
post: (index: number) => ({
|
385
|
+
// breakpoints and media queries works with dynamic function
|
386
|
+
backgroundColor: {
|
387
|
+
xs: index % 2 === 0
|
388
|
+
? theme.colors.gold
|
389
|
+
: theme.colors.silver,
|
390
|
+
sm: theme.colors.red
|
391
|
+
}
|
392
|
+
})
|
393
|
+
}))
|
394
|
+
```
|
395
|
+
|
396
|
+
## Dynamic themes
|
397
|
+
|
398
|
+
You can incorporate as many themes as you desire in your application. While there's flexibility in how you structure your theme, it's essential to maintain consistency with the TypeScript type:
|
399
|
+
|
400
|
+
To promote reusability and maintainability, it's a good practice to share as many values between themes as possible:
|
401
|
+
|
402
|
+
```ts
|
403
|
+
// move shared colors to object
|
404
|
+
const sharedColors = {
|
405
|
+
barbie: '#ff9ff3',
|
406
|
+
oak: '#1dd1a1',
|
407
|
+
sky: '#48dbfb',
|
408
|
+
fog: '#c8d6e5',
|
409
|
+
aloes: '#00d2d3'
|
410
|
+
}
|
411
|
+
|
412
|
+
export const lightTheme = {
|
413
|
+
colors: {
|
414
|
+
// reuse or override them
|
415
|
+
...sharedColors,
|
416
|
+
backgroundColor: '#ffffff',
|
417
|
+
typography: '#000000'
|
418
|
+
}
|
419
|
+
// other keys in common with darkTheme
|
420
|
+
}
|
421
|
+
|
422
|
+
export const darkTheme = {
|
423
|
+
colors: {
|
424
|
+
// reuse or override them
|
425
|
+
...sharedColors,
|
426
|
+
backgroundColor: '#000000',
|
427
|
+
typography: '#ffffff'
|
428
|
+
}
|
429
|
+
// other keys in common with lightTheme
|
430
|
+
}
|
431
|
+
|
432
|
+
// export type that will be used to describe your theme
|
433
|
+
export type AppTheme = typeof lightTheme | typeof darkTheme
|
434
|
+
```
|
435
|
+
|
436
|
+
With the themes set up, modify your `createUnistyles` to consume your `AppTheme` type:
|
437
|
+
|
438
|
+
```ts
|
439
|
+
export const { useStyles, createStyleSheet } = createUnistyles<typeof breakpoints, AppTheme>(breakpoints)
|
440
|
+
```
|
441
|
+
|
442
|
+
The final step is to switch your theme based on certain states, persisted values, databases, etc.:
|
443
|
+
|
444
|
+
```tsx
|
445
|
+
export const App: React.FunctionComponent = () => {
|
446
|
+
// obtain here your dark or light theme. It can be storage, state, mmkv, or whateber you use
|
447
|
+
// const [yourAppTheme] = useState(lightTheme)
|
448
|
+
// const [yourAppTheme] = useYourStorage()
|
449
|
+
// const [yourAppTheme] = useMMKVObject<AppTheme>(Theme)
|
450
|
+
|
451
|
+
// switching theme will re-render your stylesheets automatically
|
452
|
+
return (
|
453
|
+
<UnistylesTheme theme={yourAppTheme}>
|
454
|
+
<Examples.Extreme />
|
455
|
+
</UnistylesTheme>
|
456
|
+
)
|
457
|
+
}
|
458
|
+
```
|
459
|
+
|
460
|
+
## Variants
|
461
|
+
|
462
|
+
`react-native-unistyles` isn't a UI/component library, so you're in charge of designing variants. With no restrictions and using your creativity, you can easily create variants for your components.
|
463
|
+
|
464
|
+
Let's examine variants for the `Text` component. Imagine you want to create several variants for your `Typography` components:
|
465
|
+
- Heading
|
466
|
+
- Regular
|
467
|
+
- Thin
|
468
|
+
|
469
|
+
To achieve this, add variants to your theme:
|
470
|
+
|
471
|
+
```ts
|
472
|
+
export const lightTheme = {
|
473
|
+
colors: {
|
474
|
+
...sharedColors,
|
475
|
+
backgroundColor: '#ffffff',
|
476
|
+
typography: '#000000'
|
477
|
+
},
|
478
|
+
components: {
|
479
|
+
typography: {
|
480
|
+
base: {
|
481
|
+
fontFamily: 'Roboto',
|
482
|
+
fontSize: 12,
|
483
|
+
},
|
484
|
+
heading: {
|
485
|
+
fontFamily: 'Roboto-Medium',
|
486
|
+
fontSize: 24,
|
487
|
+
},
|
488
|
+
regular: {
|
489
|
+
fontFamily: 'Roboto',
|
490
|
+
fontSize: 12,
|
491
|
+
},
|
492
|
+
thin: {
|
493
|
+
fontFamily: 'Roboto-Thin',
|
494
|
+
fontSize: 12,
|
495
|
+
},
|
496
|
+
bold: {
|
497
|
+
fontWeight: 'bold'
|
498
|
+
}
|
499
|
+
}
|
500
|
+
}
|
501
|
+
}
|
502
|
+
```
|
503
|
+
Next, create a base component:
|
504
|
+
|
505
|
+
```tsx
|
506
|
+
import React from 'react'
|
507
|
+
import type { PropsWithChildren } from 'react'
|
508
|
+
import { Text, TextStyle } from 'react-native'
|
509
|
+
import { createStyleSheet, useStyles } from 'lib/styles'
|
510
|
+
|
511
|
+
interface BaseTextProps extends PropsWithChildren {
|
512
|
+
bold: boolean,
|
513
|
+
style: TextStyle
|
514
|
+
}
|
515
|
+
|
516
|
+
export const BaseText: React.FunctionComponent<BaseTextProps> = ({
|
517
|
+
children,
|
518
|
+
bold = false,
|
519
|
+
style = {}
|
520
|
+
}) => {
|
521
|
+
const {styles} = useStyles(stylesheet)
|
522
|
+
|
523
|
+
return (
|
524
|
+
<Text
|
525
|
+
style={{
|
526
|
+
...styles.baseText,
|
527
|
+
...bold
|
528
|
+
? styles.baseText
|
529
|
+
: {},
|
530
|
+
// pass other styles via props
|
531
|
+
...style
|
532
|
+
}}
|
533
|
+
>
|
534
|
+
{children}
|
535
|
+
</Text>
|
536
|
+
)
|
537
|
+
}
|
538
|
+
|
539
|
+
const stylesheet = createStyleSheet(theme => ({
|
540
|
+
baseText: {
|
541
|
+
...theme.components.typography.base
|
542
|
+
},
|
543
|
+
bold: {
|
544
|
+
...theme.components.typography.bold
|
545
|
+
}
|
546
|
+
}))
|
547
|
+
```
|
548
|
+
|
549
|
+
Now, let's create another variant, e.g., Heading:
|
550
|
+
|
551
|
+
```tsx
|
552
|
+
import React from 'react'
|
553
|
+
import type { PropsWithChildren } from 'react'
|
554
|
+
import { Text, TextStyle } from 'react-native'
|
555
|
+
import { createStyleSheet, useStyles } from 'lib/styles'
|
556
|
+
import { BaseText } from 'lib/components'
|
557
|
+
|
558
|
+
interface BaseTextProps extends PropsWithChildren {
|
559
|
+
bold: boolean,
|
560
|
+
text: string
|
561
|
+
}
|
562
|
+
|
563
|
+
export const Heading: React.FunctionComponent<BaseTextProps> = ({
|
564
|
+
text,
|
565
|
+
bold = false
|
566
|
+
}) => {
|
567
|
+
const { theme } = useStyles()
|
568
|
+
|
569
|
+
return (
|
570
|
+
<BaseText
|
571
|
+
bold={bold}
|
572
|
+
style={theme.components.typography.heading}
|
573
|
+
>
|
574
|
+
{text}
|
575
|
+
</BaseText>
|
576
|
+
)
|
577
|
+
}
|
578
|
+
```
|
579
|
+
And so on...
|
270
580
|
|
271
581
|
## Migrate from StyleSheet
|
272
582
|
|
273
583
|
`react-native-unistyles` embraces the simplicity of `StyleSheet`, making it easy to integrate into your project.
|
274
584
|
|
275
|
-
You can replace `StyleSheet.create` with `
|
585
|
+
You can replace `StyleSheet.create` with `createStyleSheet` and it will work exactly the same:
|
276
586
|
|
277
587
|
```diff
|
278
588
|
-const styles = StyleSheet.create({
|
279
|
-
+const styles =
|
589
|
+
+const styles = createStyleSheet({
|
280
590
|
scrollContainer: {
|
281
591
|
flex: 1,
|
282
592
|
justifyContent: 'center',
|
@@ -294,21 +604,7 @@ export const ExampleUnistyles = () => {
|
|
294
604
|
}
|
295
605
|
```
|
296
606
|
|
297
|
-
With the hook in place, you can now use
|
298
|
-
|
299
|
-
Additionally, to access the `theme` use a function instead of an `object`:
|
300
|
-
|
301
|
-
```diff
|
302
|
-
-const stylesheet = createStyles({
|
303
|
-
+const stylesheet = createStyles(theme => ({
|
304
|
-
scrollContainer: {
|
305
|
-
flex: 1,
|
306
|
-
justifyContent: 'center',
|
307
|
-
alignItems: 'center',
|
308
|
-
backgroundColor: theme.colors.background
|
309
|
-
}
|
310
|
-
}))
|
311
|
-
```
|
607
|
+
With the hook in place, you can now use all the features.
|
312
608
|
|
313
609
|
## Example
|
314
610
|
|
@@ -318,6 +614,17 @@ In order to check out working example go to [example/](./example).
|
|
318
614
|
|
319
615
|
For more detailed explanation please refer to my blog post [here](https://www.reactnativecrossroads.com/posts/level-up-react-native-styles).
|
320
616
|
|
617
|
+
|
618
|
+
## Sponsor my work
|
619
|
+
|
620
|
+
If you found the `react-native-unistyles` time-saving and valuable, please consider sponsoring my work. Your support enables me to continue creating libraries with a fresh approach.
|
621
|
+
|
622
|
+
Github: https://github.com/sponsors/jpudysz
|
623
|
+
|
624
|
+
Ko-fi: https://ko-fi.com/jpudysz
|
625
|
+
|
626
|
+
Your support is greatly appreciated and helps me dedicate more time and resources to creating quality libraries. Thank you for all the support!
|
627
|
+
|
321
628
|
## License
|
322
629
|
|
323
630
|
MIT
|
@@ -5,21 +5,32 @@ Object.defineProperty(exports, "__esModule", {
|
|
5
5
|
});
|
6
6
|
exports.createUnistyles = void 0;
|
7
7
|
var _react = require("react");
|
8
|
+
var _reactNative = require("react-native");
|
8
9
|
var _UnistylesTheme = require("./UnistylesTheme");
|
9
|
-
var _utils = require("./utils");
|
10
10
|
var _hooks = require("./hooks");
|
11
|
+
var _utils = require("./utils");
|
11
12
|
const createUnistyles = breakpoints => {
|
12
13
|
const sortedBreakpoints = (0, _utils.sortAndValidateBreakpoints)(breakpoints);
|
14
|
+
const sortedBreakpointEntries = Object.entries(sortedBreakpoints);
|
13
15
|
return {
|
14
|
-
|
16
|
+
/**
|
17
|
+
* @deprecated The method should not be used, proposed version by the community is createStyleSheet, will be removed in RC
|
18
|
+
*/
|
19
|
+
createStyles: styles => {
|
20
|
+
if (typeof styles === 'function') {
|
21
|
+
return styles;
|
22
|
+
}
|
23
|
+
return styles;
|
24
|
+
},
|
25
|
+
createStyleSheet: styles => {
|
26
|
+
if (typeof styles === 'function') {
|
27
|
+
return styles;
|
28
|
+
}
|
29
|
+
return styles;
|
30
|
+
},
|
15
31
|
useStyles: stylesheet => {
|
16
32
|
const theme = (0, _react.useContext)(_UnistylesTheme.UnistylesContext);
|
17
|
-
const
|
18
|
-
const breakpoint = (0, _utils.getBreakpointFromScreenWidth)(dimensions.width, sortedBreakpoints);
|
19
|
-
const screenSize = {
|
20
|
-
width: dimensions.width,
|
21
|
-
height: dimensions.height
|
22
|
-
};
|
33
|
+
const screenSize = (0, _hooks.useDimensions)();
|
23
34
|
if (!stylesheet) {
|
24
35
|
return {
|
25
36
|
theme,
|
@@ -27,19 +38,20 @@ const createUnistyles = breakpoints => {
|
|
27
38
|
};
|
28
39
|
}
|
29
40
|
const parsedStyles = typeof stylesheet === 'function' ? stylesheet(theme) : stylesheet;
|
41
|
+
const breakpoint = (0, _utils.getBreakpointFromScreenWidth)(screenSize.width, sortedBreakpointEntries);
|
30
42
|
const dynamicStyleSheet = Object.entries(parsedStyles).reduce((acc, _ref) => {
|
31
43
|
let [key, value] = _ref;
|
32
|
-
const
|
44
|
+
const style = value;
|
33
45
|
if (typeof value === 'function') {
|
34
46
|
return {
|
35
47
|
...acc,
|
36
|
-
[key]: (0, _utils.proxifyFunction)(value, breakpoint, screenSize,
|
48
|
+
[key]: (0, _utils.proxifyFunction)(value, breakpoint, screenSize, sortedBreakpointEntries)
|
37
49
|
};
|
38
50
|
}
|
39
|
-
return {
|
51
|
+
return _reactNative.StyleSheet.create({
|
40
52
|
...acc,
|
41
|
-
[key]: (0, _utils.parseStyle)(
|
42
|
-
};
|
53
|
+
[key]: (0, _utils.parseStyle)(style, breakpoint, screenSize, sortedBreakpointEntries)
|
54
|
+
});
|
43
55
|
}, {});
|
44
56
|
return {
|
45
57
|
theme,
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"names":["_react","require","
|
1
|
+
{"version":3,"names":["_react","require","_reactNative","_UnistylesTheme","_hooks","_utils","createUnistyles","breakpoints","sortedBreakpoints","sortAndValidateBreakpoints","sortedBreakpointEntries","Object","entries","createStyles","styles","createStyleSheet","useStyles","stylesheet","theme","useContext","UnistylesContext","screenSize","useDimensions","parsedStyles","breakpoint","getBreakpointFromScreenWidth","width","dynamicStyleSheet","reduce","acc","_ref","key","value","style","proxifyFunction","StyleSheet","create","parseStyle","exports"],"sourceRoot":"../../src","sources":["createUnistyles.ts"],"mappings":";;;;;;AAAA,IAAAA,MAAA,GAAAC,OAAA;AACA,IAAAC,YAAA,GAAAD,OAAA;AASA,IAAAE,eAAA,GAAAF,OAAA;AACA,IAAAG,MAAA,GAAAH,OAAA;AACA,IAAAI,M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G,IAAAG,sBAAe,EAAIF,KAAK,EAAER,UAAU,EAAEH,UAAU,EAAEX,uBAAuB;UACpF,CAAC;QACL;QAEA,OAAOyB,uBAAU,CAACC,MAAM,CAAC;UACrB,GAAGP,GAAG;UACN,CAACE,GAAG,GAAG,IAAAM,iBAAU,EAAQJ,KAAK,EAAET,UAAU,EAAEH,UAAU,EAAEX,uBAAuB;QACnF,CAAC,CAAC;MACN,CAAC,EAAE,CAAC,CAAO,CAAC;MAEhB,OAAO;QACHQ,KAAK;QACLJ,MAAM,EAAEa;MACZ,CAAC;IACL;EACJ,CAAC;AACL,CAAC;AAAAW,OAAA,CAAAhC,eAAA,GAAAA,eAAA"}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"names":[],"sourceRoot":"../../src","sources":["mediaQueries.ts"],"mappings":""}
|
@@ -51,7 +51,7 @@ const sortAndValidateBreakpoints = breakpoints => {
|
|
51
51
|
*
|
52
52
|
* @template B - An object type where keys are strings and values are numbers representing screen widths.
|
53
53
|
* @param {number} width - The screen width to determine the breakpoint for.
|
54
|
-
* @param
|
54
|
+
* @param breakpointEntries - sorted pairs of breakpoints
|
55
55
|
* @returns {keyof B & string} The key of the breakpoint that the screen width falls into.
|
56
56
|
*
|
57
57
|
* @example
|
@@ -59,8 +59,8 @@ const sortAndValidateBreakpoints = breakpoints => {
|
|
59
59
|
* getBreakpointFromScreenWidth(800, breakpoints) // returns 'md'
|
60
60
|
*/
|
61
61
|
exports.sortAndValidateBreakpoints = sortAndValidateBreakpoints;
|
62
|
-
const getBreakpointFromScreenWidth = (width,
|
63
|
-
const [key] =
|
62
|
+
const getBreakpointFromScreenWidth = (width, breakpointEntries) => {
|
63
|
+
const [key] = breakpointEntries.find((_ref, index, otherBreakpoints) => {
|
64
64
|
let [, value] = _ref;
|
65
65
|
const minVal = value;
|
66
66
|
const maxVal = otherBreakpoints[index + 1]?.[1];
|
@@ -84,7 +84,7 @@ const getBreakpointFromScreenWidth = (width, breakpoints) => {
|
|
84
84
|
* @param {Record<keyof B & string, string | number>} value - An object containing values associated with breakpoints or custom media queries.
|
85
85
|
* @param {keyof B & string} breakpoint - The breakpoint name to check against.
|
86
86
|
* @param {ScreenSize} screenSize - An object representing the screen size to be checked against the media queries.
|
87
|
-
* @param
|
87
|
+
* @param breakpointPairs - sorted pairs of breakpoints
|
88
88
|
*
|
89
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
90
|
*
|
@@ -97,7 +97,7 @@ const getBreakpointFromScreenWidth = (width, breakpoints) => {
|
|
97
97
|
* getValueForBreakpoint(values, 'sm', screenSize, breakpoints); // 'value1'
|
98
98
|
*/
|
99
99
|
exports.getBreakpointFromScreenWidth = getBreakpointFromScreenWidth;
|
100
|
-
const getValueForBreakpoint = (value, breakpoint, screenSize,
|
100
|
+
const getValueForBreakpoint = (value, breakpoint, screenSize, breakpointPairs) => {
|
101
101
|
// the highest priority is for custom media queries
|
102
102
|
const customMediaQueries = Object.entries(value).filter(_ref2 => {
|
103
103
|
let [key] = _ref2;
|
@@ -118,22 +118,18 @@ const getValueForBreakpoint = (value, breakpoint, screenSize, breakpoints) => {
|
|
118
118
|
}
|
119
119
|
|
120
120
|
// there is no direct hit for breakpoint nor media-query, so let's simulate CSS cascading
|
121
|
-
const
|
122
|
-
let [key
|
123
|
-
return [key.toLowerCase(), bpValue];
|
124
|
-
});
|
125
|
-
const currentBreakpoint = allBreakpoints.findIndex(_ref4 => {
|
126
|
-
let [key] = _ref4;
|
121
|
+
const currentBreakpoint = breakpointPairs.findIndex(_ref3 => {
|
122
|
+
let [key] = _ref3;
|
127
123
|
return key === unifiedKey;
|
128
124
|
});
|
129
|
-
const availableBreakpoints =
|
130
|
-
let [key] =
|
125
|
+
const availableBreakpoints = breakpointPairs.filter((_ref4, index) => {
|
126
|
+
let [key] = _ref4;
|
131
127
|
return index < currentBreakpoint && key && key in value;
|
132
|
-
}).map(
|
133
|
-
let [key] =
|
128
|
+
}).map(_ref5 => {
|
129
|
+
let [key] = _ref5;
|
134
130
|
return key;
|
135
131
|
});
|
136
|
-
return
|
132
|
+
return breakpointPairs.length > 0 ? value[availableBreakpoints[availableBreakpoints.length - 1]] : undefined;
|
137
133
|
};
|
138
134
|
exports.getValueForBreakpoint = getValueForBreakpoint;
|
139
135
|
//# sourceMappingURL=breakpoints.js.map
|
@@ -1 +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","
|
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","breakpointEntries","key","find","_ref","index","otherBreakpoints","value","minVal","maxVal","getValueForBreakpoint","breakpoint","screenSize","breakpointPairs","customMediaQueries","filter","_ref2","isMediaQuery","customMediaQueryKey","getKeyForCustomMediaQuery","unifiedKey","toLowerCase","directBreakpoint","currentBreakpoint","findIndex","_ref3","availableBreakpoints","_ref4","map","_ref5","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,GAA2BC,WAA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