Installation and the process: https://ui.shadcn.com/docs/installation/next
Initialize an empty nextjs project.
Initialize shadcn
npx shadcn@latest init
It will install some dependencies, lets talk about this one.
It will create a file libs/utils.ts
import { clsx, type ClassValue } from "clsx"
import { twMerge } from "tailwind-merge"
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
}
for a conditional rendering in a react component I use to do like this
className = ` bg-red-400 ${isActive ? "bg-red-600" : ""} ${isDisabled ? "opacity-50 : ""}
now I can just do like this using this cn function
className = {cn("bg-red-400", isActive && "bg-red-600", isDisabled && "opacity-50"
Making it easier to read by getting rid of template literals.
It will create a components.json file
It will create overwrite global.css file
Now lets add a component, its as simple as installing a package from CLI
npx shadcn@latest add button
Just import the component where you want and use it https://ui.shadcn.com/docs/installation/next
import { Button } from "@/components/ui/button"
export default function Home() {
return (
<div>
<Button>Click me</Button>
</div>
)
}
//you can use varients, size to tweak the component