Skip to content
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

Styled Linaria Components #1219

Open
jantimon opened this issue Feb 27, 2023 · 5 comments
Open

Styled Linaria Components #1219

jantimon opened this issue Feb 27, 2023 · 5 comments
Labels
feature: proposal 💬 New feature proposal that needs to be discussed needs: triage 🏷 Issue needs to be checked and prioritized

Comments

@jantimon
Copy link

Describe the feature

What do you think about an approach which would allow to toggle class names and inject css variables like this?

const Title = styled.h1<{ x: number; children: React.ReactNode }>`
  display: block;
  ${styleWhen(({ x }) => x % 2 === 0, `color: red;`)}
  position: relative;
  top: ${cssVar(({ x }) => `${x}px`)};
`;

It's totally possible with typescript and might work very well with the upcoming css nesting proposal:

https://codesandbox.io/s/yacijs-p0xt3m?file=/src/App.tsx:111-315

Motivation

This idea would allow to connect css classes with any complex logic.
The runtime code would only keep the logic and the class names.

Possible implementations

https://codesandbox.io/s/yacijs-p0xt3m?file=/src/App.tsx:111-315

@jantimon jantimon added the feature: proposal 💬 New feature proposal that needs to be discussed label Feb 27, 2023
@github-actions github-actions bot added the needs: triage 🏷 Issue needs to be checked and prioritized label Feb 27, 2023
@Anber
Copy link
Collaborator

Anber commented Apr 25, 2023

The dynamic styling in Linaria is based on css variables, but they are suitable only for property values. So… it's just impossible to implement your approach.

Something similar has been discussed here #244

@Anber Anber closed this as completed Apr 25, 2023
@jantimon
Copy link
Author

as shown in the POC it is totally possible by pregenerating class names - soon we will have native css class nesting which would make that even easier

@Anber Anber reopened this Apr 25, 2023
@Anber
Copy link
Collaborator

Anber commented Apr 25, 2023

Your PoC requires parse styles to create a list of class names. It means that if we want to interpolate or wrap a styled component in another styled, we will have to evaluate its styles, whereas now we can just use a pre-generated class name. It is slow. However, we already do it in atomic. I'll keep this issue open for future discussion.

@jantimon
Copy link
Author

jantimon commented Apr 26, 2023

thank you for reopening the discussion and for further insights why this might not be a good idea 👍

I guess you are right - one very hard problem might be the order when wrapping components - or do you see any further issues?

@Anber
Copy link
Collaborator

Anber commented Apr 28, 2023

After giving it more consideration, I believe we can still utilize pre-generated class names for referencing and compositions. This will prevent us from having to do additional work during the evaluation stage. Therefore, your approach appears to be viable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature: proposal 💬 New feature proposal that needs to be discussed needs: triage 🏷 Issue needs to be checked and prioritized
Projects
None yet
Development

No branches or pull requests

2 participants