Skip to content

Commit 30f3ba3

Browse files
Sprinkles: Support mapping values to boolean, null and undefined (#331)
1 parent 56d61dd commit 30f3ba3

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

.changeset/giant-llamas-mix.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+
`createMapValueFn`: Support mapping values to `boolean`, `null` and `undefined`

packages/sprinkles/src/createUtils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ export function createNormalizeValueFn<AtomicStyles extends Conditions<string>>(
123123
export function createMapValueFn<AtomicStyles extends Conditions<string>>(
124124
atomicStyles: AtomicStyles,
125125
): <
126-
OutputValue extends string | number,
126+
OutputValue extends string | number | boolean | null | undefined,
127127
Value extends ConditionalValue<AtomicStyles, string | number>,
128128
>(
129129
value: Value,

tests/sprinkles/sprinkles.test.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,59 @@ describe('sprinkles', () => {
514514
expect(value).toMatchInlineSnapshot(`"123_mobile"`);
515515
});
516516

517+
it('should handle booleans', () => {
518+
const mapValue = createMapValueFn(conditionalAtomicStyles);
519+
const value = mapValue(123, () => false);
520+
521+
expect(value).toBe(false);
522+
});
523+
524+
it('should handle conditional booleans', () => {
525+
const mapValue = createMapValueFn(conditionalAtomicStyles);
526+
const value = mapValue(
527+
{ mobile: 1, tablet: 2, desktop: 3 },
528+
(value) => value === 2,
529+
);
530+
531+
expect(value).toStrictEqual({
532+
mobile: false,
533+
tablet: true,
534+
desktop: false,
535+
});
536+
});
537+
538+
it('should handle nulls', () => {
539+
const mapValue = createMapValueFn(conditionalAtomicStyles);
540+
const value = mapValue(123, () => null);
541+
542+
expect(value).toBe(null);
543+
});
544+
545+
it('should handle conditional nulls', () => {
546+
const mapValue = createMapValueFn(conditionalAtomicStyles);
547+
const value = mapValue({ mobile: 1, tablet: 2, desktop: 3 }, (value) =>
548+
value === 2 ? null : value,
549+
);
550+
551+
expect(value).toStrictEqual({ mobile: 1, tablet: null, desktop: 3 });
552+
});
553+
554+
it('should handle undefined', () => {
555+
const mapValue = createMapValueFn(conditionalAtomicStyles);
556+
const value = mapValue(123, () => undefined);
557+
558+
expect(value).toBe(undefined);
559+
});
560+
561+
it('should handle conditional undefined', () => {
562+
const mapValue = createMapValueFn(conditionalAtomicStyles);
563+
const value = mapValue({ mobile: 1, tablet: 2, desktop: 3 }, (value) =>
564+
value === 2 ? undefined : value,
565+
);
566+
567+
expect(value).toStrictEqual({ mobile: 1, tablet: undefined, desktop: 3 });
568+
});
569+
517570
it('should handle responsive arrays', () => {
518571
const map = createMapValueFn(conditionalAtomicStyles);
519572
const value = map(['one'], (value, key) => `${value}_${key}` as const);

0 commit comments

Comments
 (0)