Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ref property to Buttons #2903

Merged
merged 6 commits into from
May 17, 2024
Merged

Add ref property to Buttons #2903

merged 6 commits into from
May 17, 2024

Conversation

m-bert
Copy link
Contributor

@m-bert m-bert commented May 10, 2024

Description

Currently our components (BaseButton, RectButton and BorderlessButton) don't support refs, so it is impossible to use methods like measure.

This PR adds wrapper to these components, so that they are now exported as ForwardRef.

Fixes #2894

Test plan

Tested on slightly modified code from issue
import React, { useRef } from 'react';
import { Text, StyleSheet } from 'react-native';

import {
  BaseButton,
  BorderlessButton,
  GestureHandlerRootView,
  RectButton,
} from 'react-native-gesture-handler';

export default function App() {
  const rectButtonRef = useRef(null);
  const borderlessButtonRef = useRef(null);
  const baseButtonRef = useRef(null);

  const handlePress = () => {
    try {
      baseButtonRef.current?.measure?.((x, y, width, height) => {
        console.log('baseButtonRef', x, y, width, height);
      });

      rectButtonRef.current?.measure?.((x, y) => {
        console.log('rectButtonRef', x, y);
      });

      borderlessButtonRef.current?.measure?.((x, y) => {
        console.log('borderlessButtonRef', x, y);
      });
    } catch (e) {
      console.error(e);
    }
  };

  return (
    <GestureHandlerRootView style={styles.container}>
      <RectButton onPress={handlePress} style={styles.button}>
        <Text style={styles.text}>Press me</Text>
      </RectButton>

      <BaseButton ref={baseButtonRef} style={styles.button}>
        <Text style={styles.text}>Test</Text>
      </BaseButton>
      <BorderlessButton ref={borderlessButtonRef} style={styles.button}>
        <Text style={styles.text}>Test</Text>
      </BorderlessButton>
      <RectButton ref={rectButtonRef} style={styles.button}>
        <Text style={styles.text}>Test</Text>
      </RectButton>
    </GestureHandlerRootView>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    gap: 20,
  },
  button: {
    justifyContent: 'center',
    alignItems: 'center',
    borderRadius: 5,
    backgroundColor: 'grey',
    paddingHorizontal: 20,
    paddingVertical: 10,
  },
  text: {
    color: 'white',
  },
});

@m-bert m-bert requested a review from j-piasecki May 10, 2024 10:20
src/components/GestureButtons.tsx Outdated Show resolved Hide resolved
/**
* Used to pass ref from parent into `BaseButton`
*/
innerRef?: React.ForwardedRef<unknown>;
Copy link
Member

Choose a reason for hiding this comment

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

Also, you mentioned not wanting innerRef to be accessible, I think we are exporting this type publicly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup, I've missed that. I've changed that in cf193d1, though I'm not sure if this is the best way to do this. Maybe there are some TypeScript tricks that we could use to make it look better, cc @tjzel

Copy link
Contributor

Choose a reason for hiding this comment

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

I can't really think of a solution here that wouldn't need any extensive changes sadly :c

@m-bert m-bert requested a review from j-piasecki May 14, 2024 07:54
@m-bert m-bert marked this pull request as ready for review May 15, 2024 09:23
Copy link
Member

@j-piasecki j-piasecki left a comment

Choose a reason for hiding this comment

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

Assuming that this doesn't break types, it looks ok to me.

@m-bert m-bert merged commit 93533fd into main May 17, 2024
1 check passed
@m-bert m-bert deleted the @mbert/add-ref-to-buttons branch May 17, 2024 10:09
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.

RectButton, BorderlessButton and BaseButton don't have support for View ref methods
3 participants