From c4a2754c4b0c997f024e263120d1806e051027bd Mon Sep 17 00:00:00 2001 From: Samuel Newman Date: Mon, 5 Oct 2026 20:10:28 +0300 Subject: [PATCH] APP-3187: better haptics (intent-based API + native Android haptics) (#9940) --- AGENTS.md | 23 +++ oxlint-suppressions.json | 5 - package.json | 1 - patches/expo-haptics@57.0.3.patch | 13 -- patches/expo-haptics@57.0.3.patch.md | 11 -- pnpm-lock.yaml | 5 +- pnpm-workspace.yaml | 1 - src/components/ContextMenu/index.tsx | 14 +- src/components/DraggableList/index.tsx | 9 +- src/components/Post/Embed/AtCard/index.tsx | 6 +- .../Post/Embed/ExternalEmbed/index.tsx | 6 +- .../Post/Embed/StandardSiteEmbed/index.tsx | 16 +- .../PostControls/PostControlButton.tsx | 10 +- src/components/PostControls/RepostButton.tsx | 10 +- .../dialogs/PostInteractionSettingsDialog.tsx | 4 +- src/components/dms/NewMessagesPill.tsx | 6 +- src/components/dms/SwipeToReply.tsx | 4 +- src/components/forms/SegmentedControl.tsx | 6 +- src/components/forms/Toggle/index.tsx | 6 +- .../custom-animations/GestureActionView.tsx | 10 +- src/lib/haptics.ts | 31 ---- src/lib/haptics/__tests__/android.test.ts | 32 ++++ src/lib/haptics/android.ts | 56 +++++++ src/lib/haptics/index.ts | 145 ++++++++++++++++++ .../components/CustomFeedHeader.tsx | 18 +-- .../Messages/components/ChatListItem.tsx | 6 +- .../Messages/components/MessageComposer.tsx | 4 +- .../components/ThreadComposePrompt.tsx | 6 +- .../Profile/Header/ProfileHeaderLabeler.tsx | 12 +- .../Profile/Header/ProfileHeaderStandard.tsx | 10 +- src/screens/Profile/Header/Shell.tsx | 6 +- src/screens/ProfileList/components/Header.tsx | 4 +- src/screens/SavedFeeds.tsx | 10 +- src/screens/VideoFeed/index.tsx | 4 +- src/view/com/auth/SplashScreen.tsx | 6 +- src/view/com/home/HomeHeaderLayoutMobile.tsx | 4 +- src/view/com/util/UserAvatar.tsx | 4 +- src/view/com/util/fab/FABInner.tsx | 25 ++- src/view/screens/Storybook/Haptics.tsx | 106 +++++++++++++ src/view/screens/Storybook/Storybook.tsx | 2 + src/view/shell/bottom-bar/BottomBar.tsx | 10 +- 41 files changed, 492 insertions(+), 175 deletions(-) delete mode 100644 patches/expo-haptics@57.0.3.patch delete mode 100644 patches/expo-haptics@57.0.3.patch.md delete mode 100644 src/lib/haptics.ts create mode 100644 src/lib/haptics/__tests__/android.test.ts create mode 100644 src/lib/haptics/android.ts create mode 100644 src/lib/haptics/index.ts create mode 100644 src/view/screens/Storybook/Haptics.tsx diff --git a/AGENTS.md b/AGENTS.md index 1b8741886..6606c8001 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -298,6 +298,29 @@ is `[a.text_sm, a.leading_snug, t.atoms.text]`. Pass the `emoji` prop to any `Te that may contain emoji - user-generated text (display names etc.) almost always does, so only omit it for static, emoji-free strings: `Hello!`. +### Haptics + +`const haptics = useHaptics()` from `#/lib/haptics`. Call the method that +describes what happened, not how strong it should feel. Each method maps to the +matching haptic on each platform: iOS feedback generators, and Android +`performHapticFeedback` constants with fallbacks for older API levels. Haptics +do nothing on web or when the user has disabled them. + +- `tap()` - light acknowledgement of a tap (like, reply, opening a card) +- `confirm()` - the user committed a change (follow, pin, send a message) +- `longPress()` - a long-press did something (opened a menu or share sheet) +- `toggle(on)` - a switch, checkbox or radio changed; pass the new value +- `selection()` - the highlighted option changed (segmented control, drag slot) +- `threshold()` - a gesture crossed its activation point (swipe-to-reply) +- `dragStart()` - an item was picked up to be dragged +- `success()` / `error()` - something the user was waiting on finished + +Only reach for `haptics.platform({ios, android})` when no intent fits, and never +import `expo-haptics` directly. In worklets, destructure the method you need +(`const {threshold} = useHaptics()`) and pass it to `scheduleOnRN`; don't +capture the whole object. The emulator/simulator can't play haptics, so test on +a physical device via the haptics section of the Storybook. + ## Internationalization (i18n) All user-facing strings must be wrapped for translation using Lingui. Include `comment` and/or `context` props when necessary to avoid ambiguity, e.g., “Post” as a noun vs a verb. diff --git a/oxlint-suppressions.json b/oxlint-suppressions.json index 449d71d0b..d2c9e61de 100644 --- a/oxlint-suppressions.json +++ b/oxlint-suppressions.json @@ -539,11 +539,6 @@ "count": 1 } }, - "src/lib/haptics.ts": { - "typescript/no-floating-promises": { - "count": 1 - } - }, "src/lib/hooks/useAccountSwitcher.ts": { "typescript/no-explicit-any": { "count": 1 diff --git a/package.json b/package.json index 49b793f4f..62c4bd343 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,6 @@ "android": { "buildFromSource": [ "expo-notifications", - "expo-haptics", "expo-media-library", "expo-image-picker" ] diff --git a/patches/expo-haptics@57.0.3.patch b/patches/expo-haptics@57.0.3.patch deleted file mode 100644 index 0fc6b047a..000000000 --- a/patches/expo-haptics@57.0.3.patch +++ /dev/null @@ -1,13 +0,0 @@ -diff --git a/android/src/main/java/expo/modules/haptics/HapticsModule.kt b/android/src/main/java/expo/modules/haptics/HapticsModule.kt -index e29567622b4e03f9cb872e28458aa84c3249c05f..4a73a8908b1d4d3e0db96801de83b482abada07c 100644 ---- a/android/src/main/java/expo/modules/haptics/HapticsModule.kt -+++ b/android/src/main/java/expo/modules/haptics/HapticsModule.kt -@@ -53,7 +53,7 @@ class HapticsModule : Module() { - - private fun vibrate(type: HapticsVibrationType) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { -- vibrator.vibrate(VibrationEffect.createWaveform(type.timings, type.amplitudes, -1)) -+ vibrator.vibrate(VibrationEffect.createWaveform(type.oldSDKPattern, intArrayOf(0, 100), -1)) - } else { - @Suppress("DEPRECATION") - vibrator.vibrate(type.oldSDKPattern, -1) diff --git a/patches/expo-haptics@57.0.3.patch.md b/patches/expo-haptics@57.0.3.patch.md deleted file mode 100644 index afa7395bc..000000000 --- a/patches/expo-haptics@57.0.3.patch.md +++ /dev/null @@ -1,11 +0,0 @@ -# Expo Haptics Patch - -Whenever we migrated to Expo Haptics, there was a difference between how the previous and new libraries handled the -Android implementation of an iOS "light" haptic. The previous library used the `Vibration` API solely, which does not -have any configuration for intensity of vibration. The `Vibration` API has also been deprecated since SDK 26. See: -https://github.com/mkuczera/react-native-haptic-feedback/blob/master/android/src/main/java/com/mkuczera/vibrateFactory/VibrateWithDuration.java - -Expo Haptics is using `VibrationManager` API on SDK >= 31. See: https://github.com/expo/expo/blob/main/packages/expo-haptics/android/src/main/java/expo/modules/haptics/HapticsModule.kt#L19 -The timing and intensity of their haptic configurations though differs greatly from the original implementation. This -patch uses the new `VibrationManager` API to create the same vibration that would have been seen in the deprecated -`Vibration` API. diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index bbc9697ad..0c0672cce 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -221,7 +221,6 @@ patchedDependencies: '@formatjs/bigdecimal@0.2.5': 53afe18a845c1469b8b9002a460582559dfd52f502bc66a0f4c05662c2619291 '@sentry/expo-upload-sourcemaps@8.26.0': 81bbee7250f54e9a4b55178d83bf9c2985336991795f702e31987edb1d3ec3d6 expo-age-range@57.0.2: c325225749993424461eeece1d97a99b19c00da2ebc958a73c12e4eca0c39306 - expo-haptics@57.0.3: 73221c10bd656ec7f3a9fb36226f1a872e0341f809de7964ee4bec01e92d97ef expo-media-library@57.0.5: 375b13e1c0bfbfa31fec569e07f10862154a392d383192c49df7b55e5271bd04 expo-modules-core@57.0.18: 86e57bb33bc6c3f7021e948e099c2b5378772147b92a82a16e2e90ddcb857ace expo-notifications@57.0.20: dd096e8b43ac91716d4ffefc7d65945b491612d5c6fa57035e8af3f875697deb @@ -510,7 +509,7 @@ importers: version: 57.0.3(expo@57.0.24)(react-native@0.86.3(patch_hash=d6c16c8305d443b2ed7969db4b7217555c7e686ac3757a0fb46fcb82ea8c40ac)(@babel/core@7.29.0(supports-color@8.1.1))(@react-native/jest-preset@0.86.3(@babel/core@7.29.0(supports-color@8.1.1))(react@19.2.3)(supports-color@8.1.1))(@react-native/metro-config@0.86.3(@babel/core@7.29.0(supports-color@8.1.1))(supports-color@8.1.1))(@types/react@19.2.18)(react@19.2.3)(supports-color@8.1.1))(react@19.2.3) expo-haptics: specifier: ~57.0.3 - version: 57.0.3(patch_hash=73221c10bd656ec7f3a9fb36226f1a872e0341f809de7964ee4bec01e92d97ef)(expo@57.0.24) + version: 57.0.3(expo@57.0.24) expo-image: specifier: 57.0.5 version: 57.0.5(expo@57.0.24)(react-native-web@0.21.2(react-dom@19.2.3(react@19.2.3))(react@19.2.3))(react-native@0.86.3(patch_hash=d6c16c8305d443b2ed7969db4b7217555c7e686ac3757a0fb46fcb82ea8c40ac)(@babel/core@7.29.0(supports-color@8.1.1))(@react-native/jest-preset@0.86.3(@babel/core@7.29.0(supports-color@8.1.1))(react@19.2.3)(supports-color@8.1.1))(@react-native/metro-config@0.86.3(@babel/core@7.29.0(supports-color@8.1.1))(supports-color@8.1.1))(@types/react@19.2.18)(react@19.2.3)(supports-color@8.1.1))(react@19.2.3) @@ -15036,7 +15035,7 @@ snapshots: react: 19.2.3 react-native: 0.86.3(patch_hash=d6c16c8305d443b2ed7969db4b7217555c7e686ac3757a0fb46fcb82ea8c40ac)(@babel/core@7.29.0(supports-color@8.1.1))(@react-native/jest-preset@0.86.3(@babel/core@7.29.0(supports-color@8.1.1))(react@19.2.3)(supports-color@8.1.1))(@react-native/metro-config@0.86.3(@babel/core@7.29.0(supports-color@8.1.1))(supports-color@8.1.1))(@types/react@19.2.18)(react@19.2.3)(supports-color@8.1.1) - expo-haptics@57.0.3(patch_hash=73221c10bd656ec7f3a9fb36226f1a872e0341f809de7964ee4bec01e92d97ef)(expo@57.0.24): + expo-haptics@57.0.3(expo@57.0.24): dependencies: expo: 57.0.24(patch_hash=0856ae2db56b30b378fdaa8dbec8e7cda50b13fc29d89b873e1b12cc0affac33)(c4988a1376a5172d6a99e45a7015a76a) diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml index 59380fe9f..61d57a2d0 100644 --- a/pnpm-workspace.yaml +++ b/pnpm-workspace.yaml @@ -34,7 +34,6 @@ patchedDependencies: '@formatjs/bigdecimal@0.2.5': patches/@formatjs__bigdecimal@0.2.5.patch '@sentry/expo-upload-sourcemaps@8.26.0': patches/@sentry__expo-upload-sourcemaps@8.26.0.patch 'expo-age-range@57.0.2': patches/expo-age-range@57.0.2.patch - 'expo-haptics@57.0.3': patches/expo-haptics@57.0.3.patch 'expo-media-library@57.0.5': patches/expo-media-library@57.0.5.patch 'expo-modules-core@57.0.18': patches/expo-modules-core@57.0.18.patch expo-notifications@57.0.20: patches/expo-notifications@57.0.20.patch diff --git a/src/components/ContextMenu/index.tsx b/src/components/ContextMenu/index.tsx index 883215866..6124dd7e2 100644 --- a/src/components/ContextMenu/index.tsx +++ b/src/components/ContextMenu/index.tsx @@ -111,7 +111,7 @@ export function Provider({children}: {children: React.ReactNode}) { } export function Root({children}: {children: React.ReactNode}) { - const playHaptic = useHaptics() + const haptics = useHaptics() const [mode, setMode] = useState<'full' | 'auxiliary-only'>('full') const [measurement, setMeasurement] = useState(null) const returnLocationSV = useSharedValue<{x: number; y: number} | null>(null) @@ -199,7 +199,7 @@ export function Root({children}: {children: React.ReactNode}) { onTouchUpMenuItem: onHoverableTouchUp, hoveredMenuItem, setHoveredMenuItem: item => { - if (item) playHaptic('Light') + if (item) haptics.selection() setHoveredMenuItem(item) }, }) satisfies ContextType, @@ -215,7 +215,7 @@ export function Root({children}: {children: React.ReactNode}) { onHoverableTouchUp, hoveredMenuItem, setHoveredMenuItem, - playHaptic, + haptics, mode, ], ) @@ -243,7 +243,7 @@ export function Trigger({ swipeGesture, }: TriggerProps) { const context = useContextMenuContext() - const playHaptic = useHaptics() + const haptics = useHaptics() const insets = useSafeAreaInsets() const ref = useRef>(null) const isFocused = useIsFocused() @@ -255,7 +255,7 @@ export function Trigger({ const open = useNonReactiveCallback( async (mode: 'full' | 'auxiliary-only') => { - playHaptic() + haptics.longPress() const [measurement, capture] = await Promise.all([ measureView(ref.current, insets), captureRef(ref, {result: 'data-uri'}).catch(err => { @@ -769,7 +769,7 @@ export function Item({ }: ItemProps) { const t = useTheme() const context = useContextMenuContext() - const playHaptic = useHaptics() + const haptics = useHaptics() const {state: focused, onIn: onFocus, onOut: onBlur} = useInteractionState() const { state: pressed, @@ -842,7 +842,7 @@ export function Item({ onPressIn={e => { onPressIn() rest.onPressIn?.(e) - playHaptic('Light') + haptics.selection() }} onPressOut={e => { onPressOut() diff --git a/src/components/DraggableList/index.tsx b/src/components/DraggableList/index.tsx index 9a6c988ab..8634bb0fc 100644 --- a/src/components/DraggableList/index.tsx +++ b/src/components/DraggableList/index.tsx @@ -249,7 +249,8 @@ function SortableItem({ onDragEnd?: () => void }) { const t = useTheme() - const playHaptic = useHaptics() + const {dragStart: playDragStartHaptic, selection: playSelectionHaptic} = + useHaptics() const lastHapticSlot = useSharedValue(-1) @@ -267,7 +268,7 @@ function SortableItem({ if (onDragStart) { scheduleOnRN(onDragStart) } - scheduleOnRN(playHaptic) + scheduleOnRN(playDragStartHaptic) }, onUpdate: e => { 'worklet' @@ -283,9 +284,9 @@ function SortableItem({ (startSlot * itemHeight + clampedY) / itemHeight, ) const clampedSlot = Math.max(0, Math.min(currentSlot, itemCount - 1)) - if (IS_IOS && clampedSlot !== lastHapticSlot.get()) { + if (clampedSlot !== lastHapticSlot.get()) { lastHapticSlot.set(clampedSlot) - scheduleOnRN(playHaptic, 'Light') + scheduleOnRN(playSelectionHaptic) } }, onDeactivate: () => { diff --git a/src/components/Post/Embed/AtCard/index.tsx b/src/components/Post/Embed/AtCard/index.tsx index 54e374905..5ce0d1332 100644 --- a/src/components/Post/Embed/AtCard/index.tsx +++ b/src/components/Post/Embed/AtCard/index.tsx @@ -33,7 +33,7 @@ export function AtCard({ const t = useTheme() const {t: l} = useLingui() const {gtPhone} = useBreakpoints() - const playHaptic = useHaptics() + const haptics = useHaptics() const {data: authorProfile, isError: authorLookupFailed} = useProfileQuery({ did: authorDid, }) @@ -62,13 +62,13 @@ export function AtCard({ : l`Visit site on ${provider.name}` } onPress={() => { - playHaptic('Light') + haptics.tap() onEmbedInteractionCallback?.() }} onLongPress={ IS_NATIVE ? () => { - playHaptic('Heavy') + haptics.longPress() void shareUrl(view.uri) return false } diff --git a/src/components/Post/Embed/ExternalEmbed/index.tsx b/src/components/Post/Embed/ExternalEmbed/index.tsx index 64940d710..01550da54 100644 --- a/src/components/Post/Embed/ExternalEmbed/index.tsx +++ b/src/components/Post/Embed/ExternalEmbed/index.tsx @@ -39,7 +39,7 @@ export const ExternalEmbed = ({ }) => { const {_} = useLingui() const t = useTheme() - const playHaptic = useHaptics() + const haptics = useHaptics() const externalEmbedPrefs = useExternalEmbedsPrefs() const niceUrl = toNiceDomain(link.uri) const imageUri = link.thumb @@ -54,14 +54,14 @@ export const ExternalEmbed = ({ const hasMedia = Boolean(imageUri || embedPlayerParams) const onPress = () => { - playHaptic('Light') + haptics.tap() onOpen?.() } const onShareExternal = IS_NATIVE ? () => { if (link.uri) { - playHaptic('Heavy') + haptics.longPress() void shareUrl(link.uri) } } diff --git a/src/components/Post/Embed/StandardSiteEmbed/index.tsx b/src/components/Post/Embed/StandardSiteEmbed/index.tsx index 0c50723a1..28d110e28 100644 --- a/src/components/Post/Embed/StandardSiteEmbed/index.tsx +++ b/src/components/Post/Embed/StandardSiteEmbed/index.tsx @@ -44,7 +44,7 @@ export const StandardSiteEmbed = ({ const ax = useAnalytics() const {t: l, i18n} = useLingui() const t = useTheme() - const playHaptic = useHaptics() + const haptics = useHaptics() const niceUrl = toNiceDomain(view.uri) const imageUri = view.thumb const hasMedia = Boolean(imageUri) @@ -76,21 +76,21 @@ export const StandardSiteEmbed = ({ onOut: onInteractOut, } = useInteractionState() const onPress = () => { - playHaptic('Light') + haptics.tap() onEmbedInteractionCallback?.() ax.metric('embed:standardSite:article:press', {url: view.uri}) } const onLongPress = IS_NATIVE ? () => { if (view.uri) { - playHaptic('Heavy') + haptics.longPress() void shareUrl(view.uri) ax.metric('embed:standardSite:article:longPress', {url: view.uri}) } } : undefined const onPressPublication = () => { - playHaptic('Light') + haptics.tap() onEmbedInteractionCallback?.() ax.metric('embed:standardSite:publication:press', { url: view.source?.uri || '', @@ -99,7 +99,7 @@ export const StandardSiteEmbed = ({ const onLongPressPublication = IS_NATIVE ? () => { if (view.source?.uri) { - playHaptic('Heavy') + haptics.longPress() void shareUrl(view.source.uri) ax.metric('embed:standardSite:publication:longPress', { url: view.source.uri, @@ -439,7 +439,7 @@ export function SubscribeButton({ }) { const ax = useAnalytics() const {t: l} = useLingui() - const playHaptic = useHaptics() + const haptics = useHaptics() const highlightedPublisher = matchStandardSitePublisher(view) const cta = highlightedPublisher @@ -478,7 +478,7 @@ export function SubscribeButton({ : l`View publication` const onPress = () => { - playHaptic('Light') + haptics.tap() onEmbedInteractionCallback?.() if (highlightedPublisher) { ax.metric('embed:standardSite:subscribe:press', { @@ -494,7 +494,7 @@ export function SubscribeButton({ const onLongPress = IS_NATIVE ? () => { if (view.source?.uri) { - playHaptic('Heavy') + haptics.longPress() void shareUrl(view.source.uri) if (highlightedPublisher) { ax.metric('embed:standardSite:subscribe:longPress', { diff --git a/src/components/PostControls/PostControlButton.tsx b/src/components/PostControls/PostControlButton.tsx index e65a931f7..2e449e32e 100644 --- a/src/components/PostControls/PostControlButton.tsx +++ b/src/components/PostControls/PostControlButton.tsx @@ -35,7 +35,7 @@ export function PostControlButton({ hitSlop?: Insets }) { const t = useTheme() - const playHaptic = useHaptics() + const haptics = useHaptics() const ctx = useMemo( () => ({ @@ -62,18 +62,18 @@ export function PostControlButton({ const handlePress = useMemo(() => { if (!onPress) return return (evt: GestureResponderEvent) => { - playHaptic('Light') + haptics.tap() onPress(evt) } - }, [onPress, playHaptic]) + }, [onPress, haptics]) const handleLongPress = useMemo(() => { if (!onLongPress) return return (evt: GestureResponderEvent) => { - playHaptic('Heavy') + haptics.longPress() onLongPress(evt) } - }, [onLongPress, playHaptic]) + }, [onLongPress, haptics]) return ( + + iOS: {haptic.ios} · Android: {describeAndroid(haptic.android)} + + + ))} + + + iOS + + Impact styles, notification types, and selection + + + {IOS_HAPTICS.map(haptic => ( + + ))} + + + Android + + performHapticFeedback constants + + + {ANDROID_HAPTICS.map(haptic => ( + + ))} + + + ) +} diff --git a/src/view/screens/Storybook/Storybook.tsx b/src/view/screens/Storybook/Storybook.tsx index 633022bf4..6178ac1e2 100644 --- a/src/view/screens/Storybook/Storybook.tsx +++ b/src/view/screens/Storybook/Storybook.tsx @@ -18,6 +18,7 @@ import {Breakpoints} from './Breakpoints' import {Buttons} from './Buttons' import {Dialogs} from './Dialogs' import {Forms} from './Forms' +import {Haptics} from './Haptics' import {Icons} from './Icons' import {Links} from './Links' import {Menus} from './Menus' @@ -121,6 +122,7 @@ export default function Storybook() { + diff --git a/src/view/shell/bottom-bar/BottomBar.tsx b/src/view/shell/bottom-bar/BottomBar.tsx index bbf2d6c36..000cc6225 100644 --- a/src/view/shell/bottom-bar/BottomBar.tsx +++ b/src/view/shell/bottom-bar/BottomBar.tsx @@ -82,7 +82,7 @@ export function BottomBar({navigation}: BottomTabBarProps) { const dedupe = useDedupe() const accountSwitchControl = useDialogControl() const messagesMenuControl = Menu.useMenuControl() - const playHaptic = useHaptics() + const haptics = useHaptics() const hideBorder = useHideBottomBarBorder() const iconWidth = 28 @@ -146,15 +146,15 @@ export function BottomBar({navigation}: BottomTabBarProps) { }, [onPressTab]) const onLongPressProfile = useCallback(() => { - playHaptic() + haptics.longPress() accountSwitchControl.open() - }, [accountSwitchControl, playHaptic]) + }, [accountSwitchControl, haptics]) const onLongPressMessages = useCallback(() => { if (aa.flags.chatDisabled) return - playHaptic() + haptics.longPress() messagesMenuControl.open() - }, [aa.flags.chatDisabled, messagesMenuControl, playHaptic]) + }, [aa.flags.chatDisabled, messagesMenuControl, haptics]) const [demoMode] = useDemoMode() const {isActive: live} = useActorStatus(profile) -- knotmirror 0.0.1