Commit a373442
authored
[Android] Handle hover in
## Description
This PR brings hover effects for the `Touchable` component on Android.
Previously, animations based on hover were possible only on web.
Hovering out is handled in post-frame callback. This is due to the fact
that on Android `ACTION_HOVER_EXIT` is dispatched right before
`ACTION_DOWN`, so without delaying hover out effect we would be left
with flickers.
## Test plan
<details>
<summary>Tested on existing Touchable example and on the code
below:</summary>
```tsx
import * as React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import {
GestureHandlerRootView,
Touchable as GHTouchable,
} from 'react-native-gesture-handler';
const SLOW = 700;
export default function App() {
const [pressCount, setPressCount] = React.useState(0);
const [disabled, setDisabled] = React.useState(false);
// Auto-toggle `disabled` every 2s so hover mask/resume can be tested with a
// mouse held still over the button — tapping a separate control would move
// the pointer off first. Hover and watch: the visual masks to default while
// disabled and resumes the hover look when re-enabled, pointer unmoved.
React.useEffect(() => {
const id = setInterval(() => setDisabled((d) => !d), 2000);
return () => clearInterval(id);
}, []);
return (
<GestureHandlerRootView style={styles.root}>
<Text style={styles.title}>Slow hover + press</Text>
<Text style={styles.hint}>
Use a mouse / stylus. Hover to grow & fade, press to shrink & fade more.
Transitions should never flicker through the default state.
</Text>
<View style={styles.stage}>
<GHTouchable
// Press (active) visuals
defaultOpacity={1}
defaultScale={1}
activeOpacity={0.3}
activeScale={0.8}
// Hover visuals
hoverOpacity={0.6}
hoverScale={1.2}
// Underlay so the change is extra visible
underlayColor="black"
defaultUnderlayOpacity={0}
hoverUnderlayOpacity={0.15}
activeUnderlayOpacity={0.35}
// Slow everything down: in/out for both tap and hover
animationDuration={{
tap: { in: SLOW, out: SLOW },
hover: { in: SLOW, out: SLOW },
}}
disabled={disabled}
style={styles.button}
onPress={() => setPressCount((c) => c + 1)}>
<Text style={styles.buttonText}>Hover / Press me</Text>
</GHTouchable>
</View>
<Text style={styles.counter}>
{disabled ? 'DISABLED' : 'enabled'} · Presses: {pressCount}
</Text>
</GestureHandlerRootView>
);
}
const styles = StyleSheet.create({
root: {
flex: 1,
backgroundColor: '#ecf0f1',
alignItems: 'center',
justifyContent: 'center',
padding: 24,
},
title: {
fontSize: 22,
fontWeight: 'bold',
color: '#2c3e50',
marginBottom: 8,
},
hint: {
fontSize: 14,
color: '#7f8c8d',
textAlign: 'center',
marginBottom: 40,
},
stage: {
width: 260,
height: 260,
alignItems: 'center',
justifyContent: 'center',
},
button: {
width: 180,
height: 180,
borderRadius: 24,
backgroundColor: '#8e44ad',
alignItems: 'center',
justifyContent: 'center',
},
buttonText: {
color: 'white',
fontSize: 18,
fontWeight: 'bold',
},
counter: {
marginTop: 40,
fontSize: 16,
color: '#2c3e50',
fontWeight: 'bold',
},
});
```
</details>Touchable (#4282)1 parent 1a713d0 commit a373442
4 files changed
Lines changed: 175 additions & 26 deletions
File tree
- packages
- docs-gesture-handler/docs/components
- react-native-gesture-handler
- android/src/main/java/com/swmansion/gesturehandler/react
- src
- components
- specs
Lines changed: 4 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
237 | 237 | | |
238 | 238 | | |
239 | 239 | | |
240 | | - | |
| 240 | + | |
241 | 241 | | |
242 | 242 | | |
243 | 243 | | |
| |||
247 | 247 | | |
248 | 248 | | |
249 | 249 | | |
250 | | - | |
| 250 | + | |
251 | 251 | | |
252 | 252 | | |
253 | 253 | | |
| |||
257 | 257 | | |
258 | 258 | | |
259 | 259 | | |
260 | | - | |
| 260 | + | |
261 | 261 | | |
262 | 262 | | |
263 | 263 | | |
| |||
296 | 296 | | |
297 | 297 | | |
298 | 298 | | |
299 | | - | |
| 299 | + | |
300 | 300 | | |
301 | 301 | | |
302 | 302 | | |
| |||
Lines changed: 158 additions & 17 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| 20 | + | |
20 | 21 | | |
21 | 22 | | |
22 | 23 | | |
| |||
331 | 332 | | |
332 | 333 | | |
333 | 334 | | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
334 | 360 | | |
335 | 361 | | |
336 | 362 | | |
| |||
381 | 407 | | |
382 | 408 | | |
383 | 409 | | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
384 | 418 | | |
385 | 419 | | |
386 | 420 | | |
| |||
402 | 436 | | |
403 | 437 | | |
404 | 438 | | |
| 439 | + | |
405 | 440 | | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
406 | 457 | | |
407 | 458 | | |
408 | 459 | | |
| |||
501 | 552 | | |
502 | 553 | | |
503 | 554 | | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
504 | 561 | | |
505 | 562 | | |
506 | 563 | | |
| |||
518 | 575 | | |
519 | 576 | | |
520 | 577 | | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
| 592 | + | |
| 593 | + | |
521 | 594 | | |
522 | 595 | | |
523 | 596 | | |
524 | 597 | | |
525 | 598 | | |
526 | 599 | | |
527 | 600 | | |
528 | | - | |
| 601 | + | |
529 | 602 | | |
530 | 603 | | |
531 | 604 | | |
| |||
548 | 621 | | |
549 | 622 | | |
550 | 623 | | |
| 624 | + | |
| 625 | + | |
| 626 | + | |
| 627 | + | |
| 628 | + | |
| 629 | + | |
| 630 | + | |
| 631 | + | |
| 632 | + | |
551 | 633 | | |
552 | 634 | | |
553 | 635 | | |
| |||
562 | 644 | | |
563 | 645 | | |
564 | 646 | | |
565 | | - | |
| 647 | + | |
566 | 648 | | |
567 | 649 | | |
568 | | - | |
| 650 | + | |
569 | 651 | | |
570 | 652 | | |
571 | 653 | | |
572 | 654 | | |
573 | 655 | | |
574 | 656 | | |
575 | 657 | | |
576 | | - | |
577 | | - | |
578 | | - | |
579 | | - | |
| 658 | + | |
580 | 659 | | |
581 | 660 | | |
582 | 661 | | |
| |||
594 | 673 | | |
595 | 674 | | |
596 | 675 | | |
597 | | - | |
| 676 | + | |
598 | 677 | | |
599 | 678 | | |
600 | | - | |
| 679 | + | |
601 | 680 | | |
602 | 681 | | |
603 | 682 | | |
604 | | - | |
| 683 | + | |
605 | 684 | | |
606 | 685 | | |
607 | 686 | | |
608 | 687 | | |
609 | 688 | | |
610 | 689 | | |
611 | | - | |
| 690 | + | |
612 | 691 | | |
613 | 692 | | |
614 | | - | |
| 693 | + | |
615 | 694 | | |
616 | 695 | | |
617 | 696 | | |
618 | | - | |
| 697 | + | |
619 | 698 | | |
620 | 699 | | |
621 | 700 | | |
| |||
635 | 714 | | |
636 | 715 | | |
637 | 716 | | |
| 717 | + | |
| 718 | + | |
| 719 | + | |
| 720 | + | |
| 721 | + | |
| 722 | + | |
| 723 | + | |
| 724 | + | |
| 725 | + | |
| 726 | + | |
| 727 | + | |
| 728 | + | |
| 729 | + | |
| 730 | + | |
| 731 | + | |
| 732 | + | |
| 733 | + | |
| 734 | + | |
| 735 | + | |
| 736 | + | |
| 737 | + | |
| 738 | + | |
| 739 | + | |
| 740 | + | |
| 741 | + | |
| 742 | + | |
| 743 | + | |
| 744 | + | |
| 745 | + | |
| 746 | + | |
| 747 | + | |
| 748 | + | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
| 752 | + | |
| 753 | + | |
| 754 | + | |
| 755 | + | |
| 756 | + | |
| 757 | + | |
| 758 | + | |
| 759 | + | |
| 760 | + | |
| 761 | + | |
| 762 | + | |
| 763 | + | |
| 764 | + | |
| 765 | + | |
| 766 | + | |
| 767 | + | |
638 | 768 | | |
639 | 769 | | |
640 | 770 | | |
| |||
645 | 775 | | |
646 | 776 | | |
647 | 777 | | |
648 | | - | |
| 778 | + | |
649 | 779 | | |
650 | 780 | | |
651 | | - | |
| 781 | + | |
652 | 782 | | |
653 | 783 | | |
654 | | - | |
| 784 | + | |
655 | 785 | | |
656 | 786 | | |
657 | 787 | | |
658 | 788 | | |
659 | 789 | | |
660 | 790 | | |
661 | | - | |
| 791 | + | |
662 | 792 | | |
663 | 793 | | |
664 | 794 | | |
| |||
783 | 913 | | |
784 | 914 | | |
785 | 915 | | |
| 916 | + | |
786 | 917 | | |
787 | 918 | | |
| 919 | + | |
788 | 920 | | |
789 | 921 | | |
790 | 922 | | |
| |||
913 | 1045 | | |
914 | 1046 | | |
915 | 1047 | | |
| 1048 | + | |
| 1049 | + | |
| 1050 | + | |
| 1051 | + | |
| 1052 | + | |
| 1053 | + | |
| 1054 | + | |
| 1055 | + | |
| 1056 | + | |
916 | 1057 | | |
917 | 1058 | | |
918 | 1059 | | |
| |||
Lines changed: 5 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
102 | 102 | | |
103 | 103 | | |
104 | 104 | | |
105 | | - | |
| 105 | + | |
106 | 106 | | |
107 | 107 | | |
108 | 108 | | |
109 | 109 | | |
110 | 110 | | |
111 | 111 | | |
112 | 112 | | |
113 | | - | |
| 113 | + | |
114 | 114 | | |
115 | 115 | | |
116 | 116 | | |
117 | 117 | | |
118 | 118 | | |
119 | 119 | | |
120 | 120 | | |
121 | | - | |
| 121 | + | |
122 | 122 | | |
123 | 123 | | |
124 | 124 | | |
125 | 125 | | |
126 | 126 | | |
127 | 127 | | |
128 | 128 | | |
129 | | - | |
| 129 | + | |
130 | 130 | | |
131 | 131 | | |
132 | 132 | | |
133 | 133 | | |
134 | 134 | | |
135 | 135 | | |
136 | | - | |
| 136 | + | |
137 | 137 | | |
138 | 138 | | |
139 | 139 | | |
| |||
0 commit comments