compiled icon indicating copy to clipboard operation
compiled copied to clipboard

Conditional ternary CSS rules

Open itsdouges opened this issue 5 years ago • 1 comments

We've implemented conditional logical expressions in #379. We now need to add support for ternaries.

<div css={[isPrimary ? { color: 'red' } : { color: 'blue' }]} />

<div css={`color $isPrimary ? r b]} />

itsdouges avatar Dec 07 '20 09:12 itsdouges

Piling on my support for this one! This a common (and nice) pattern that we leverage across the Atlassian Design System

alexreardon avatar Jun 17 '24 23:06 alexreardon

This should now work with css(), though does not work with cssMap(…), refer to https://github.com/atlassian-labs/compiled/issues/1750

Tested and this generates as expected:

const fontSizeTrue = css({ color: 'red' });
const fontSizeFalse = css({ color: 'blue' });

<div css={[
  hasTextAncestor ? fontSizeTrue : fontSizeFalse,
}] />

Outputs something like:

const fontSizeTrue = null;
const fontSizeFalse = null;

className: ax([hasTextAncestor ? "_syaz32ev" : "_syazr3uz"])

kylorhall-atlassian avatar Dec 04 '24 01:12 kylorhall-atlassian