Closed fulminmaxi closed 3 years ago
CSS variables really offers some helpful features like handling sizes, but after some fiddling with it here are the things I discovered:
Some more info in this topic (it's vanilla js examples though): https://christianheilmann.com/2021/02/08/sharing-data-between-css-and-javascript-using-custom-properties/ I'm beginning to lean towards that idea
One idea about the issue with passing values via props: instead of doing that we could pass them directly vis CSS vars. The child would consume variables defined by the parent instead of using the prop
Done! 🎉
Currently our styling process is based on theme values set in some static files. This has been working fine for now, but I would like to discuss what might be an improved and more maintainable system. I propose we move our theme to use CSS variables, I hope that discussing and evaluating this proposal can help us improve our current theme regardless of if we end up adopting it.
What are CSS Variables? And how can they be useful to us?
From MDN:
In practical terms, if now we style a
Button
like this:With CSS Variables we would instead do this:
CSS Variables have several small benefits, which I'm not going to go into since I don't think are relevant for us:
The main benefit I see for us, which makes the idea worth exploring is the possibility of defining how things should change relative to screen size in one single point of the codebase. Let me give an example:
Suppose we want the text of whole app to shrink or grow based on screen size, and we want every component to have this behaviour. How would we do that now? As far as I can see, with our current system we would have 2 options for achieving this: a. Look up each component that defines a
font-size
, add media queries or afluidRange
to each component to scale it accordingly. b. Add media queries to the top level component, change theme values based on the results and switch up the theme in the theme provider for that.Option
a
is a nightmare to implement in terms of development experience, one might argue that if we wrap all the app text into aText
component than we have just one component to change, while true, I believe this is hard to enforce and a bad DX as well. For example you would have to do:instead of
Option
b
is more easy to implement but requires swapping out the theme, which would make every styled component re-render.With CSS Variables, it would look like this:
I find the CSS var option to be superior by far to both options in terms on maintainability. One aspect that would be fun to explore is if this also helps keep animations in sync, since CSS variables are animatable, it might be possible for example to add a fade animation to all text at once, without having to change components.
Downsides and possible workarounds
For now, I only came up with two possible downsides to this approach.
Regarding
1
, a quick workaround would be to define the theme values in a static file and use that to initialise CSS Variables, then take use this file to define a typed getter function for CSS variables, going a bit more into details:Then to use it in a component
And type safety is restored.
For point 2. I don't think there is a workaround, but I can't think of a single use case where we would change a global CSS variable at runtime in multiple components. And if we needed to do so, we could redefine the variable in the components that need it to prevent global conflicts.
Feedback
I'm sure there are some downsides I might have overlooked or ways to improve this proposal, so any feedback is welcome :)