Emotion: Condition with css not working.

Created on 1 Oct 2017  路  6Comments  路  Source: emotion-js/emotion

  • emotion version: 7.3.2
  • react version: 15.5.4

Relevant code.

export const Title1 = styled('h1')`
  letter-spacing: 1px;
  color: #000000;
  font-weight: 300;
  ${props => ( props.margin && `
    margin: ${props.margin};
  `)}
  ${props => ( props.padding && `
    padding: ${props.padding};
  `)}
`

What you did:
I try to set css if have props send to emotion component

What happened:
It not working.

Most helpful comment

It will work without a semicolon in emotion 8

All 6 comments

Try adding css to your style fragments.

export const Title1 = styled('h1')`
  letter-spacing: 1px;
  color: #000000;
  font-weight: 300;
  ${props => ( props.margin && css`
    margin: ${props.margin};
  `)}
  ${props => ( props.padding && css`
    padding: ${props.padding};
  `)}
`

thankyou. It work only first condition.

${props => ( props.margin && css`
    margin: ${props.margin};
  `)}
  ${props => ( props.padding && css`
    padding: ${props.padding};
  `)}

with this code margin will work only.

if swap padding to first of condition. It will work only padding.

How do you think about this ?

Adding a semicolon after the margin stuff should fix it.

export const Title1 = styled('h1')`
  letter-spacing: 1px;
  color: #000000;
  font-weight: 300;
  ${props => ( props.margin && css`
    margin: ${props.margin};
  `)};
  ${props => ( props.padding && css`
    padding: ${props.padding};
  `)}
`

I ran into this exact same issue. Is there any way of making the semi-colon not required in these situations, or at least throwing an error rather than silently failing?

It will work without a semicolon in emotion 8

Based on the comments this should be closed

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kylegach picture kylegach  路  63Comments

orpheus picture orpheus  路  35Comments

yonatanmn picture yonatanmn  路  29Comments

krzysztofzuraw picture krzysztofzuraw  路  78Comments

JustinTRoss picture JustinTRoss  路  26Comments