Commit 4a7639d
authored
[General] Implement basic button interactions using native primitives (#4032)
## Description
Implements basic button interactions (scale, opacity, underlay) using
the native primitives and animations instead of relying on JS:
- `ObjectAnimator` and `Drawable` on Android
- CoreAnimation and `CALayer` on iOS/macOS
- CSS transitions on web
## Test plan
<details>
<summary>Tested on this</summary>
```jsx
import React from 'react';
import { ScrollView, StyleSheet, Text, View } from 'react-native';
import { GestureHandlerRootView } from 'react-native-gesture-handler';
import GestureHandlerButton from 'react-native-gesture-handler/src/components/GestureHandlerButton';
import createNativeWrapper from 'react-native-gesture-handler/src/v3/createNativeWrapper';
const RawButton = createNativeWrapper(GestureHandlerButton, {
shouldCancelWhenOutside: false,
shouldActivateOnStart: false,
});
function ButtonRow({
label,
children,
}: {
label: string;
children: React.ReactNode;
}) {
return (
<View style={styles.row}>
<Text style={styles.label}>{label}</Text>
{children}
</View>
);
}
export default function EmptyExample() {
return (
<GestureHandlerRootView>
<ScrollView contentContainerStyle={styles.container}>
{/* ── Opacity ─────────────────────────────────────────── */}
<ButtonRow label="Opacity: defaultOpacity=1 → activeOpacity=0.2 (strong fade)">
<RawButton
style={[
styles.button,
{ transform: [{ rotate: '5deg' }, { scale: 0.75 }] },
]}
defaultOpacity={1}
activeOpacity={0.2}
animationDuration={600}
rippleColor="transparent">
<Text style={styles.text}>Press me</Text>
</RawButton>
</ButtonRow>
<ButtonRow label="Opacity: defaultOpacity=0.5 → activeOpacity=1 (fade in on press)">
<RawButton
style={styles.button}
defaultOpacity={0.5}
activeOpacity={1}
animationDuration={200}>
<Text style={styles.text}>Press me</Text>
</RawButton>
</ButtonRow>
<ButtonRow label="Opacity: defaultOpacity=1 → activeOpacity=0 (disappears on press)">
<RawButton
style={styles.button}
defaultOpacity={1}
activeOpacity={0}
animationDuration={150}>
<Text style={styles.text}>Press me</Text>
</RawButton>
</ButtonRow>
{/* ── Scale ───────────────────────────────────────────── */}
<ButtonRow label="Scale: defaultScale=1 → activeScale=0.85 (shrink, fast)">
<RawButton
style={[styles.button, { opacity: 0.5 }]}
defaultScale={1}
activeScale={0.85}
animationDuration={150}>
<Text style={styles.text}>Press me</Text>
</RawButton>
</ButtonRow>
<ButtonRow label="Scale: defaultScale=0.9 → activeScale=1.05 (grow on press)">
<RawButton
style={styles.button}
defaultScale={0.9}
activeScale={1.05}
animationDuration={200}>
<Text style={styles.text}>Press me</Text>
</RawButton>
</ButtonRow>
<ButtonRow label="Scale: defaultScale=1 → activeScale=0.7 (heavy squish, slow)">
<RawButton
style={styles.button}
defaultScale={1}
activeScale={0.7}
animationDuration={500}>
<Text style={styles.text}>Press me</Text>
</RawButton>
</ButtonRow>
{/* ── Underlay ────────────────────────────────────────── */}
<ButtonRow label="Underlay: royalblue, defaultUnderlayOpacity=0 → activeUnderlayOpacity=0.4">
<RawButton
style={styles.button}
underlayColor="royalblue"
defaultUnderlayOpacity={0}
activeUnderlayOpacity={0.4}
animationDuration={200}>
<Text style={styles.text}>Press me</Text>
</RawButton>
</ButtonRow>
<ButtonRow label="Underlay: gold, defaultUnderlayOpacity=0.1 → activeUnderlayOpacity=0.9">
<RawButton
style={styles.button}
underlayColor="gold"
defaultUnderlayOpacity={0.1}
activeUnderlayOpacity={0.9}
animationDuration={300}>
<Text style={styles.text}>Press me</Text>
</RawButton>
</ButtonRow>
<ButtonRow label="Underlay: limegreen, defaultUnderlayOpacity=0.3 → activeUnderlayOpacity=0.3 (static)">
<RawButton
style={styles.button}
underlayColor="limegreen"
defaultUnderlayOpacity={0.3}
activeUnderlayOpacity={0.3}
animationDuration={200}>
<Text style={styles.text}>Press me</Text>
</RawButton>
</ButtonRow>
{/* ── Opacity + Scale ─────────────────────────────────── */}
<ButtonRow label="Opacity + Scale: fade out while shrinking (slow)">
<RawButton
style={styles.button}
defaultOpacity={1}
activeOpacity={0.4}
defaultScale={1}
activeScale={0.9}
animationDuration={600}>
<Text style={styles.text}>Press me</Text>
</RawButton>
</ButtonRow>
<ButtonRow label="Opacity + Scale: already dim, grows and brightens on press">
<RawButton
style={styles.button}
defaultOpacity={0.4}
activeOpacity={1}
defaultScale={0.95}
activeScale={1.05}
animationDuration={200}>
<Text style={styles.text}>Press me</Text>
</RawButton>
</ButtonRow>
{/* ── Opacity + Underlay ──────────────────────────────── */}
<ButtonRow label="Opacity + Underlay: fades while tomato underlay sweeps in">
<RawButton
style={styles.button}
defaultOpacity={1}
activeOpacity={0.6}
underlayColor="tomato"
defaultUnderlayOpacity={0}
activeUnderlayOpacity={0.5}
animationDuration={250}>
<Text style={styles.text}>Press me</Text>
</RawButton>
</ButtonRow>
{/* ── Scale + Underlay ────────────────────────────────── */}
<ButtonRow label="Scale + Underlay: shrinks and mediumpurple underlay appears">
<RawButton
style={styles.button}
defaultScale={1}
activeScale={0.9}
underlayColor="mediumpurple"
defaultUnderlayOpacity={0}
activeUnderlayOpacity={0.45}
animationDuration={200}>
<Text style={styles.text}>Press me</Text>
</RawButton>
</ButtonRow>
{/* ── All props combined ──────────────────────────────── */}
<ButtonRow label="All props: subtle feedback (opacity + scale + tomato underlay)">
<RawButton
style={styles.button}
defaultOpacity={1}
activeOpacity={0.6}
defaultScale={1}
activeScale={0.92}
underlayColor="tomato"
defaultUnderlayOpacity={0}
activeUnderlayOpacity={0.35}
animationDuration={250}>
<Text style={styles.text}>Press me</Text>
</RawButton>
</ButtonRow>
<ButtonRow label="All props: non-default start state (dim + small, active is full)">
<RawButton
style={styles.button}
defaultOpacity={0.5}
activeOpacity={1}
defaultScale={0.85}
activeScale={1}
underlayColor="deepskyblue"
defaultUnderlayOpacity={0.2}
activeUnderlayOpacity={0.6}
animationDuration={300}>
<Text style={styles.text}>Press me</Text>
</RawButton>
</ButtonRow>
<ButtonRow label="All props: bouncy grow + teal underlay + instant (duration=80)">
<RawButton
style={styles.button}
defaultOpacity={1}
activeOpacity={0.8}
defaultScale={1}
activeScale={1.08}
underlayColor="teal"
defaultUnderlayOpacity={0}
activeUnderlayOpacity={0.5}
animationDuration={80}>
<Text style={styles.text}>Press me</Text>
</RawButton>
</ButtonRow>
<ButtonRow label="All props: start dim+large, active invisible+tiny (extreme)">
<RawButton
style={styles.button}
defaultOpacity={0.5}
activeOpacity={0}
defaultScale={0.8}
activeScale={0.92}
underlayColor="tomato"
defaultUnderlayOpacity={0}
activeUnderlayOpacity={0.35}
animationDuration={250}>
<Text style={styles.text}>Press me</Text>
</RawButton>
</ButtonRow>
</ScrollView>
</GestureHandlerRootView>
);
}
const styles = StyleSheet.create({
container: {
padding: 24,
gap: 24,
paddingTop: 100,
},
row: {
gap: 8,
},
label: {
fontSize: 12,
color: '#666',
fontWeight: '500',
},
button: {
backgroundColor: '#e0e0e0',
borderRadius: 8,
padding: 16,
alignItems: 'center',
},
text: {
fontSize: 16,
fontWeight: '600',
},
});
```
</details>1 parent 1a7aea1 commit 4a7639d
File tree
15 files changed
+628
-123
lines changed- packages/react-native-gesture-handler
- android/src/main/java/com/swmansion/gesturehandler/react
- apple
- src
- components
- specs
- v3
- components
- types
- web/tools
15 files changed
+628
-123
lines changedLines changed: 142 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
3 | 6 | | |
4 | 7 | | |
5 | 8 | | |
| |||
19 | 22 | | |
20 | 23 | | |
21 | 24 | | |
22 | | - | |
23 | 25 | | |
24 | 26 | | |
25 | 27 | | |
| 28 | + | |
26 | 29 | | |
27 | 30 | | |
28 | 31 | | |
| |||
133 | 136 | | |
134 | 137 | | |
135 | 138 | | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
136 | 179 | | |
137 | 180 | | |
138 | 181 | | |
| |||
212 | 255 | | |
213 | 256 | | |
214 | 257 | | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
215 | 272 | | |
216 | 273 | | |
217 | 274 | | |
| |||
220 | 277 | | |
221 | 278 | | |
222 | 279 | | |
| 280 | + | |
| 281 | + | |
223 | 282 | | |
224 | 283 | | |
225 | 284 | | |
| |||
331 | 390 | | |
332 | 391 | | |
333 | 392 | | |
334 | | - | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
335 | 460 | | |
336 | 461 | | |
337 | 462 | | |
| |||
340 | 465 | | |
341 | 466 | | |
342 | 467 | | |
343 | | - | |
| 468 | + | |
344 | 469 | | |
345 | | - | |
| 470 | + | |
346 | 471 | | |
347 | 472 | | |
348 | 473 | | |
| |||
365 | 490 | | |
366 | 491 | | |
367 | 492 | | |
| 493 | + | |
| 494 | + | |
368 | 495 | | |
369 | 496 | | |
370 | 497 | | |
| |||
375 | 502 | | |
376 | 503 | | |
377 | 504 | | |
378 | | - | |
| 505 | + | |
379 | 506 | | |
380 | 507 | | |
381 | | - | |
| 508 | + | |
382 | 509 | | |
383 | | - | |
| 510 | + | |
384 | 511 | | |
| 512 | + | |
| 513 | + | |
385 | 514 | | |
386 | 515 | | |
387 | 516 | | |
| |||
540 | 669 | | |
541 | 670 | | |
542 | 671 | | |
| 672 | + | |
| 673 | + | |
| 674 | + | |
| 675 | + | |
| 676 | + | |
| 677 | + | |
543 | 678 | | |
544 | 679 | | |
545 | 680 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
| |||
23 | 24 | | |
24 | 25 | | |
25 | 26 | | |
| 27 | + | |
26 | 28 | | |
27 | 29 | | |
28 | 30 | | |
| |||
Lines changed: 21 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
31 | 52 | | |
32 | 53 | | |
33 | 54 | | |
| |||
0 commit comments