Skip to content

Order of CSS injection #1

Closed
Closed

Description

@geelen

Just hit a problem:

const SVG = styled.svg`
  line {
    stroke-width: 1px;
  }
`

const Horizontal = styled.g`
  line {
    shape-rendering:crispEdges;
  }
`

const Vertical = styled.g`
  line {
    stroke-width: 1.5px;
  }
`

export default () => (
  <SVG>
    <Horizontal>
      <line x1={0} y1={0} x2={width} y2={0}/>
    </Horizontal>
    <Vertical>
      <line x1={0} y1={0} x2={width} y2={height}/>
    </Vertical>
  </SVG>
)

Because Horizontal and Vertical get rendered before SVG, and I'm only injecting the styles on render, ._compile_from_Vertical line {} appears before ._compiled_from_SVG line {}. They have the same specificity of course and so SVG wins.

Injected source should be in the same order it is written, not in the order it's rendered.

Activity

  1. geelen commented on Sep 26, 2016

    @geelen
    MemberAuthor

    The above demo works now because of #29 but this one doesn't:

    import React from 'react'
    import styled from 'styled-components'
    
    const Outer = styled.div`
      padding: 1rem;
      p {
        color: red;
      }
    `
    
    const Inner = styled.div`
      p {
        color: blue;
      }
    `
    
    const AlternateInner = styled(Inner)`
      p {
        color: green;
      }
    `
    
    export default () => (
      <div>
        {/* This causes AlternateInner to be rendered, then Inner */}
        <AlternateInner/>
        {/* Then outer gets rendered, overwriting both of them */}
        <Outer>
          <p>This should be red</p>
          <Inner>
            <p>This should be blue</p>
          </Inner>
          <AlternateInner>
            <p>This should be green</p>
          </AlternateInner>
        </Outer>
      </div>
    )

    image

    If you drop the initial <AlternateInner/> it works:

    export default () => (
      <div>
        <Outer>
          <p>This should be red</p>
          <Inner>
            <p>This should be blue</p>
          </Inner>
          <AlternateInner>
            <p>This should be green</p>
          </AlternateInner>
        </Outer>
      </div>
    )

    image

    This is... going to be a pain to fix.

  2. geelen commented on Sep 26, 2016

    @geelen
    MemberAuthor

    Simpler example not using descendant tag selectors since those are going to hurt no matter what:

    const Outer = styled.div`
      padding: 1rem;
      color: red;
    `
    
    const Inner = styled.div`
      color: blue;
    `
    
    const AlternateInner = styled(Inner)`
      color: green;
    `
    
    export default () => (
      <div>
        <AlternateInner/>
        <Outer>
          This should be red
          <Inner>
            This should be blue
          </Inner>
          <AlternateInner>
            This should be green
          </AlternateInner>
        </Outer>
      </div>
    )

    image

    Again, commenting out <AlternateInner/> makes it work.

  3. geelen commented on Sep 26, 2016

    @geelen
    MemberAuthor

    Ah, I think componentDidUpdate will fire in the right order. So maybe we have to defer injection of the CSS until that point.

  4. mxstbr commented on Sep 26, 2016

    @mxstbr
    Member

    So maybe we have to defer injection of the CSS until [componentDidUpdate]

    Hmm, sounds like that might introduce flashes of unstyled content?

  5. geelen commented on Sep 26, 2016

    @geelen
    MemberAuthor

    I just implemented it, it doesn't seem to. I think we're good.

  6. geelen commented on Sep 26, 2016

    @geelen
    MemberAuthor

    The idea being that if you called styled(X) then Xs styles are injected before your own. That's the same guarantee composes gives you in CSS Modules.

  7. mxstbr commented on Sep 26, 2016

    @mxstbr
    Member

    Wouldn't that break in this case?

    const Comp = styled.something`
    styles: here;
    `;
    
    const SomeOtherComp = styled(Comp)`
    styles: overridden;
    `;
    
    // …
    
    <SomeOtherComp>
      <SomeComp />
    </SomeOtherComp>
  8. geelen commented on Sep 26, 2016

    @geelen
    MemberAuthor

    Nope, that should be fine.

  9. geelen commented on Sep 26, 2016

    @geelen
    MemberAuthor

    Interesting, by fixing the more complex case we break the original case :/

    Test cases:

    const SVG = styled.svg`
      line {
        stroke-width: 4px;
      }
    `
    
    const Vertical = styled.g`
      line {
        stroke-width: 1px;
      }
    `
    
    export default () => (
      <SVG>
        <line x1={10} y1={10} x2={90} y2={10}/>
        <Vertical>
          <line x1={10} y1={10} x2={90} y2={90}/>
        </Vertical>
      </SVG>
    )
    const Outer = styled.div`
      padding: 1rem;
      color: red;
    `
    
    const Inner = styled.p`
      color: blue;
    `
    
    const AlternateInner = styled(Inner)`
      color: green;
    `
    
    export default () => (
      <div>
        <AlternateInner/>
        <Outer>
          This should be red
          <Inner>
            This should be blue
          </Inner>
          <AlternateInner>
            This should be green
          </AlternateInner>
        </Outer>
      </div>
    )

    If we inject the CSS during componentWillReceiveProps we get CSS injected as Components get rendered:

    image

    If we wait until componentDidUpdate we get:

    image

    So... I think we need to actually go off the source order of the style.tagName calls then inject the CSS into that spot. Having a separate style tag for each one would work, but 😱😱😱

  10. geelen commented on Sep 26, 2016

    @geelen
    MemberAuthor

    Mind you, the only reason that SVG example breaks is because there are two descendant tag selectors (which is not good practice), so I'm going to ship the version that works for const AlternateInner = styled(Inner) (i.e. componentDidUpdate).

    I did just confirm that if the styles have the right specificity then the picture is right for both examples:

    image

    I did that by repeating the class selectors N times where N is the order of their definition. But we can't do that in production, so we have to insert rules out-of-order. Glamor can't do that at the moment, and I'm a bit worried about the pef implications (one rule injected at the top could invalidate A LOT of styles). But if I can figure out some other approach I will.

  11. geelen commented on Sep 26, 2016

    @geelen
    MemberAuthor

    Woo, found a manual escape hatch:

    const Vertical = styled.g`
      && line {
        stroke-width: 1px;
      }
    `

    This generates .xxx.xxx line which lifts its specificity above .yyy line (from SVG above), so tbh I think this is "good enough" to consider this option closed.

    In fact you can actually use && { some: thing; } to target the element itself (not just descendants). In case you have some greedy rules outside (like html p) which would override a styled.p with a single class name. This is great, since we don't have to design good JS to cover bad CSS, we can just give folks a way to generate even worse CSS 👍

  12. mxstbr commented on Sep 26, 2016

    @mxstbr
    Member

    && ≈≈ aphrodite/important – I like it!

  13. geelen commented on Sep 28, 2016

    @geelen
    MemberAuthor

    So this didn't stay solved for long. I found that server-side rendering wasn't firing the componentDidMount, so we can't use that to inject.

  14. reopened this on Sep 28, 2016
  15. added a commit that references this issue on Nov 20, 2016
  16. added a commit that references this issue on May 28, 2017
    2662c9f
  17. locked as resolved and limited conversation to collaborators on Nov 16, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions