# React Native Badge

> A React Native badge for iOS, Android and the web: a small label for status, counts and tags, in default, secondary, outline, success, warning and destructive variants.

# Badge

A small label for a status, a count or a tag.

```tsx
import { Badge } from 'patine-ui';
```

```tsx live
import { Badge, List, ListItem } from 'patine-ui';

const ORDERS = [
  { id: '#1042', customer: 'Maya Cohen', total: '$248.00', status: 'Paid' },
  { id: '#1041', customer: 'Leo Martin', total: '$92.50', status: 'Pending' },
  { id: '#1040', customer: 'Ana Silva', total: '$1,310.00', status: 'Overdue' },
  { id: '#1039', customer: 'Noa Levi', total: '$64.00', status: 'Refunded' },
] as const;

const VARIANT = { Paid: 'success', Pending: 'warning', Overdue: 'destructive', Refunded: 'secondary' } as const;

export default function Example() {
  return (
    <List title="Recent orders" style={{ maxWidth: 440, width: '100%' }}>
      {ORDERS.map((order) => (
        <ListItem
          key={order.id}
          title={`${order.id} · ${order.customer}`}
          description={order.total}
          trailing={<Badge variant={VARIANT[order.status]}>{order.status}</Badge>}
          onPress={() => {}}
        />
      ))}
    </List>
  );
}
```

## Example: a support queue

A help desk's ticket list: the filter shows how many tickets each view holds, and each ticket carries its priority and whether it breached its reply time.

```tsx live
import { useState } from 'react';
import { Badge, HStack, List, ListItem, SegmentedControl, Text, VStack } from 'patine-ui';

const TICKETS = [
  { id: 4821, subject: 'Card charged twice for one order', customer: 'Lena Novak', priority: 'Urgent', status: 'open', late: true },
  { id: 4819, subject: 'Can’t reset my password', customer: 'Omar Haddad', priority: 'High', status: 'open', late: false },
  { id: 4815, subject: 'Change the shipping address', customer: 'Yuki Tanaka', priority: 'Normal', status: 'pending', late: false },
  { id: 4802, subject: 'Invoice for October', customer: 'Carlos Ruiz', priority: 'Low', status: 'solved', late: false },
] as const;

const PRIORITY = { Urgent: 'destructive', High: 'warning', Normal: 'secondary', Low: 'outline' } as const;
const VIEWS = ['open', 'pending', 'solved'] as const;

export default function Example() {
  const [view, setView] = useState<string>('open');
  const count = (status: string) => TICKETS.filter((t) => t.status === status).length;
  return (
    <VStack gap="md" style={{ width: '100%', maxWidth: 480 }}>
      <SegmentedControl
        value={view}
        onValueChange={setView}
        items={VIEWS.map((v) => ({ value: v, label: `${v[0].toUpperCase()}${v.slice(1)} (${count(v)})` }))}
      />
      <List>
        {TICKETS.filter((t) => t.status === view).map((t) => (
          <ListItem
            key={t.id}
            title={t.subject}
            description={`#${t.id} · ${t.customer}`}
            trailing={
              <HStack gap="xs">
                {t.late ? <Badge variant="outline">Overdue</Badge> : null}
                <Badge variant={PRIORITY[t.priority]}>{t.priority}</Badge>
              </HStack>
            }
            onPress={() => {}}
          />
        ))}
      </List>
    </VStack>
  );
}
```

## Variants

`default` is the theme's primary color. `secondary` and `outline` are quiet, for tags and versions. `success`, `warning` and `destructive` are for status.

```tsx live
import { Badge, HStack } from 'patine-ui';

export default function Example() {
  return (
    <HStack gap="sm" wrap>
      <Badge>New</Badge>
      <Badge variant="secondary">Draft</Badge>
      <Badge variant="outline">v2.4.0</Badge>
      <Badge variant="success">Paid</Badge>
      <Badge variant="warning">Pending</Badge>
      <Badge variant="destructive">Overdue</Badge>
    </HStack>
  );
}
```

## Counts

A badge can hold a number, such as unread messages. Pin it to a corner with a wrapping `View`, and cap big numbers ("99+").

```tsx live
import { View } from 'react-native';
import { Badge, Button, HStack } from 'patine-ui';
import { Icon } from '@patine-ui/icons';

const count = (n: number) => (n > 99 ? '99+' : String(n));

export default function Example() {
  return (
    <HStack gap="xl" align="center" style={{ paddingTop: 10 }}>
      {[
        { icon: 'bell', label: 'Notifications', n: 3 },
        { icon: 'messageSquare', label: 'Messages', n: 128 },
        { icon: 'cart', label: 'Cart', n: 0 },
      ].map(({ icon, label, n }) => (
        <View key={label}>
          <Button size="icon" variant="outline" accessibilityLabel={n ? `${label}, ${n} new` : label}>
            <Icon name={icon} />
          </Button>
          {n ? (
            <Badge variant="destructive" style={{ position: 'absolute', top: -8, right: -10 }} accessibilityElementsHidden importantForAccessibility="no-hide-descendants">
              {count(n)}
            </Badge>
          ) : null}
        </View>
      ))}
    </HStack>
  );
}
```

## With an icon

`children` can be any node, so a badge can carry an icon or a dot before its label.

```tsx live
import { View } from 'react-native';
import { Badge, HStack, Text } from 'patine-ui';
import { Icon } from '@patine-ui/icons';

const Dot = ({ color }: { color: string }) => <View style={{ width: 6, height: 6, borderRadius: 3, backgroundColor: color }} />;

export default function Example() {
  return (
    <HStack gap="sm" wrap>
      <Badge variant="outline">
        <HStack gap="xs" align="center">
          <Dot color="#22C55E" />
          <Text size="xs" weight="medium">All systems operational</Text>
        </HStack>
      </Badge>
      <Badge variant="secondary">
        <HStack gap="xs" align="center">
          <Icon name="star" size={12} />
          <Text size="xs" weight="medium">Featured</Text>
        </HStack>
      </Badge>
      <Badge variant="success">
        <HStack gap="xs" align="center">
          <Icon name="check" size={12} color="success" />
          <Text size="xs" weight="medium">Verified</Text>
        </HStack>
      </Badge>
    </HStack>
  );
}
```

## Tags

A row of `outline` or `secondary` badges tags a post or a product. For tags people can pick, use a [ChipGroup](./chip-group) instead: a badge isn't pressable.

```tsx live
import { Badge, Card, CardContent, CardDescription, CardHeader, CardTitle, HStack } from 'patine-ui';

export default function Example() {
  return (
    <Card style={{ maxWidth: 380, width: '100%' }}>
      <CardHeader>
        <HStack gap="sm" align="center">
          <CardTitle>Building a design system in React Native</CardTitle>
        </HStack>
        <CardDescription>8 min read · March 12</CardDescription>
      </CardHeader>
      <CardContent>
        <HStack gap="xs" wrap>
          {['React Native', 'Design systems', 'Accessibility', 'Expo'].map((tag) => (
            <Badge key={tag} variant="outline">{tag}</Badge>
          ))}
        </HStack>
      </CardContent>
    </Card>
  );
}
```

## Custom colors

`theme` recolors a variant, and `style` and `labelStyle` reach the box and the text, for a brand color or a bigger badge.

```tsx live
import { Badge, HStack } from 'patine-ui';

export default function Example() {
  return (
    <HStack gap="sm" wrap align="center">
      <Badge theme={{ colors: { primary: '#7C3AED' } }}>Pro</Badge>
      <Badge style={{ backgroundColor: '#0EA5E9', borderColor: '#0EA5E9' }} labelStyle={{ color: '#fff' }}>Beta</Badge>
      <Badge variant="outline" style={{ paddingHorizontal: 12, paddingVertical: 4 }} labelStyle={{ fontSize: 14 }}>Large</Badge>
    </HStack>
  );
}
```

## Accessibility

- A badge is text, read where it sits, after the content around it.
- Don't rely on its color alone: the label should say the status ("Overdue", not just a red dot).
- A count pinned to a button belongs in the button's `accessibilityLabel` ("Messages, 128 new"); hide the badge itself from screen readers so it isn't read twice, as in [Counts](#counts).
- A badge isn't pressable. For something people tap, use a [Button](./button) or a [ChipGroup](./chip-group).

## Props

Badge also takes `View` props.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `children` | `ReactNode` | — | The label. A string is drawn in the variant's color. |
| `variant` | `'default' \| 'secondary' \| 'outline' \| 'success' \| 'warning' \| 'destructive'` | `'default'` | The look. |
| `labelStyle` | `StyleProp<TextStyle>` | — | The label's text. |
| `theme` | `DeepPartial<Theme>` | — | Theme override. |
| `design` | `'brand' \| 'ios' \| 'material' \| 'web' \| 'native'` | from `PatineProvider` | The look to follow: [brand, iOS, Material or web](../guides/designs), or `'native'` for the platform's own. |
