Styled-jsx: style child component from parent component

Created on 24 May 2017  路  2Comments  路  Source: vercel/styled-jsx

How can i apply style to a child component that i define in the parent?
for example:

// Footer.js
export default function Footer () {
  return (
    <div className='footer-col'>
    <Button> i'm a button </Button>

      <style jsx>{`
          span {
            background-color: red;
            font-weight: 500;
          }
      `}</style>
      </div>
  )
}

// Button.js
export default function Button ({children}) {
  return (
    <button>{children}</button>
  )
}

I want my button to be red but i need to set this from Footer component because maybe in my Header the button must be yellow.
How can i do that?
(i would prefer to not give a class because this is quite simple example but i have other cases where the style is not so simple like .btn-primary and .btn-secondary)

Btw, i also try to use the "external style" feature exactly like in the read me example, but it doesn't work for me.
I get an error saying styled-jsx is expecting a regular string, instead there is an identifier (lie in the examples, i used a variable name i.e. styles)

Most helpful comment

Btw, i also try to use the "external style" feature exactly like in the read me example, but it doesn't work for me.

this is because we haven't published styled-jsx 1.0.0 to npm yet

All 2 comments

if you're not willing to pass a className or inline styles the only option left is :global

// Footer.js
export default function Footer () {
  return (
    <div className='footer-col'>
    <Button> i'm a button </Button>

      <style jsx>{`
          span {
              background-color: red;
              font-weight: 500;
          }
          .footer-col > :global(button) {
              color: red;
          }
      `}</style>
      </div>
  )
}

Btw, i also try to use the "external style" feature exactly like in the read me example, but it doesn't work for me.

this is because we haven't published styled-jsx 1.0.0 to npm yet

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rauchg picture rauchg  路  24Comments

thysultan picture thysultan  路  24Comments

slaskis picture slaskis  路  16Comments

rauchg picture rauchg  路  18Comments

rauchg picture rauchg  路  25Comments