-
-
Notifications
You must be signed in to change notification settings - Fork 183
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
Add initial support for linaria for static CSS interop #248
Conversation
thanks for the PR's, you got a good base ready this feature - 🤞 it can be done! |
I have given this a quick initial test with the This is working: import tw from 'twin.macro'
import { styled } from 'linaria/react'
const Button = styled.button`
${tw`bg-black`}
` But importing from twin doesn't work: import tw, { styled } from 'twin.macro'
const Button = styled.button`
${tw`bg-black`}
` Results in Array stylingThere's also a common syntax twin uses/advertises which is adding the items to an array. import tw from "twin.macro";
const Button = tw.button`bg-black`;
// ↓ ↓ ↓ ↓ ↓ ↓
import { styled as _styled } from "linaria/react";
const Button = _styled.button({
"--tw-bg-opacity": "1",
"backgroundColor": "rgba(0, 0, 0, var(--tw-bg-opacity))"
}); and also this: import tw, { styled } from "twin.macro";
const Button = styled.button([tw`bg-black`]);
// ↓ ↓ ↓ ↓ ↓ ↓
import { styled as _styled } from "linaria/react";
const Button = _styled.button([{
"--tw-bg-opacity": "1",
"backgroundColor": "rgba(0, 0, 0, var(--tw-bg-opacity))"
}]); |
Agreed, I haven't figured out the glue here but while I've got the example rendering linaria fully can't see any of the twin.macro ones working as expected. I don't understand the twin code well enough to determine the fix at the moment. |
I'm keen to explore this further, but I've got quite a bit on my plate at the moment so I'll have to circle back around and tackle it 👍 |
will it merged in near future? |
What is this PR missing? Can I help in any way? |
This PR was never completed and is still just a draft. Was hoping @ben-rogerson would pick it up or give some pointers. |
I'm planning to wait for version 4 to come out of beta before pursuing an integration. Unless the API is similar enough? |
I would be so excited to see twin.macro add support for a zero-runtime CSS library. With linaria V4 recently being released, I would love to help twin.macro implement support for linaria. @ben-rogerson, would you be open to PRs adding linaria support? Or is that something you would prefer to tackle on your own? |
Hey Luke - yeah definitely open to a PR - how about a new "Linaria + Emotion + TypeScript" example? As there's so many changes in the next As there's no
|
Note to whoever implements this following @ben-rogerson's comment about custom imports: global styles in Linaria work a little bit differently: https://github.com/callstack/linaria/blob/v4.0.0/docs/BASICS.md#adding-global-styles. EDIT: well, actually, it's kind of different: https://scottspence.com/posts/linaria-getting-started#global-style 🤷 the import for |
With global styles we should use the Something like this: import { globalStyles } from 'twin.macro'
import { css } from 'linaria'
export const globals = css(globalStyles) |
sounds good i'll put one together :) |
Started working on this but ran into an issue with vite and linaria. |
Ok I gave this a first shot in a public repo (codesandbox demo). I can add that code to the examples repo but there were so many things that didn't work that the default example app used in all the other examples didn't seem like a reasonable place to start. Feel free to fork my code. I can add a license of your choosing to it if that is an issue. But I don't place any claims on it at all. Note: The linaria -> vite integration only works with the beta version, not with the 4.0 release but the API between the last beta and the current release seems to be identical. This is due to this issue callstack/linaria#1044 Things that work
// array interpolation works 👍
css`${[tw`bg-red-500`, tw`text-sm`]}` Things that don't work
Potential issues Linaria styled tags allow for conditional styling via prop interpolation functions. // this works
const Title = styled.h1`
color: ${props => (props.primary ? 'tomato' : 'black')};
`;
// this will never work
const Title = styled.h1`
${props => (props.primary ? tw`text-red-500` : tw`text-black`)};
`; Linaria css interpolation is not linked to components and doesn't have access to state or props. If you want to condtionally apply styles you need to separate each potential style into a separate // this works
const Title = ({primary, children}) => {
return <h1 className={cx(primary ? css`${tw`text-red-500`}` : css`${tw`text-black`}`)}>{children}</h1>
}
// this will never work
const Title = ({primary, children}) => {
return <h1 className={css`${primary ? tw`text-red-500` : tw`text-black`}`}>{children}</h1>
} So in summary I'm optimistic that we can get this working but we probably need to do some leg work to get the css prop working. Most likely we want the css prop to accept any of |
if it's possible to implement support for the tw and css props without modifying tw.macro itself i could try to do that but i would need a little hint. |
There’s some great findings here Luke! Let's put this on hold for a little while until I'm through the v3 updates - both me and the repo will be better primed to pick this up again and complete the new |
Related discussion: