Repository navigation
Get ssr styles as React Element - #736
Conversation
Generated by 🚫 dangerJS |
| } | ||
|
|
||
| toElement() { | ||
| return this.tags.map(tag => tag.toElement()) |
There was a problem hiding this comment.
The elements here will need to have a key. You should be able to just use cloneElement and add it here
There was a problem hiding this comment.
Hm yes but what should I use for keys? this.names.join('') in ServerTag.toElement?
There was a problem hiding this comment.
I set the key in toElement() instead.
There was a problem hiding this comment.
Just use the index, since this will only be used once and is thus static
There was a problem hiding this comment.
Hm fighting with Flow:
toElement() {
return this.tags.map((tag, index) => (
React.cloneElement(
tag.toElement(),
{ key: index },
)
))
}Gives error:
[flow] property `key` of object literal (Property not found in props of React element `style` See also: call of method `cloneElement`)
Seems like Flow doesn't like adding props to existing elements: meta/flow#1606
Any idea how to get around that?
| toElement() { | ||
| const attributes = { | ||
| [SC_ATTR]: this.names.join(' '), | ||
| [LOCAL_ATTR]: this.isLocal, |
There was a problem hiding this comment.
We're searching for el.getAttribute(LOCAL_ATTR) === 'true'. I'm not sure whether this works, if React just adds the property shorthand; Can you check please? :)
There was a problem hiding this comment.
The result written to file is something like this:
<style type="text/css" data-styled-components="biyTF hdZKtG gqDZGS clHZEQ yOoIr fUPLnt inUVIL fSmkBB kZQtlQ PIsWY fPJRNv jgRNmm TsYdo iZXahJ bDCuCY kUYNDb foZtll cqjtay fjMziR gqhtmN cEnRUY dhfSfb NkGiE hFJQDZ cOZxoD jYtjIs jskQpj fIEXtD isATaW btuKsl dReRJd dhLCfp gESlQT hoNHWU hRbfbz bMefOW cHPONr fFcRsM" data-styled-components-is-local="true">You mean it wouldn't work if React omits ="true"?
There was a problem hiding this comment.
I'm not sure, I guess it's possible
There was a problem hiding this comment.
Not sure I follow :)
| addComponent(componentId: string): void, | ||
| inject(componentId: string, css: string, name: ?string): void, | ||
| toHTML(): string, | ||
| toElement(): any, |
There was a problem hiding this comment.
I'd guess the type here should just be ReactElement or sth
|
For certain things, output seem to break. Example: .jhjnFP:before {
content: "";
}Is output as: .jhjnFP:before {
content: "";
}Also instead of: background-image: url(/22d13351d85a31fb85621052174c8493.jpg);I get: background-image: url('/22d13351d85a31fb85621052174c8493.jpg');Which looks like this when console.log in the template (before Gatsby/React outputs to file: Gatsby passes this through @KyleAMathews any idea? |
|
React escapes text automatically. You probably just need to use |
|
@KyleAMathews Of course, thanks! |
|
@philpl Any outstanding issues you want me to look at? |
917e62e to
6a3b595
Compare
Makes it possible to render `{sheet.getStyleElement()}` directly in <head> using React
6a3b595 to
8ddc6f0
Compare
Seems like phil is ok with these changes now
|
Merged and added a couple of small tweaks, then released as |
Proof of concept, would appreciate comments.
Makes it possible to render
{sheet.getStyleElement()}directly in using React. Current ssr solution requires that the extracted styles are injected into the html outside of React, for instance via Express string substitution. This isn't suitable in for example GatsbyJS.This needs tests, and proper Flow thingies.
With this change, I can do something like this in Gatsby: