UIBadge
UIBadge
A versatile badge component that can be used to display status indicators, counters, and notifications with support for different types, positions, and customizations.
<template> <UIBadge :type="type" :value="value" :offset="offset" :max="99" :dot="showDot" :processing="isProcessing" > <UIAvatar /> </UIBadge></template>
<script setup lang="ts">const type = ref('default')const value = ref(5)const offset = ref([0, 0])const showDot = ref(false)const isProcessing = ref(false)</script>Props
id(string, required): Unique identifiertype(‘default’ | ‘success’ | ‘error’ | ‘warning’ | ‘info’): Badge typevalue(string | number): Badge contentcolor(string): Custom badge colordot(boolean): Show as dotmax(number): Maximum value to showprocessing(boolean): Show processing animationshowZero(boolean): Show badge when value is zeroshow(boolean): Control badge visibilityoffset([number | string, number | string]): Badge position offset
Slots
content: Content to attach badge tovalue: Custom badge content
Usage Examples
- Basic Badge:
<template> <UIBadge value="New"> <UIButton>Notifications</UIButton> </UIBadge></template>- Status Indicator:
<template> <UIBadge :dot="true" type="success" :offset="[-2, -2]" > <UIAvatar src="user-avatar.jpg" size="md" /> </UIBadge></template>- Counter Badge:
<template> <UIBadge :value="notificationCount" :max="99" type="error" > <BellIcon class="w-6 h-6" /> </UIBadge></template>
<script setup>const notificationCount = ref(150)</script>- Custom Styling:
<template> <UIBadge value="Premium" color="#FFD700" :offset="[0, -8]" > <div class="p-4 border rounded"> Premium Content </div> </UIBadge></template>- Processing State:
<template> <UIBadge :dot="true" :processing="true" type="warning" > <UIAvatar> <template #overlay> <div class="absolute inset-0 bg-black/40 flex items-center justify-center"> <UISpinner size="sm" class="text-white" /> </div> </template> </UIAvatar> </UIBadge></template>Best Practices
-
Visual Design:
- Use appropriate colors for different states
- Maintain consistent sizing
- Ensure good contrast
- Consider badge placement
-
User Experience:
- Clear and readable content
- Appropriate max values
- Smooth animations
- Responsive layout
-
Accessibility:
- Include ARIA labels
- Sufficient color contrast
- Screen reader support
- Keyboard navigation
-
Common Use Cases:
- Notification counters
- Status indicators
- Feature badges
- Online/offline status
Component Composition
The UIBadge component works well with:
- UIAvatar for user status
- UIButton for action indicators
- UIIcon for notifications
- UIMenu for counts
- UITabs for updates
- UIList for item status
Translation Support
<template> <UIBadge :value="$t('badge.value')" :type="type" > {{ $t('badge.content') }} </UIBadge></template>Mobile Considerations
-
Touch Targets:
- Ensure sufficient spacing
- Appropriate badge size
- Clear visibility
- Touch feedback
-
Responsive Behavior:
- Adapt to screen size
- Maintain readability
- Consider orientation
- Handle overflow
Error Handling
-
Value Validation:
- Handle empty values
- Validate numbers
- Check max limits
- Format display
-
Type Safety:
- Validate prop types
- Handle edge cases
- Provide defaults
- Clear error states