Skip to content

Instantly share code, notes, and snippets.

@infomiho
Last active October 17, 2024 23:56
Show Gist options
  • Save infomiho/b35e9366e16913949e13eaba0538f553 to your computer and use it in GitHub Desktop.
Save infomiho/b35e9366e16913949e13eaba0538f553 to your computer and use it in GitHub Desktop.
Using ShadCN with Wasp 0.12+

Using ShadCN with Wasp 0.12 and beyond

Note

We'll be loosly following the Vite instructions for ShadCN since Wasp is using Vite + React: https://ui.shadcn.com/docs/installation/vite We'll skip some of the steps since they don't apply or they are done differently with Wasp.

You won't be able to use the @ alias setup since it's not currently supported by Wasp. Because of this you'll need to adjust some imports when we generate components, but it should be fairly straightforward to do.

1. Enable TailwindCSS if you haven't already

Based on: https://wasp-lang.dev/docs/project/css-frameworks#enabling-tailwind-step-by-step

Add ./tailwind.config.cjs:

const { resolveProjectPath } = require('wasp/dev')

/** @type {import('tailwindcss').Config} */
module.exports = {
  content: [resolveProjectPath('./src/**/*.{js,jsx,ts,tsx}')],
  theme: {
    extend: {},
  },
  plugins: [],
}

Add ./postcss.config.cjs:

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

Run wasp start to make sure that npm dependencies are installed.

2. Temporarily set up the @ alias (which we'll remove later)

We need to temporarily setup the @ alias to pass the ShadCN preflight checks.

Adjust the tsconfig.json:

// =============================== IMPORTANT =================================
//
// This file is only used for Wasp IDE support. You can change it to configure
// your IDE checks, but none of these options will affect the TypeScript
// compiler. Proper TS compiler configuration in Wasp is coming soon :)
{
  "compilerOptions": {
    // ...
+   "baseUrl": ".",
+   "paths": {
+     "@/*": ["./src/*"]
+   }
  }
}

3. Setup ShadCN

Go into your project dir and run:

npx shadcn@latest init

Select the options like this:

✔ Preflight checks.
✔ Verifying framework. Found Vite.
✔ Validating Tailwind CSS.
✔ Validating import alias.
✔ Which style would you like to use? › New York
✔ Which color would you like to use as the base color? › Neutral
✔ Would you like to use CSS variables for theming? … yes
✔ Writing components.json.
✔ Checking registry.
✔ Updating tailwind.config.cjs
✔ Updating src/Main.css
✔ Installing dependencies.
✔ Created 1 file:
  - src/lib/utils.ts

4. Remove the @ alias

Adjust the tsconfig.json:

// =============================== IMPORTANT =================================
//
// This file is only used for Wasp IDE support. You can change it to configure
// your IDE checks, but none of these options will affect the TypeScript
// compiler. Proper TS compiler configuration in Wasp is coming soon :)
{
  "compilerOptions": {
    // ...
-   "baseUrl": ".",
-   "paths": {
-     "@/*": ["./src/*"]
-   }
  }
}

5. Adjust the components.json

Adjust the aliases in components.json to be:

{
  "$schema": "https://ui.shadcn.com/schema.json",
  "style": "new-york",
  "rsc": false,
  "tsx": true,
  "tailwind": {
    "config": "tailwind.config.cjs",
    "css": "src/Main.css",
    "baseColor": "neutral",
    "cssVariables": true,
    "prefix": ""
  },
  "aliases": {
-   "components": "@/components",
+   "components": "src/components",
-   "utils": "@/lib/utils",
+   "utils": "src/lib/utils",
-   "ui": "@/components/ui",
+   "ui": "src/components/ui",
-   "lib": "@/lib",
+   "lib": "src/lib",
-   "hooks": "@/hooks"
+   "hooks": "src/hooks"
  }
}

6. Let's add a new component

We'll add a button component with:

npx shadcn@latest add button

7. Adjust the utils import in button.tsx (for each component you add)

You'll notice that you now have a brand new button.tsx file in src/components/ui. We need to fix some import issues:

import * as React from "react"
import { Slot } from "@radix-ui/react-slot"
import { cva, type VariantProps } from "class-variance-authority"

-import { cn } from "s/lib/utils"
+import { cn } from "../../lib/utils"

8. Use the Button component

Now you are ready to use the Button component. That's it!

import './Main.css'

import { Button } from './components/ui/button'

export const MainPage = () => {
  return (
    <div className="container">
      <Button>This works</Button>
    </div>
  )
}
@SarkarKurdish
Copy link

Thanks

@grsh50
Copy link

grsh50 commented Jun 18, 2024

Now i have two location of button component. One in src/components/ui and another /.wasp/sdk/wasp/ext-src/components/ui/button

wasp should update this path /.wasp/sdk/wasp/ext-src/components/ui/button based on code in src/components/ui ?

@SarkarKurdish
Copy link

SarkarKurdish commented Jun 25, 2024

@grsh50

Now i have two location of button component. One in src/components/ui and another /.wasp/sdk/wasp/ext-src/components/ui/button

wasp should update this path /.wasp/sdk/wasp/ext-src/components/ui/button based on code in src/components/ui ?

Ignore /.wasp/sdk/wasp/ext-src/components/ui/button this is generated during runtime just focus on the one in the src folder

@grsh50
Copy link

grsh50 commented Jun 26, 2024

That's what I thought, but for some reason these files were not overwritten after recompilation
/.wasp/sdk/wasp/ext-src/components/ui/button

@infomiho
Copy link
Author

Come to the Wasp Discord and let's figure it out together https://discord.gg/rzdnErX

@arresejo
Copy link

arresejo commented Sep 9, 2024

It's seems the CLI library name has changed, so the new command would be npx shadcn@latest init

@infomiho
Copy link
Author

Updated the gist to reflect the new CLI name :)

@gevalinho
Copy link

I am using WSL I tried every resource I saw online but it did not work for me finally what worked was running npm i shadcn-ui (https://www.npmjs.com/package/shadcn-ui). However, after installation,
I encountered a high-severity issue related to lodash.template. I fixed it using npm audit fix --force.
Then I ran npx shadcn-ui init

This solution is for those using WSL with Windows.
shadcn

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment