Skip to content

Commit 152a1c7

Browse files
Sprinkles: Ignore undefined values in conditional objects (#139)
Co-authored-by: Mark Dalgleish <[email protected]>
1 parent 2247cdc commit 152a1c7

File tree

4 files changed

+90
-20
lines changed

4 files changed

+90
-20
lines changed

.changeset/dull-seahorses-cough.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@vanilla-extract/sprinkles': patch
3+
---
4+
5+
Ignore undefined values in conditional objects

packages/sprinkles/src/createAtomsFn.ts

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,16 @@ export function createAtomsFn<Args extends ReadonlyArray<AtomicStyles>>(
141141
// Conditional style
142142
const value = propValue[conditionName];
143143

144-
if (process.env.NODE_ENV !== 'production') {
145-
if (!atomicProperty.values[value].conditions[conditionName]) {
146-
throw new Error();
144+
if (value != null) {
145+
if (process.env.NODE_ENV !== 'production') {
146+
if (!atomicProperty.values[value].conditions[conditionName]) {
147+
throw new Error();
148+
}
147149
}
150+
classNames.push(
151+
atomicProperty.values[value].conditions[conditionName],
152+
);
148153
}
149-
classNames.push(
150-
atomicProperty.values[value].conditions[conditionName],
151-
);
152154
}
153155
}
154156
} catch (e) {
@@ -235,20 +237,22 @@ export function createAtomsFn<Args extends ReadonlyArray<AtomicStyles>>(
235237
for (const conditionName in propValue) {
236238
const value = propValue[conditionName];
237239

238-
if (!atomicProperty.values[value]) {
239-
invalidPropValue(prop, value, atomicProperty.values);
240-
}
240+
if (value != null) {
241+
if (!atomicProperty.values[value]) {
242+
invalidPropValue(prop, value, atomicProperty.values);
243+
}
241244

242-
if (!atomicProperty.values[value].conditions[conditionName]) {
243-
throw new SprinklesError(
244-
`"${prop}" has no condition named ${format(
245-
conditionName,
246-
)}. Possible values are ${Object.keys(
247-
atomicProperty.values[value].conditions,
248-
)
249-
.map(format)
250-
.join(', ')}`,
251-
);
245+
if (!atomicProperty.values[value].conditions[conditionName]) {
246+
throw new SprinklesError(
247+
`"${prop}" has no condition named ${format(
248+
conditionName,
249+
)}. Possible values are ${Object.keys(
250+
atomicProperty.values[value].conditions,
251+
)
252+
.map(format)
253+
.join(', ')}`,
254+
);
255+
}
252256
}
253257
}
254258
}

packages/sprinkles/src/createUtils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@ export function createMapValueFn<AtomicStyles extends Conditions<string>>(
126126
const mappedObject: Record<string, string> = {};
127127

128128
for (const key in normalizedObject) {
129-
mappedObject[key] = mapFn(normalizedObject[key], key);
129+
if (normalizedObject[key] != null) {
130+
mappedObject[key] = mapFn(normalizedObject[key], key);
131+
}
130132
}
131133

132134
return mappedObject;

tests/sprinkles/sprinkles.test.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,17 @@ describe('sprinkles', () => {
3131
);
3232
});
3333

34+
it('should ignore undefined values', () => {
35+
const atoms = createAtomsFn(conditionalAtomicStyles);
36+
37+
expect(
38+
atoms({
39+
display: 'block',
40+
paddingTop: undefined,
41+
}),
42+
).toMatchInlineSnapshot(`"sprinkles_display_block_mobile__1kw4brej"`);
43+
});
44+
3445
it('should handle falsey values on conditional styles', () => {
3546
const atoms = createAtomsFn(conditionalAtomicStyles);
3647

@@ -85,6 +96,25 @@ describe('sprinkles', () => {
8596
);
8697
});
8798

99+
it('should ignore undefined conditional values', () => {
100+
const atoms = createAtomsFn(
101+
atomicWithShorthandStyles,
102+
conditionalAtomicStyles,
103+
);
104+
105+
expect(
106+
atoms({
107+
paddingTop: {
108+
mobile: 'medium',
109+
tablet: undefined,
110+
desktop: 'large',
111+
},
112+
}),
113+
).toMatchInlineSnapshot(
114+
`"sprinkles_paddingTop_medium_mobile__1kw4brev sprinkles_paddingTop_large_desktop__1kw4bre10"`,
115+
);
116+
});
117+
88118
it('should handle responsive arrays', () => {
89119
const atoms = createAtomsFn(
90120
atomicWithShorthandStyles,
@@ -450,6 +480,20 @@ describe('sprinkles', () => {
450480
}
451481
`);
452482
});
483+
484+
it('should handle conditional objects with undefined', () => {
485+
const normalizeValue = createNormalizeValueFn(conditionalAtomicStyles);
486+
487+
expect(
488+
normalizeValue({ mobile: 'one', tablet: undefined, desktop: 'three' }),
489+
).toMatchInlineSnapshot(`
490+
Object {
491+
"desktop": "three",
492+
"mobile": "one",
493+
"tablet": undefined,
494+
}
495+
`);
496+
});
453497
});
454498

455499
describe('createMapValueFn', () => {
@@ -551,6 +595,21 @@ describe('sprinkles', () => {
551595
}
552596
`);
553597
});
598+
599+
it('should handle conditional objects with undefined', () => {
600+
const mapValue = createMapValueFn(conditionalAtomicStyles);
601+
const value = mapValue(
602+
{ mobile: 'one', tablet: undefined, desktop: 'three' } as const,
603+
(value, key) => `${value}_${key}` as const,
604+
);
605+
606+
expect(value).toMatchInlineSnapshot(`
607+
Object {
608+
"desktop": "three_desktop",
609+
"mobile": "one_mobile",
610+
}
611+
`);
612+
});
554613
});
555614

556615
it('should create atomic styles', () => {

0 commit comments

Comments
 (0)