Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

not able to use custom classes in @apply in scss file tailwind nextjs project?

I'm trying to use a custom class overflow:inherit as @apply overflow-inherit in tailwind next.js project but it's throwing error. However, I can @apply pre-built tailwind classes like @apply flex flex-col md:h-full h-screen; but not custom.

Full repo: https://github1s.com/GorvGoyl/Personal-Site-Gourav.io

tailwind.scss:

@tailwind base;
@tailwind components;
@tailwind utilities;

@layer utilities {
  @variants responsive {
    .overflow-inherit {
      overflow: inherit;
    }
  }
}

project.module.scss:

.css {
  :global {
    .wrapper-outer {
      @apply overflow-inherit; //trying to apply custom utility
    }
  }
}

Error:

wait  - compiling...
event - build page: /next/dist/pages/_error
error - ./layouts/css/project.module.scss:4:6
Syntax error: C:\Users\1gour\Personal-Site\project.module.scss The `overflow-inherit` class does not exist, but `overflow-hidden` does. If you're sure that `overflow-inherit` exists, make sure that any `@import` statements are being properly processed before Tailwind CSS sees your CSS, as `@apply` can only be used for classes in the same CSS tree.

  2 |   :global {
  3 |     .wrapper-outer {
> 4 |       @apply overflow-inherit;
    |      ^
  5 |     }
  6 |   }

postcss.config.js:

module.exports = {
  plugins: {
    tailwindcss: {},
    autoprefixer: {},
  },
};

  • "next": "^10.0.7",
  • "tailwindcss": "^2.0.3",
  • "sass": "^1.32.8",
  • "postcss": "^8.2.6",
like image 358
GorvGoyl Avatar asked Mar 15 '21 14:03

GorvGoyl


People also ask

Can I use Tailwind in SCSS?

Can I use Tailwind CSS with sass? Since Tailwind is a PostCSS plugin, there's nothing stopping you from using it with Sass, Less, Stylus, or other preprocessors, just like you can with other PostCSS plugins like Autoprefixer.

Can I use CSS modules with Tailwind?

We use CSS variables extensively within Tailwind itself, so if you can use Tailwind, you can use native CSS variables.

What is PostCSS Tailwind?

Tailwind CSS works by scanning all of your HTML files, JavaScript components, and any other templates for class names, generating the corresponding styles and then writing them to a static CSS file.


1 Answers

I had the same problem on my Laravel project.

I decided that postcss is not enough for me. So in the webpack.mix.js I deleted the following

mix.postCss('resources/css/app.css', 'public/css', [
    require('postcss-import'),
    require('tailwindcss'),
    require('autoprefixer'),
]);

and replaced it with sass like this

mix.sass("resources/css/app.scss", "public/css").options({
    postCss: [
        require('postcss-import'),
        require('tailwindcss'),
        require('autoprefixer'),
    ], });

Now, I got a number of cool features including the one you want

I don't even need to use @layer

in my scss file I can combine @apply and @extend and it works

.btn-my-theme{
    @apply rounded border py-2 px-3 shadow-md hover:shadow-lg;
}

.btn-my-primary{
    @extend .btn-my-theme;
    @apply text-blue-600 bg-blue-200 hover:bg-blue-100 border-blue-500;
}

.btn-my-secondary{
    @extend .btn-my-theme;
    @apply text-gray-600 bg-gray-200 hover:bg-gray-100 border-gray-500;
}
like image 88
Yevgeniy Afanasyev Avatar answered Nov 13 '22 05:11

Yevgeniy Afanasyev