Repository navigation
Description
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
mxstbr commentedon Sep 21, 2016
As for global styles, let's just allow people to write that in any styled tag they want:
Why do we need a separate export?
geelen commentedon Sep 21, 2016
Because that parses as
You'd need to invent something like css module's
:globalto distinguish it. I prefer a separate export greatlymxstbr commentedon Sep 21, 2016
Right, but with CSS modules all tag name selectors aren't localized, we could do the same thing here?
geelen commentedon Sep 21, 2016
It's not localisation, it's scoping.
.classname tagNameis scoped,tagName {}is not. If you don't scope by default you can't do nesting.mxstbr commentedon Sep 21, 2016
Ah, right. Okay then
styled.globalis probably the best bet!