ui icon indicating copy to clipboard operation
ui copied to clipboard

react + vite

Open ahmedRSA opened this issue 2 years ago • 7 comments

how can i use this with reactjs ??

ahmedRSA avatar May 05 '23 04:05 ahmedRSA

just follow the installation docs

update: haha Maybe the main problem is the setting of tailwind css, and the tailwind has official docs for vite as mentioned below

stimw avatar May 06 '23 10:05 stimw

Yes, although shadcn UI was created with next.js in mind, there's absolutely no issues when using it with react and vite.

follow setup guides from vite: https://vitejs.dev/guide/

and then follow the Manual Installation from the shadcn/ui kit: https://ui.shadcn.com/docs/installation

if you have any questions go ahead, if not please close the issue :)

F-PTS avatar May 06 '23 12:05 F-PTS

Also don't forget to follow Tailwind CSS guide after initial vite setup: https://tailwindcss.com/docs/guides/vite

Path aliasing did not work for me as described in the manual docs, and had to manually change the file imports to "../.." syntax.

Also modify tailwind.config.js for react instead of next.js

export default {
  content: [
-     "app/**/*.{ts,tsx}", 
-     "components/**/*.{ts,tsx}"
+    "./index.html",
+.   "./src/**/*.{js,ts,jsx,tsx}",
  ],
  theme: {},
  plugins: [],
}

puneet-sarhali avatar May 06 '23 20:05 puneet-sarhali

Also don't forget to follow Tailwind CSS guide after initial vite setup: https://tailwindcss.com/docs/guides/vite

Path aliasing did not work for me as described in the manual docs, and had to manually change the file imports to "../.." syntax.

Also modify tailwind.config.js for react instead of next.js

export default {
  content: [
-     "app/**/*.{ts,tsx}", 
-     "components/**/*.{ts,tsx}"
+    "./index.html",
+.   "./src/**/*.{js,ts,jsx,tsx}",
  ],
  theme: {},
  plugins: [],
}

Thanks, i beat up my head for hours setting up path alias did u found any solution?

ahmedRSA avatar May 07 '23 19:05 ahmedRSA

I think your issue was resolved @ahmedRSA if so can you please close the issue 👼🏼

tuminzee avatar May 09 '23 12:05 tuminzee

Path aliasing did not work for me as described in the manual docs, and had to manually change the file imports to "../.." syntax.

Hey @puneet-sarhali you might find this useful: https://github.com/aleclarson/vite-tsconfig-paths

rawnly avatar May 10 '23 10:05 rawnly

There is a repository created by shadcn for it, although it is not updated. https://github.com/shadcn/vite-template

It is somewhat broken, and I have a fork that actually works for you to use out of the box: https://github.com/tcortega/vite-template

Last time I checked it worked out.

tcortega avatar May 13 '23 05:05 tcortega

There is a repository created by shadcn for it, although it is not updated. https://github.com/shadcn/vite-template

It is somewhat broken, and I have a fork that actually works for you to use out of the box: https://github.com/tcortega/vite-template

Last time I checked it worked out.

thanks for this.

ahmedRSA avatar May 15 '23 11:05 ahmedRSA

There is a repository created by shadcn for it, although it is not updated. https://github.com/shadcn/vite-template

It is somewhat broken, and I have a fork that actually works for you to use out of the box: https://github.com/tcortega/vite-template

Last time I checked it worked out.

ofc he did!! love it that he did thought of this, True developer

CyrusZei avatar May 18 '23 14:05 CyrusZei

Yes, although shadcn UI was created with next.js in mind, there's absolutely no issues when using it with react and vite.

follow setup guides from vite: https://vitejs.dev/guide/

and then follow the Manual Installation from the shadcn/ui kit: https://ui.shadcn.com/docs/installation

if you have any questions go ahead, if not please close the issue :)

if followed all the steps mentioned by docs but still @ folder is creating outside src, how can i create that inside src?? image

image image

Znoy108x avatar Nov 05 '23 05:11 Znoy108x

Yes, although shadcn UI was created with next.js in mind, there's absolutely no issues when using it with react and vite. follow setup guides from vite: https://vitejs.dev/guide/ and then follow the Manual Installation from the shadcn/ui kit: https://ui.shadcn.com/docs/installation if you have any questions go ahead, if not please close the issue :)

if followed all the steps mentioned by docs but still @ folder is creating outside src, how can i create that inside src?? image image image

same thing

to fix (idk if we are even supposed to fix this "bug"), you can replace ./src with ./@/* in both your vite.config.js and tsconfig.json

jagadeesh-k-ti avatar Dec 26 '23 16:12 jagadeesh-k-ti

if followed all the steps mentioned by docs but still @ folder is creating outside src, how can i create that inside src??

Dont know if that would help since you posted this a few months ago but your tsconfig.json is wrong but that's an easy mistake to make, baseUrl and path are supposed to be in compilerOptions here's my tsconfig.json

{
  "compilerOptions": {
    "target": "ES2020",
    "useDefineForClassFields": true,
    "lib": ["ES2020", "DOM", "DOM.Iterable"],
    "module": "ESNext",
    "skipLibCheck": true,

    "moduleResolution": "bundler",
    "allowImportingTsExtensions": true,
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react-jsx",

    "strict": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noFallthroughCasesInSwitch": true,
    
    /* here */
    "baseUrl": ".",
    "paths": {
      "@/*": [
        "./src/*"
      ]
    }
  },
  "include": ["src"],
  "references": [{ "path": "./tsconfig.node.json" }],
}

Lxdovic avatar Jan 03 '24 11:01 Lxdovic

It took me a while to figure out how to use Shadcn with react and vite, even with the documentation. Here is what worked for me.

components.json

{
  "$schema": "https://ui.shadcn.com/schema.json",
  "style": "default",
  "rsc": true,
  "tsx": true,
  "tailwind": {
    "config": "tailwind.config.js",
    "css": "src/index.css",
    "baseColor": "slate",
    "cssVariables": false,
    "prefix": ""
  },
  "aliases": {
    "components": "src/components",
    "utils": "src/lib/utils"
  }
}

vite.config.ts

import path from "path"
import react from "@vitejs/plugin-react"
import { defineConfig } from "vite"

export default defineConfig({
  plugins: [react()],
  resolve: {
    alias: {
      "@": path.resolve(__dirname, "./src"),
    },
  },
})

adding the bit to tsconfig.json

"baseUrl": ".",
    "paths": {
      "@/*": [
        "./src/*"
      ]
    },

Folder Structure

image

React function imports

image

lalitx17 avatar Jul 01 '24 14:07 lalitx17

The document is very misleading, it has to be

   "components": "src/components",
    "utils": "src/lib/utils"

bluedusk avatar Jul 14 '24 13:07 bluedusk