Skip to content

Commit ec3be84

Browse files
committed
feat: implement media queries with web selector support
1 parent 800a353 commit ec3be84

File tree

17 files changed

+328
-195
lines changed

17 files changed

+328
-195
lines changed

README.md

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ Add babel-plugin-macros to your `.babelrc` or `babel.config.js` and you're all s
107107

108108
### Using the `tw` prop
109109

110-
The best and easiest usage is to simply use the `tw` prop that is artificially added to all JSX elements. Under the hood, the macro removes the `tw` prop completely and instead applies or extends a `style` prop and also adds a web-only media id used by [react-native-media-query](https://github.com/kasinskas/react-native-media-query) to apply CSS-based media queries.
110+
The best and easiest usage is to simply use the `tw` prop that is artificially added to all JSX elements. Under the hood, the macro removes the `tw` prop completely and instead applies or extends a `style` prop and also adds a web-only media id used to apply CSS-based media queries.
111111

112112
All you have to do is have _any_ import of react-native-tailwind.macro in your file, either `import "react-native-tailwind.macro"` or `import { /* whatever import you need */ } from "react-native-tailwind.macro"`.
113113

@@ -314,7 +314,7 @@ Behind the scenes, `react-native-tailwind.macro` turns your _simple_ code from t
314314
import "react-native-tailwind.macro"
315315

316316
const Example = () => (
317-
<View tw="w-[100px] h-[100px] bg-purple-500 dark:ios:lg:bg-pink-500" />
317+
<View tw="w-[100px] h-[100px] bg-purple-500 dark:ios:lg:bg-pink-500 hover:bg-indigo-500" />
318318
)
319319
```
320320

@@ -330,6 +330,7 @@ const useStyles = ReactNativeTailwindMacro.createUseTailwindStyles({
330330
a7gsbs: [
331331
{
332332
dark: false,
333+
selectors: [],
333334
style: {
334335
width: 100,
335336
height: 100,
@@ -338,13 +339,22 @@ const useStyles = ReactNativeTailwindMacro.createUseTailwindStyles({
338339
},
339340
{
340341
dark: true,
341-
breakpoint: "lg",
342+
breakpoint: {
343+
label: "lg",
344+
minWidth: "1024px",
345+
},
346+
selectors: [],
342347
platform: "ios",
343348
style: {
344-
// Output for react-native-media-query
345-
"@media(min-width: 1024px)": {
346-
backgroundColor: "#ec4899",
347-
},
349+
backgroundColor: "#ec4899",
350+
},
351+
},
352+
{
353+
dark: false,
354+
// Styles on web will only be applied on web
355+
selectors: ["hover"],
356+
style: {
357+
backgroundColor: "#6366f1",
348358
},
349359
},
350360
],
@@ -359,7 +369,7 @@ const Example = () => {
359369
<View
360370
// Apply the memoized style
361371
style={tailwindStyles["a7gsbs"]}
362-
// Apply media id for CSS-based media queries using react-native-media-query
372+
// Apply media id for CSS-based media queries
363373
dataSet={{ media: tailwindStyles["a7gsbs"].id }}
364374
/>
365375
)
@@ -380,7 +390,7 @@ For more examples and use cases, check the [macro test snapshots](packages/react
380390

381391
- [tailwind-react-native-classnames](https://github.com/jaredh159/tailwind-react-native-classnames): Used for compiling Tailwind styles
382392

383-
- [react-native-media-query](https://github.com/kasinskas/react-native-media-query): Used for applying responsive styles with CSS media queries on the web
393+
- [react-native-media-query](https://github.com/kasinskas/react-native-media-query): Provides the base implementation used to enable CSS media query support
384394

385395
- [twin.macro](https://github.com/ben-rogerson/twin.macro): Inspiration for writing a babel macro
386396

examples/expo/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
"react": "16.13.1",
1919
"react-dom": "16.13.1",
2020
"react-native": "0.63.4",
21-
"react-native-media-query": "^1.0.7",
2221
"react-native-tailwind.macro": "*",
2322
"react-native-unimodules": "~0.12.0",
2423
"react-native-web": "~0.14.9"

examples/next/components/Button.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const Button: FunctionComponent<ButtonProps> = ({ label, onPress }) => {
1414
(tw) => ({
1515
button: [
1616
tw`px-8 py-4 rounded-3xl bg-blue-500`,
17-
pressed && tw`bg-blue-200`,
17+
pressed ? tw`bg-blue-200` : tw`hover:bg-blue-400`,
1818
],
1919
text: tw`text-white dark:text-black`,
2020
}),
@@ -27,7 +27,7 @@ export const Button: FunctionComponent<ButtonProps> = ({ label, onPress }) => {
2727
onPressOut={() => setPressed(false)}
2828
onPress={onPress}
2929
>
30-
<View style={styles.button}>
30+
<View style={styles.button} dataSet={{ media: styles.button.id }}>
3131
<Text style={styles.text}>{label}</Text>
3232
</View>
3333
</TouchableWithoutFeedback>

packages/react-native-tailwind.macro/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
"@babel/template": "^7.15.4",
4848
"babel-plugin-macros": "^3.1.0",
4949
"nanoid": "^3.1.25",
50-
"react-native-media-query": "^1.0.9",
5150
"twrnc": "2.1.0-beta.5"
5251
},
5352
"devDependencies": {

packages/react-native-tailwind.macro/src/__tests__/__snapshots__/macro.test.ts.snap

Lines changed: 65 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ const _useStyles = _ReactNativeTailwindMacro.createUseTailwindStyles({
4949
"mockId-1": [
5050
{
5151
dark: false,
52+
selectors: [],
53+
id: "rntwm-tgfXYl7K1yBpFmsixLSeHA",
5254
style: {
5355
paddingLeft: 32,
5456
paddingRight: 32,
@@ -95,6 +97,8 @@ const _useStyles = _ReactNativeTailwindMacro.createUseTailwindStyles({
9597
"mockId-1": [
9698
{
9799
dark: false,
100+
selectors: [],
101+
id: "rntwm-tgfXYl7K1yBpFmsixLSeHA",
98102
style: {
99103
paddingLeft: 32,
100104
paddingRight: 32,
@@ -152,6 +156,8 @@ const _useStyles = _ReactNativeTailwindMacro.createUseTailwindStyles({
152156
"mockId-1": [
153157
{
154158
dark: false,
159+
selectors: [],
160+
id: "rntwm-QCNapfedmWWq_pCNAW1Vbw",
155161
style: {
156162
backgroundColor: "#3b82f6",
157163
},
@@ -163,23 +169,29 @@ const _useStyles2 = _ReactNativeTailwindMacro.createUseTailwindStyles({
163169
"mockId-2": [
164170
{
165171
dark: false,
166-
breakpoint: "md",
172+
selectors: [],
173+
breakpoint: {
174+
label: "md",
175+
minWidth: "768px",
176+
},
177+
id: "rntwm-ICQub9WQ6pxaCmSgEP_Dyw",
167178
style: {
168-
"@media(min-width: 768px)": {
169-
paddingLeft: 32,
170-
paddingRight: 32,
171-
},
179+
paddingLeft: 32,
180+
paddingRight: 32,
172181
},
173182
},
174183
],
175184
"mockId-3": [
176185
{
177186
dark: false,
178-
breakpoint: "lg",
187+
selectors: [],
188+
breakpoint: {
189+
label: "lg",
190+
minWidth: "1024px",
191+
},
192+
id: "rntwm-l8OzFlAAuhkMqH0mxSHFRA",
179193
style: {
180-
"@media(min-width: 1024px)": {
181-
paddingTop: 64,
182-
},
194+
paddingTop: 64,
183195
},
184196
},
185197
],
@@ -224,6 +236,8 @@ const _useStyles = _ReactNativeTailwindMacro.createUseTailwindStyles({
224236
"mockId-1": [
225237
{
226238
dark: false,
239+
selectors: [],
240+
id: "rntwm-USsRiS2Y14wokhNvpfV_cA",
227241
style: {
228242
paddingTop: 32,
229243
},
@@ -281,6 +295,8 @@ const _useStyles = _ReactNativeTailwindMacro.createUseTailwindStyles({
281295
"mockId-1": [
282296
{
283297
dark: false,
298+
selectors: [],
299+
id: "rntwm-tgfXYl7K1yBpFmsixLSeHA",
284300
style: {
285301
paddingLeft: 32,
286302
paddingRight: 32,
@@ -293,6 +309,8 @@ const _useStyles2 = _ReactNativeTailwindMacro.createUseTailwindStyles({
293309
"mockId-2": [
294310
{
295311
dark: false,
312+
selectors: [],
313+
id: "rntwm-tgfXYl7K1yBpFmsixLSeHA",
296314
style: {
297315
paddingLeft: 32,
298316
paddingRight: 32,
@@ -359,6 +377,8 @@ const _useStyles = _ReactNativeTailwindMacro.createUseTailwindStyles({
359377
"mockId-1": [
360378
{
361379
dark: false,
380+
selectors: [],
381+
id: "rntwm-tgfXYl7K1yBpFmsixLSeHA",
362382
style: {
363383
paddingLeft: 32,
364384
paddingRight: 32,
@@ -368,6 +388,8 @@ const _useStyles = _ReactNativeTailwindMacro.createUseTailwindStyles({
368388
"mockId-2": [
369389
{
370390
dark: false,
391+
selectors: [],
392+
id: "rntwm-rtskDjeUkZ+Qoz23W9Kucw",
371393
style: {
372394
paddingLeft: 64,
373395
paddingRight: 64,
@@ -412,6 +434,8 @@ const _useStyles = _ReactNativeTailwindMacro.createUseTailwindStyles({
412434
"mockId-1": [
413435
{
414436
dark: false,
437+
selectors: [],
438+
id: "rntwm-tgfXYl7K1yBpFmsixLSeHA",
415439
style: {
416440
paddingLeft: 32,
417441
paddingRight: 32,
@@ -454,6 +478,8 @@ const _useStyles = _ReactNativeTailwindMacro.createUseTailwindStyles({
454478
"mockId-1": [
455479
{
456480
dark: false,
481+
selectors: [],
482+
id: "rntwm-tgfXYl7K1yBpFmsixLSeHA",
457483
style: {
458484
paddingLeft: 32,
459485
paddingRight: 32,
@@ -505,34 +531,44 @@ const _useStyles = _ReactNativeTailwindMacro.createUseTailwindStyles({
505531
"mockId-1": [
506532
{
507533
dark: false,
534+
selectors: [],
535+
id: "rntwm-QCNapfedmWWq_pCNAW1Vbw",
508536
style: {
509537
backgroundColor: "#3b82f6",
510538
},
511539
},
512540
{
513541
dark: false,
514-
breakpoint: "md",
542+
selectors: [],
543+
breakpoint: {
544+
label: "md",
545+
minWidth: "768px",
546+
},
547+
id: "rntwm-e0SKczQBFuXuSi8Lb9mOyA",
515548
style: {
516-
"@media(min-width: 768px)": {
517-
backgroundColor: "#ec4899",
518-
},
549+
backgroundColor: "#ec4899",
519550
},
520551
},
521552
],
522553
"mockId-2": [
523554
{
524555
dark: false,
556+
selectors: [],
557+
id: "rntwm-0YDC3qrbZAUxx0mzUCnxnw",
525558
style: {
526559
fontWeight: "bold",
527560
},
528561
},
529562
{
530563
dark: false,
531-
breakpoint: "md",
564+
selectors: [],
565+
breakpoint: {
566+
label: "md",
567+
minWidth: "768px",
568+
},
569+
id: "rntwm-eLOoCGNsuFm4oGXCi6HChg",
532570
style: {
533-
"@media(min-width: 768px)": {
534-
fontWeight: "100",
535-
},
571+
fontWeight: "100",
536572
},
537573
},
538574
],
@@ -573,6 +609,8 @@ const _useStyles = _ReactNativeTailwindMacro.createUseTailwindStyles({
573609
"mockId-1": [
574610
{
575611
dark: false,
612+
selectors: [],
613+
id: "rntwm-tgfXYl7K1yBpFmsixLSeHA",
576614
style: {
577615
paddingLeft: 32,
578616
paddingRight: 32,
@@ -616,6 +654,8 @@ const _useStyles = _ReactNativeTailwindMacro.createUseTailwindStyles({
616654
"mockId-1": [
617655
{
618656
dark: false,
657+
selectors: [],
658+
id: "rntwm-tgfXYl7K1yBpFmsixLSeHA",
619659
style: {
620660
paddingLeft: 32,
621661
paddingRight: 32,
@@ -668,6 +708,8 @@ const _useStyles = _ReactNativeTailwindMacro.createUseTailwindStyles({
668708
"mockId-1": [
669709
{
670710
dark: false,
711+
selectors: [],
712+
id: "rntwm-tgfXYl7K1yBpFmsixLSeHA",
671713
style: {
672714
paddingLeft: 32,
673715
paddingRight: 32,
@@ -706,6 +748,8 @@ const _useStyles = _ReactNativeTailwindMacro.createUseTailwindStyles({
706748
"mockId-1": [
707749
{
708750
dark: false,
751+
selectors: [],
752+
id: "rntwm-tgfXYl7K1yBpFmsixLSeHA",
709753
style: {
710754
paddingLeft: 32,
711755
paddingRight: 32,
@@ -753,6 +797,8 @@ const _useStyles = _ReactNativeTailwindMacro.createUseTailwindStyles({
753797
"mockId-1": [
754798
{
755799
dark: false,
800+
selectors: [],
801+
id: "rntwm-tgfXYl7K1yBpFmsixLSeHA",
756802
style: {
757803
paddingLeft: 32,
758804
paddingRight: 32,
@@ -791,6 +837,8 @@ const _useStyles = _ReactNativeTailwindMacro.createUseTailwindStyles({
791837
"mockId-1": [
792838
{
793839
dark: false,
840+
selectors: [],
841+
id: "rntwm-tgfXYl7K1yBpFmsixLSeHA",
794842
style: {
795843
paddingLeft: 32,
796844
paddingRight: 32,

0 commit comments

Comments
 (0)