Day 1.
What shadcn/ui is and the quick start.
- Shadcn/ui is not a component library. It is how you build your component library.
- You don't need to install the entire package and import the components like you do the traditional way, instead you only install the components you want.
- And the best part is it installs the component's code into your codebase, that way you have all the freedom to customize it however you want to fit into your design system.
- Read the get started docs for more on shadcn https://ui.shadcn.com/docs
Quick start
Installation and the process: https://ui.shadcn.com/docs/installation/next
- 1
Initialize an empty Next.js project.
- 2
Initialize shadcn.
bashnpx shadcn@latest init - 3
It will install some dependencies, let's talk about this one.
Class-Variance-Authority
https://cva.style/docs- CVA is a small library that helps you manage “variants” of CSS classes in components.
- Shadcn uses this library to create different variants of a component like theme, size, variant etc.
- 4
It will create a file
lib/utils.tslib/utils.tsimport { clsx, type ClassValue } from "clsx" import { twMerge } from "tailwind-merge" export function cn(...inputs: ClassValue[]) { return twMerge(clsx(inputs)) }This small function does a lot of things with the help of these two libraries:
- clsx : takes a mix of values like arrays, objects and strings and converts them into a clean, space-separated string for constructing a className.
- tailwind-merge : while writing tailwind utilities it is common to accidentally include multiple conflicting utilities (e.g.
p-2 p-4, accidentally used both), so tailwind-merge resolves this by removing the earlier conflicting classes and keeping the last applicable one (i.e.p-4).
What main use case did I learn from this?
For conditional styling in a React component I used to do it like this:
tsxclassName={`bg-red-400 ${isActive ? "bg-red-600" : ""} ${isDisabled ? "opacity-50" : ""}`}Now I can just do it like this using the cn function:
tsxclassName={cn("bg-red-400", isActive && "bg-red-600", isDisabled && "opacity-50")}- Making it easier to read by getting rid of template literals.
- 5
It will create a
components.jsonfile.Think of it as the configuration file for the shadcn CLI. It tells the CLI:
- Where to put generated components
- How to style them
- What conventions to follow (aliases, naming, tailwind config location, etc.)
- 6
It will overwrite the
globals.cssfile.By default it will use the light theme, so you might see your Next.js project go from dark theme to light theme. You can change this with the theme-provider which is provided in the docs https://ui.shadcn.com/docs/dark-mode/next
- 7
Now let's add a component, it's as simple as installing a package from the CLI.
bashnpx shadcn@latest add buttonThis will add
button.tsxatcomponents/ui/button.tsx - 8
Just import the component where you want and use it https://ui.shadcn.com/docs/installation/next
app/page.tsximport { Button } from "@/components/ui/button" export default function Home() { return ( <div> <Button>Click me</Button> </div> ) } // you can use variants, size to tweak the component