Component stylesheet support #130
Replies: 2 comments 6 replies
-
Great writeup. I'd love to see this implemented! For the current VM, we mostly use Styled Components for CSS. However, the current implementation has performance issues since we're forced to define the Styled Components inside of the component function body. This would be the proper way of using Styled Components in a typical React app: const MyStyledDiv = styled.div`
color: red;
`;
const MyComponent = () => {
return <MyStyledDiv />;
}; But the current VM implementation forces us into implementing it like so: const MyComponent = () => {
const MyStyledDiv = styled.div`
color: red;
`;
return <MyStyledDiv />;
}; You'll see all kinds of warnings and errors related to this non-standard use of Styled Components when running the current gateway locally. It also causes performance issues since these Styled Components are forced to re-initialize every render. Your proposal would be a great alternative that's simpler to use for most developers and avoids the performance issues I outlined with Styled Components. I have a question about how you're currently rendering this <div id="dom-michaelpeter.near/widget/HelloWorld" class="container-child">
<div>
<span>Hello</span> World
</div>
</div> HTML |
Beta Was this translation helpful? Give feedback.
-
I like this approach a lot, but I do wonder to what extent it's a stopgap for styled components. Do we expect this to be a reasonable alternative even after styled components are available? Can we see a use case where it's used in conjunction with styled components? Also once that CSS is on chain it may be difficult to stop supporting this in the future, potentially exacerbating schema/architecture changes down the line. Will we always need to check if Components have this CSS document at fetch time? If so then we'd need to keep checking on every fetch until we could deprecate this.
This might be getting too far into implementation details, but is the goal to build a stylesheet for an entire Component tree or inline the styles on individual Components at render time? The former seems like the better performance option but I don't know how that would actually work, while the latter seems like it would interfere with a future styled components implementation. |
Beta Was this translation helpful? Give feedback.
-
I've been kicking around an idea for how we might support a more vanilla CSS workflow. Essentially, can we support saving a stylesheet alongside component source?
Two main hurdles:
Potential solution
Alongside component src, the compiler provides the CSS saved on chain which is wrapped in a selector by the OWA such that the rules only apply to descendants of that component on the rendered DOM.
Example
Component:
michaelpeter.near/widget/HelloWorld
Source
Currently in BWE, this yields visible DOM of
So now let's say we have a stylesheet saved alongside the component source on chain which is the following:
We would convert that to something suitable for being injected on the OWA, like so
The process is pretty straightforward— we apply an id selector wrapper, then prefix every rule with
&
to make it a descendant selector (& docs)Note that we would need to inject the
all: initial
for every component, even those that don't supply stylesheets, since that is what prevents inheritance of any styles from components higher in the treeEager to hear thoughts on if there is anything CSS-wise I am missing which might make this impractical or infeasible
Beta Was this translation helpful? Give feedback.
All reactions