Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

1.7 KB
Raw Blame
import type { ReactElement, Ref } from 'react'
import type { ViewInstance } from 'react-native'
import {
    type GestureResponderEvent,
    Pressable,
    type PressableProps,
    type PressableStateCallbackType,
    type StyleProp,
    type ViewStyle,
} from 'react-native'

import { type HapticKind, haptic } from '@/lib/haptics'

export interface Pressable44Props extends Omit<PressableProps, 'style'> {
    ref?: Ref<ViewInstance>
    style?: StyleProp<ViewStyle>
    pressedBackground?: string
    pressedStyle?: StyleProp<ViewStyle>
    haptic?: HapticKind
}

/** react-native-web adds `hovered` to the press state. */
type PressState = PressableStateCallbackType & { hovered?: boolean }

// Visual sizes go down to 34pt, so the default hit slop adds 6pt on each side; smaller targets pass a larger `hitSlop`.
export function Pressable44({
    style,
    pressedBackground,
    pressedStyle,
    haptic: hapticKind,
    hitSlop = 6,
    onPress,
    accessibilityRole = 'button',
    ...rest
}: Pressable44Props): ReactElement {
    const handlePress = (event: GestureResponderEvent): void => {
        if (hapticKind) {
            haptic(hapticKind)
        }
        onPress?.(event)
    }
    return (
        <Pressable
            {...rest}
            accessibilityRole={accessibilityRole}
            hitSlop={hitSlop}
            onPress={handlePress}
            style={(state: PressState) => {
                const active = (state.pressed || state.hovered === true) && !rest.disabled
                return [
                    style,
                    active && pressedBackground ? { backgroundColor: pressedBackground } : null,
                    active ? pressedStyle : null,
                ]
            }}
        />
    )
}