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

Add a note about using short-circuit evaluation to conditionally add CSS rules #744

Merged

Conversation

TyMick
Copy link
Contributor

@TyMick TyMick commented Dec 9, 2020

I just discovered that in the process of flattening CSS, styled-components ignores interpolations that evaluate to undefined, null, false, or an empty string. I think this fact may not be widely known, though, because in a certain component library I'm working on, I'm seeing a lot of ${props.condition ? 'rule-name: value;' : ''} in styled components instead of ${props.condition && 'rule-name: value;'}, seemingly out of a fear that false or undefined will stringify and break their CSS.

So I've written a short addition to the Tagged Template Literals section that lets people know that using short-circuit evaluation to add CSS rules is okay. Here's the new content:

Speaking of which, during flattening, styled-components ignores interpolations that evaluate to undefined, null, false, or an empty string (""), which means you're free to use short-circuit evaluation to conditionally add CSS rules.

const Title = styled.h1`
  /* Text centering won't break if props.upsidedown is falsy */
  ${props.upsidedown && 'transform: rotate(180deg);'}
  text-align: center;
`;

How's that look? Is there a better place to put something like this?

And please pardon the adding of semicolons to the preexisting examples
on the page—that was the precommit linting that did that.

...for conditionally adding CSS rules.

And please pardon the adding of semicolons to the preexisting examples
on the page--that was the precommit linting that did that.
@vercel
Copy link

vercel bot commented Dec 9, 2020

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/styledcomponents/styled-components/o1405d4ix
✅ Preview: https://styled-components-git-short-circuit-evaluation-note.styledcomponents.vercel.app

Copy link
Member

@kitten kitten left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a really nice touch!

@kitten kitten merged commit c65fbf7 into styled-components:master Dec 10, 2020
@TyMick
Copy link
Contributor Author

TyMick commented Dec 10, 2020

Thanks! 😊

@SDP190
Copy link

SDP190 commented Oct 4, 2021

The example given above can't run: "Props Is Not Defined"

const Title = styled.h1`
  /* Text centering won't break if props.upsidedown is falsy */
  ${props.upsidedown && 'transform: rotate(180deg);'}
  text-align: center;
`;

${props.upsidedown && 'transform: rotate(180deg);'} tries to evaluate immediately, while there is not a props variable available yet, should instead be an arrow function which is being passed the props argument later at flattening:

const Title = styled.h1`
  /* Text centering won't break if props.upsidedown is falsy */
  ${props => props.upsidedown && 'transform: rotate(180deg);'}
  text-align: center;
`;

@TyMick
Copy link
Contributor Author

TyMick commented Oct 9, 2021

Oops, thanks for catching that, @SDP190! I'll make a quick follow-up PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants