Home > Web Front-end > JS Tutorial > Enhance Your React Apps with ShadCn Utilities and Components

Enhance Your React Apps with ShadCn Utilities and Components

尊渡假赌尊渡假赌尊渡假赌
Release: 2025-02-08 13:36:12
Original
819 people have browsed it

This tutorial guides you through integrating ShadCn, a versatile React component library, into your projects. We'll cover setup, configuration, and customization, suitable for both beginners and experienced developers. The complete source code is available on GitHub.

Enhance Your React Apps with ShadCn Utilities and Components

Understanding ShadCn

ShadCn offers a rich collection of pre-built components and utilities, streamlining React development. Built upon Tailwind CSS and Radix UI, it provides high customizability and a seamless integration with Tailwind's utility-first approach.

Prerequisites

Before you begin, ensure you have a solid grasp of JavaScript and React.js, with Node.js installed. Familiarity with CSS, HTML, and Tailwind CSS is beneficial.

Creating a New React Application

Start by creating a new React project using Vite:

npm create vite@latest
Copy after login

Choose a project name and select TypeScript (recommended for optimal ShadCn compatibility). After project creation, navigate to the project directory and run:

npm install
npm run dev
Copy after login

Enhance Your React Apps with ShadCn Utilities and Components Enhance Your React Apps with ShadCn Utilities and Components

Integrating Tailwind CSS

ShadCn leverages Tailwind CSS for styling. Install it using:

npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init -p
Copy after login

Import Tailwind directives into your index.css:

@import "tailwindcss/base";
@import "tailwindcss/components";
@import "tailwindcss/utilities";
Copy after login

Configure path aliases in tsconfig.json:

/* Path resolution */
"baseUrl": ".",
"paths": {
  "@/*": ["./src/*"]
}
Copy after login

Install Node types and update vite.config.ts:

npm i -D @types/node
Copy after login
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"),
    },
  },
})
Copy after login

Installing and Configuring ShadCn

Install ShadCn:

npx shadcn-ui@latest init
Copy after login

Select your preferred options during the installation process.

Enhance Your React Apps with ShadCn Utilities and Components

Using ShadCn Components

Let's use the hover-card component as an example. Add it using:

npx shadcn-ui@latest add hover-card
Copy after login

Import and use it in your React component:

import { HoverCard, HoverCardContent, HoverCardTrigger } from "@/components/ui/hover-card";

// ... your component ...
Copy after login

Enhance Your React Apps with ShadCn Utilities and Components

Customizing ShadCn Components

Use Tailwind CSS classes to customize ShadCn components:

<HoverCard>
  <HoverCardTrigger className="rounded-xl text-white py-2 px-4 bg-slate-500">First Shadcn Component</HoverCardTrigger>
  <HoverCardContent className="font-bold text-slate-500 w-max">My first of many components</HoverCardContent>
</HoverCard>
Copy after login

Enhance Your React Apps with ShadCn Utilities and Components

Conclusion

Integrating ShadCn into your React workflow is straightforward. With the steps outlined above, you can leverage its powerful components and utilities to build efficient and user-friendly applications. Explore the ShadCn documentation and consider using pre-built React boilerplates for a quicker start.

The above is the detailed content of Enhance Your React Apps with ShadCn Utilities and Components. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template