Skip to content

Commit

Permalink
feat: simple Badge component
Browse files Browse the repository at this point in the history
  • Loading branch information
BrickheadJohnny committed Nov 19, 2024
1 parent e397e95 commit 077ce8b
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/app/components/ui/Badge.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { type VariantProps, cva } from "class-variance-authority";
import { cn } from "lib/cn";
import { type ElementRef, type HTMLAttributes, forwardRef } from "react";

export const badgeVariants = cva(
"inline-flex items-center rounded-md px-2 transition-colors focus:outline-none focus-visible:ring-4 focus:ring-ring font-medium max-w-max gap-1.5 bg-badge-background text-badge-foreground",
{
variants: {
size: {
sm: "text-xs h-5",
md: "text-sm h-6",
lg: "text-base h-8",
},
},
defaultVariants: {
size: "md",
},
},
);

export interface BadgeProps
extends HTMLAttributes<HTMLDivElement>,
VariantProps<typeof badgeVariants> {}

const Badge = forwardRef<ElementRef<"div">, BadgeProps>(
({ className, size, ...props }, ref) => (
<div
ref={ref}
className={cn(badgeVariants({ size }), className)}
{...props}
/>
),
);

export { Badge };
3 changes: 3 additions & 0 deletions src/app/globals.css
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@
--button-success-foreground: #fff;
--button-success-subtle: var(--green-300);
--button-success-subtle-foreground: var(--green-500);

--badge-background: rgba(0, 0, 0, 0.06);
--badge-foreground: var(--foreground);
}

:root[data-theme="dark"],
Expand Down
25 changes: 25 additions & 0 deletions src/stories/Badge.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type { Meta, StoryObj } from "@storybook/react";
import { Badge, type BadgeProps } from "app/components/ui/Badge";

const meta: Meta<typeof Badge> = {
title: "Design system/Badge",
component: Badge,
};

export default meta;

type Story = StoryObj<typeof Badge>;

export const Default: Story = {
args: {
children: "Default",
size: "md",
},
argTypes: {
size: {
type: "string",
control: "select",
options: ["sm", "md", "lg"] satisfies BadgeProps["size"][],
},
},
};
4 changes: 4 additions & 0 deletions tailwind.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ const config = {
card: "var(--card)",
image: "var(--image)",
skeleton: "var(--skeleton)",
badge: {
background: "var(--badge-background)",
foreground: "var(--badge-foreground)"
}
},
},
},
Expand Down

0 comments on commit 077ce8b

Please sign in to comment.