New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
A mechanism to use css variable #3965
base: main
Are you sure you want to change the base?
Conversation
027a57e
to
207ae84
Compare
207ae84
to
f63dad1
Compare
I wonder if synthesizing css variables dynamically to avoid re-injecting styles would have some perf wins… like making it more automatic than this method so you don’t have to manually define anything. 🤔 At the moment, interpolations in styles lead to style branching which injects a lot of extra CSS. Leveraging variables in common scenarios that don’t require branching (like just updating a primitive value) could drastically trim down generated CSS for many projects. |
Then we have to move logics from separated Like this: const Comp = styled.div`
font-size: var(--fontSize);
`;
<Comp style={{'--fontSize': props.fontSize || "1rm" }} />
Exactly, but this PR can prevent any branching cause it relays on CSS variables and does not generate any other branch for dynamic values. please check this section:
|
@@ -154,6 +157,14 @@ function useStyledComponentImpl<Target extends WebTarget, Props extends Executio | |||
} | |||
} | |||
|
|||
propsForElement.style = varRules?.reduce( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When I used Linaria I had an issue that style tag was too long when you have a lot of props and thus the DOM tree in devtools was almost unreadable. Maybe it is worth to insert css variables in style tag like we have for plain styles?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you have any idea how?
for (let i = 0, len = chunk.length; i < len; i += 1) { | ||
const next = chunk[i]; | ||
if (isVar(next)) { | ||
result[i - 1] = result[i - 1] + 'var(--' + next.var + ')'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is one thing to think about - css variables accept default value, here you have no possibility to pass default value. However, I do not have ideas how to do that properly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We always can have a default value in another way, please have a look at this example:
const Comp = styled.div<{ $$fontSize?: string }>`
font-size: ${{ var: 'fontSize', compute: props => props.$$fontSize || "1rm" }};
`;
Here 1rm
is the default value as programmatically
This PR related to Support CSS Varaible cause:
styled-components
for dynamic valueHow can we use it?
To identify which prop is used for CSS variable purposes we can use
$$
prefix, but it is not required.Usage:
Then our component will be something like this:
and it just generates a style rule even if we pass any dynamic values:
That's mean if I pass a new value:
It can still rely on the previous style rule and it doesn't need to create a new style rule branch
Without this PR
Without this PR if we want to do something like the above example we should move logic inside the style to the component:
Style:
Component:
Despite it using CSS variables, it creates a style rule branch per any new value for
--fontSize