Skip to content

Commit 5bee64f

Browse files
Sprinkles: Support resolving falsey values for conditional atoms (#64)
1 parent e999308 commit 5bee64f

File tree

4 files changed

+77
-4
lines changed

4 files changed

+77
-4
lines changed

.changeset/dirty-beans-dance.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
'@vanilla-extract/sprinkles': patch
3+
---
4+
5+
Support resolving falsey values for conditional atoms
6+
7+
Fixes bug where falsey values such as `opacity: 0` would not resolve classes via the conditional object or responsive array syntax.
8+
9+
```ts
10+
export const atoms = createAtomicStyles({
11+
defaultCondition: 'mobile',
12+
conditions: {
13+
mobile: {},
14+
desktop: {
15+
'@media': 'screen and (min-width: 786px)'
16+
}
17+
},
18+
responsiveArray: ['mobile', 'desktop'],
19+
properties: {
20+
opacity: [0, 1]
21+
}
22+
});
23+
```

packages/sprinkles/src/createAtomsFn.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,11 @@ export function createAtomsFn<Args extends ReadonlyArray<AtomicStyles>>(
137137
} else if (Array.isArray(propValue)) {
138138
for (const responsiveIndex in propValue) {
139139
const responsiveValue = propValue[responsiveIndex];
140-
if (responsiveValue) {
140+
141+
if (
142+
typeof responsiveValue === 'string' ||
143+
typeof responsiveValue === 'number'
144+
) {
141145
const conditionName =
142146
atomicProperty.responsiveArray[responsiveIndex];
143147
classNames.push(
@@ -150,7 +154,7 @@ export function createAtomsFn<Args extends ReadonlyArray<AtomicStyles>>(
150154
// Conditional style
151155
const value = propValue[conditionName];
152156

153-
if (value) {
157+
if (typeof value === 'string' || typeof value === 'number') {
154158
classNames.push(
155159
atomicProperty.values[value].conditions[conditionName],
156160
);

tests/sprinkles/index.css.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export const conditionalAtomicStyles = createAtomicStyles({
4949
display: ['block', 'none', 'flex'],
5050
paddingTop: spacing,
5151
paddingBottom: spacing,
52+
opacity: [0, 1],
5253
},
5354
shorthands: {
5455
paddingY: ['paddingBottom', 'paddingTop'],

tests/sprinkles/sprinkles.test.ts

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,26 @@ describe('sprinkles', () => {
2525
);
2626
});
2727

28+
it('should handle falsey values on conditional styles', () => {
29+
const atoms = createAtomsFn(conditionalAtomicStyles);
30+
31+
expect(
32+
atoms({ display: 'block', opacity: { mobile: 0, desktop: 1 } }),
33+
).toMatchInlineSnapshot(
34+
`"sprinkles_display_block_mobile__1kw4brej sprinkles_opacity_0_mobile__1kw4bre1a sprinkles_opacity_1_desktop__1kw4bre1f"`,
35+
);
36+
});
37+
38+
it('should handle falsey values from responsive array on conditional styles', () => {
39+
const atoms = createAtomsFn(conditionalAtomicStyles);
40+
41+
expect(
42+
atoms({ display: 'block', opacity: [0, 1] }),
43+
).toMatchInlineSnapshot(
44+
`"sprinkles_display_block_mobile__1kw4brej sprinkles_opacity_0_mobile__1kw4bre1a sprinkles_opacity_1_tablet__1kw4bre1e"`,
45+
);
46+
});
47+
2848
it('should handle conditional styles with different variants', () => {
2949
const atoms = createAtomsFn(conditionalAtomicStyles);
3050

@@ -203,7 +223,7 @@ describe('sprinkles', () => {
203223
padding: 'large',
204224
}),
205225
).toMatchInlineSnapshot(
206-
`"sprinkles_paddingTop_small__1kw4bre1q sprinkles_paddingBottom_medium__1kw4bre1u sprinkles_paddingLeft_small__1kw4bre1k sprinkles_paddingRight_small__1kw4bre1n"`,
226+
`"sprinkles_paddingTop_small__1kw4bre1w sprinkles_paddingBottom_medium__1kw4bre20 sprinkles_paddingLeft_small__1kw4bre1q sprinkles_paddingRight_small__1kw4bre1t"`,
207227
);
208228
});
209229

@@ -216,7 +236,7 @@ describe('sprinkles', () => {
216236
padding: 'large',
217237
}),
218238
).toMatchInlineSnapshot(
219-
`"sprinkles_paddingTop_large__1kw4bre1s sprinkles_paddingBottom_large__1kw4bre1v sprinkles_paddingLeft_small__1kw4bre1k sprinkles_paddingRight_small__1kw4bre1n"`,
239+
`"sprinkles_paddingTop_large__1kw4bre1y sprinkles_paddingBottom_large__1kw4bre21 sprinkles_paddingLeft_small__1kw4bre1q sprinkles_paddingRight_small__1kw4bre1t"`,
220240
);
221241
});
222242
});
@@ -315,6 +335,31 @@ describe('sprinkles', () => {
315335
},
316336
},
317337
},
338+
"opacity": Object {
339+
"responsiveArray": Array [
340+
"mobile",
341+
"tablet",
342+
"desktop",
343+
],
344+
"values": Object {
345+
"0": Object {
346+
"conditions": Object {
347+
"desktop": "sprinkles_opacity_0_desktop__1kw4bre1c",
348+
"mobile": "sprinkles_opacity_0_mobile__1kw4bre1a",
349+
"tablet": "sprinkles_opacity_0_tablet__1kw4bre1b",
350+
},
351+
"defaultClass": "sprinkles_opacity_0_mobile__1kw4bre1a",
352+
},
353+
"1": Object {
354+
"conditions": Object {
355+
"desktop": "sprinkles_opacity_1_desktop__1kw4bre1f",
356+
"mobile": "sprinkles_opacity_1_mobile__1kw4bre1d",
357+
"tablet": "sprinkles_opacity_1_tablet__1kw4bre1e",
358+
},
359+
"defaultClass": "sprinkles_opacity_1_mobile__1kw4bre1d",
360+
},
361+
},
362+
},
318363
"paddingBottom": Object {
319364
"responsiveArray": Array [
320365
"mobile",

0 commit comments

Comments
 (0)