Badge
nativeui/badgeFreeComponent
A badge component for React Native applications.
Live preview
live · rendered by the registry
Rendered by nativeui on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add https://nativeui.ribbi.ch/r/badge.jsonSource
1import * as React from "react";2import { View, Text, Pressable, ViewStyle } from "react-native";3import { cn } from "@/lib/utils";4import { cva, type VariantProps } from "class-variance-authority";56const badgeVariants = cva(7 "flex-row items-center justify-center rounded-full px-2.5 py-1",8 {9 variants: {10 variant: {11 default: "bg-primary",12 secondary: "bg-secondary",13 destructive: "bg-destructive",14 outline: "border border-input bg-transparent",15 },16 size: {17 default: "h-8 px-3",18 sm: "h-6 px-2.5",19 lg: "h-10 px-4",20 },21 },22 defaultVariants: {23 variant: "default",24 size: "default",25 },26 }27);2829export interface BadgeProps30 extends VariantProps<typeof badgeVariants> {31 className?: string;32 style?: ViewStyle;33 children: React.ReactNode;34 onPress?: () => void;35 accessibilityLabel?: string;36}3738function Badge({39 className,40 variant,41 size,42 style,43 children,44 onPress,45 accessibilityLabel,46}: BadgeProps) {47 const getTextStyle = () => {48 let textStyle = "font-medium";4950 // Adjust text size based on badge size51 if (size === "lg") {52 textStyle = cn(textStyle, "text-sm");53 } else if (size === "sm") {54 textStyle = cn(textStyle, "text-xs");55 } else {56 textStyle = cn(textStyle, "text-xs");57 }5859 // Adjust text color based on variant60 if (variant === "default") {61 textStyle = cn(textStyle, "text-primary-foreground");62 } else if (variant === "secondary") {63 textStyle = cn(textStyle, "text-secondary-foreground");64 } else if (variant === "destructive") {65 textStyle = cn(textStyle, "text-destructive-foreground");66 } else if (variant === "outline") {67 textStyle = cn(textStyle, "text-foreground");68 }6970 return textStyle;71 };7273 const content = (74 <View className={cn(badgeVariants({ variant, size, className }))} style={style}>75 {typeof children === 'string' ? (76 <Text77 className={getTextStyle()}78 numberOfLines={1}79 >80 {children}81 </Text>82 ) : (83 children84 )}85 </View>86 );8788 if (onPress) {89 return (90 <Pressable91 onPress={onPress}92 accessibilityRole="button"93 accessibilityLabel={accessibilityLabel || (typeof children === 'string' ? children : undefined)}94 >95 {({ pressed }) => (96 <View style={{ opacity: pressed ? 0.7 : 1 }}>97 {content}98 </View>99 )}100// … truncated
What it pulls in
npm packages
Files it writes
More from nativeui
All 29 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Accordionaccordion | nativeui | Components | Free | 2 deps | |
| Alertalert | nativeui | Components | Free | 2 deps | |
| Alert Dialogalert-dialog | nativeui | Components | Free | 1 dep | |
| Avataravatar | nativeui | Components | Free | 1 dep | |
| Breadcrumbbreadcrumb | nativeui | Components | Free | 2 deps | |
| Buttonbutton | nativeui | Components | Free | 2 deps | |
| Calendarcalendar | nativeui | Components | Free | 4 deps | |
| Cardcard | nativeui | Components | Free | 1 dep |
