Badge
Displays a badge or a component that looks like a badge.
Installation
bash npx @moe/cli add badge Copy/paste the following code to @/components/ui/badge.tsx
import { cva, type VariantProps } from "class-variance-authority";
import * as React from "react";
import { View } from "react-native";
import { cn } from "@/lib/utils";
import { TextClassContext } from "@/components/ui/text";
const badgeVariants = cva(
"web:inline-flex items-center rounded-full border border-border px-2.5 py-0.5 web:transition-colors web:focus:outline-none web:focus:ring-2 web:focus:ring-ring web:focus:ring-offset-2",
{
variants: {
variant: {
default:
"border-transparent bg-primary web:hover:opacity-80 active:opacity-80",
secondary:
"border-transparent bg-secondary web:hover:opacity-80 active:opacity-80",
destructive:
"border-transparent bg-destructive web:hover:opacity-80 active:opacity-80",
outline: "text-foreground",
},
},
defaultVariants: {
variant: "default",
},
},
);
const badgeTextVariants = cva("text-xs font-semibold", {
variants: {
variant: {
default: "text-primary-foreground",
secondary: "text-secondary-foreground",
destructive: "text-destructive-foreground",
outline: "text-foreground",
},
},
defaultVariants: {
variant: "default",
},
});
function Badge({
className,
variant,
...props
}: React.ComponentPropsWithoutRef<typeof View> &
VariantProps<typeof badgeVariants>) {
return (
<TextClassContext.Provider value={badgeTextVariants({ variant })}>
<View className={cn(badgeVariants({ variant }), className)} {...props} />
</TextClassContext.Provider>
);
}
export { Badge, badgeTextVariants, badgeVariants };
export type { VariantProps };Usage
import { Badge } from "@moe/registry/ui/badge";
export function BadgeDemo() {
return <Badge>Badge</Badge>;
}Variants
<Badge variant="default">Default</Badge>
<Badge variant="secondary">Secondary</Badge>
<Badge variant="destructive">Destructive</Badge>
<Badge variant="outline">Outline</Badge>Props
| Prop | Type | Default | Description |
|---|---|---|---|
variant | 'default' | 'secondary' | 'destructive' | 'outline' | 'default' | The visual style of the badge |