Badge
Small mobile status pill (View + Text) with 7 variants, attention pulse loop, and opt-in entering/exiting motion.
Install
First install? Set up the registry in your app.
npx --yes --package=https://native.uipkge.dev/cli/uipkge-native-0.1.1.tgz uipkge-native add badgeSource files
Expand a file to read the source the installer copies into your app.
components/ui/badge/Badge.tsx
import * as React from 'react'
import { Text, View, type ViewProps } from 'react-native'
import Animated, {
useAnimatedStyle,
useReducedMotion,
useSharedValue,
withRepeat,
withTiming,
} from 'react-native-reanimated'
import { cn } from '@/lib/utils'
import { enterFor, exitFor, motionDurations, type EnterPreset, type ExitPreset } from '@/lib/motion'
import { badgeTextVariants, badgeVariants, type BadgeVariants } from './badge.variants'
export interface BadgeProps extends ViewProps, BadgeVariants {
className?: string
children?: React.ReactNode
textClassName?: string
text?: string
/** Looping attention pulse (opacity 1 <-> 0.55). Use sparingly — one pulsing badge per screen. */
pulse?: boolean
/** Mount entrance. */
enter?: EnterPreset
/** Unmount exit, for badges removed from a conditional tree. */
exit?: ExitPreset
}
function Badge({
className,
textClassName,
variant = 'default',
children,
text,
pulse = false,
enter = 'none',
exit = 'none',
...props
}: BadgeProps) {
const reduced = useReducedMotion()
const opacity = useSharedValue(1)
const pulseStyle = useAnimatedStyle(() => ({ opacity: opacity.value }))
React.useEffect(() => {
opacity.value =
pulse && !reduced ? withRepeat(withTiming(0.55, { duration: motionDurations.slow * 2 }), -1, true) : 1
}, [pulse, reduced, opacity])
// Entering and pulse live on separate views: Reanimated warns when a layout
// animation and an animated style share one view.
return (
<Animated.View entering={reduced ? undefined : enterFor(enter)} exiting={reduced ? undefined : exitFor(exit)}>
<Animated.View cssInterop={false} style={pulse && !reduced ? pulseStyle : undefined}>
<View accessibilityRole="text" className={cn(badgeVariants({ variant }), className)} {...props}>
{(text ?? typeof children === 'string') ? (
<Text className={cn(badgeTextVariants({ variant }), textClassName)}>{text ?? children}</Text>
) : (
children
)}
</View>
</Animated.View>
</Animated.View>
)
}
export { Badge }
components/ui/badge/badge.variants.ts
import type { VariantProps } from 'class-variance-authority'
import { cva } from 'class-variance-authority'
export const badgeVariants = cva('flex-row items-center justify-center rounded-full border px-2 py-0.5', {
variants: {
variant: {
default: 'border-transparent bg-primary',
secondary: 'border-transparent bg-secondary',
destructive: 'border-transparent bg-destructive',
outline: 'border-border',
// Solid tokens, not `bg-success/15` tints like web: opacity modifiers on
// plain var() colors compile to nothing in the Tailwind v3 pipeline.
success: 'border-transparent bg-success',
warning: 'border-transparent bg-warning',
info: 'border-transparent bg-info',
},
},
defaultVariants: {
variant: 'default',
},
})
export const badgeTextVariants = cva('text-xs font-medium text-center', {
variants: {
variant: {
default: 'text-primary-foreground',
secondary: 'text-secondary-foreground',
destructive: 'text-destructive-foreground',
outline: 'text-foreground',
success: 'text-success-foreground',
warning: 'text-warning-foreground',
info: 'text-info-foreground',
},
},
defaultVariants: {
variant: 'default',
},
})
export type BadgeVariants = VariantProps<typeof badgeVariants>
components/ui/badge/index.ts
export { Badge, type BadgeProps } from './Badge'
export { badgeTextVariants, badgeVariants, type BadgeVariants } from './badge.variants'
Dependencies
Registry items
npm packages
class-variance-authorityreact-native-reanimated