Skip to content

[Android] Release the press when the button gets disabled - #4502

Merged
m-bert merged 1 commit into
@mbert/ios-disabled-button-mid-pressfrom
@mbert/android-disable-button-mid-press
Sep 9, 2026
Merged

m-bert merged 1 commit into
@mbert/ios-disabled-button-mid-pressfrom
@mbert/android-disable-button-mid-press

Conversation

@m-bert

@m-bert m-bert commented Sep 9, 2026

Copy link
Copy Markdown
Collaborator

Description

Follow-up to #4495.

When a Touchable/Pressable is disabled while a press is in progress, the button stayed dimmed until the next tap. Disabling the managed handler posts a cancel that reaches the button as a synthetic ACTION_CANCEL, but a disabled View only clears its pressed flag on ACTION_UP, so the press-in animation was never released.

setEnabled(false) now releases the press itself. The press visuals are reset with a zero-duration animation instead of the regular press-out fade: no finger lifted, and a disable usually restyles the button in the same frame, so the fade showed as a brief flash.

Test plan

Add a Touchable that disables itself shortly after onPressIn, e.g. a 700 ms timeout, and hold it past that point:

  • the button snaps back to its resting look the moment it gets disabled, with no flash
  • onPressOut fires at that moment and onPress does not fire on release
  • re-enabling and pressing again works as before
Tested on the following code:
import React, { useCallback, useRef, useState } from 'react';
import { StyleSheet, Text, View } from 'react-native';
import { Touchable } from 'react-native-gesture-handler';

// A button that gets disabled while a press is in progress.
// Press and hold the blue box. After `DISABLE_AFTER_MS` it disables itself.
// Expected: onPressOut fires at that moment, onPress never fires on release,
// and the press animation resets instead of staying stuck (macOS especially).
const DISABLE_AFTER_MS = 700;

export default function EmptyExample() {
  const [disabled, setDisabled] = useState(false);
  const [log, setLog] = useState<string[]>([]);
  const timer = useRef<ReturnType<typeof setTimeout> | null>(null);

  const push = useCallback((entry: string) => {
    console.log(`RNGH-mid-press: ${entry}`);
    setLog((l) => [...l.slice(-7), entry]);
  }, []);

  const onPressIn = useCallback(() => {
    push('onPressIn');
    timer.current = setTimeout(() => {
      push(`disabled after ${DISABLE_AFTER_MS}ms`);
      setDisabled(true);
    }, DISABLE_AFTER_MS);
  }, [push]);

  const onPressOut = useCallback(() => {
    push('onPressOut');
    if (timer.current) {
      clearTimeout(timer.current);
      timer.current = null;
    }
  }, [push]);

  const onPress = useCallback(() => push('onPress'), [push]);

  const reset = useCallback(() => {
    setDisabled(false);
    setLog([]);
  }, []);

  return (
    <View style={styles.container}>
      <Text>
        Hold the box for longer than {DISABLE_AFTER_MS}ms. It disables itself
        mid-press.
      </Text>
      <Touchable
        disabled={disabled}
        onPressIn={onPressIn}
        onPressOut={onPressOut}
        onPress={onPress}
        activeOpacity={0.4}
        style={[styles.box, disabled ? styles.boxDisabled : styles.boxEnabled]}>
        <Text style={styles.boxText}>{disabled ? 'disabled' : 'hold me'}</Text>
      </Touchable>
      <Touchable onPress={reset} style={styles.reset}>
        <Text style={styles.boxText}>re-enable and clear log</Text>
      </Touchable>
      {log.map((entry, i) => (
        <Text key={i} style={styles.log}>
          {entry}
        </Text>
      ))}
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    padding: 16,
    gap: 12,
  },
  box: {
    height: 100,
    borderRadius: 8,
    justifyContent: 'center',
    alignItems: 'center',
  },
  boxEnabled: {
    backgroundColor: '#5b7bd5',
  },
  boxDisabled: {
    backgroundColor: '#9a9a9a',
  },
  boxText: {
    color: 'white',
    fontWeight: '600',
  },
  reset: {
    height: 44,
    borderRadius: 8,
    backgroundColor: '#3a3a3a',
    justifyContent: 'center',
    alignItems: 'center',
  },
  log: {
    fontVariant: ['tabular-nums'],
  },
});

@m-bert
m-bert added this pull request to stack #4503 September 9, 2026 10:14
Copilot AI lite review requested due to automatic review settings September 9, 2026 10:14
@coderabbitai

coderabbitai Bot commented Sep 9, 2026 •

Copy link
Copy Markdown

Review Change StackReview Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Advanced

Run ID: 2e04a553-5797-45c3-bd5f-a01e5d9a6a4d

📥 Commits

Reviewing files that changed from the base of the PR and between 4623093 and aba1943.

📒 Files selected for processing (1)
  • packages/react-native-gesture-handler/android/src/main/java/com/swmansion/gesturehandler/react/RNGestureHandlerButtonViewManager.kt

Included review availability: Your plan provides up to 8 included reviews per hour; 6 remain after this review.


📝 Summary

Summary by CodeRabbit

  • Bug Fixes
    • Disabled buttons now immediately return to their resting visual state when pressed, preventing lingering pressed effects or delayed fade-out animations.

Walkthrough

Disabling a pressed button now clears its pressed state, cancels pending press-out handling, and immediately restores resting opacity, scale, and underlay values.

Changes

Button disable state

Layer / File(s) Summary
Reset pressed button on disable
packages/react-native-gesture-handler/android/src/main/java/com/swmansion/gesturehandler/react/RNGestureHandlerButtonViewManager.kt
When a pressed button is disabled, the view clears the pressed state, cancels the pending press-out callback, and applies the resting visual state without animation.

Suggested reviewers: j-piasecki, tomekzaw

Priority: ⬇️ Low

Merge Risk: ⚪ Minimal · up to aba19

Disabling an actively pressed Android button now immediately returns it to its resting state and prevents the release from completing the press. The targeted state transition is covered without an active merge-blocking risk.

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly and concisely describes the main Android change: releasing the press when the button becomes disabled.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The change is small, localized, and consistent with the existing animation/callback cancellation patterns in this view implementation.

Pull request overview

This PR fixes an Android press-state edge case for RNGestureHandlerButton: when a Touchable/Pressable becomes disabled during an active press, the visual “pressed” state could remain stuck until a subsequent interaction. The change ensures disabling the view actively releases the pressed visuals immediately (without a press-out fade that can flash during restyling).

Changes:

  • On setEnabled(false), if the button is currently pressed, it explicitly clears the pressed state and snaps animations back to the resting (non-pressed) visual state.
  • Cancels any pending press-out runnable to avoid delayed press-out behavior after a mid-press disable.
File summaries
File Description
packages/react-native-gesture-handler/android/src/main/java/com/swmansion/gesturehandler/react/RNGestureHandlerButtonViewManager.kt Release pressed visuals immediately when disabling mid-press by clearing pressed state, canceling pending press-out, and snapping animation to resting state.
Review details
  • Files reviewed: 1/1 changed files
  • Comments generated: 0
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@m-bert
m-bert requested a review from j-piasecki September 9, 2026 13:11
@m-bert
m-bert merged commit eabe157 into main Sep 9, 2026
6 checks passed
@m-bert
m-bert deleted the @mbert/android-disable-button-mid-press branch September 9, 2026 14:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants