AgentSkillsCN

shadcn-components

当用户请求“添加按钮”“创建对话框”“使用卡片组件”“Shadcn 折叠面板”“下拉菜单”“添加警示”“工具提示组件”,或讨论 Shadcn/ui 特定组件的使用、自定义或实现方式时,应使用此技能。

SKILL.md
--- frontmatter
name: shadcn-components
description: This skill should be used when the user asks to "add a button", "create a dialog", "use card component", "shadcn accordion", "dropdown menu", "add alert", "tooltip component", or mentions specific shadcn/ui component usage, customization, or implementation patterns.
version: 0.1.8

shadcn/ui Component Usage

Guide for using, customizing, and composing shadcn/ui components in React applications.

Adding Components

CLI Installation

bash
# Add single component
npx shadcn@latest add button

# Add multiple components
npx shadcn@latest add button card dialog input

# Add with dependencies
npx shadcn@latest add form  # Adds form + label + input

Manual Installation

Components can be copied directly from https://ui.shadcn.com/docs/components

Component Categories

Form Components

ComponentUse Case
inputText input fields
textareaMulti-line text
selectDropdown selection
checkboxBoolean toggle
radio-groupSingle selection from options
switchOn/off toggle
sliderRange selection
formForm validation wrapper

Layout Components

ComponentUse Case
cardContent container
separatorVisual divider
aspect-ratioFixed aspect container
scroll-areaCustom scrollbars
resizableResizable panels

Feedback Components

ComponentUse Case
alertImportant messages
badgeStatus indicators
progressLoading progress
skeletonLoading placeholders
toast/sonnerNotifications

Overlay Components

ComponentUse Case
dialogModal windows
alert-dialogConfirmation dialogs
sheetSide panels
drawerBottom/side drawers
popoverFloating content
tooltipHover information
hover-cardRich hover content

Navigation Components

ComponentUse Case
tabsTabbed interfaces
navigation-menuMain navigation
breadcrumbLocation breadcrumbs
paginationPage navigation
commandCommand palette
menubarApplication menu
context-menuRight-click menu
dropdown-menuAction menus

Data Display

ComponentUse Case
tableData tables
data-tableAdvanced tables (TanStack)
accordionCollapsible sections
collapsibleSingle collapse
avatarUser images
calendarDate picker
carouselImage galleries

Component Variants

Most components support variants via variant prop:

Button Variants

tsx
<Button variant="default">Primary</Button>
<Button variant="secondary">Secondary</Button>
<Button variant="destructive">Destructive</Button>
<Button variant="outline">Outline</Button>
<Button variant="ghost">Ghost</Button>
<Button variant="link">Link</Button>

Button Sizes

tsx
<Button size="default">Default</Button>
<Button size="sm">Small</Button>
<Button size="lg">Large</Button>
<Button size="icon">Icon Only</Button>

Badge Variants

tsx
<Badge variant="default">Default</Badge>
<Badge variant="secondary">Secondary</Badge>
<Badge variant="destructive">Destructive</Badge>
<Badge variant="outline">Outline</Badge>

Component Customization

Using cn() for Custom Classes

tsx
import { cn } from "@/lib/utils"

<Button className={cn(
  "w-full",
  isActive && "ring-2 ring-primary",
  disabled && "opacity-50"
)}>
  Custom Button
</Button>

Extending Components

Create wrapper components for reusable customizations:

tsx
// components/ui/button-loading.tsx
import { Button, ButtonProps } from "@/components/ui/button"
import { Loader2 } from "lucide-react"

interface ButtonLoadingProps extends ButtonProps {
  loading?: boolean
}

export function ButtonLoading({
  loading,
  children,
  disabled,
  ...props
}: ButtonLoadingProps) {
  return (
    <Button disabled={loading || disabled} {...props}>
      {loading && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
      {children}
    </Button>
  )
}

Adding New Variants

Edit the component's cva() definition:

tsx
// components/ui/button.tsx
const buttonVariants = cva(
  "...", // base styles
  {
    variants: {
      variant: {
        // existing variants...
        success: "bg-green-500 text-white hover:bg-green-600",
      },
    },
  }
)

Common Patterns

Dialog with Form

tsx
<Dialog>
  <DialogTrigger asChild>
    <Button>Edit Profile</Button>
  </DialogTrigger>
  <DialogContent className="sm:max-w-[425px]">
    <DialogHeader>
      <DialogTitle>Edit Profile</DialogTitle>
      <DialogDescription>
        Make changes to your profile here.
      </DialogDescription>
    </DialogHeader>
    <form onSubmit={handleSubmit}>
      <div className="grid gap-4 py-4">
        <div className="grid gap-2">
          <Label htmlFor="name">Name</Label>
          <Input id="name" defaultValue="John Doe" />
        </div>
      </div>
      <DialogFooter>
        <Button type="submit">Save changes</Button>
      </DialogFooter>
    </form>
  </DialogContent>
</Dialog>

Dropdown Menu with Icons

tsx
import {
  User, Settings, LogOut, Plus
} from "lucide-react"

<DropdownMenu>
  <DropdownMenuTrigger asChild>
    <Button variant="ghost">Menu</Button>
  </DropdownMenuTrigger>
  <DropdownMenuContent align="end">
    <DropdownMenuLabel>My Account</DropdownMenuLabel>
    <DropdownMenuSeparator />
    <DropdownMenuItem>
      <User className="mr-2 h-4 w-4" />
      Profile
    </DropdownMenuItem>
    <DropdownMenuItem>
      <Settings className="mr-2 h-4 w-4" />
      Settings
    </DropdownMenuItem>
    <DropdownMenuSeparator />
    <DropdownMenuItem className="text-destructive">
      <LogOut className="mr-2 h-4 w-4" />
      Log out
    </DropdownMenuItem>
  </DropdownMenuContent>
</DropdownMenu>

Tabs with Content

tsx
<Tabs defaultValue="account" className="w-full">
  <TabsList className="grid w-full grid-cols-2">
    <TabsTrigger value="account">Account</TabsTrigger>
    <TabsTrigger value="password">Password</TabsTrigger>
  </TabsList>
  <TabsContent value="account">
    <Card>
      <CardHeader>
        <CardTitle>Account</CardTitle>
        <CardDescription>
          Make changes to your account here.
        </CardDescription>
      </CardHeader>
      <CardContent>
        {/* Account form */}
      </CardContent>
    </Card>
  </TabsContent>
  <TabsContent value="password">
    <Card>
      <CardHeader>
        <CardTitle>Password</CardTitle>
      </CardHeader>
      <CardContent>
        {/* Password form */}
      </CardContent>
    </Card>
  </TabsContent>
</Tabs>

Reference Files

  • references/component-catalog.md - Full component list with props
  • references/icon-patterns.md - Lucide icon usage patterns

Resources