Skip to content

Commit df6eb1a

Browse files
shadcn badge
1 parent 11d8d4a commit df6eb1a

File tree

5 files changed

+115
-5
lines changed

5 files changed

+115
-5
lines changed

package-lock.json

Lines changed: 49 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
"lint": "eslint"
1010
},
1111
"dependencies": {
12+
"@radix-ui/react-slot": "^1.2.3",
13+
"class-variance-authority": "^0.7.1",
1214
"clsx": "^2.1.1",
1315
"fetch-loading": "^0.0.7",
1416
"next": "15.5.2",

src/app/globals.css

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,18 @@ html {
4444
--radius-md: calc(var(--radius) - 2px);
4545
--radius-lg: var(--radius);
4646
--radius-xl: calc(var(--radius) + 4px);
47+
--color-accent: var(--accent);
48+
--color-accent-foreground: var(--accent-foreground);
4749
--color-background: var(--background);
48-
--color-foreground: var(--foreground);
4950
--color-card: var(--card);
5051
--color-card-foreground: var(--card-foreground);
52+
--color-destructive: var(--destructive);
53+
--color-foreground: var(--foreground);
5154
--color-muted-foreground: var(--muted-foreground);
55+
--color-primary: var(--primary);
56+
--color-primary-foreground: var(--primary-foreground);
57+
--color-secondary: var(--secondary);
58+
--color-secondary-foreground: var(--secondary-foreground);
5259
}
5360

5461
:root {
@@ -57,7 +64,14 @@ html {
5764
--foreground: oklch(0.141 0.005 285.823);
5865
--card: oklch(1 0 0);
5966
--card-foreground: oklch(0.141 0.005 285.823);
67+
--accent: oklch(0.967 0.001 286.375);
68+
--accent-foreground: oklch(0.21 0.006 285.885);
69+
--destructive: oklch(0.577 0.245 27.325);
6070
--muted-foreground: oklch(0.552 0.016 285.938);
71+
--primary: oklch(0.21 0.006 285.885);
72+
--primary-foreground: oklch(0.985 0 0);
73+
--secondary: oklch(0.967 0.001 286.375);
74+
--secondary-foreground: oklch(0.21 0.006 285.885);
6175
}
6276

6377
@layer base {

src/components/feed/Feed.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import { useEffect, useRef, useState } from 'react'
44
import { FetchLoading } from 'fetch-loading'
5-
import fetchFeedItems from '@/api/fetchFeedItems'
5+
import { Badge } from '@/components/ui/badge'
66
import {
77
Card,
88
CardContent,
@@ -11,6 +11,7 @@ import {
1111
CardHeader,
1212
CardTitle,
1313
} from '@/components/ui/card'
14+
import fetchFeedItems from '@/api/fetchFeedItems'
1415

1516
const Feed = () => {
1617
const PAGE_SIZE = 10
@@ -68,7 +69,7 @@ const Feed = () => {
6869
<p>{item.body}</p>
6970
</CardContent>
7071
<CardFooter>
71-
<p>Post #{item.id}</p>
72+
<Badge variant="outline">Post #{item.id}</Badge>
7273
</CardFooter>
7374
</Card>
7475
))}

src/components/ui/badge.tsx

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import * as React from 'react'
2+
import { Slot } from '@radix-ui/react-slot'
3+
import { cva, type VariantProps } from 'class-variance-authority'
4+
5+
import { cn } from '@/lib/utils'
6+
7+
const badgeVariants = cva(
8+
'inline-flex items-center justify-center rounded-md 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',
9+
{
10+
variants: {
11+
variant: {
12+
default:
13+
'border-transparent bg-primary text-primary-foreground [a&]:hover:bg-primary/90',
14+
secondary:
15+
'border-transparent bg-secondary text-secondary-foreground [a&]:hover:bg-secondary/90',
16+
destructive:
17+
'border-transparent bg-destructive text-white [a&]:hover:bg-destructive/90 focus-visible:ring-destructive/20 dark:focus-visible:ring-destructive/40 dark:bg-destructive/60',
18+
outline:
19+
'text-foreground [a&]:hover:bg-accent [a&]:hover:text-accent-foreground',
20+
},
21+
},
22+
defaultVariants: {
23+
variant: 'default',
24+
},
25+
}
26+
)
27+
28+
function Badge({
29+
className,
30+
variant,
31+
asChild = false,
32+
...props
33+
}: React.ComponentProps<'span'> &
34+
VariantProps<typeof badgeVariants> & { asChild?: boolean }) {
35+
const Comp = asChild ? Slot : 'span'
36+
37+
return (
38+
<Comp
39+
data-slot="badge"
40+
className={cn(badgeVariants({ variant }), className)}
41+
{...props}
42+
/>
43+
)
44+
}
45+
46+
export { Badge, badgeVariants }

0 commit comments

Comments
 (0)