Badge

The Badge component is used as an adornment to other elements, typically displaying a count, status, or short notification.

Installation

$ cbui-cli install @crossbuildui/badge

Import

index.tsx
1import { Badge } from '@/crossbuildui/badge'; 2// import Icon from 'react-native-vector-icons/MaterialCommunityIcons'; // If using icons in badge content 3// import { View, Text } from 'react-native'; // For children elements

Basic Usage

Wrap any component with Badge and provide content. Use isDot for a simple dot indicator.

index.tsx
1// Basic badge with text content 2<Badge content="NEW"> 3 <Button>My Button</Button> 4</Badge> 5 6// Badge with numerical content 7<Badge content={5} color="primary"> 8 <View style={{width: 50, height: 50, backgroundColor: 'lightgrey'}} /> 9</Badge> 10 11// Dot badge 12<Badge isDot color="success"> 13 <Text>Notifications</Text> 14</Badge>

Variants & Colors

Badges support solid (default), flat, faded, and shadow variants. Apply thematic colors using the color prop.

index.tsx
1// Solid (default) 2<Badge content="S" color="primary"> 3 <View style={{width: 40, height: 40, backgroundColor: 'aliceblue'}} /> 4</Badge> 5 6// Flat 7<Badge content="F" variant="flat" color="secondary"> 8 <View style={{width: 40, height: 40, backgroundColor: 'aliceblue'}} /> 9</Badge> 10 11// Faded 12<Badge content="Fd" variant="faded" color="success"> 13 <View style={{width: 40, height: 40, backgroundColor: 'aliceblue'}} /> 14</Badge> 15 16// Shadow 17<Badge content="Sh" variant="shadow" color="warning"> 18 <View style={{width: 40, height: 40, backgroundColor: 'aliceblue'}} /> 19</Badge>

Sizes

Adjust badge size with sm, md (default), or lg. This also affects dot size.

index.tsx
1<Badge content="Small" size="sm" color="danger"> 2 <Text>SM</Text> 3</Badge> 4 5<Badge content="Medium" size="md" color="primary"> 6 <Text>MD</Text> 7</Badge> 8 9<Badge content="Large" size="lg" color="success"> 10 <Text>LG</Text> 11</Badge> 12 13// Dot sizes 14<Badge isDot size="sm"><View style={{width:20, height:20, backgroundColor:'lightgray'}}/></Badge> 15<Badge isDot size="md"><View style={{width:20, height:20, backgroundColor:'lightgray'}}/></Badge> 16<Badge isDot size="lg"><View style={{width:20, height:20, backgroundColor:'lightgray'}}/></Badge>

Shapes

Badges can be rectangle or circle. The shape defaults to circle for dots (isDot) or single characters (isOneChar or auto-detected).

index.tsx
1// Rectangle (default for multi-char or custom content) 2<Badge content="Sale" shape="rectangle" color="primary"> 3 <Text>Item</Text> 4</Badge> 5 6// Circle (default for single char, isDot, or explicit) 7<Badge content="1" shape="circle" color="secondary"> 8 <Text>Msg</Text> 9</Badge> 10 11// isOneChar prop for single character content to enforce circle 12<Badge content="A" isOneChar color="success"> 13 <Text>User</Text> 14</Badge> 15 16// isDot prop (always circle) 17<Badge isDot color="warning"> 18 <Text>Status</Text> 19</Badge>

Placement

Position the badge relative to its child using placement: top-right (default), top-left, bottom-right, bottom-left.

index.tsx
1<View style={{ flexDirection: 'row', justifyContent: 'space-around', width: '100%', padding: 20 }}> 2 <Badge content="TR" placement="top-right"><View style={styles.box} /></Badge> 3 <Badge content="TL" placement="top-left"><View style={styles.box} /></Badge> 4 <Badge content="BR" placement="bottom-right"><View style={styles.box} /></Badge> 5 <Badge content="BL" placement="bottom-left"><View style={styles.box} /></Badge> 6</View> 7 8// styles.box: { width: 50, height: 50, backgroundColor: 'lightgrey' }

Max Count

Use maxCount to cap numerical content (e.g., 99+).

index.tsx
1<Badge content={100} maxCount={99} color="primary"> 2 <Text>Notifications</Text> 3</Badge> 4 5<Badge content={50} maxCount={99} color="secondary"> 6 <Text>Messages</Text> 7</Badge>

Custom Content

The content prop can accept any ReactNode, allowing for icons or custom components within the badge.

index.tsx
1const CustomBadgeIcon = () => <Icon name="star" size={12} color="white" />; 2 3<Badge content={<CustomBadgeIcon />} color="warning" shape="circle"> 4 <Button>Featured</Button> 5</Badge>

Visibility & Animation

Control visibility with isInvisible and toggle animations with disableAnimation.

index.tsx
1// Invisible badge 2<Badge content="Hidden" isInvisible> 3 <Text>This badge is not visible.</Text> 4</Badge> 5 6// Badge with animation disabled 7<Badge content="No Anim" disableAnimation color="success"> 8 <Text>This badge appears/disappears instantly.</Text> 9</Badge>

Outline

The showOutline prop (default true) adds a contrasting border around the badge.

index.tsx
1<Badge content="Outline" color="primary" showOutline={true}> 2 <Text>Item A</Text> 3</Badge> 4 5<Badge content="No Outline" color="secondary" showOutline={false}> 6 <Text>Item B</Text> 7</Badge>

Props Overview

PROPTYPEDEFAULTDESCRIPTION
childrenReactNode-The component the badge adorns.
contentstring | number | ReactNode-Content inside the badge. Ignored if isDot is true.
variant'solid' | 'flat' | 'faded' | 'shadow''solid'Visual style.
colorBadgeColor'default'Semantic color.
sizeBadgeSize'md'Size of the badge.
shapeBadgeShape'rectangle'Shape of the badge (or 'circle' for dots/single char).
placementBadgePlacement'top-right'Position relative to children.
showOutlinebooleantrueShow contrasting outline.
disableAnimationbooleanfalseDisable enter/exit animations.
isInvisiblebooleanfalseHide the badge.
isOneCharbooleanfalseHint for circular shape with single char content.
isDotbooleanfalseRender as a small dot, ignores content.
styleStyleProp<ViewStyle>-Style for the badge's outer wrapper (Badge + Children).
badgeStyleStyleProp<ViewStyle>-Custom style for the badge element itself.
textStyleStyleProp<TextStyle>-Custom style for text content within the badge.
maxCountnumber99Max number to show before displaying maxCount+.

Styling

Customize the appearance using:

  • style: Applied to the root View that wraps both the children and the badge itself. Useful for positioning the entire unit.
  • badgeStyle: Applied directly to the Animated.View that renders the badge. Use this for badge-specific styles like background, border, dimensions (though size prop is preferred for dimensions).
  • textStyle: Applied to the Text component inside the badge if the content is a string or number.
index.tsx
1<Badge 2 content="Styled" 3 color="primary" 4 style={{ margin: 10 }} // Styles the outer View container (Badge + Children) 5 badgeStyle={{ 6 borderWidth: 2, 7 borderColor: 'darkblue', 8 transform: [{ rotate: '-5deg' }] 9 }} 10 textStyle={{ 11 fontStyle: 'italic', 12 textDecorationLine: 'underline' 13 }} 14> 15 <View style={{padding: 10, backgroundColor: 'lightyellow'}}> 16 <Text>Content Here</Text> 17 </View> 18</Badge>

Accessibility

The Badge component itself is primarily decorative. Accessibility considerations depend on its context and content:

  • If the badge content conveys important information (e.g., "3 new messages"), ensure this information is also available to assistive technologies through the parent component it adorns. For example, an icon button with a badge might have an accessibilityLabel like "Notifications, 3 unread".
  • For purely decorative badges (e.g., a status dot that's visually explained elsewhere), no specific accessibility props might be needed on the badge itself.
  • The badge text will be read by screen readers if it's a string or number. If using custom ReactNode content, ensure that node is accessible.