UIPackage React Native
Search

Progress

Determinate progress bar with clamped value and progressbar semantics, plus an indeterminate pulsing mode when value is omitted. Reduced-motion safe.

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 progress

Source files

Expand a file to read the source the installer copies into your app.

components/ui/progress/Progress.tsx
import * as React from 'react'
import { StyleSheet, View, type ViewProps } from 'react-native'
import Animated, {
  useAnimatedStyle,
  useReducedMotion,
  useSharedValue,
  withRepeat,
  withTiming,
} from 'react-native-reanimated'
import { cn } from '@/lib/utils'
import { motionDurations } from '@/lib/motion'

export interface ProgressProps extends ViewProps {
  className?: string
  indicatorClassName?: string
  /** Current value, clamped to 0..max. Omit or pass null for indeterminate. */
  value?: number | null
  max?: number
}

function Progress({ className, indicatorClassName, value, max = 100, accessibilityState, ...props }: ProgressProps) {
  const reduced = useReducedMotion()
  const indeterminate = value === undefined || value === null
  const safeMax = max > 0 ? max : 100
  const clamped = indeterminate ? 0 : Math.min(safeMax, Math.max(0, value))
  const percent = indeterminate ? 100 : (clamped / safeMax) * 100

  const fill = useSharedValue(percent)
  const opacity = useSharedValue(1)
  // Transform-only fill: the indicator is a full-width bar translated left by
  // the remaining percent, clipped by the track. Never animate `width` —
  // layout properties cost a layout pass per frame; transforms composite.
  const fillStyle = useAnimatedStyle(() => ({
    transform: [{ translateX: `${fill.value - 100}%` }],
    opacity: opacity.value,
  }))

  React.useEffect(() => {
    fill.value = reduced ? percent : withTiming(percent, { duration: motionDurations.base })
  }, [percent, reduced, fill])

  React.useEffect(() => {
    opacity.value =
      indeterminate && !reduced ? withRepeat(withTiming(0.45, { duration: motionDurations.slow * 2 }), -1, true) : 1
  }, [indeterminate, reduced, opacity])

  return (
    <View
      accessibilityRole="progressbar"
      accessibilityState={{ ...accessibilityState, busy: indeterminate || accessibilityState?.busy }}
      accessibilityValue={indeterminate ? undefined : { min: 0, max: safeMax, now: clamped }}
      className={cn('bg-secondary h-2 w-full overflow-hidden rounded-full', className)}
      {...props}
    >
      {/* Keep the animated transform out of NativeWind's style resolution. */}
      <Animated.View cssInterop={false} style={[StyleSheet.absoluteFill, fillStyle]}>
        <View className={cn('bg-primary h-full w-full rounded-full', indicatorClassName)} />
      </Animated.View>
    </View>
  )
}

export { Progress }
components/ui/progress/index.ts
export { Progress, type ProgressProps } from './Progress'

Dependencies

Registry items

npm packages

  • react-native-reanimated
Browse overlays and feedback