mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2025-12-24 18:50:56 +08:00
feat(badge): add Badge component and its variants with Storybook examples
- Introduced a new Badge component with multiple visual style variants: default, secondary, destructive, and outline. - Added comprehensive Storybook stories to demonstrate the Badge component's usage, including examples with icons and as links. - Updated the component index to export the new Badge component.
This commit is contained in:
parent
a701b0a4b9
commit
7e7d10f966
@ -59,6 +59,7 @@ export {
|
||||
export { Sortable } from './composites/Sortable'
|
||||
|
||||
/* Shadcn Primitive Components */
|
||||
export * from './primitives/badge'
|
||||
export * from './primitives/breadcrumb'
|
||||
export * from './primitives/button'
|
||||
export * from './primitives/checkbox'
|
||||
|
||||
35
packages/ui/src/components/primitives/badge.tsx
Normal file
35
packages/ui/src/components/primitives/badge.tsx
Normal file
@ -0,0 +1,35 @@
|
||||
import { cn } from '@cherrystudio/ui/utils/index'
|
||||
import { Slot } from '@radix-ui/react-slot'
|
||||
import { cva, type VariantProps } from 'class-variance-authority'
|
||||
import * as React from 'react'
|
||||
|
||||
const badgeVariants = cva(
|
||||
'inline-flex items-center justify-center rounded-full border px-2 py-0.5 text-xs font-medium w-fit whitespace-nowrap shrink-0 [&>svg]:size-3 gap-1 [&>svg]:pointer-events-none focus-visible:border-ring focus-visible:ring-ring/50 focus-visible:ring-[3px] aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 aria-invalid:border-destructive transition-[color,box-shadow] overflow-hidden',
|
||||
{
|
||||
variants: {
|
||||
variant: {
|
||||
default: 'border-transparent bg-background-subtle text-secondary-foreground [a&]:hover:bg-primary/90',
|
||||
secondary: 'border-transparent bg-secondary text-secondary-foreground [a&]:hover:bg-secondary/90',
|
||||
destructive:
|
||||
'border-transparent text-destructive bg-[red]/10 [a&]:hover:bg-destructive/90 focus-visible:ring-destructive/20 dark:focus-visible:ring-destructive/40 dark:bg-destructive/60',
|
||||
outline: 'text-foreground [a&]:hover:bg-accent [a&]:hover:text-accent-foreground'
|
||||
}
|
||||
},
|
||||
defaultVariants: {
|
||||
variant: 'default'
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
function Badge({
|
||||
className,
|
||||
variant,
|
||||
asChild = false,
|
||||
...props
|
||||
}: React.ComponentProps<'span'> & VariantProps<typeof badgeVariants> & { asChild?: boolean }) {
|
||||
const Comp = asChild ? Slot : 'span'
|
||||
|
||||
return <Comp data-slot="badge" className={cn(badgeVariants({ variant }), className)} {...props} />
|
||||
}
|
||||
|
||||
export { Badge, badgeVariants }
|
||||
207
packages/ui/stories/components/primitives/Badge.stories.tsx
Normal file
207
packages/ui/stories/components/primitives/Badge.stories.tsx
Normal file
@ -0,0 +1,207 @@
|
||||
import { Badge } from '@cherrystudio/ui'
|
||||
import type { Meta, StoryObj } from '@storybook/react'
|
||||
import { Check, X } from 'lucide-react'
|
||||
|
||||
const meta: Meta<typeof Badge> = {
|
||||
title: 'Components/Primitives/Badge',
|
||||
component: Badge,
|
||||
parameters: {
|
||||
layout: 'centered',
|
||||
docs: {
|
||||
description: {
|
||||
component: 'Displays a badge or a component that looks like a badge. Based on shadcn/ui.'
|
||||
}
|
||||
}
|
||||
},
|
||||
tags: ['autodocs'],
|
||||
argTypes: {
|
||||
variant: {
|
||||
control: { type: 'select' },
|
||||
options: ['default', 'secondary', 'destructive', 'outline'],
|
||||
description: 'The visual style variant of the badge'
|
||||
},
|
||||
asChild: {
|
||||
control: { type: 'boolean' },
|
||||
description: 'Render as a child element'
|
||||
},
|
||||
className: {
|
||||
control: { type: 'text' },
|
||||
description: 'Additional CSS classes'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default meta
|
||||
type Story = StoryObj<typeof meta>
|
||||
|
||||
// Default
|
||||
export const Default: Story = {
|
||||
args: {
|
||||
children: 'Badge'
|
||||
}
|
||||
}
|
||||
|
||||
// Variants
|
||||
export const Secondary: Story = {
|
||||
args: {
|
||||
variant: 'secondary',
|
||||
children: 'Secondary'
|
||||
}
|
||||
}
|
||||
|
||||
export const Destructive: Story = {
|
||||
args: {
|
||||
variant: 'destructive',
|
||||
children: 'Destructive'
|
||||
}
|
||||
}
|
||||
|
||||
export const Outline: Story = {
|
||||
args: {
|
||||
variant: 'outline',
|
||||
children: 'Outline'
|
||||
}
|
||||
}
|
||||
|
||||
// All Variants
|
||||
export const AllVariants: Story = {
|
||||
render: () => (
|
||||
<div className="flex flex-wrap gap-2">
|
||||
<Badge variant="default">Default</Badge>
|
||||
<Badge variant="secondary">Secondary</Badge>
|
||||
<Badge variant="destructive">Destructive</Badge>
|
||||
<Badge variant="outline">Outline</Badge>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
// With Icons
|
||||
export const WithIcon: Story = {
|
||||
render: () => (
|
||||
<div className="flex flex-wrap gap-2">
|
||||
<Badge>
|
||||
<Check />
|
||||
Success
|
||||
</Badge>
|
||||
<Badge variant="destructive">
|
||||
<X />
|
||||
Error
|
||||
</Badge>
|
||||
<Badge variant="secondary">
|
||||
<Check />
|
||||
Completed
|
||||
</Badge>
|
||||
<Badge variant="outline">
|
||||
<Check />
|
||||
Verified
|
||||
</Badge>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
// As Link
|
||||
export const AsLink: Story = {
|
||||
render: () => (
|
||||
<div className="flex flex-col gap-4">
|
||||
<div>
|
||||
<p className="mb-2 text-sm text-muted-foreground">Using asChild to render as an anchor tag:</p>
|
||||
<Badge asChild>
|
||||
<a href="https://github.com" target="_blank" rel="noopener noreferrer">
|
||||
GitHub
|
||||
</a>
|
||||
</Badge>
|
||||
</div>
|
||||
<div>
|
||||
<p className="mb-2 text-sm text-muted-foreground">All variants as links (hover to see effect):</p>
|
||||
<div className="flex gap-2">
|
||||
<Badge asChild variant="default">
|
||||
<a href="#">Default Link</a>
|
||||
</Badge>
|
||||
<Badge asChild variant="secondary">
|
||||
<a href="#">Secondary Link</a>
|
||||
</Badge>
|
||||
<Badge asChild variant="outline">
|
||||
<a href="#">Outline Link</a>
|
||||
</Badge>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
// Status Badges
|
||||
export const StatusBadges: Story = {
|
||||
render: () => (
|
||||
<div className="flex flex-wrap gap-2">
|
||||
<Badge variant="default">Active</Badge>
|
||||
<Badge variant="secondary">Pending</Badge>
|
||||
<Badge variant="destructive">Failed</Badge>
|
||||
<Badge variant="outline">Draft</Badge>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
// Real World Examples
|
||||
export const RealWorldExamples: Story = {
|
||||
render: () => (
|
||||
<div className="flex flex-col gap-6">
|
||||
{/* Status Indicators */}
|
||||
<div>
|
||||
<h3 className="mb-3 text-sm font-semibold">Status Indicators</h3>
|
||||
<div className="flex gap-2">
|
||||
<Badge>Online</Badge>
|
||||
<Badge variant="secondary">Away</Badge>
|
||||
<Badge variant="destructive">Offline</Badge>
|
||||
<Badge variant="outline">Unknown</Badge>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Labels */}
|
||||
<div>
|
||||
<h3 className="mb-3 text-sm font-semibold">Labels</h3>
|
||||
<div className="flex gap-2">
|
||||
<Badge variant="secondary">New</Badge>
|
||||
<Badge variant="secondary">Featured</Badge>
|
||||
<Badge variant="destructive">Hot</Badge>
|
||||
<Badge variant="outline">Beta</Badge>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Tags */}
|
||||
<div>
|
||||
<h3 className="mb-3 text-sm font-semibold">Tags</h3>
|
||||
<div className="flex gap-2">
|
||||
<Badge variant="outline">React</Badge>
|
||||
<Badge variant="outline">TypeScript</Badge>
|
||||
<Badge variant="outline">Tailwind</Badge>
|
||||
<Badge variant="outline">Shadcn</Badge>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Notification Counts */}
|
||||
<div>
|
||||
<h3 className="mb-3 text-sm font-semibold">Notification Counts</h3>
|
||||
<div className="flex gap-2">
|
||||
<Badge>3</Badge>
|
||||
<Badge variant="destructive">99+</Badge>
|
||||
<Badge variant="secondary">12</Badge>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* With Icons */}
|
||||
<div>
|
||||
<h3 className="mb-3 text-sm font-semibold">With Icons</h3>
|
||||
<div className="flex gap-2">
|
||||
<Badge>
|
||||
<Check />
|
||||
Verified
|
||||
</Badge>
|
||||
<Badge variant="destructive">
|
||||
<X />
|
||||
Rejected
|
||||
</Badge>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user