mui / mui/material-ui

Cannot use conditional CSS blocks inside styled() definitions written as functions

Open
#35,195 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

customization: theme scope: system type: bug
Dominant language
JavaScript
Stars
99.1k
Forks
32.5k
Avg merge
2d 17h
Merged PRs (30d)
106

Description

Duplicates
  • I have searched the existing issues
Latest version
  • I have tested the latest version
Steps to reproduce 🕹

Link to live example: https://codesandbox.io/s/exciting-agnesi-xnoes2?file=/demo.js

  1. There are 2 elements creating an override for a background color
  2. First override uses the styled()/* styles here */ syntax and this works correctly
  3. Second uses uses the styled()(({ theme }) => /* styles here */ syntax and the overrides are not applied

The syntax follows: https://github.com/styled-components/styled-components/issues/3189

Current behavior 😯

Conditional styles should get applied to a provided element.

Expected behavior 🤔

Styles don't get applied, and no errors are thrown.

Context 🔦

I'm trying to render a css block conditionally within a block using theme variables. Excerpt from the demo:

const MyComponent = () => {
  const Component = styled("div")(
    ({ theme }) => `
      background: black;
      ${true && css`
        background: red;
      `}
  `);
  return <Component />;
};

The result is element with black background rather than red;

Your environment 🌎

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with demo.js in the linked CodeSandbox and reproduce the difference between the direct styled() form and the function form. Compare how the conditional css block is handled in each case. Done means the function form applies the conditional red background to the rendered element without errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
css, javascript, react
Domain
frontend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.