Skip to content

Commit 249bff2

Browse files
authored
[iOS] Fix activeOpacity in BorderlessButton (#3433)
## Description Currently `activeOpacity` property in `BorderlessButton` doesn't work. It seems like it was broken by #2903. In this PR, `AnimatedBaseButton` is created from `InnerBaseButton` instead of `BaseButton` (which is a wrapper), so that props are correctly passed into animated view. Fixes #3426 ## Test plan <details> <summary>Tested on the following example:</summary> ```jsx import React, { useEffect, useRef } from 'react'; import { StyleSheet } from 'react-native'; import { BorderlessButton, GestureHandlerRootView, } from 'react-native-gesture-handler'; export default function EmptyExample() { const buttonRef = useRef(null); useEffect(() => { console.log(buttonRef); }, [buttonRef]); return ( <GestureHandlerRootView style={styles.container}> <BorderlessButton activeOpacity={0.7} ref={buttonRef} style={{ width: 100, height: 25, backgroundColor: 'crimson' }} /> </GestureHandlerRootView> ); } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#F5FCFF', }, }); ``` </details>
1 parent c9cd8d7 commit 249bff2

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

src/components/GestureButtons.tsx

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class InnerBaseButton extends React.Component<BaseButtonWithRefProps> {
3636
private longPressTimeout: ReturnType<typeof setTimeout> | undefined;
3737
private longPressDetected: boolean;
3838

39-
constructor(props: BaseButtonProps) {
39+
constructor(props: BaseButtonWithRefProps) {
4040
super(props);
4141
this.lastActive = false;
4242
this.longPressDetected = false;
@@ -146,12 +146,18 @@ class InnerBaseButton extends React.Component<BaseButtonWithRefProps> {
146146
}
147147
}
148148

149+
const AnimatedInnerBaseButton =
150+
Animated.createAnimatedComponent<typeof InnerBaseButton>(InnerBaseButton);
151+
149152
export const BaseButton = React.forwardRef<
150-
any,
153+
React.ComponentType,
151154
Omit<BaseButtonProps, 'innerRef'>
152155
>((props, ref) => <InnerBaseButton innerRef={ref} {...props} />);
153156

154-
const AnimatedBaseButton = Animated.createAnimatedComponent(BaseButton);
157+
const AnimatedBaseButton = React.forwardRef<
158+
React.ComponentType,
159+
BaseButtonWithRefProps
160+
>((props, ref) => <AnimatedInnerBaseButton innerRef={ref} {...props} />);
155161

156162
const btnStyles = StyleSheet.create({
157163
underlay: {
@@ -171,7 +177,7 @@ class InnerRectButton extends React.Component<RectButtonWithRefProps> {
171177

172178
private opacity: Animated.Value;
173179

174-
constructor(props: RectButtonProps) {
180+
constructor(props: RectButtonWithRefProps) {
175181
super(props);
176182
this.opacity = new Animated.Value(0);
177183
}
@@ -216,7 +222,7 @@ class InnerRectButton extends React.Component<RectButtonWithRefProps> {
216222
}
217223

218224
export const RectButton = React.forwardRef<
219-
any,
225+
React.ComponentType,
220226
Omit<RectButtonProps, 'innerRef'>
221227
>((props, ref) => <InnerRectButton innerRef={ref} {...props} />);
222228

@@ -228,7 +234,7 @@ class InnerBorderlessButton extends React.Component<BorderlessButtonWithRefProps
228234

229235
private opacity: Animated.Value;
230236

231-
constructor(props: BorderlessButtonProps) {
237+
constructor(props: BorderlessButtonWithRefProps) {
232238
super(props);
233239
this.opacity = new Animated.Value(1);
234240
}
@@ -247,8 +253,6 @@ class InnerBorderlessButton extends React.Component<BorderlessButtonWithRefProps
247253
return (
248254
<AnimatedBaseButton
249255
{...rest}
250-
// @ts-ignore We don't want `innerRef` to be accessible from public API.
251-
// However in this case we need to set it indirectly on `BaseButton`, hence we use ts-ignore
252256
innerRef={innerRef}
253257
onActiveStateChange={this.onActiveStateChange}
254258
style={[style, Platform.OS === 'ios' && { opacity: this.opacity }]}>
@@ -259,7 +263,7 @@ class InnerBorderlessButton extends React.Component<BorderlessButtonWithRefProps
259263
}
260264

261265
export const BorderlessButton = React.forwardRef<
262-
any,
266+
React.ComponentType,
263267
Omit<BorderlessButtonProps, 'innerRef'>
264268
>((props, ref) => <InnerBorderlessButton innerRef={ref} {...props} />);
265269

0 commit comments

Comments
 (0)