Skip to content

Parser implementation #27

Closed
Closed

Description

@geelen

So I think I've got a vague sense of the scope of the syntax we support.

/* 
  Primary entry point, wraps everything in a hashed classname
  and returns a smart React component
*/
styled.tagName`
  simple: rules;
  @media (query) {
  }
  &:pseudo,
  &:selectors {}
  > nesting {
    as: well;
  }
`

/*
  Same syntax but can inject global CSS when you need it.
  (This could be a top-level export `global` instead of `styled.global`)
*/
styled.global`
html {
  font-size: 16px;
}
@font-face {
  probably: useful too;
}
`

/* 
  Special syntax for keyframes because they're global and need
  to be hashed like class names but aren't tied to a tag.
  Again, this syntax could totes change.
*/
styled.keyframes`
  0% { some: thing; }
  100% { something: else; }
`

Basically, at this point, that's totally valid CSS so an existing parser should work. But, it's not the whole story...

Interpolations

const size = 100;
const sizeWithUnits = '1rem';
const wholeRule = 'color: black;'
const multipleRules = `display: flex; flex-direction;`
const withNesting = `
  &:hover,&:active {
    text-decoration: underline;
  }
`

styled.tagName`
  width: ${size}px;
  margin: ${sizeWithUnits};
  ${wholeRule}
  ${multipleRules}
  ${withNesting}
`

These are all static interpolations. We can just call toString() on all these before the parser runs. So they're fine.

let ready = false;
const Tag = styled.tagName`
  ${ready ? 'display: block' : 'display: none'}
  ${props => `font-size: ${props.size}px;`}
  ${({bold}) => bold && 'font-weight: 900'}
`

export default () => {
  ready = true
  return <Tag size={18} bold/>
}

This is the problem, it follows on from my work in #24. I really think makes our approach vastly more powerful than the CSS-in-JS approaches I've seen, so I want to preserve this "delayed" rendering of these properties. As far as I can tell, the way I've used ready will never work because each interpolation is evaluated straight away. The only way to defer things is to interpolate a function:

let ready = false;
const Tag = styled.tagName`
  ${() => ready ? 'display: block' : 'display: none'}
`

That seems ok to me. If you interpolate a function, we defer it to render time and evaluate it every time. If you interpolate a value, we toString it and continue the parser.

We can either defer all CSS parsing until render time (which I think is a performance problem waiting to happen) or we can figure out a clever parser that parses the static bits and leaves holes for the dynamic bits. I would prefer doing it well, since we might find that it enables something to do with server-side rendering or build-time optimisations (like a Babel transform that extracts CSS a la Sheetify) but I don't have an idea of those quite yet.

Anyway, that's what we need I think. Now to invent a time machine to give me a chance to work on it!

Activity

  1. mxstbr commented on Sep 21, 2016

    @mxstbr
    Member

    As for global styles, let's just allow people to write that in any styled tag they want:

    styled.div`
    html {
      font-size: 16px;
    }
    `

    Why do we need a separate export?

  2. geelen commented on Sep 21, 2016

    @geelen
    MemberAuthor

    Because that parses as

    .hashed-class-abcdef1345 html {
    }

    You'd need to invent something like css module's :global to distinguish it. I prefer a separate export greatly

  3. mxstbr commented on Sep 21, 2016

    @mxstbr
    Member

    Right, but with CSS modules all tag name selectors aren't localized, we could do the same thing here?

  4. geelen commented on Sep 21, 2016

    @geelen
    MemberAuthor

    It's not localisation, it's scoping. .classname tagName is scoped, tagName {} is not. If you don't scope by default you can't do nesting.

  5. mxstbr commented on Sep 21, 2016

    @mxstbr
    Member

    Ah, right. Okay then styled.global is probably the best bet!

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