|
| 1 | +import { Meta, Canvas, Story, Controls } from '@storybook/addon-docs/blocks'; |
| 2 | +import * as BadgeStories from './Badge.stories'; |
| 3 | + |
| 4 | +<Meta of={BadgeStories} /> |
| 5 | + |
| 6 | +# Badge |
| 7 | + |
| 8 | +Badges are small, circular indicators that display numerical counts or short status text. They are typically used to draw attention to new, updated, or important information. |
| 9 | + |
| 10 | +## When to Use |
| 11 | + |
| 12 | +- **Notification counts**: Show unread messages, notifications, or pending items |
| 13 | +- **Status indicators**: Display simple status like "NEW", "HOT", or error counts |
| 14 | +- **Numerical data**: Show counts, quantities, or metrics at a glance |
| 15 | +- **Activity indicators**: Highlight areas with recent activity or updates |
| 16 | +- **Completion tracking**: Display progress counts (e.g., "3/10") |
| 17 | + |
| 18 | +## When Not to Use |
| 19 | + |
| 20 | +- **Long text**: Use `Tag` for longer labels or categorization |
| 21 | +- **Interactive elements**: Use `Button` for clickable actions |
| 22 | +- **Detailed information**: Use `Tooltip` or expanded content for complex details |
| 23 | +- **Navigation**: Use `Button` or `Link` for navigation purposes |
| 24 | + |
| 25 | +## Component |
| 26 | + |
| 27 | +<Story of={BadgeStories.Default} /> |
| 28 | + |
| 29 | +--- |
| 30 | + |
| 31 | +### Properties |
| 32 | + |
| 33 | +<Controls of={BadgeStories.Default} /> |
| 34 | + |
| 35 | +### Base Properties |
| 36 | + |
| 37 | +Badge extends from `Item` component and supports all base design system properties including: |
| 38 | +- Layout props: `width`, `height`, `margin`, `padding` |
| 39 | +- Color props: `fill`, `color`, `border` |
| 40 | +- Typography props: `preset`, `fontSize`, `fontWeight` |
| 41 | +- Other Item props: `tooltip`, `isDisabled`, `qa` |
| 42 | + |
| 43 | +## Examples |
| 44 | + |
| 45 | +### Themes |
| 46 | + |
| 47 | +Badges support multiple themes for different semantic meanings: |
| 48 | + |
| 49 | +<Story of={BadgeStories.Themes} /> |
| 50 | + |
| 51 | +Available themes: |
| 52 | +- **Special** (default): Purple/yellow background, highlighted features |
| 53 | +- **Note**: Blue background, informational content |
| 54 | +- **Success**: Green background, positive states |
| 55 | +- **Danger**: Red background, errors or urgent items |
| 56 | +- **Disabled**: Gray background, inactive items |
| 57 | + |
| 58 | +### Numbers |
| 59 | + |
| 60 | +Badges commonly display numerical counts: |
| 61 | + |
| 62 | +<Story of={BadgeStories.Numbers} /> |
| 63 | + |
| 64 | +Best practices for numbers: |
| 65 | +- Use single digits for small counts (1-9) |
| 66 | +- Show exact numbers up to 99 |
| 67 | +- Consider "99+" format for counts over 99 |
| 68 | +- Keep numerical badges concise and scannable |
| 69 | + |
| 70 | +```jsx |
| 71 | +<Badge>5</Badge> |
| 72 | +<Badge>99</Badge> |
| 73 | +<Badge theme="danger">12</Badge> |
| 74 | +``` |
| 75 | + |
| 76 | +### Text Content |
| 77 | + |
| 78 | +Short text labels work well in badges: |
| 79 | + |
| 80 | +<Story of={BadgeStories.TextContent} /> |
| 81 | + |
| 82 | +Guidelines for text badges: |
| 83 | +- Keep text to 2-4 characters maximum |
| 84 | +- Use uppercase for better readability |
| 85 | +- Common patterns: "NEW", "HOT", "BETA", "PRO" |
| 86 | + |
| 87 | +```jsx |
| 88 | +<Badge>NEW</Badge> |
| 89 | +<Badge theme="success">OK</Badge> |
| 90 | +<Badge theme="danger">ERR</Badge> |
| 91 | +``` |
| 92 | + |
| 93 | +### Sizes |
| 94 | + |
| 95 | +Badges support multiple sizes inherited from the Item component: |
| 96 | + |
| 97 | +<Story of={BadgeStories.Sizes} /> |
| 98 | + |
| 99 | +- **inline** (default): Compact, inline badge optimized for text flow |
| 100 | +- **xsmall**: Extra small size |
| 101 | +- **small**: Small size |
| 102 | +- **medium**: Medium size |
| 103 | +- **large**: Large size |
| 104 | +- **xlarge**: Extra large size |
| 105 | + |
| 106 | +### With Icons |
| 107 | + |
| 108 | +Badges can include icons for additional visual context: |
| 109 | + |
| 110 | +<Story of={BadgeStories.WithIcon} /> |
| 111 | + |
| 112 | +Add an icon before the badge content using the `icon` prop: |
| 113 | + |
| 114 | +```jsx |
| 115 | +import { IconCoin } from '@tabler/icons-react'; |
| 116 | + |
| 117 | +<Badge icon={<IconCoin />} theme="success"> |
| 118 | + 12 |
| 119 | +</Badge> |
| 120 | +``` |
| 121 | + |
| 122 | +### With Right Icon |
| 123 | + |
| 124 | +Place icons after the content using the `rightIcon` prop: |
| 125 | + |
| 126 | +<Story of={BadgeStories.WithRightIcon} /> |
| 127 | + |
| 128 | +```jsx |
| 129 | +<Badge rightIcon={<IconCoin />} theme="danger"> |
| 130 | + ERR |
| 131 | +</Badge> |
| 132 | +``` |
| 133 | + |
| 134 | +### With Both Icons |
| 135 | + |
| 136 | +Combine both `icon` and `rightIcon` for complex layouts: |
| 137 | + |
| 138 | +<Story of={BadgeStories.WithBothIcons} /> |
| 139 | + |
| 140 | +```jsx |
| 141 | +<Badge icon={<IconCoin />} rightIcon={<IconCoin />} theme="success"> |
| 142 | + NEW |
| 143 | +</Badge> |
| 144 | +``` |
| 145 | + |
| 146 | +## Best Practices |
| 147 | + |
| 148 | +### Content |
| 149 | + |
| 150 | +- Keep badge content concise and scannable |
| 151 | +- Use numbers for counts ("3" not "three") |
| 152 | +- Use uppercase for text badges for better readability |
| 153 | +- Consider "99+" format for very large counts |
| 154 | + |
| 155 | +### Placement |
| 156 | + |
| 157 | +- Position badges near related content without obscuring it |
| 158 | +- Maintain consistent positioning across similar contexts |
| 159 | + |
| 160 | +### Visual Design |
| 161 | + |
| 162 | +- Use theme colors consistently for semantic meaning |
| 163 | +- Ensure adequate spacing from surrounding content |
| 164 | + |
| 165 | +### Accessibility |
| 166 | + |
| 167 | +- Provide context for counts via tooltips when needed |
| 168 | +- Don't rely solely on color to convey meaning |
| 169 | +- All themes maintain sufficient color contrast |
| 170 | + |
| 171 | +## Use Cases |
| 172 | + |
| 173 | +### Navigation Badges |
| 174 | + |
| 175 | +Display notification counts on menu items: |
| 176 | + |
| 177 | +```jsx |
| 178 | +<Item icon={<IconBell />}> |
| 179 | + Notifications |
| 180 | + <Badge theme="danger">5</Badge> |
| 181 | +</Item> |
| 182 | +``` |
| 183 | + |
| 184 | +### Status Indicators |
| 185 | + |
| 186 | +Show item status or state: |
| 187 | + |
| 188 | +```jsx |
| 189 | +<Item> |
| 190 | + Beta Feature <Badge theme="special">BETA</Badge> |
| 191 | +</Item> |
| 192 | +``` |
| 193 | + |
| 194 | +### Inline Counts |
| 195 | + |
| 196 | +Display counts within text: |
| 197 | + |
| 198 | +```jsx |
| 199 | +<Text> |
| 200 | + Unread Messages <Badge>12</Badge> |
| 201 | +</Text> |
| 202 | +``` |
| 203 | + |
| 204 | +## Related Components |
| 205 | + |
| 206 | +- **Tag**: For longer labels, categorization, or closable elements |
| 207 | +- **Chip**: For interactive selection chips |
| 208 | +- **Item**: Base component that Badge extends from |
| 209 | +- **Text**: For displaying badge content inline with text |
0 commit comments