tailwindcss: @apply doesn't work inside
Hi @adamwathan ,
I was working on a React component in NextJS and was using Tailwind to customize the designs. Putting the tailwind classes in className
works fine while using like :
<component className="tw-class-1 tw-class-2 tw-class-3 tw-class-4 tw-class-5 tw-class-6" />
But, since I am using too many classes for the component, and it will be a common style. I wanted to make it work by using @apply into another custom class. Something like :
<component className="custom-class" />
and CSS goes like :
.custom-class {
@apply tw-class-1 tw-class-2 tw-class-3 tw-class-4 tw-class-5 tw-class-6;
}
BTW I am using Styles as JSX. So, finally I want my component to look like :
import React from 'react';
const Component = (props) => (
<div>
<div className="custom-class">
</div>
<style jsx>
{`
.custom-class {
@apply tw-class-1 tw-class-2 tw-class-3 tw-class-4 tw-class-5 tw-class-6;
}
`}
</style>
</div>
);
export default Component;
But this doesn’t works out. Error I get is while inspecting the element
Can you help me out in understanding if I am doing anything wrong, or does it support working under <style jsx> or not? An alternate solution would be appreciated.
Thanks
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 24 (1 by maintainers)
@loicnestler @alexvilchis
This works perfectly fine in latest next.js and tailwindcss packages:
BTW there is no need to have PurgeCSS now since Tailwind includes it. Also, I haven’t updated the repo but I’ve tested this in a separate private repo and it just works.
Thanks @hacknug ,
The plugin you shared worked for me.
If you are using the plugin in nextJS then you need to use this plugin with
next/babel
preset. You can configure it like this in your.babelrc
:Hi guys, i have the same issue. @apply dont work for me inside style tag with jsx
deps:
"postcss": "^8.3.6", "tailwindcss": "^2.2.7",
// .babelrc { "presets": [ [ "next/babel", { "styled-jsx": { "plugins": ["styled-jsx-plugin-postcss"] } } ] ] }
The config above, dont work for me. 😦
Does anyone know how to do this and still keep SWC support? Seems like the moment you add a .babelrc file it switches back to using webpack.
Okay, got it working! For future reference the Next.js docs explain that if we want to modify the PostCSS config we need to replicate the default one and use an object syntax:
Here’s how a config would look like nowadays in order for it to work without problems with styled-jsx:
Here’s the deployment URL, the repo is the same link. Pretty neat.
Is postcss running on your project? Tailwind is nothing but a postcss plugin. so you’ll always need to process everything with it to get actual CSS code.
I have no idea how
<style jsx>
works but maybe this plugin would help? https://github.com/giuseppeg/styled-jsx-plugin-postcssFor me it was a bit confusing to fix the issue. First install
npm i -D styled-jsx-plugin-postcss
And then you need this in
.babelrc
file at the rrot of the projectAdding
"styled-jsx-plugin-postcss"
worked for me.@DaniGuardiola @codfish using the tailwindcss postcss7 compatibility build sorted my issues and now have taildwindcss@2 and next@10 working with styled-jsx
https://tailwindcss.com/docs/installation#post-css-7-compatibility-build
When I extend the babel presets I run into this error?
if I remove the
styled-jsx-plugin-postcss
everything works fine.Here are my files:
I have also tried to use the method that is described in the error message, but it gives the same error
@fillipvt It works! Thanks a lot
Thanks @hacknug for responding.
Yes, I have configured a postcss plugin in project. But I am not sure that it will take care of <style jsx> or not.
Here is my postcss.config.js
And I am also using @zeit/next-css, to process external CSS files, as it has to be server side rendered.
I will try the plugin you shared, and will get back to you.
Thanks