Step Indicator
Composed progress steps with caller-owned navigation, status indicators and optional triggers.
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 step-indicatorSource files
Expand a file to read the source the installer copies into your app.
components/ui/step-indicator/StepIndicator.tsx
import * as React from 'react'
import { Pressable, View, type PressableProps, type ViewProps } from 'react-native'
import { Text, type TextProps } from '@/components/ui/text'
import { renderTextChildren } from '@/lib/label-children'
import { cn } from '@/lib/utils'
export type StepIndicatorStatus = 'upcoming' | 'current' | 'complete' | 'error'
const StepContext = React.createContext<{
value: string | undefined
orientation: 'vertical' | 'horizontal'
disabled: boolean
select: (value: string) => void
} | null>(null)
const ItemContext = React.createContext<{ value: string; status: StepIndicatorStatus; disabled: boolean } | null>(null)
export interface StepIndicatorProps extends ViewProps {
value?: string
defaultValue?: string
onValueChange?: (value: string) => void
orientation?: 'vertical' | 'horizontal'
disabled?: boolean
className?: string
}
export const StepIndicator = React.forwardRef<React.ComponentRef<typeof View>, StepIndicatorProps>(
({ value, defaultValue, onValueChange, orientation = 'vertical', disabled = false, className, ...props }, ref) => {
const [internal, setInternal] = React.useState(defaultValue)
const resolved = value ?? internal
return (
<StepContext.Provider
value={{
value: resolved,
orientation,
disabled,
select(next) {
if (disabled || next === resolved) return
if (value === undefined) setInternal(next)
onValueChange?.(next)
},
}}
>
<View
ref={ref}
{...props}
className={cn(
orientation === 'vertical' ? 'gap-[4px]' : 'flex-row flex-wrap items-center gap-[8px]',
className,
)}
/>
</StepContext.Provider>
)
},
)
StepIndicator.displayName = 'StepIndicator'
export interface StepIndicatorItemProps extends ViewProps {
value: string
status?: StepIndicatorStatus
disabled?: boolean
className?: string
}
export const StepIndicatorItem = React.forwardRef<React.ComponentRef<typeof View>, StepIndicatorItemProps>(
({ value, status, disabled = false, className, ...props }, ref) => {
const steps = React.useContext(StepContext)
if (!steps) throw new Error('StepIndicatorItem must be inside StepIndicator')
const resolvedStatus = status ?? (steps.value === value ? 'current' : 'upcoming')
return (
<ItemContext.Provider value={{ value, status: resolvedStatus, disabled: steps.disabled || disabled }}>
<View
ref={ref}
{...props}
className={cn('min-w-0', steps.orientation === 'horizontal' && 'max-w-full', className)}
/>
</ItemContext.Provider>
)
},
)
StepIndicatorItem.displayName = 'StepIndicatorItem'
export interface StepIndicatorTriggerProps extends Omit<PressableProps, 'children'> {
children?: React.ReactNode
className?: string
}
export const StepIndicatorTrigger = React.forwardRef<React.ComponentRef<typeof Pressable>, StepIndicatorTriggerProps>(
({ children, className, disabled, onPress, accessibilityState, ...props }, ref) => {
const steps = React.useContext(StepContext)
const item = React.useContext(ItemContext)
if (!steps || !item) throw new Error('StepIndicatorTrigger must be inside StepIndicatorItem')
const isDisabled = !!disabled || item.disabled
return (
<Pressable
ref={ref}
{...props}
accessibilityRole="button"
accessibilityState={{ ...accessibilityState, selected: steps.value === item.value, disabled: isDisabled }}
disabled={isDisabled}
onPress={(event) => {
if (isDisabled) return
onPress?.(event)
if (!event.defaultPrevented) steps.select(item.value)
}}
className={cn(
'min-h-[48px] flex-row items-center gap-[12px] rounded-md px-[8px] py-[10px] active:bg-muted',
isDisabled && 'opacity-50',
className,
)}
>
{renderTextChildren(children)}
</Pressable>
)
},
)
StepIndicatorTrigger.displayName = 'StepIndicatorTrigger'
export interface StepIndicatorIndicatorProps extends ViewProps {
className?: string
}
export const StepIndicatorIndicator = React.forwardRef<React.ComponentRef<typeof View>, StepIndicatorIndicatorProps>(
({ children, className, ...props }, ref) => {
const item = React.useContext(ItemContext)
if (!item) throw new Error('StepIndicatorIndicator must be inside StepIndicatorItem')
const filled = item.status === 'current' || item.status === 'complete'
return (
<View
ref={ref}
{...props}
aria-hidden
className={cn(
'min-h-[32px] min-w-[32px] shrink-0 items-center justify-center rounded-full border border-border px-[6px] py-[4px]',
filled && 'border-primary bg-primary',
item.status === 'error' && 'border-destructive bg-destructive',
className,
)}
>
{renderTextChildren(
children,
cn(
'text-center font-medium',
filled && 'text-primary-foreground',
item.status === 'error' && 'text-destructive-foreground',
),
)}
</View>
)
},
)
StepIndicatorIndicator.displayName = 'StepIndicatorIndicator'
export const StepIndicatorTitle = React.forwardRef<React.ComponentRef<typeof Text>, TextProps>(
({ className, ...props }, ref) => <Text ref={ref} {...props} className={cn('font-medium', className)} />,
)
StepIndicatorTitle.displayName = 'StepIndicatorTitle'
export const StepIndicatorDescription = React.forwardRef<React.ComponentRef<typeof Text>, TextProps>(
({ className, ...props }, ref) => <Text ref={ref} size="sm" variant="muted" {...props} className={cn(className)} />,
)
StepIndicatorDescription.displayName = 'StepIndicatorDescription'
export const StepIndicatorSeparator = React.forwardRef<
React.ComponentRef<typeof View>,
ViewProps & { className?: string }
>(({ className, ...props }, ref) => {
const steps = React.useContext(StepContext)
if (!steps) throw new Error('StepIndicatorSeparator must be inside StepIndicator')
return (
<View
ref={ref}
{...props}
aria-hidden
className={cn(
'bg-border',
steps.orientation === 'vertical' ? 'ml-[24px] h-[16px] w-px' : 'h-px min-w-[16px] flex-1 self-center',
className,
)}
/>
)
})
StepIndicatorSeparator.displayName = 'StepIndicatorSeparator'
components/ui/step-indicator/index.ts
export {
StepIndicator,
StepIndicatorItem,
StepIndicatorTrigger,
StepIndicatorIndicator,
StepIndicatorTitle,
StepIndicatorDescription,
StepIndicatorSeparator,
} from './StepIndicator'
export type {
StepIndicatorProps,
StepIndicatorItemProps,
StepIndicatorTriggerProps,
StepIndicatorIndicatorProps,
StepIndicatorStatus,
} from './StepIndicator'