|
| 1 | +--- |
| 2 | +title: "Badge Component | NativeCN UI" |
| 3 | +description: "Versatile badge component for React Native apps - display status indicators, labels, and interactive elements with customizable styles." |
| 4 | +--- |
| 5 | + |
| 6 | +# Badge Component |
| 7 | + |
| 8 | +import { Badge } from '../../components/ui/badge' |
| 9 | +import ComponentPreview, { PreviewModeContext } from '../../components/ComponentPreview' |
| 10 | +import ComponentCode from '../../components/ComponentCode' |
| 11 | +import { useContext } from 'react' |
| 12 | + |
| 13 | +export const BadgeWithMode = (props) => { |
| 14 | + const mode = useContext(PreviewModeContext) || 'light'; |
| 15 | + return <Badge mode={mode} {...props} />; |
| 16 | +} |
| 17 | + |
| 18 | +## Installation |
| 19 | + |
| 20 | +The Badge component is used to highlight and display status, labels, or counts. |
| 21 | + |
| 22 | +<ComponentCode |
| 23 | + language="bash" |
| 24 | + code="npx @nativecn/cli add badge" |
| 25 | + title="Installation Command" |
| 26 | +/> |
| 27 | + |
| 28 | +## Basic Variants |
| 29 | + |
| 30 | +<ComponentPreview |
| 31 | + title="Badge Variants" |
| 32 | + code={`// The mode prop will automatically be set to 'light' or 'dark' based on the preview container |
| 33 | +<Badge mode="..." variant="default">Default</Badge> |
| 34 | +<Badge mode="..." variant="secondary">Secondary</Badge> |
| 35 | +<Badge mode="..." variant="destructive">Destructive</Badge> |
| 36 | +<Badge mode="..." variant="outline">Outline</Badge>`} |
| 37 | +> |
| 38 | + <div style={{ display: 'flex', flexDirection: 'row', gap: '8px', flexWrap: 'wrap' }}> |
| 39 | + <BadgeWithMode>Default</BadgeWithMode> |
| 40 | + <BadgeWithMode variant="secondary">Secondary</BadgeWithMode> |
| 41 | + <BadgeWithMode variant="destructive">Destructive</BadgeWithMode> |
| 42 | + <BadgeWithMode variant="outline">Outline</BadgeWithMode> |
| 43 | + </div> |
| 44 | +</ComponentPreview> |
| 45 | + |
| 46 | +## Interactive Badges |
| 47 | + |
| 48 | +<ComponentPreview |
| 49 | + title="Interactive Badges" |
| 50 | + code={`<Badge mode="..." interactive>Interactive Default</Badge> |
| 51 | +<Badge mode="..." variant="secondary" interactive>Interactive Secondary</Badge> |
| 52 | +<Badge mode="..." variant="destructive" interactive>Interactive Destructive</Badge> |
| 53 | +<Badge mode="..." variant="outline" interactive>Interactive Outline</Badge>`} |
| 54 | +> |
| 55 | + <div style={{ display: 'flex', flexDirection: 'row', gap: '8px', flexWrap: 'wrap' }}> |
| 56 | + <BadgeWithMode interactive>Interactive Default</BadgeWithMode> |
| 57 | + <BadgeWithMode variant="secondary" interactive>Interactive Secondary</BadgeWithMode> |
| 58 | + <BadgeWithMode variant="destructive" interactive>Interactive Destructive</BadgeWithMode> |
| 59 | + <BadgeWithMode variant="outline" interactive>Interactive Outline</BadgeWithMode> |
| 60 | + </div> |
| 61 | +</ComponentPreview> |
| 62 | + |
| 63 | +## Custom Styling |
| 64 | + |
| 65 | +<ComponentPreview |
| 66 | + title="Custom Styled Badges" |
| 67 | + code={`<Badge mode="..." className="bg-blue-500">Custom Background</Badge> |
| 68 | +<Badge mode="..." textClassName="text-red-500">Custom Text Color</Badge> |
| 69 | +<Badge mode="..." className="border-2 border-green-500" variant="outline">Custom Border</Badge>`} |
| 70 | +> |
| 71 | + <div style={{ display: 'flex', flexDirection: 'row', gap: '8px', flexWrap: 'wrap' }}> |
| 72 | + <BadgeWithMode className="bg-blue-500">Custom Background</BadgeWithMode> |
| 73 | + <BadgeWithMode textClassName="text-red-500">Custom Text Color</BadgeWithMode> |
| 74 | + <BadgeWithMode className="border-2 border-green-500" variant="outline">Custom Border</BadgeWithMode> |
| 75 | + </div> |
| 76 | +</ComponentPreview> |
| 77 | + |
| 78 | +## Usage Example |
| 79 | + |
| 80 | +<ComponentCode |
| 81 | + title="Basic Badge Usage with Responsive Mode" |
| 82 | + code={`import { Badge } from '../components/ui/badge'; |
| 83 | +import { useColorScheme } from 'react-native'; |
| 84 | +
|
| 85 | +export function NotificationBadge({ count }) { |
| 86 | + const colorScheme = useColorScheme(); |
| 87 | + |
| 88 | + return ( |
| 89 | + <Badge |
| 90 | + mode={colorScheme} // 'light' or 'dark' based on system preference |
| 91 | + variant="destructive" |
| 92 | + interactive |
| 93 | + > |
| 94 | + {count} |
| 95 | + </Badge> |
| 96 | + ); |
| 97 | +}`} |
| 98 | +/> |
| 99 | + |
| 100 | +## Reference |
| 101 | + |
| 102 | +<ComponentCode |
| 103 | + title="Badge Props" |
| 104 | + language="typescript" |
| 105 | + code={`interface BadgeProps extends Omit<TouchableOpacityProps, 'style'> { |
| 106 | + variant?: 'default' | 'secondary' | 'destructive' | 'outline'; |
| 107 | + className?: string; |
| 108 | + textClassName?: string; |
| 109 | + style?: StyleProp<ViewStyle>; |
| 110 | + textStyle?: StyleProp<TextStyle>; |
| 111 | + mode?: 'light' | 'dark'; // Controls light/dark appearance |
| 112 | + children: React.ReactNode; |
| 113 | + interactive?: boolean; // Makes the badge clickable |
| 114 | +}`} |
| 115 | +/> |
0 commit comments