|
| 1 | +# How do I add shadcn to a SAFE project? |
| 2 | + |
| 3 | +> Note: The SAFE.Template has recently been updated to move `package.json` and `package-lock.json`. If you are using a version of the template prior to this change, you will need to move those files into the `src/Client` directory. For reference, here is the [PR](https://github.com/SAFE-Stack/SAFE-template/pull/658) detailing this change. |
| 4 | +
|
| 5 | +Integrating Feliz.Shadcn into your SAFE Stack application is straightforward. The following example demonstrates how to integrate Shadcn copmonents within an existing SAFE app. |
| 6 | + |
| 7 | +We will be using the [Feliz.Shadcn](https://github.com/reaptor/feliz.shadcn) wrapper written by [reaptor](https://github.com/reaptor) |
| 8 | + |
| 9 | +#### 1. Setup Tailwind |
| 10 | +> Note: When you use the SAFE template you will already have Tailwind installed by default. You can skip this step. |
| 11 | +
|
| 12 | +Check out the following recipe here to install Tailwind: [Add Tailwind](https://safe-stack.github.io/docs/recipes/ui/add-tailwind/) |
| 13 | + |
| 14 | +#### 2. Configure import alias in tsconfig: |
| 15 | + |
| 16 | +Create a file named `tsconfig.json` in `/src/Client` and add the following: |
| 17 | + |
| 18 | +```json |
| 19 | +{ |
| 20 | + "files": [], |
| 21 | + "compilerOptions": { |
| 22 | + "baseUrl": ".", |
| 23 | + "paths": { |
| 24 | + "@/*": [ |
| 25 | + "./*" |
| 26 | + ] |
| 27 | + } |
| 28 | + } |
| 29 | +} |
| 30 | +``` |
| 31 | + |
| 32 | +#### 3. Update the `vite.config.mts` within `/src/Client` |
| 33 | + |
| 34 | +Add the `resolve` property below under the `defineConfig` |
| 35 | + |
| 36 | +``` |
| 37 | +export default defineConfig({ |
| 38 | + ... |
| 39 | + resolve: { |
| 40 | + alias: { |
| 41 | + "@": path.resolve(__dirname), |
| 42 | + }, |
| 43 | + }, |
| 44 | + ... |
| 45 | +}); |
| 46 | +``` |
| 47 | + |
| 48 | +#### 4. Install shadcn/ui |
| 49 | + |
| 50 | +> Note: ensure your node version is > 20.5.0 |
| 51 | +
|
| 52 | +Inside the `/src/Client` directory run: |
| 53 | +``` |
| 54 | +npx shadcn@latest init |
| 55 | +``` |
| 56 | + |
| 57 | +You will be asked a few questions to configure components.json |
| 58 | + |
| 59 | +#### 5. Add Feliz.Shadcn |
| 60 | + |
| 61 | +Inside the `/src/Client` directory run: |
| 62 | +``` |
| 63 | +dotnet add package Feliz.Shadcn |
| 64 | +``` |
| 65 | + |
| 66 | +#### 6. Start adding any shadcn component |
| 67 | + |
| 68 | +Specify first which components you want to use. |
| 69 | +You can find the list of available components [here](https://reaptor.github.io/Feliz.Shadcn/): |
| 70 | + |
| 71 | +Inside the `/src/Client` directory run: |
| 72 | +``` |
| 73 | +npx shadcn@latest add button |
| 74 | +``` |
| 75 | + |
| 76 | +#### 7. Use it in Feliz: |
| 77 | + |
| 78 | +```fsharp |
| 79 | +open Feliz.Shadcn |
| 80 | +
|
| 81 | +
|
| 82 | +let view = |
| 83 | + Shadcn.button [ |
| 84 | + prop.text "Button" ] |
| 85 | +
|
| 86 | +``` |
| 87 | + |
| 88 | +Congratulations, now you can use shadcn components! |
| 89 | +Further documentation can be found at [https://reaptor.github.io/Feliz.Shadcn/](https://reaptor.github.io/Feliz.Shadcn/) |
0 commit comments