Button
Mobile button (Pressable + Text) with 6 variants, 4 sizes, busy loading state, spring/scale press physics, and opt-in entering/exiting motion. NativeWind styled, Expo + bare RN.
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 buttonSource files
Expand a file to read the source the installer copies into your app.
components/ui/button/Button.tsx
import * as React from 'react'
import { ActivityIndicator, Pressable, Text, type GestureResponderEvent, type PressableProps } from 'react-native'
import Animated, {
useAnimatedStyle,
useReducedMotion,
useSharedValue,
withSpring,
withTiming,
} from 'react-native-reanimated'
import { cn } from '@/lib/utils'
import { useThemeColors } from '@/lib/theme'
import { enterFor, exitFor, motionDurations, motionSprings, type EnterPreset, type ExitPreset } from '@/lib/motion'
import { buttonTextVariants, buttonVariants, type ButtonVariants } from './button.variants'
export type ButtonPress = 'none' | 'scale' | 'spring'
export interface ButtonProps extends Omit<PressableProps, 'children'>, ButtonVariants {
children?: React.ReactNode
className?: string
textClassName?: string
/** Show the platform activity indicator, disable presses, and mark the control busy. */
loading?: boolean
/** Override the activity indicator color when the variant default does not fit a custom palette. */
loadingColor?: React.ComponentProps<typeof ActivityIndicator>['color']
/** Press physics. `scale` dips to 0.97 in 120ms; `spring` uses the shared snappy spring. */
press?: ButtonPress
/** Mount entrance. Defaults to none — a list of buttons animating on mount reads as noise. */
enter?: EnterPreset
/** Unmount exit, for buttons removed from a conditional tree. */
exit?: ExitPreset
}
const Button = React.forwardRef<React.ComponentRef<typeof Pressable>, ButtonProps>(
(
{
className,
textClassName,
variant = 'default',
size = 'default',
disabled,
loading = false,
loadingColor,
press = 'scale',
enter = 'none',
exit = 'none',
onPressIn,
onPressOut,
accessibilityState,
children,
...props
},
ref,
) => {
const colors = useThemeColors()
const reduced = useReducedMotion()
const scale = useSharedValue(1)
const isDisabled = !!disabled || loading
const animated = press !== 'none' && !reduced && !isDisabled
const pressStyle = useAnimatedStyle(() => ({ transform: [{ scale: animated ? scale.value : 1 }] }))
const resolvedVariant = variant ?? 'default'
const resolvedSize = size ?? 'default'
const indicatorColor =
loadingColor ??
(resolvedVariant === 'default'
? colors.primaryForeground
: resolvedVariant === 'destructive'
? colors.destructiveForeground
: resolvedVariant === 'secondary'
? colors.secondaryForeground
: resolvedVariant === 'link'
? colors.primary
: colors.foreground)
React.useEffect(() => {
if (!animated) scale.value = 1
}, [animated, scale])
function handlePressIn(e: GestureResponderEvent) {
if (!isDisabled && animated) {
scale.value =
press === 'spring'
? withSpring(0.96, motionSprings.snappy)
: withTiming(0.97, { duration: motionDurations.fast })
}
onPressIn?.(e)
}
function handlePressOut(e: GestureResponderEvent) {
if (!isDisabled && animated) {
scale.value =
press === 'spring' ? withSpring(1, motionSprings.snappy) : withTiming(1, { duration: motionDurations.fast })
}
onPressOut?.(e)
}
// Entering and press-transform live on separate views: Reanimated warns
// when a layout animation and an animated transform share one view.
return (
<Animated.View entering={reduced ? undefined : enterFor(enter)} exiting={reduced ? undefined : exitFor(exit)}>
<Animated.View style={pressStyle}>
<Pressable
ref={ref}
accessibilityRole="button"
accessibilityState={{ ...accessibilityState, disabled: isDisabled, busy: loading }}
disabled={isDisabled}
onPressIn={handlePressIn}
onPressOut={handlePressOut}
className={cn(buttonVariants({ variant, size }), disabled && 'opacity-50', className)}
{...props}
>
{loading ? <ActivityIndicator color={indicatorColor} size="small" /> : null}
{loading && resolvedSize === 'icon'
? null
: // React Native requires text nodes to be inside a Text host.
React.Children.map(children, (child) =>
typeof child === 'string' || typeof child === 'number' ? (
<Text className={cn(buttonTextVariants({ variant, size }), textClassName)}>{child}</Text>
) : (
child
),
)}
</Pressable>
</Animated.View>
</Animated.View>
)
},
)
Button.displayName = 'Button'
export { Button }
components/ui/button/button.variants.ts
import type { VariantProps } from 'class-variance-authority'
import { cva } from 'class-variance-authority'
import { Platform } from 'react-native'
export const buttonVariants = cva('min-h-[48px] min-w-[48px] flex-row items-center justify-center gap-2 rounded-md', {
variants: {
variant: {
default: 'bg-primary active:opacity-90',
destructive: 'bg-destructive active:opacity-90',
outline: 'border border-border bg-background active:bg-accent',
secondary: 'bg-secondary active:opacity-80',
ghost: 'active:bg-accent',
link: '',
},
size: {
default: 'px-4 py-2',
sm: 'px-3 py-1.5',
lg: 'min-h-[52px] px-6 py-2.5',
icon: 'size-[48px]',
},
},
defaultVariants: {
variant: 'default',
size: 'default',
},
})
export const buttonTextVariants = cva('shrink text-center font-medium', {
variants: {
variant: {
default: 'text-primary-foreground',
destructive: 'text-destructive-foreground',
outline: 'text-foreground',
secondary: 'text-secondary-foreground',
ghost: 'text-foreground',
link: 'text-primary underline',
},
size: {
default: Platform.OS === 'ios' ? 'text-[17px] leading-[22px]' : 'text-[16px] leading-[24px]',
sm: 'text-[15px] leading-[21px]',
lg: 'text-[18px] leading-[24px]',
icon: 'text-[15px] leading-[21px]',
},
},
defaultVariants: {
variant: 'default',
size: 'default',
},
})
export type ButtonVariants = VariantProps<typeof buttonVariants>
components/ui/button/index.ts
export { Button, type ButtonProps } from './Button'
export { buttonTextVariants, buttonVariants, type ButtonVariants } from './button.variants'
Dependencies
Registry items
npm packages
class-variance-authorityreact-native-reanimated