styled-jss icon indicating copy to clipboard operation
styled-jss copied to clipboard

Media queries don't work when using theming function

Open aloker opened this issue 7 years ago • 9 comments

When I provide a literal style object, media queries are considered.

const ResponsiveOk = styled("div")({
  color: "green",
  "@media screen and (min-width: 400px)": {
    color: "red"
  }
});

If I use a theme callback instead, media queries do not work anymore:

const ResponsiveNotOk = styled("div")(({ theme }) => ({
  color: "green",
  "@media screen and (min-width: 400px)": {
    color: "red"
  }
}));

Demo: https://codesandbox.io/s/m9v69xvo3x Resize the output window above/below 400 pixel to see the difference: the top row switches between red and green, the bottom row doesn't.

Am I missing something conceptually here or is this supposed to work?

aloker avatar Apr 28 '18 17:04 aloker

Looks like a bug

kof avatar Apr 28 '18 22:04 kof

Hi! Thank you for the issue, it looks like a bug with @media and function rules in jss, described here: https://github.com/cssinjs/jss/issues/706

lttb avatar Apr 29 '18 07:04 lttb

The same problem exists for '&:hover', '&:before' and '&:after'. Those all do work in jss in combination with theming.

vbersch avatar Jun 14 '18 12:06 vbersch

Example snippet for @vbersch's issue: this works:

const Link = styled('a')({
  color: '#24292e',
  '&:before': {
    content: '"*"'
  }
})

this doesn't:

const Link = styled('a')((props) => ({
  color: '#24292e',
  '&:before': {
    content: '"*"'
  }
}))

Both patterns are working fine in react-jss

MarcusNotheis avatar Jun 14 '18 12:06 MarcusNotheis

I love this JSS plugin! Unfortunately we're experiencing the same issue. Our team has been able to work around it by providing an object which contains functions when the props/theme are needed, but I feel it would be more efficient if we could provide a function which returns a single object instead.

For example (modifying the above for illustration purposes), this works:

const Link = styled('a')({
  color: ({ theme }) => theme.palette.primary.main
  textDecoration: "none",
  '&:hover': {
    textDecoration: "underline"
  }
})

But this does not:

const Link = styled('a')(({ theme }) => ({
  color: theme.palette.primary.main
  textDecoration: "none",
  '&:hover': {
    textDecoration: "underline"
  }
}))

jmadson avatar Jul 25 '18 23:07 jmadson

Still experiencing the bug with '&:hover' as @MarcusNotheis described

likethemammal avatar May 06 '19 07:05 likethemammal

We are currently working on porting this implementation to v10 of jss and also moving it into react-jss in cssinjs/jss#1094

HenriBeck avatar May 06 '19 07:05 HenriBeck

There will be no more work done in this repo. I will add a test case for this bug for the new styled API to see if it's fixed there.

HenriBeck avatar May 06 '19 07:05 HenriBeck

@HenriBeck Ah okay. Amazingly quick response, thanks!

likethemammal avatar May 06 '19 07:05 likethemammal