Skip to content

Commit 6c7528e

Browse files
agriffisgregberge
authored andcommitted
fix: move at-rules to end of nested responsive states
Closes #288
1 parent 3e8b0ff commit 6c7528e

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

packages/system/src/style.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,37 @@ describe('#style', () => {
152152
fontFamily: 'title2',
153153
},
154154
})
155+
// https://github.com/gregberge/xstyled/issues/288
156+
expect(
157+
JSON.stringify(
158+
fontFamily({
159+
fontFamily: {
160+
_: { _: 'title', hover: 'title2' },
161+
md: { _: 'title3', hover: 'title4' },
162+
},
163+
theme,
164+
}),
165+
null,
166+
2,
167+
),
168+
).toEqual(
169+
JSON.stringify(
170+
{
171+
fontFamily: 'title',
172+
'&:hover': {
173+
fontFamily: 'title2',
174+
},
175+
'@media (min-width: 400px)': {
176+
fontFamily: 'title3',
177+
'&:hover': {
178+
fontFamily: 'title4',
179+
},
180+
},
181+
},
182+
null,
183+
2,
184+
),
185+
)
155186
})
156187

157188
it('works with breakpoints', () => {

packages/system/src/theme.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,20 @@ export const getVariants = <T extends Props>(props: T): PropsVariants<T> => {
3636
for (const value in screens) {
3737
medias[value] = mediaMinWidth(getBreakpointMin(screens, value))
3838
}
39-
return { ...medias, ...states }
39+
const variants = { ...medias, ...states }
40+
41+
// Move at-rules to the end, since they don't increase specificity by
42+
// themselves but might need to override something that does.
43+
// See https://github.com/gregberge/xstyled/issues/288
44+
for (const [value, selector] of Object.entries(variants)) {
45+
if (selector && selector.startsWith('@')) {
46+
delete variants[value]
47+
// @ts-ignore
48+
variants[value] = selector
49+
}
50+
}
51+
52+
return variants
4053
}
4154

4255
export const getCachedVariants = <T extends Props>(

0 commit comments

Comments
 (0)