Replies: 30 comments 3 replies
-
This looks great! Is one of the goals of this to get documentation into the docs site? |
Beta Was this translation helpful? Give feedback.
-
Yup! After testing everything (including some more advanced cases) and fixing anything that's fixable by Astro, I believe we want to document what works, what doesn't work, and suggest alternatives. |
Beta Was this translation helpful? Give feedback.
-
I feel like it should be possible to fix The As for @matthewp Would it be possible for Astro to allow hooking into the rendering pipeline? That might solve both of these issues. Footnotes
|
Beta Was this translation helpful? Give feedback.
-
@mayank99 Since these are for React apps I think it makes the most sense that this is part of some API for the React renderer. That might also require core Astro changes, I'm not sure. There already is a hook into the rendering pipeline via the renderer API, so it might be possible to prototype this idea as a separate renderer before plugging it into the React render (although either approach is reasonable). You'd want it to work with streaming, it looks like styled-components has as solution for that: https://styled-components.com/docs/advanced#streaming-rendering |
Beta Was this translation helpful? Give feedback.
This comment has been hidden.
This comment has been hidden.
-
Another lib worth trying is |
Beta Was this translation helpful? Give feedback.
-
@nstepien I did look into astroturf but excluded it from the list because there is no vite/rollup plugin and the library does not look like it's maintained anymore. |
Beta Was this translation helpful? Give feedback.
-
I've not tried it myself, but the docs do mention a rollup plugin: https://4catalyzer.github.io/astroturf/setup |
Beta Was this translation helpful? Give feedback.
-
Also btw https://github.com/typestyle/typestyle worked fine when i tried it. |
Beta Was this translation helpful? Give feedback.
-
@osdiab I've added an example for typestyle and updated the table. Thanks for the recommendation. |
Beta Was this translation helpful? Give feedback.
-
@mayank99 if you have failing Stackblitz examples for the ones that are possibly bugs that would be helpful for debugging. |
Beta Was this translation helpful? Give feedback.
-
@matthewp You should be able to open the examples repo in stackblitz. https://stackblitz.com/github/mayank99/astro-css-in-js-tests/tree/main/styled-components Swap out the last part of the url with the name of the library you want to debug.
|
Beta Was this translation helpful? Give feedback.
-
In solidjs/solid-styled-components#27, I saw mentioning of running Basically, we need to put the |
Beta Was this translation helpful? Give feedback.
-
We are also evaluating Astro now for SSR at @RapidAPI and this seems a huge blocker, since we rely on |
Beta Was this translation helpful? Give feedback.
-
vanilla-extract now supports Astro! vanilla-extract-css/vanilla-extract#796 |
Beta Was this translation helpful? Give feedback.
-
I've updated the table with the most recent status. A lot more green now 💚 In addition to official
|
Beta Was this translation helpful? Give feedback.
-
Hey @mayank99 Thanks for this great overview 🙏 Edit: |
Beta Was this translation helpful? Give feedback.
-
I'm not sure if it's normal but there is some FOUC (in dev mode only) with Astro + CleanShot.2022-09-15.at.08.42.56.mp4No FOUC when building the app in production mode. |
Beta Was this translation helpful? Give feedback.
-
@jon301 This is related to the Vanilla Vite plugin itself, same behaviour can be observed in SvelteKit. |
Beta Was this translation helpful? Give feedback.
-
@mayank99 , thank you for great overview and for the Stackblitz examples! I needed to make Astro work with Emotion, because I use Material UI in my website. Even if I've could use also styled-components, the default approach of Emotion seemed interesting to me, pretty simple and no big changes needed. I tried your repo with examples and somehow the workaround for import styledImport, { CreateStyled } from '@emotion/styled';
let styled = typeof styledImport.default !== 'undefined' ? styledImport.default as any as CreateStyled : styledImport; Then I've got it working but with FOUC. Emotion didn't render So my second workaround was to patch There are 3 ways forward for Emotion that I see:
What do you think we should do? |
Beta Was this translation helpful? Give feedback.
-
@igorbt Thanks for looking into this, great work!
So in my repo I'm using a conditional vite config, and it doesn't seem to get the correct values. It works in prod (but not in dev) if I just do this:
This is actually great! I think I tried a variation of this and couldn't get it to work, so I'm happy you were able to. This is better than
I think this would help with styled-components too (as I mentioned earlier). I'm not sure what would be the best way to expose this to users, outside of asking them to make a customized version of the react integration.
I agree, it should be opt-in if astro decides to use @matthewp What do you think? |
Beta Was this translation helpful? Give feedback.
-
It seems advanced approach for Emotion SSR only works with |
Beta Was this translation helpful? Give feedback.
-
For styled-componets situation is almost the same as with emotion - there is no support for new React SSR API Actually React team stated pretty clear that the architectural changes for v18 will make it pretty hard for CSS-in-JS libs to get along: reactwg/react-18#110 and they even advice for "You could however build a CSS-in-JS library that extracts static rules into external files using a compiler". On a more pragmatic note, because Astro mainly uses SSG, the streaming support is not important, and using (deprecated) |
Beta Was this translation helpful? Give feedback.
-
I can confirm that the astro + solid-styled-components (goober solid wrapper) + twin.macro works nicely in both SSR and CSR mode, and also in both dev and prod settings |
Beta Was this translation helpful? Give feedback.
-
@mayank99 btw I've been using Panda CSS and it also has been really nice. And they have explicit Astro support |
Beta Was this translation helpful? Give feedback.
-
How does Astro still not support CSS-in-JS via SSR easily??? I am using I want to also try Linaria in Astro, but there seems to be limited documentation to integrate with Astro. Anyone try? |
Beta Was this translation helpful? Give feedback.
This comment was marked as off-topic.
This comment was marked as off-topic.
-
I just realized I never shared that I built my own CSS-in-JS library specifically for Astro and as a direct result of this issue. https://www.ecsstatic.dev/ I don't like promoting my own stuff, especially in an issue I opened myself, but I'm sharing it here because it looks like folks are still trying to find a solution to this day (I hope that's ok). It's not 1:1 to Styled-components, but it's zero runtime and very similar to the Emotion/Linaria |
Beta Was this translation helpful? Give feedback.
-
Hello, I was doing a project in Astro and I decided to use certain Material UI components that I think are unique and very necessary for my project until I realized that Material isn't supported by Astro. Since there is a Material component that is really important to my project, I have devised an alternative using self-redirect on-load. At least for me, the components in Material are rendered correctly on the first load or display of the website, if I reload the page the CSS styles disappear. After trying a rendertostring and rendertopipeable without success, I decided that reload page really is the best option right now if you want to keep the component's java script. Another problem is the operation of the scripts in Astro, because it emulates a SPA I think their operation is quite confusing, sometimes they only work the first time they are loaded (like Material components) instead of every time you view the site, and script threads are not killed on page change, I think This should be improved, and this is why for this solution I use a svelte or react component. Svelte:
The 40ms could be better, the page could be refreshed right when the page is initialized. then i use the component:
And material components should be client:idle to get better performance |
Beta Was this translation helpful? Give feedback.
-
I don't see it mentioned in the list at the top, but the standard import attributes don't work in Astro: import 'something/something.js' with { type: 'css' }
// will be imported as a javascript module, not css, even
// on Node 21+ which has support for `with`, so no css
// gets injected into the component. |
Beta Was this translation helpful? Give feedback.
-
It's not super clear which CSS-in-JS libraries work, so I'm creating this issue as sort of a place to start the conversation and document the current status.
Here's a few popular libraries that I know about (will keep this list updated):
styled.div is not a function
.Can be worked around with
client:only
or by usingbuildSsrCjsExternalHeuristics
andssr.noExternal
(will cause FOUC).styled.div is not a function
.Can be worked around with
client:only
or by using conditional default import (will cause FOUC). Can also patch@astrojs/react
.Named export 'styled' not found
.Can be worked around using
buildSsrCjsExternalHeuristics
andssr.noExternal
or by downgrading to v3.<style>
tag for SSR needs to be in React componentFrom what I understand, if a library doesn't work in astro, it's because of one or more of these reasons*:
*I could be wrong so would be nice if someone can verify the above points.
Additionally, here's what @FredKSchott would like to see (quote message from discord):
GitHub repo with examples:
https://github.com/mayank99/astro-css-in-js-tests
Participation
Beta Was this translation helpful? Give feedback.
All reactions