ShadCN is a powerful tool that combines the best of Tailwind CSS and Radix UI to supercharge your React projects. Tailwind CSS is a utility-first CSS framework that allows you to quickly build beautiful and responsive designs by applying classes directly to your HTML elements. Radix UI, on the other hand, provides a set of unstyled, low-level React components that you can use to create custom user interfaces.
By leveraging the strengths of both Tailwind CSS and Radix UI, ShadCN empowers you to create stunning React applications with minimal effort. In this tutorial, we will walk you through the process of setting up ShadCN in your React project and show you how to use its features to streamline your development workflow.
Step 1: Install ShadCN
To get started with ShadCN, you first need to install it in your React project. You can do this by running the following command in your terminal:
npm install shadcn
Once ShadCN is installed, you can import it into your React components and start using its utilities to style your UI.
Step 2: Set up Tailwind CSS
Before you can start using ShadCN, you need to set up Tailwind CSS in your project. To do this, you will need to create a tailwind.config.js file in the root of your project and add the following code to it:
module.exports = {
mode: 'jit',
purge: [
'./src/**/*.{js,jsx,ts,tsx}',
],
darkMode: false,
theme: {
extend: {},
},
variants: {},
plugins: [],
}
Next, you will need to create a styles.css file in your src folder and import Tailwind CSS into it:
@import 'tailwindcss/base';
@import 'tailwindcss/components';
@import 'tailwindcss/utilities';
Finally, you can import the styles.css file into your index.js file to apply Tailwind CSS styles to your React application:
import React from 'react';
import ReactDOM from 'react-dom';
import './styles.css';
import App from './App';
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root')
);
Step 3: Use ShadCN components
Now that you have set up Tailwind CSS in your project, you can start using ShadCN components to build your UI. ShadCN provides a set of styled Radix UI components that you can easily incorporate into your React components.
For example, you can use the Button component from ShadCN to create a custom button in your application. To do this, you can import the Button component into your component file and use it like this:
import React from 'react';
import { Button } from 'shadcn';
function App() {
return (
<div>
<Button variant="primary">Click me</Button>
</div>
);
}
export default App;
In this example, we are using the Button component from ShadCN with the primary variant to style the button in our UI. By using ShadCN components, you can quickly create visually appealing and responsive UI elements in your React application.
Step 4: Customize ShadCN styles
One of the key benefits of using ShadCN is the ability to easily customize the styles of Radix UI components using Tailwind CSS utilities. You can override the default styles of ShadCN components by adding Tailwind CSS classes to them.
For example, if you want to change the color of the primary button in our previous example, you can add a custom class to the Button component like this:
<Button className="bg-red-500 hover:bg-red-600">Click me</Button>
By adding the bg-red-500 class to the Button component, you can change the background color of the button to red. You can also add other Tailwind CSS classes to ShadCN components to further customize their styles according to your design requirements.
In conclusion, ShadCN is a powerful tool that combines the flexibility of Tailwind CSS with the simplicity of Radix UI to help you create stunning React applications with ease. By following the steps outlined in this tutorial, you can set up ShadCN in your project and start using its features to streamline your development workflow and build beautiful user interfaces.