|
| 1 | +--- |
| 2 | +comments: true |
| 3 | +last_checked_with_versions: |
| 4 | + Wasp: "0.21" |
| 5 | + Shadcn: 2026-02-16 |
| 6 | +--- |
| 7 | + |
| 8 | +# Shadcn |
| 9 | + |
| 10 | +## Setting up Shadcn in a Wasp project |
| 11 | + |
| 12 | +We'll be loosely following [the Vite instructions for Shadcn](https://ui.shadcn.com/docs/installation/vite) since Wasp is using Vite + React. Some of the steps don't apply, so we've adjusted them accordingly. |
| 13 | + |
| 14 | +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. |
| 15 | + |
| 16 | +### 1. Add Tailwind CSS |
| 17 | + |
| 18 | +If you haven't added Tailwind CSS to your Wasp project yet, follow the instructions in the [Tailwind CSS documentation](../../project/css-frameworks.md#tailwind) first. |
| 19 | + |
| 20 | +### 2. Temporarily set up the `@` alias |
| 21 | + |
| 22 | +We need to temporarily setup the `@` alias to pass Shadcn's "Preflight checks". We'll remove it later. |
| 23 | + |
| 24 | +Add the following lines to your `tsconfig.json`: |
| 25 | + |
| 26 | +```json title="tsconfig.json" |
| 27 | +{ |
| 28 | + "compilerOptions": { |
| 29 | + // ... |
| 30 | + // highlight-start |
| 31 | + "baseUrl": ".", |
| 32 | + "paths": { |
| 33 | + "@/*": ["./src/*"] |
| 34 | + } |
| 35 | + // highlight-end |
| 36 | + // ... |
| 37 | + } |
| 38 | +} |
| 39 | +``` |
| 40 | + |
| 41 | +### 3. Setup Shadcn |
| 42 | + |
| 43 | +Go into your project directory and run: |
| 44 | + |
| 45 | +```bash |
| 46 | +npx shadcn@latest init |
| 47 | +``` |
| 48 | + |
| 49 | +Select the options like this: |
| 50 | + |
| 51 | +```bash |
| 52 | +✔ Preflight checks. |
| 53 | +✔ Verifying framework. Found Vite. |
| 54 | +✔ Validating Tailwind CSS. |
| 55 | +✔ Validating import alias. |
| 56 | +✔ Which style would you like to use? › New York |
| 57 | +✔ Which color would you like to use as the base color? › Neutral |
| 58 | +✔ Would you like to use CSS variables for theming? … yes |
| 59 | +✔ Writing components.json. |
| 60 | +✔ Checking registry. |
| 61 | +✔ Updating tailwind.config.js |
| 62 | +✔ Updating src/Main.css |
| 63 | +✔ Installing dependencies. |
| 64 | +✔ Created 1 file: |
| 65 | +- src/lib/utils.ts |
| 66 | +``` |
| 67 | + |
| 68 | +### 4. Remove the `@` alias |
| 69 | + |
| 70 | +Remove the lines we added in the `tsconfig.json`: |
| 71 | + |
| 72 | +```diff title="tsconfig.json" |
| 73 | + { |
| 74 | + "compilerOptions": { |
| 75 | + // ... |
| 76 | +- "baseUrl": ".", |
| 77 | +- "paths": { |
| 78 | +- "@/*": ["./src/*"] |
| 79 | +- } |
| 80 | + } |
| 81 | + } |
| 82 | +``` |
| 83 | + |
| 84 | +### 5. Adjust the `components.json` |
| 85 | + |
| 86 | +Adjust the `aliases` in `components.json` to be: |
| 87 | + |
| 88 | +```json title="components.json" |
| 89 | +{ |
| 90 | + "$schema": "https://ui.shadcn.com/schema.json", |
| 91 | + // ... |
| 92 | + "aliases": { |
| 93 | + // highlight-start |
| 94 | + "components": "src/components", |
| 95 | + "utils": "src/lib/utils", |
| 96 | + "ui": "src/components/ui", |
| 97 | + "lib": "src/lib", |
| 98 | + "hooks": "src/hooks" |
| 99 | + // highlight-end |
| 100 | + }, |
| 101 | + } |
| 102 | +``` |
| 103 | + |
| 104 | +## Adding a component |
| 105 | + |
| 106 | +In this example, we'll add the `Button` component. |
| 107 | + |
| 108 | +### 1. Use the `shadcn` CLI to add the component |
| 109 | + |
| 110 | +We'll add a button component with: |
| 111 | + |
| 112 | +```bash |
| 113 | +npx shadcn@latest add button |
| 114 | +``` |
| 115 | + |
| 116 | +### 2. Adjust the `utils` import |
| 117 | + |
| 118 | +You'll notice that you now have a brand new `button.tsx` file in `src/components/ui`. We need to fix some import issues: |
| 119 | + |
| 120 | +```diff title="src/components/ui/button.tsx" |
| 121 | + import * as React from "react" |
| 122 | + import { Slot } from "@radix-ui/react-slot" |
| 123 | + import { cva, type VariantProps } from "class-variance-authority" |
| 124 | + |
| 125 | +-import { cn } from "src/lib/utils" |
| 126 | ++import { cn } from "../../lib/utils" |
| 127 | +``` |
| 128 | + |
| 129 | +### 3. Use the `Button` component |
| 130 | + |
| 131 | +That's it, now you are ready to use the `Button` component! |
| 132 | + |
| 133 | +```tsx title="src/MainPage.tsx" |
| 134 | +import "./Main.css"; |
| 135 | + |
| 136 | +import { Button } from "./components/ui/button"; |
| 137 | + |
| 138 | +export const MainPage = () => { |
| 139 | + return ( |
| 140 | + <div className="container"> |
| 141 | + <Button>This works</Button> |
| 142 | + </div> |
| 143 | + ); |
| 144 | +}; |
| 145 | +``` |
0 commit comments