Shadcn UI installation breaks Tailwind CSS
Asked Answered
D

13

15

Shadcn UI (https://ui.shadcn.com/) was working fine until I just for a couple weeks until yesterday, when I ran my NextJS app in my local host and none of the tailwind was working. To debug the issue, I created a blank NextJS 13 app in a completely new file location, and everything worked fine; tailwind was working on the default nextJS 13 page. I then ran

npx shadcn-ui init

without installing any of the components. Which did not spit out any errors, but then none of the tailwind styling worked anymore.

my tailwind.config.js after instillation:

/** @type {import('tailwindcss').Config} */
module.exports = {
  darkMode: ["class"],
  content: [
    './pages/**/*.{ts,tsx}',
    './components/**/*.{ts,tsx}',
    './app/**/*.{ts,tsx}',
    ],
  theme: {
    container: {
      center: true,
      padding: "2rem",
      screens: {
        "2xl": "1400px",
      },
    },
    extend: {
      colors: {
        border: "hsl(var(--border))",
        input: "hsl(var(--input))",
        ring: "hsl(var(--ring))",
        background: "hsl(var(--background))",
        foreground: "hsl(var(--foreground))",
        primary: {
          DEFAULT: "hsl(var(--primary))",
          foreground: "hsl(var(--primary-foreground))",
        },
        secondary: {
          DEFAULT: "hsl(var(--secondary))",
          foreground: "hsl(var(--secondary-foreground))",
        },
        destructive: {
          DEFAULT: "hsl(var(--destructive))",
          foreground: "hsl(var(--destructive-foreground))",
        },
        muted: {
          DEFAULT: "hsl(var(--muted))",
          foreground: "hsl(var(--muted-foreground))",
        },
        accent: {
          DEFAULT: "hsl(var(--accent))",
          foreground: "hsl(var(--accent-foreground))",
        },
        popover: {
          DEFAULT: "hsl(var(--popover))",
          foreground: "hsl(var(--popover-foreground))",
        },
        card: {
          DEFAULT: "hsl(var(--card))",
          foreground: "hsl(var(--card-foreground))",
        },
      },
      borderRadius: {
        lg: "var(--radius)",
        md: "calc(var(--radius) - 2px)",
        sm: "calc(var(--radius) - 4px)",
      },
      keyframes: {
        "accordion-down": {
          from: { height: 0 },
          to: { height: "var(--radix-accordion-content-height)" },
        },
        "accordion-up": {
          from: { height: "var(--radix-accordion-content-height)" },
          to: { height: 0 },
        },
      },
      animation: {
        "accordion-down": "accordion-down 0.2s ease-out",
        "accordion-up": "accordion-up 0.2s ease-out",
      },
    },
  },
  plugins: [require("tailwindcss-animate")],
}

my utils.ts after instillation

import { ClassValue, clsx } from "clsx"
import { twMerge } from "tailwind-merge"
 
export function cn(...inputs: ClassValue[]) {
  return twMerge(clsx(inputs))
}

the default page after instillation

EDIT: after some testing, the issue seems to be coming from the globals.css and tailwind.config.js, still not sure what about them though.

Dump answered 19/6, 2023 at 15:53 Comment(1)
I think you would want this import { clsx, type ClassValue } from "clsx" in your utils.ts?Member
T
15

In my case the Shadcn components did not find the styles exported by tailwind. As answered by @moyindavid the problem is in tailwind.config.

Solution: Add the '@' folder to the exports of the tailwind attributes.

module.exports = {
   darkMode: ["class"],
   content: [
     './pages/**/*.{ts,tsx}',
     './components/**/*.{ts,tsx}',
     './app/**/*.{ts,tsx}',
     './@/**/*.{ts,tsx}', // <- HERE
     ],
Thorlay answered 28/8, 2023 at 20:13 Comment(0)
M
7

If your case is similar to mine and you used the src folder to wrap the app folder, the problem comes from the tailwind config file.

/** @type {import('tailwindcss').Config} */
module.exports = {
  darkMode: ["class"],
  content: [
    './pages/**/*.{ts,tsx}',
    './components/**/*.{ts,tsx}',
    './app/**/*.{ts,tsx}',
    ],

This is what you have. However, the content maps to the wrong directories. Update the strings in the content array using this.

content: [
    './src/app/**/*.{ts,tsx}', './src/components/**/*.{ts,tsx}',
    ],
Multicolored answered 21/6, 2023 at 12:21 Comment(1)
This sadly did not resolve my issue. For some reason, it turned out I had to add the line @import './globals.css' in my index.css that resided in my src folder. For context, my src folder was on the same level as all my configs + held the globals.css file. So if anyone is still having issues, maybe try this?Grecism
S
2

I came across a similar problem, where my form inputs started behaving in a weird way after the shadcn installation. The problem is that the installation overrides the plugins array, on the tailwind configuration file, and in my case, removed the @tailwindcss/forms. So, writing it back solved the problem. In tailwind.config.js:


module.exports = {
...
plugins: [require("@tailwindcss/forms"), require("tailwindcss-animate")],
}
Soche answered 28/6, 2023 at 0:2 Comment(0)
F
1

I ran into this problem multiple times. npx shadcn-ui init will overwrite your tailwind.config.js So remember to backup your previous settings and then merge it manually.

Fishmonger answered 13/11, 2023 at 6:9 Comment(0)
C
1

The reason I have found for this is because sometimes when you run

npx shadcn-ui@latest init

command, it might override your glabal css - check to make sure your previous rules are persisted

Also check the tailwind.config.ts file to make settings are persisted especially the theme.extend.colors - this might be completely overwritten. Always a good idea to commit prior to making major changes to existing projects that way you can have a reference.

Criminality answered 5/2, 2024 at 18:22 Comment(0)
S
0

In my case it broke Tailwind dark mode. I bring back by simply changing darkMode property to media instead of class.

Official Documentation:

If you want to support toggling dark mode manually instead of relying on the operating system preference, use the class strategy instead of the media strategy

Scotfree answered 3/9, 2023 at 3:26 Comment(0)
C
0

If your css is not being applied, verify if you have imported globals.css in src/layout.tsx

Calhoun answered 27/4, 2024 at 5:8 Comment(0)
F
0

fix in tailwind.config.js

/** @type {import('tailwindcss').Config} */
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}',
    './@/**/*.{ts,tsx}',
  ],
Flaggy answered 27/4, 2024 at 22:33 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Orbicular
R
0

If you are not using Typescript:

module.exports = {
  content: [
    './pages/**/*.{ts,tsx,js}',
    './components/**/*.{ts,tsx,js}',
    './app/**/*.{ts,tsx,js}',
    './@/**/*.{ts,tsx,js}',
  ],
...
}
Reimer answered 10/5, 2024 at 20:25 Comment(0)
T
0

In my case, After installing shadcn, It created one more file "tailwind.config.js" I deleted it. It fixed all the issues. (I'm using typescript)

Townshend answered 30/5, 2024 at 10:50 Comment(0)
N
0

I had this problem when I merged main branch to my customBranch I was working on in a github repo. If you have something similar like this, you can fix it by copying all the custom styles in your tailwind.config.ts file and doing

npx shadcn-ui@latest init

This will kind of factory reset your tailwind config file, you can then paste the manual styling in it which you copied earlier and it should work

Namecalling answered 18/6, 2024 at 8:31 Comment(0)
A
0

Make sure that you don't have tailwind.config.js already there if you are using typescript.

Atlantean answered 22/6, 2024 at 18:51 Comment(0)
D
0

A couple other things to check are:

  1. Are you importing your css file in the main.tsx or App.js? import './index.css
  2. Is your components.json file pointing to the correct css file? "css": "src/index.css"
Dissever answered 14/8, 2024 at 17:47 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.