Skip to content

Commit

Permalink
fix: move at-rules to end of nested responsive states
Browse files Browse the repository at this point in the history
Closes #288
  • Loading branch information
agriffis authored and gregberge committed Apr 15, 2022
1 parent 3e8b0ff commit 6c7528e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
31 changes: 31 additions & 0 deletions packages/system/src/style.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,37 @@ describe('#style', () => {
fontFamily: 'title2',
},
})
// https://github.com/gregberge/xstyled/issues/288
expect(
JSON.stringify(
fontFamily({
fontFamily: {
_: { _: 'title', hover: 'title2' },
md: { _: 'title3', hover: 'title4' },
},
theme,
}),
null,
2,
),
).toEqual(
JSON.stringify(
{
fontFamily: 'title',
'&:hover': {
fontFamily: 'title2',
},
'@media (min-width: 400px)': {
fontFamily: 'title3',
'&:hover': {
fontFamily: 'title4',
},
},
},
null,
2,
),
)
})

it('works with breakpoints', () => {
Expand Down
15 changes: 14 additions & 1 deletion packages/system/src/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,20 @@ export const getVariants = <T extends Props>(props: T): PropsVariants<T> => {
for (const value in screens) {
medias[value] = mediaMinWidth(getBreakpointMin(screens, value))
}
return { ...medias, ...states }
const variants = { ...medias, ...states }

// Move at-rules to the end, since they don't increase specificity by
// themselves but might need to override something that does.
// See https://github.com/gregberge/xstyled/issues/288
for (const [value, selector] of Object.entries(variants)) {
if (selector && selector.startsWith('@')) {
delete variants[value]
// @ts-ignore
variants[value] = selector
}
}

return variants
}

export const getCachedVariants = <T extends Props>(
Expand Down

0 comments on commit 6c7528e

Please sign in to comment.