callstack / linaria

Zero-runtime CSS in JS library
https://linaria.dev
MIT License
11.68k stars 417 forks source link

Support `@container` #1234

Open taichi221228 opened 1 year ago

taichi221228 commented 1 year ago

Describe the enhancement

It is already compatible with most modern browsers. https://caniuse.com/css-container-queries

image

Motivation

Corresponds to @container and named container @container foo with the same feel as @media.

summary of the container queries

properties

Possible implementations

Container queries are only truly valuable with CSS-in-JS! It improves expressiveness!

Related Issues

jsanthosh-godaddy commented 1 year ago

@taichi221228 Are container queries supported currently in linaria ?

taichi221228 commented 1 year ago

@jsanthosh-godaddy I just checked in the development environment at hand and I don't think it is supported yet.

altano commented 1 year ago

One thing that's missing is that a container query doesn't get the class name. For example, this css:

const Section = styled.section`
  color: red;

  @media (min-width: 5px) {
    color: yellowgreen;
  }

  @container (min-width: 5px) {
    color: cyan;
  }
`;

Will produce:

.Section_s9q1p8m {
    color: red;
}

@media (min-width: 5px) {
    .Section_s9q1p8m {
      color: yellowgreen;
    }
}

@container (min-width: 5px) {
    color: cyan;
}

Note that the .Section_s9q1p8m class is injected into the @media rule but not the @container rule.

orzechowskid commented 10 months ago

container queries seem to work for me in linaria 6:

const Div = styled.div`
  width: 50vw;
  height: 100vh;

  container-type: size;
`;

const P = styled.p`
  background-color: blue;

  @container (min-width: 500px) {
    background-color: tomato;
  }
`;

function App() {
  const [count, setCount] = useState(0);

  return (
    <Div>
      <div>
        <a href="https://vitejs.dev" target="_blank">
          <img src={viteLogo} className="logo" alt="Vite logo" />
        </a>
        <a href="https://react.dev" target="_blank">
          <img src={reactLogo} className="logo react" alt="React logo" />
        </a>
      </div>
      <h1>Vite + React</h1>
      <div className="card">
        <button onClick={() => setCount((count) => count + 1)}>
          count is {count}
        </button>
        <P>
          Edit <code>src/App.tsx</code> and save to test HMR
        </P>
      </div>
      <P className="read-the-docs">
        Click on the Vite and React logos to learn more
      </P>
    </Div>
  );
}

gives me this CSS:

.duwg8pj {
  width:50vw; height:100vh; container-type:size;
}
.p1llctjr {
  background-color:blue;
}
@container (min-width: 500px) {
  .p1llctjr {
    background-color:tomato;
  }
}

image

image

sandbox: https://codesandbox.io/p/devbox/sharp-shirley-dpzjf3