Pressable Feedback
Shared press-state container (Pressable + Reanimated) with optional highlight scrim for cards, tiles, and list rows without forcing button chrome or role.
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 pressable-feedbackSource files
Expand a file to read the source the installer copies into your app.
components/ui/pressable-feedback/PressableFeedback.tsx
import * as React from 'react'
import {
Pressable,
StyleSheet,
type GestureResponderEvent,
type PressableProps,
type StyleProp,
type ViewProps,
type ViewStyle,
} from 'react-native'
import Animated, {
useAnimatedStyle,
useReducedMotion,
useSharedValue,
withSpring,
withTiming,
type SharedValue,
} from 'react-native-reanimated'
import { motionDurations, motionSprings } from '@/lib/motion'
import { cn } from '@/lib/utils'
export type PressableFeedbackMode = 'scale' | 'spring' | 'none'
export interface PressableFeedbackContextValue {
pressed: boolean
disabled: boolean
progress: SharedValue<number>
}
const PressableFeedbackContext = React.createContext<PressableFeedbackContextValue | null>(null)
export function usePressableFeedback(): PressableFeedbackContextValue {
const ctx = React.useContext(PressableFeedbackContext)
const fallbackProgress = useSharedValue(0)
return React.useMemo(
() =>
ctx ?? {
pressed: false,
disabled: false,
progress: fallbackProgress,
},
[ctx, fallbackProgress],
)
}
function readProgressValue(raw: unknown): number {
'worklet'
if (typeof raw === 'number') return raw
if (raw && typeof raw === 'object' && 'value' in raw) {
const candidate = (raw as { value: unknown }).value
if (typeof candidate === 'number') return candidate
}
return 0
}
function clampScaleTo(scaleTo: number): number {
if (!Number.isFinite(scaleTo)) return 0.98
return Math.min(1, Math.max(0.01, scaleTo))
}
function clampOpacity(opacity: number): number {
if (!Number.isFinite(opacity)) return 0.08
return Math.min(1, Math.max(0, opacity))
}
export interface PressableFeedbackProps extends Omit<PressableProps, 'children'> {
children?: React.ReactNode
className?: string
/** Press physics. `scale` uses fast timing, `spring` uses the shared snappy spring, `none` skips scale. */
feedback?: PressableFeedbackMode
/** Target scale when pressed, clamped to `(0, 1]`. Defaults to `0.98`. */
scaleTo?: number
disabled?: boolean
}
const PressableFeedback = React.forwardRef<React.ComponentRef<typeof Pressable>, PressableFeedbackProps>(
(
{
children,
className,
feedback = 'scale',
scaleTo = 0.98,
disabled,
onPressIn,
onPressOut,
accessibilityRole,
accessibilityState,
...props
},
ref,
) => {
const reduced = useReducedMotion()
const isDisabled = Boolean(disabled)
const [pressed, setPressed] = React.useState(false)
const progress = useSharedValue(0)
const targetScale = clampScaleTo(scaleTo)
const shouldAnimateScale = feedback !== 'none' && !reduced && !isDisabled
React.useEffect(() => {
if (isDisabled) {
setPressed(false)
progress.value = 0
}
}, [isDisabled, progress])
const scaleStyle = useAnimatedStyle(() => {
const current = readProgressValue(progress.value)
return {
transform: [{ scale: 1 - (1 - targetScale) * current }],
}
})
function handlePressIn(event: GestureResponderEvent) {
if (!isDisabled) {
setPressed(true)
progress.value = reduced
? 1
: feedback === 'spring'
? withSpring(1, motionSprings.snappy)
: withTiming(1, { duration: motionDurations.fast })
}
onPressIn?.(event)
}
function handlePressOut(event: GestureResponderEvent) {
if (!isDisabled) {
setPressed(false)
progress.value = reduced
? 0
: feedback === 'spring'
? withSpring(0, motionSprings.snappy)
: withTiming(0, { duration: motionDurations.fast })
}
onPressOut?.(event)
}
const contextValue = React.useMemo<PressableFeedbackContextValue>(
() => ({
pressed: isDisabled ? false : pressed,
disabled: isDisabled,
progress,
}),
[isDisabled, pressed, progress],
)
const mergedAccessibilityState =
isDisabled || accessibilityState ? { ...accessibilityState, disabled: isDisabled } : undefined
return (
<PressableFeedbackContext.Provider value={contextValue}>
<Animated.View style={shouldAnimateScale ? scaleStyle : undefined}>
<Pressable
ref={ref}
disabled={isDisabled}
accessibilityRole={accessibilityRole}
accessibilityState={mergedAccessibilityState}
onPressIn={handlePressIn}
onPressOut={handlePressOut}
className={cn(isDisabled && 'opacity-50', className)}
{...props}
>
{children}
</Pressable>
</Animated.View>
</PressableFeedbackContext.Provider>
)
},
)
PressableFeedback.displayName = 'PressableFeedback'
export interface PressableFeedbackHighlightProps extends Omit<ViewProps, 'style' | 'pointerEvents'> {
className?: string
/** Peak highlight opacity while pressed, clamped to `[0, 1]`. Defaults to `0.08`. */
opacity?: number
style?: StyleProp<ViewStyle>
}
const PressableFeedbackHighlight = React.forwardRef<
React.ComponentRef<typeof Animated.View>,
PressableFeedbackHighlightProps
>(({ className, opacity = 0.08, style, ...props }, ref) => {
const { pressed, disabled, progress } = usePressableFeedback()
const reduced = useReducedMotion()
const peakOpacity = clampOpacity(opacity)
const animatedStyle = useAnimatedStyle(() => {
const current = readProgressValue(progress.value)
return {
opacity: current * peakOpacity,
}
})
const resolvedOpacityStyle = reduced ? { opacity: !disabled && pressed ? peakOpacity : 0 } : animatedStyle
return (
<Animated.View
ref={ref}
pointerEvents="none"
accessibilityElementsHidden
importantForAccessibility="no-hide-descendants"
className={cn('bg-foreground', className)}
style={[StyleSheet.absoluteFill, resolvedOpacityStyle, style]}
{...props}
/>
)
})
PressableFeedbackHighlight.displayName = 'PressableFeedbackHighlight'
export { PressableFeedback, PressableFeedbackHighlight }
components/ui/pressable-feedback/index.ts
export {
PressableFeedback,
PressableFeedbackHighlight,
usePressableFeedback,
type PressableFeedbackContextValue,
type PressableFeedbackHighlightProps,
type PressableFeedbackMode,
type PressableFeedbackProps,
} from './PressableFeedback'