Shadcn

From elophanto/EloPhanto · View interactive version

Shadcn is an AI agent skill from the GitHub repository elophanto/EloPhanto. Installation, components, blocks, forms, theming, and MCP guidance for shadcn/ui in modern Next.js projects using pnpm

How to use this skill

Skill file

shadcn/ui Skills

Status: Next.js 16 ready

Package Manager: pnpm (required)

Official Docs:


Triggers

Table of Contents

  1. Installation & Setup
  2. Project Configuration (Next.js 16)
  3. Components & Usage
  4. Blocks
  5. Forms
  6. Sonner (Toast Replacement)
  7. Dark Mode
  8. MCP Integration
  9. Best Practices

Installation & Setup

```bash

pnpm dlx shadcn@latest init

```

```bash

pnpm dlx shadcn@latest add button card input textarea select

# add blocks or utilities on demand

```

```bash

pnpm lint && pnpm test # if configured

pnpm dev # verify styles render

```


Project Configuration (Next.js 16)

```ts

// tailwind.config.ts

import type { Config } from "tailwindcss"

import { fontFamily } from "tailwindcss/defaultTheme"

const config: Config = {

darkMode: ["class"],

content: [

"./app/*/.{ts,tsx}",

"./components/*/.{ts,tsx}",

"./src/*/.{ts,tsx}",

],

theme: {

extend: {

fontFamily: {

sans: ["var(--font-sans)", ...fontFamily.sans],

},

},

},

plugins: [require("tailwindcss-animate")],

}

export default config

```


Components & Usage

```tsx

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

export function CTA() {

return <Button size="lg">Get started</Button>

}

```

```tsx

<Button asChild>

<Link href="/docs">Docs</Link>

</Button>

```


Blocks

```bash

pnpm dlx shadcn@latest add blocks/application-shells/sidebar-02

```


Forms

```tsx

"use client"

import { zodResolver } from "@hookform/resolvers/zod"

import { useForm } from "react-hook-form"

import { z } from "zod"

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

import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form"

import { Input } from "@/components/ui/input"

const schema = z.object({

email: z.string().email(),

name: z.string().min(2),

})

export function ProfileForm() {

const form = useForm<z.infer<typeof schema>>({

resolver: zodResolver(schema),

defaultValues: { email: "", name: "" },

})

return (

<Form {...form}>

<form onSubmit={form.handleSubmit(console.log)} className="space-y-4">

<FormField

control={form.control}

name="email"

render={({ field }) => (

<FormItem>

<FormLabel>Email</FormLabel>

<FormControl>

<Input placeholder="you@example.com" {...field} />

</FormControl>

<FormMessage />

</FormItem>

)}

/>

<Button type="submit">Save</Button>

</form>

</Form>

)

}

```


Sonner (Toast Replacement)

```bash

pnpm dlx shadcn@latest add sonner

```

import { Toaster } from "@/components/ui/sonner"
import { toast } from "sonner"

  export function RootProviders({ children }: { children: React.ReactNode }) {
    return (
      <>
        {children}
        <Toaster richColors position="top-right" />
      </>
    )
  }

  // usage
  toast.success("Profile saved")
  ```
- Keep Sonner provider outside `app/(marketing)` vs `app/(dashboard)` duplication to avoid multiple toasters.

---

## Dark Mode

- Use class-based theming with `next-themes`.
- Example provider (`components/theme-provider.tsx`):
  ```tsx
  "use client"

  import { ThemeProvider as NextThemesProvider } from "next-themes"

  export function ThemeProvider({ children }: { children: React.ReactNode }) {
    return (
      <NextThemesProvider attribute="class" defaultTheme="system" enableSystem disableTransitionOnChange>
        {children}
      </NextThemesProvider>
    )
  }
  ```
- Wrap the App Router layout:
  ```tsx
  // app/layout.tsx
  import { ThemeProvider } from "@/components/theme-provider"

  export default function RootLayout({ children }: { children: React.ReactNode }) {
    return (
      <html lang="en" suppressHydrationWarning>
        <body>
          <ThemeProvider>{children}</ThemeProvider>
        </body>
      </html>
    )
  }
  ```
- Keep `suppressHydrationWarning` on `<html>` to avoid mismatches when switching themes.

---

## MCP Integration

- shadcn/ui ships an MCP server (see [docs](https://ui.shadcn.com/docs/mcp)) so agents can browse/add components and blocks safely.
- Register the server in your MCP client config, pointing at your project root where `components.json` lives.
- Prefer MCP-driven adds over manual copy/paste to keep component versions consistent with the catalog.

---

## Component Architecture

shadcn/ui is **not a library** — components are copied into your project. You own them.

### File Structure

src/

├── components/

│ ├── ui/ # shadcn components (don't modify directly)

│ │ ├── button.tsx

│ │ ├── card.tsx

│ │ └── dialog.tsx

│ ├── blocks/ # higher-level page sections

│ └── [custom]/ # your composed components

│ └── loading-button.tsx

├── lib/

│ └── utils.ts # cn() utility

└── app/

└── page.tsx


### The `cn()` Utility

All shadcn components use this for class merging:

import { clsx, type ClassValue } from "clsx"

import { twMerge } from "tailwind-merge"

export function cn(...inputs: ClassValue[]) {

return twMerge(clsx(inputs))

}


### Component Variants with cva

Use `class-variance-authority` for variant logic:

import { cva } from "class-variance-authority"

const buttonVariants = cva(

"inline-flex items-center justify-center rounded-md",

{

variants: {

variant: {

default: "bg-primary text-primary-foreground",

outline: "border border-input",

destructive: "bg-destructive text-destructive-foreground",

},

size: {

default: "h-10 px-4 py-2",

sm: "h-9 rounded-md px-3",

lg: "h-11 rounded-md px-8",

},

},

defaultVariants: { variant: "default", size: "default" },

}

)


### Extending Components

Create wrappers in `components/` (not `components/ui/`):

// components/loading-button.tsx

import { Button, type ButtonProps } from "@/components/ui/button"

import { Loader2 } from "lucide-react"

export function LoadingButton({

loading, children, ...props

}: ButtonProps & { loading?: boolean }) {

return (

<Button disabled={loading} {...props}>

{loading && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}

{children}

</Button>

)

}

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.