Introducing Shadcn UI: A reusable UI component collection
Tags: typescript, webdev
Published: 2023-11-14 15:02:10

**Written by [Nefe James](https://blog.logrocket.com/author/nefejames/)✏️**

Shadcn UI takes a different approach from other UI and component libraries like Material UI and Chakra UI. Where those libraries offer access to components through their bundled packages, Shadcn UI does something different and instead allows you to download the source code for individual UI components into your codebase. Also, according to its creator, Shadcn UI isn't actually a component library. What exactly is it, then? We’ll explore that and more in this article! 

We’ll learn about Shadcn UI, its features, its benefits, and why developers love and can’t seem to get enough of it. Along the way, we’ll build some UI interfaces with it, as well. Let's dive in!

Jump ahead:

*   [What is Shadcn UI?](#what-shadcn-ui)
*   [Shadcn UI features](#shadcn-ui-features)
    *   [Themes and theme editor](#themes-theme-editor)
    *   [Dark mode](#dark-mode)
    *   [CLI](#cli)
    *   [Multiple components](#multiple-components)
*   [Benefits of Shadcn UI](#benefits-shadcn-ui)
*   [Caveats](#caveats)
*   [Getting started with Shadcn UI](#getting-started-shadcn-ui)
*   [Creating a button with Shadcn UI](#creating-button-shadcn-ui)
*   [Creating a login form with Shadcn UI](#creating-login-form-shadcn-ui)

## What is Shadcn UI? <a name="what-shadcn-ui">

[Shadcn UI](https://ui.shadcn.com/) is not actually a component library or UI framework. Instead, according to [the documentation](https://ui.shadcn.com/docs), it is “a collection of reusable components that we can copy and paste into our apps.” This reusable component collection was created by [Shadcn](https://twitter.com/shadcn), who has also created great open source projects like [Taxonomy](https://vercel.com/templates/next.js/taxonomy), [Next.js for Drupal](https://next-drupal.org), and [Reflexjs](https://reflexjs.org). 

Shadcn UI is built on top of Tailwind CSS and Radix UI and currently supports Next.js, Gatsby, Remix, Astro, Laravel, and Vite. However, there is a [guide for manually integrating it with other technologies](https://ui.shadcn.com/docs/installation/manual). 

Although Shadcn UI was released only a few months ago, it has quickly become one of the most viral UI solutions in the web development ecosystem. Everyone seems to be talking about and building with it, including the folks at Vercel. As of this writing, it has [34.5k GitHub stars](https://github.com/shadcn-ui/ui) and [over 1 million page views](https://twitter.com/shadcn/status/1679494239305224192?t=LL5VtyMT1H1wwMaOjmUi3A&s=19). 

Shadcn UI has grown so popular that [there is already an unofficial Svelte extension for it](https://twitter.com/huntabyte/status/1663320668887195648?t=gcZK91soVXh6WPK4JWNooQ&s=19), which itself has already gained [2k GitHub stars](https://github.com/huntabyte/shadcn-svelte) as of this writing. The impact of the library has been so great that [its creator landed a job at Vercel](https://twitter.com/rauchg/status/1688947478652231680), meaning that Shadcn UI is now backed by Vercel. 

Let’s get to know Shadcn UI a little better by exploring several of its features.

## Shadcn UI features <a name="shadcn-ui-features">

Shadcn UI provides several features that you can leverage for an enhanced user experience. Let’s take a closer look at the following features: themes and theme editor, dark mode, CLI, and multiple components.

### Themes and theme editor <a name="themes-theme-editor">

Shadcn UI provides hand-picked themes that you can copy and paste into your applications. You can manually add theme tokens through the codebase, but you also achieve the same results using [Shadcn UI’s theme editor](https://ui.shadcn.com/themes). 

The theme editor’s interface allows you to configure themes for properties such as color, border radius, and mode (light or dark). You can also choose between two styles: `default` and `new-york`. The styles have unique components, animations, icons, and more. The `default` style has larger input fields and uses [lucide-react](https://lucide.dev/) icons and [tailwindcss-animate](https://www.npmjs.com/package/tailwindcss-animate) for animations. The `new-york` style has smaller buttons, cards with shadows, and uses [Radix Icons](https://www.radix-ui.com/icons). 

You can also easily create custom themes via a Shadcn UI’s graphical interface. The editor outputs a piece of code containing your custom style definitions. Then, you simply copy the code and paste it into your application: ![Shadcn UI Theme Editor](https://blog.logrocket.com/wp-content/uploads/2023/11/shadcn-ui-theme-editor.gif)

Here’s a sample of the theme editor’s code output. It provides style tokens for both light mode and dark mode:

```typescript 
@layer base {
  :root {
    --background: 0 0% 100%;
    --foreground: 222.2 84% 4.9%;
    --card: 0 0% 100%;
    --card-foreground: 222.2 84% 4.9%;
    --popover: 0 0% 100%;
    --popover-foreground: 222.2 84% 4.9%;
    --primary: 221.2 83.2% 53.3%;
    --primary-foreground: 210 40% 98%;
    --secondary: 210 40% 96.1%;
    --secondary-foreground: 222.2 47.4% 11.2%;
    --muted: 210 40% 96.1%;
    --muted-foreground: 215.4 16.3% 46.9%;
    --accent: 210 40% 96.1%;
    --accent-foreground: 222.2 47.4% 11.2%;
    --destructive: 0 84.2% 60.2%;
    --destructive-foreground: 210 40% 98%;
    --border: 214.3 31.8% 91.4%;
    --input: 214.3 31.8% 91.4%;
    --ring: 221.2 83.2% 53.3%;
    --radius: 0.3rem;
  }

  .dark {
    --background: 222.2 84% 4.9%;
    --foreground: 210 40% 98%;
    --card: 222.2 84% 4.9%;
    --card-foreground: 210 40% 98%;
    --popover: 222.2 84% 4.9%;
    --popover-foreground: 210 40% 98%;
    --primary: 217.2 91.2% 59.8%;
    --primary-foreground: 222.2 47.4% 11.2%;
    --secondary: 217.2 32.6% 17.5%;
    --secondary-foreground: 210 40% 98%;
    --muted: 217.2 32.6% 17.5%;
    --muted-foreground: 215 20.2% 65.1%;
    --accent: 217.2 32.6% 17.5%;
    --accent-foreground: 210 40% 98%;
    --destructive: 0 62.8% 30.6%;
    --destructive-foreground: 210 40% 98%;
    --border: 217.2 32.6% 17.5%;
    --input: 217.2 32.6% 17.5%;
    --ring: 224.3 76.3% 48%;
  }
}
```

### Dark mode <a name="dark-mode">

Shadcn UI supports dark mode for [Next.js](https://ui.shadcn.com/docs/dark-mode/next) and [Vite](https://ui.shadcn.com/docs/dark-mode/vite) applications. For Next.js applications, Shadcn UI uses [next-themes](https://github.com/pacocoursey/next-themes) for the dark mode toggling functionality. When a user toggles between light and dark mode, the application switches between the light and dark theme tokens.

### CLI <a name="cli">

You can use [Shadcn UI’s CLI](https://ui.shadcn.com/docs/cli) to integrate the library with your applications, add dependencies, and apply the relevant `tailwind.config.js` configurations. You can also add UI components to your applications with the CLI. 

You can either manually copy and paste the code for each component from the documentation or add them with the CLI. The CLI provides a superior developer experience and is one more feature that makes Shadcn UI to work with.

### Multiple components <a name="multiple-components">

As of this writing, [Shadcn UI has 40 components](https://ui.shadcn.com/docs/components/accordion), including `Accordion`, `Skeleton`, `Table`, and `Popover`. Instead of building components from scratch, you can save time by leveraging the Shadcn UI’s prebuilt components.

## Benefits of Shadcn UI <a name="benefits-shadcn-ui">

Shadcn UI provides several benefits compared to other component libraries. Here are the most significant:

*   **Ease of use**: Anyone with basic knowledge of HTML, CSS, JavaScript, and a framework or two can quickly get started with Shadcn UI. Its components are easily accessed using either the copy and paste or CLI installation method. I’ve used different component libraries like Material UI and Chakra UI, and Shadcn UI doesn’t fall short in that regard
*   **Accessibility**: Shadcn UI’s components are fully accessible and adhere to Web Content Accessibility Guidelines (WCAG) standards. It supports screen readers, keyboard navigation, and other accessibility features. You can use Shadcn UI to create applications that are both beautiful and accessible to all users
*   **Fine-grained control and extensibility**: Shadcn UI provides direct access to each component’s source code, so you can easily adjust the code to fit unique use cases and application needs. This ease of customization helps Shadcn UI stand out among other UI solutions and makes it a delight to work with. Easy access to component code also improves flexibility and makes it easier to scale and maintain applications

## Caveats <a name="caveats">

There are a couple of things you should know before deciding whether to use Shadcn UI in future projects. 

First, having to manually install or copy every component that you need can be a hassle. This extra step is not a big issue, but can add more stress to the developer experience, particularly for those who are used to importing components from a package. 

Second, while having direct access to components’ code is beneficial in terms of modularity and extensibility, it also results in a larger codebase with more lines of code.

## Getting started with Shadcn UI <a name="getting-started-shadcn-ui">

This tutorial will cover how to integrate Shadcn UI with Next.js. Refer to the [installation guide](https://ui.shadcn.com/docs/installation) for instructions on integrating with other frameworks. 

To get started, create a new Next.js application by running the command below:

```bash
npx create-next-app@latest my-app --typescript --tailwind --eslint
```

Next, run the `init` command to initialize dependencies for a new project:

```bash
npx shadcn-ui@latest init
```

The CLI will prompt you to make some configurations. Here’s a sample of the configuration questions:

```plaintext
Would you like to use TypeScript (recommended)? no / yes
Which style would you like to use? › Default
Which color would you like to use as base color? › Slate
Where is your global CSS file? › › app/globals.css
Do you want to use CSS variables for colors? › no / yes
Where is your tailwind.config.js located? › tailwind.config.js
Configure the import alias for components: › @/components
Configure the import alias for utils: › @/lib/utils
Are you using React Server Components? › no / yes
```

That’s it! Now you can start adding components to your applications.

## Creating a button with Shadcn UI <a name="creating-button-shadcn-ui">

Creating a button component with Shadcn UI is as easy as [copying its code from the button docs](https://ui.shadcn.com/docs/components/button) or adding it with the CLI. Run the command below to add it using the CLI:

```bash
npx shadcn-ui@latest add button
```

I prefer using the CLI because it automatically creates a `components` folder for me and makes the process more seamless. Once I have the component, all I need to do is export it from its folder, and bam! Here’s the code:

```typescript
import { Button } from "@/components/ui/button"

<Button variant="outline">Button</Button>
```

The `Button` component has six variants: `default`, `destructive`, `outline`, `secondary`, `ghost`, and `link`.

## Creating a login form with Shadcn UI <a name="creating-login-form-shadcn-ui">

I love that when it comes to forms, Shadcn UI goes a step further than just offering form components like `Input`, `Textarea`, `Checkbox`, and `RadioGroup`. It also provides a [`Form` component](https://ui.shadcn.com/docs/components/form), which is a wrapper around [react-hook-form](https://blog.logrocket.com/react-hook-form-complete-guide/). 

Here are some of the things the `Form` component provides:

*   A `<FormField />` component for building controlled form fields
*   Support for validation with form validation libraries like [Valibot, Yup, and Zod](https://blog.logrocket.com/validating-structural-data-valibot/#comparing-valibots-design-approach-zod-yup).
*   Error message handling

You can access the component by running the command below:

```bash
npx shadcn-ui@latest add form input
```

Next, set up the components for the form, like so:

```typescript
"use client"

import { zodResolver } from "@hookform/resolvers/zod"
import { useForm } from "react-hook-form"
import * as z from "zod"

import { Button } from "@/components/ui/button"
import {
  Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage,
} from "@/components/ui/form"
import { Input } from "@/components/ui/input"

const FormSchema = z.object({
  username: z.string().min(2, {
    message: "Username must be at least 2 characters.",
  }),
})

export function InputForm() {
  const form = useForm({ resolver: zodResolver(FormSchema) })

  function onSubmit(data) {
    <pre className="mt-2 w-[340px] rounded-md bg-slate-950 p-4">
      `{JSON.stringify(data, null, 2)}` </pre> } return ( <Form {...form}> <form onSubmit={form.handleSubmit(onSubmit)} className="w-2/3 space-y-6"> <FormField control={form.control} name="username" render={({ field }) => ( <FormItem> <FormLabel>Username</FormLabel> <FormControl> <Input placeholder="Input username" {...field} /> </FormControl> <FormMessage /> </FormItem> )} /> <Button type="submit">Submit</Button> </form> </Form> ) }
```

Here’s the online form built with the above code: ![Online Form Built Shadcn UI](https://blog.logrocket.com/wp-content/uploads/2023/11/online-form-built-with-shadcn-ui.png) 

## Conclusion

Shadcn UI is a breath of fresh air compared to existing component libraries, enabling developers to create beautiful user interfaces with a unique flair. It ticks the box in terms of user experience and development speed, while also giving devs fine-grained control over the components they use. 

No library is ever a one-size-fits-all solution, but based on current trends, I expect Shadcn UI to become a top contender in the frontend ecosystem. It’s already being adopted by big players like Vercel. For example, Vercel’s [v0 application](https://v0.dev/) is an AI-powered tool that uses Shadcn UI, Tailwind CSS, and text prompts to generate UI code that developers can copy and paste into their applications. 

Shadcn UI is still a relatively new UI solution, and I hope to see more components added to it over time. What do you think about this new tool? Will you try it out in future projects? Are you already using it? Drop a comment — I’d love to know!

---

##[LogRocket](https://lp.logrocket.com/blg/typescript-signup): Full visibility into your web and mobile apps

[![LogRocket signup](https://blog.logrocket.com/wp-content/uploads/2017/03/1d0cd-1s_rmyo6nbrasp-xtvbaxfg.png)](https://lp.logrocket.com/blg/typescript-signup)

[LogRocket](https://lp.logrocket.com/blg/typescript-signup) is a frontend application monitoring solution that lets you replay problems as if they happened in your own browser. Instead of guessing why errors happen, or asking users for screenshots and log dumps, LogRocket lets you replay the session to quickly understand what went wrong. It works perfectly with any app, regardless of framework, and has plugins to log additional context from Redux, Vuex, and @ngrx/store.

In addition to logging Redux actions and state, LogRocket records console logs, JavaScript errors, stacktraces, network requests/responses with headers + bodies, browser metadata, and custom logs. It also instruments the DOM to record the HTML and CSS on the page, recreating pixel-perfect videos of even the most complex single-page and mobile apps.

[Try it for free](https://lp.logrocket.com/blg/typescript-signup).