Repository navigation
Closed as not planned
Labels
- P2: nice to haveNot breaking anything but nice to have (priority)
Description
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):
| Library | Status | Notes |
|---|---|---|
| styled-components | 🟡 Partially works | Prod build errors with: styled.div is not a function. Can be worked around with client:only or by using buildSsrCjsExternalHeuristics and ssr.noExternal (will cause FOUC). |
| emotion | 🟡 Partially works | Prod build errors with: 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. |
| linaria | 🟡 Partially works | Prod build errors with: Named export 'styled' not found. Can be worked around using buildSsrCjsExternalHeuristics and ssr.noExternal or by downgrading to v3. |
| stitches | 🟡 Partially works | <style> tag for SSR needs to be in React component |
| typestyle | ✅ Works | - |
| vanilla-extract | ✅ Works | - |
| solid-styled | 🟡 Partially works | Causes FOUC |
| styled-jsx | ❌ Doesn't work | No vite or rollup plugin, requires babel |
| compiled | ❌ Doesn't work | No vite or rollup plugin, requires babel |
From what I understand, if a library doesn't work in astro, it's because of one or more of these reasons*:
- no support for vite/rollup
- it requires babel
- it uses context
- it requires access to the renderer for SSR styles
- it doesn't work with new react 18 apis
*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):
- A table of which CSS-in-JS libraries are/aren't supported in Astro. If not supported, it would also list the reason. If supported, these could also have tests in the repo so that we don't break them in the future.
- Some time to go through the ones that aren't supported and try to fix any issues that are fixable by Astro.
- For any the css-in-JS libraries that still aren't supported after that, a clear error message inside of Astro core when it sees you try to import the package.
GitHub repo with examples:
Participation
- I am willing to submit a pull request for this issue.
Reacted by Alexander Ryzhikov, Enteleform, Shaun, Cristian Perez, igorbt, Shane Warren, Dylan Gattey, Niels van der Vegt, Stefan Luk, fourthquar and 45 more
Reacted by Kainoa Kanter
Reacted by Enteleform, Yuji Sugiura, Niels van der Vegt, Nate Moore, Wes Souza, Kacper Potyrała, Chris Swithinbank, Oliver Barnwell, peetzweg/, Anthony Luzquiños and 3 more
Reacted by Enteleform, YM, Niels van der Vegt, Benjamin Solum, Corbin Crutchley, Kacper Potyrała, Oliver Barnwell, Muhammad Bayu Kurnia Prakosa, Anthony Luzquiños, Steven Gustavo Lara and 1 more
Metadata
Metadata
Assignees
Labels
- P2: nice to haveNot breaking anything but nice to have (priority)
Activity
matthewp commentedon Aug 23, 2022
This looks great! Is one of the goals of this to get documentation into the docs site?
mayank99 commentedon Aug 23, 2022
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.
mayank99 commentedon Aug 23, 2022
I feel like it should be possible to fix
styled-componentsandemotion. The big problem with both of them is that their SSR1 strategy requires altering the html produced by react'srenderToString/renderToNodeStreammethods.The
styled-componentsdocs suggest wrapping the whole<App />with a provider but only on the server. Theoretically, it should be possible to do this for every island in astro, but we don't have access to react's render methods in userland and we cannot do this with a wrapper component.2As for
emotion, the docs suggest two strategies. The default approach is supposed to work without any additional configuration, but I'm still seeing FOUC3 (this might be worth investigating). The advanced approach is similar tostyled-componentsin that it needs to wrap the app in a provider and do a sort of "double render" to add the styles after the first render, and hydrate the styles on the client.@matthewp Would it be possible for Astro to allow hooking into the rendering pipeline? That might solve both of these issues.
Footnotes
SSR = server-side rendering but really I mean any form of prerendering, including static generation ↩
From sc docs: "sheet.getStyleTags() and sheet.getStyleElement() can only be called after your element is rendered. As a result, components from sheet.getStyleElement() cannot be combined with into a larger component." ↩
FOUC = flash of unstyled content ↩
matthewp commentedon Aug 23, 2022
@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
mayank99 commentedon Aug 24, 2022
nstepien commentedon Aug 24, 2022
Another lib worth trying is
astroturf, which would be good fit for this project just for the name alone 🙃https://github.com/4Catalyzer/astroturf
https://4catalyzer.github.io/astroturf/introduction/
mayank99 commentedon Aug 24, 2022
@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.
nstepien commentedon Aug 24, 2022
I've not tried it myself, but the docs do mention a rollup plugin: https://4catalyzer.github.io/astroturf/setup
osdiab commentedon Aug 25, 2022
Also btw https://github.com/typestyle/typestyle worked fine when i tried it.
mayank99 commentedon Aug 25, 2022
@osdiab I've added an example for typestyle and updated the table. Thanks for the recommendation.
matthewp commentedon Aug 25, 2022
@mayank99 if you have failing Stackblitz examples for the ones that are possibly bugs that would be helpful for debugging.
mayank99 commentedon Aug 25, 2022
@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
https://stackblitz.com/github/mayank99/astro-css-in-js-tests/tree/main/vanilla-extract
Swap out the last part of the url with the name of the library you want to debug.
dephiros commentedon Sep 7, 2022
In solidjs/solid-styled-components#27, I saw mentioning of running
extractCSSor similar methods for css-in-js libs. Did anyone take a look in something similar?This is my failed attempt at it trying to get goober working. As is, it will work only when
client:loadis enabled for a component: https://stackblitz.com/edit/github-rqtizn-easxa3?file=astro.config.mjsBasically, we need to put the
cssfromextractCsscalled to astyleinheadwith id_gooberbut now sure how to do that with trCoobaha commentedon Sep 7, 2022
We are also evaluating Astro now for SSR at @RapidAPI and this seems a huge blocker, since we rely on
emotionin our ui libkevinseabourne commentedon Sep 8, 2022
If you guys can get styled components to work with Astro I am totally going to make the switch. I love styled components, works perfectly with component based styling and makes your code so easy to read.
27 remaining items
igl commentedon Mar 15, 2023
Not having great styled-components support is such a deal breaker for astro :(
kedzior-io commentedon Mar 29, 2023
90% of my projects use MUI because it's just the best & most complete library out there.
This is literary the only thing that prevents me from switching to Astro.
dohomi commentedon Mar 29, 2023
Would love to see support for MUI as well
ndemasie commentedon Mar 29, 2023
Works like a charm 2023-03-29
Here is some docker help for anyone that needs
AngeloCore commentedon May 20, 2023
When should we expect emotion to be fully operational? This is the only problem that stops me from using astro atm
mayank99 commentedon May 22, 2023
just a reminder that any "+1"/"ETA?" or similar comments are unhelpful and spammy.
if you would like this issue resolved for your use case, the best way to do that is to work with the maintainers of both tools and try to contribute.
beyond that, i want to share two things:
renderToNodeStreamapi. see comment above from igorbt: Supporting CSS-in-JS #4432 (comment)kireerik commentedon May 30, 2023
Emotion works for static site generation (on the
serverside (and of course on theserverside in general as well)). Here is an example:related documentation:
https://emotion.sh/docs/install#vanilla
https://emotion.sh/docs/ssr#extractcritical
admhemed commentedon May 30, 2023
Anyone could figure out how to avoid FOUC on Next.js 13, App router, typescript, Twin macros?
AngeloCore commentedon May 31, 2023
Bro's asking a nextjs question in an astro issue 💀
kireerik commentedon Jun 17, 2023
So Emotion definitely works for static site generation (on the
serverside (and of course on theserverside in general as well)): Here is a full example:refo
example:
index/
style
index/index/site/
index/index/index.js#L4-L6
main/
header/
style/index.js
index.js
#L14-L19
#L29
gsipos commentedon Jun 23, 2023
Hello!
I think i found an another workaround for mui and emotion, but it is probably not very elegant.
So I basically render the React component with renderToString and extract the emotion css stuff and concatenate them together.
after this, I have an
ExtractEmotionStyles.astrofile:and with this I can just use it in other astro files, like this:
I'm fairly new to Astro and the SSG/SSR stuff, so please tell me if this solution is problematic.
grctest commentedon Sep 15, 2023
Mantine has FOUC, have to refresh the page after hyperlink transition for CSS to render.
alexturpin commentedon Sep 15, 2023
@grctest the next Mantine version, v7 currently in alpha, does away with CSS-in-JS altogether and works with Next.js app directory SSR. Upgrading to Mantine v7 when it's out will likely be the best solution going forward.
lilnasy commentedon Nov 21, 2023
Moving this issue to a discussion on our roadmaps repo.