Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot use dynamic values in CSS media queries #1443

Open
dddlr opened this issue Apr 19, 2023 · 0 comments
Open

Cannot use dynamic values in CSS media queries #1443

dddlr opened this issue Apr 19, 2023 · 0 comments
Labels
bug 🐛 Something isn't working

Comments

@dddlr
Copy link
Collaborator

dddlr commented Apr 19, 2023

Describe the bug

Dynamic values (e.g. props) in CSS media queries currently do not result in correct CSS output. This is due to the use of CSS variables, which are not supported within media queries.

To Reproduce

import { styled } from '@compiled/react';
import React from 'react';

export const StyledText = styled.span<{ mediaMaxWidth?: string }>`
  @media (max-width: ${(props) => props.mediaMaxWidth}) {
    width: 80vw;
  }
`;

const App = <StyledText mediaMaxWidth="650px">Styled text</StyledText>;

Expected behavior

CSS variables are not currently supported in media queries, so the output CSS should look something like this:

@media (max-width: 650px) {
    ._1hma11y2 { width: 80vw; }
}
<span class="_1hma11y2">Styled text</span>

Actual behaviour

This CSS is generated:

@media (max-width:var(--_1enk3bj)) {
    ._1hma11y2 { width:80vw; }
}
<span class="_1hma11y2" style="--_1enk3bj:650px;">Styled text</span>

Screenshot 2023-04-19 at 17 24 58

No error is generated, but the browser will never apply width: 80vw to the element.

Additional context

  1. Compiled's object notation does not support this use case either - something like this would give an error:
const StyledText = styled.span<{ mediaMaxWidth?: string }>({
  [`@media (max-width: ${(props) => props.mediaMaxWidth || "650px"})`]: {
    width: '80vw'
  }
});
  1. If standardised, env() would address the problem of var() not working in media queries. We could potentially switch from var() to env() in the future depending on how the latter gets standardised. See here: https://stackoverflow.com/a/47212942
@dddlr dddlr added the bug 🐛 Something isn't working label Apr 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant