Published on

Fixing Shadcn UI Installation Breaking Tailwind CSS

Authors
  • Name
    Ripal & Zalak
    Twitter

Fixing Shadcn UI Installation Breaking Tailwind CSS

When using Shadcn UI in a Next.js project, you might encounter issues where Tailwind CSS stops working. This guide explores the reasons behind these conflicts and provides effective solutions to restore your Tailwind setup.


Understanding the Problem

Installing Shadcn UI with npx shadcn-ui init can sometimes overwrite your existing Tailwind CSS configuration or global styles. This can lead to unexpected behavior, such as:

  • Tailwind styles not applying.
  • Overwritten tailwind.config.js settings.
  • Issues with dark mode or custom utilities.

Common Symptoms

  • Tailwind styles disappear after Shadcn UI installation.
  • Dark mode stops working.
  • Custom theme extensions are removed or replaced.

Diagnosing the Issue

1. Check Your Tailwind Configuration

After running npx shadcn-ui init, inspect your tailwind.config.js file. Ensure that your content paths and theme extensions are intact. A typical issue is missing directories in the content array.

Example of Missing Content Directories:

content: ['./pages/**/*.{ts,tsx}', './components/**/*.{ts,tsx}', './app/**/*.{ts,tsx}']

2. Verify globals.css Import

Ensure your globals.css file is correctly imported in your main layout file (_app.tsx or layout.tsx).

Example:

import './globals.css'

3. Check for Overwritten Plugins

If you use Tailwind plugins, such as @tailwindcss/forms, verify that they are still included in the plugins array.


Solutions

1. Update Tailwind Content Paths

If Shadcn UI components are not being styled, add the relevant paths to your tailwind.config.js file.

Fixed Configuration:

module.exports = {
  darkMode: ['class'],
  content: [
    './pages/**/*.{ts,tsx}',
    './components/**/*.{ts,tsx}',
    './app/**/*.{ts,tsx}',
    './src/**/*.{astro,html,js,jsx,md,mdx,svelte,ts,tsx,vue}', // Include src folder
    './@/**/*.{ts,tsx}', // Include @ folder
  ],
  theme: {
    extend: {
      // Custom theme settings
    },
  },
  plugins: [require('@tailwindcss/forms'), require('tailwindcss-animate')],
}

2. Preserve Custom Styles

Before running npx shadcn-ui init, back up your tailwind.config.js file. After initialization, merge the new settings with your existing configuration.

Steps:

  1. Backup Configuration:
    cp tailwind.config.js tailwind.config.backup.js
    
  2. Run Initialization:
    npx shadcn-ui init
    
  3. Merge Settings: Compare tailwind.config.js with tailwind.config.backup.js and restore any missing settings.

3. Add Missing Imports

Ensure your globals.css or any other CSS files required by Tailwind are properly imported.

Example Import:

import './globals.css'

4. Restore Plugins

If Shadcn UI overwrites your plugins array, re-add missing plugins.

Example:

plugins: [require('@tailwindcss/forms'), require('tailwindcss-animate')]

5. Test with a Fresh Setup

If debugging existing projects proves challenging, create a new Next.js project and test Shadcn UI and Tailwind integration from scratch.

Steps:

npx create-next-app@latest my-project
cd my-project
npm install tailwindcss postcss autoprefixer
npx tailwindcss init
npx shadcn-ui init

Preventing Future Issues

  • Commit Changes: Always commit your code before major installations to easily revert changes.
  • Document Configuration: Maintain detailed comments in tailwind.config.js to track customizations.
  • Test in Isolation: Test third-party library installations in isolated environments before integrating into your main project.

Conclusion

Shadcn UI is a powerful tool, but its installation can disrupt Tailwind CSS if not handled carefully. By understanding common pitfalls and applying these solutions, you can seamlessly integrate Shadcn UI into your Next.js projects without breaking Tailwind CSS.

Key Takeaways

  • Verify and update your tailwind.config.js file after Shadcn UI installation.
  • Preserve custom settings and plugins by backing up configurations.
  • Ensure global styles and imports are intact.

Follow these steps to restore and enhance your development workflow with Shadcn UI and Tailwind CSS!