-
Notifications
You must be signed in to change notification settings - Fork 7
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
Error when using the CSS prop with an object #240
Comments
oh you are right - objects are not supported @Mad-Kat can remember why we added the css prop docs like this? currently the css prop only works like this:
|
Ok thanks, good to know ! I was also wondering if it was possible to have dynamic values inside the css props instead of having to create a dedicated styled variable with props, when needed for quick inline styling on tags like whats possible with styled-components / emotion ? ❌ Not working example:export default function Home({ isDarkMode }: { isDarkMode: boolean }) {
return (
<p
css={css`
font-size: 20px;
font-weight: bold;
font-family: "Inter";
color: ${isDarkMode ? "white" : "black"};
`}
>
Text
</p>
);
}
✅ Working (but superfluous)I know it is possible as of now by using a ternary around the entire css prop, which may lead to code duplication (but at some point, it seems logical to create a styled component variable if needed...) export default function Home({ isDarkMode }: { isDarkMode: boolean }) {
return (
<p
css={
isDarkMode
? css`
font-size: 20px;
font-weight: bold;
font-family: "Inter";
color: white;
`
: css`
font-size: 20px;
font-weight: bold;
font-family: "Inter";
color: black;
`
}
>
Text
</p>
);
} |
Thanks a lot for posting this :) We're always happy for input.
I don't remember exactly. I assume it worked with our babel plugin, but doesn't work currently with the SWC plugin. I really need to overhaul the docs. 🙈
We implemented the css prop in a very limited scope. It only works on native elements and without any interpolation. To support the interpolation we would have to extract a styled component for you in the correct scope the same way as So #241 doesn't work, as it's already a styled component and our simple implementation just takes care of it on native HTML elements. I assume your problem is mainly that your codebase is full with the css prop and it's very tedious to migrate? Or what is the use case here? Is it only for the light-dark theming or do you need to have other dynamic properties? |
And btw: Thank you for the very clear issue |
Is the scope really an issue in this case? We don't move the code around so actually the scope should be unchanged - or am I wrong? import { styled, css } from "next-yak";
export const Example = () => {
return (
<p
css={css`
color: ${() => (isDarkMode ? "white" : "black")};
`}
>
Hello world
</p>
);
}; import { styled, css } from "next-yak";
export const Example = () => {
return (
<p
css={css`
${() => (isDarkMode && css`color: white`)};
`}
>
Hello world
</p>
);
}; |
Sorry I was wrong. I didn't have enough coffee 😅 You're right @jantimon this could work. |
Actually it does work to apply the style to the component, the only problem here as detailed on the other issue is the Typescript error that results on using the css prop. I forgot to add a screenshot there but the html result is correct with the font color applied. ✅ Working example using css prop on styled component/* @jsxImportSource next-yak */
import { css, styled } from "next-yak";
export default async function Test() {
return (
<div>
<Text
css={css`
color: red;
`}
>
test
</Text>
</div>
);
}
const Text = styled.p`
font-size: 20px;
font-weight: bold;
font-family: "Inter";
`; (but i have a typescript error in code and when building) ❌ Not working example with parent container with css propHowever if I try to have two elements with css props with one inside the other, the styling doesn't seem to be applied on the children, they seem to be inheriting the styling on the parent /* @jsxImportSource next-yak */
import { css, styled } from "next-yak";
export default async function Test() {
return (
<div
css={css`
background-color: blue;
padding: 20px;
`}
>
<p
css={css`
color: red;
`}
>
test
</p>
<p
css={css`
color: green;
`}
>
test
</p>
</div>
);
} 📄 HTML output:<div class="page_yak__b_FCE">
<p class="page_yak__b_FCE">test</p>
<p class="page_yak__b_FCE">test</p>
</div> 👨🎨 Css file output:.page_yak__b_FCE {
background-color: blue;
padding: 20px;
}
.page_yak-01__NSkmr {
color: red;
}
.page_yak-02__i6J5G {
color: green;
} |
Yes ! We were actively trying to find a replacement for our existing codebase using emotion (very similar to styled-components) but with something that would work inside Next and React Server Components. In the end yours checks the most boxes for us to try and have a gradual migration from existing codebase even if we need to change some things mostly on the css prop that we use copiously for quick inline styling with variables (that would help us a lot if you can find ways to apply dynamic properties but we could work around that). |
This looks like a bug we should fix. Thank you for posting it. Regarding some dynamic properties within css props, the idea of @jantimon should work and I will prepare a PR to make it possible. Thank you for clarifying in which situation you are. It always helps us to get a feel from the outside and where we should head next. |
With next-yak 4.0.2 you can now use logic inside css properties We also added it to our (not very meaningful) example: |
We fixed more issues with the css prop in Let us know if you find something else that doesn't work with the css prop. We're always happy to help & fix |
Resolved - please let us know if it does not work correctly |
When using the CSS Prop as an object, as shown in the docs here, it fails to compile and give a TypeError
✅ Working Example with the CSS Utility Function:
❌ Not working using as an object:
The text was updated successfully, but these errors were encountered: