Skip to content

Commit a8f181d

Browse files
authored
Sprinkles: Fix style object bug when no conditions are present (#307)
1 parent fddd31c commit a8f181d

File tree

4 files changed

+86
-53
lines changed

4 files changed

+86
-53
lines changed

.changeset/eight-moons-lie.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+
Allow style objects to be passed when no conditions are present

fixtures/sprinkles/src/styles.css.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
} from '@vanilla-extract/sprinkles';
88

99
const alpha = createVar();
10+
const textAlpha = createVar();
1011

1112
const responsiveStyles = createAtomicStyles({
1213
defaultCondition: 'mobile',
@@ -46,7 +47,24 @@ const responsiveStyles = createAtomicStyles({
4647
},
4748
});
4849

49-
export const atoms = createAtomsFn(responsiveStyles);
50+
const styleWithNoConditions = createAtomicStyles({
51+
properties: {
52+
color: {
53+
red: {
54+
vars: {
55+
[textAlpha]: '1',
56+
},
57+
color: `rgba(255, 0, 0, ${textAlpha})`,
58+
},
59+
},
60+
textOpacity: {
61+
1: { vars: { [textAlpha]: '1' } },
62+
0.8: { vars: { [textAlpha]: '0.8' } },
63+
},
64+
},
65+
});
66+
67+
export const atoms = createAtomsFn(responsiveStyles, styleWithNoConditions);
5068

5169
export const mapResponsiveValue = createMapValueFn(responsiveStyles);
5270
export const normalizeResponsiveValue =
@@ -57,6 +75,8 @@ export const preComposedAtoms = atoms({
5775
paddingTop: 'small',
5876
background: 'red',
5977
backgroundOpacity: { mobile: 0.1, tablet: 0.2, desktop: 0.3 },
78+
color: 'red',
79+
textOpacity: 0.8,
6080
});
6181

6282
export const preComposedAtomsUsedInSelector = atoms({

packages/sprinkles/src/index.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -237,19 +237,14 @@ export function createAtomicStyles(options: any): any {
237237
};
238238

239239
for (const conditionName in options.conditions) {
240+
let styleValue: StyleRule =
241+
typeof value === 'object' ? value : { [key]: value };
242+
240243
const condition =
241244
options.conditions[
242245
conditionName as keyof typeof options.conditions
243246
];
244247

245-
let styleValue: any = {};
246-
247-
if (typeof value === 'object') {
248-
styleValue = value;
249-
} else {
250-
styleValue[key] = value;
251-
}
252-
253248
if (condition['@supports']) {
254249
styleValue = {
255250
'@supports': {
@@ -286,8 +281,11 @@ export function createAtomicStyles(options: any): any {
286281
}
287282
}
288283
} else {
284+
const styleValue: StyleRule =
285+
typeof value === 'object' ? value : { [key]: value };
286+
289287
styles[key].values[valueName] = {
290-
defaultClass: style({ [key]: value }, `${key}_${String(valueName)}`),
288+
defaultClass: style(styleValue, `${key}_${String(valueName)}`),
291289
};
292290
}
293291
};

0 commit comments

Comments
 (0)