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
github-merge-queue bot pushed a commit to valora-inc/wallet that referenced this pull request Jun 26, 2024
…5575)

[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
|
[react-native-gesture-handler](https://togithub.com/software-mansion/react-native-gesture-handler)
| [`^2.16.2` ->
`^2.17.1`](https://renovatebot.com/diffs/npm/react-native-gesture-handler/2.16.2/2.17.1)
|
[![age](https://developer.mend.io/api/mc/badges/age/npm/react-native-gesture-handler/2.17.1?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/react-native-gesture-handler/2.17.1?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/react-native-gesture-handler/2.16.2/2.17.1?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/react-native-gesture-handler/2.16.2/2.17.1?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>software-mansion/react-native-gesture-handler
(react-native-gesture-handler)</summary>

###
[`v2.17.1`](https://togithub.com/software-mansion/react-native-gesture-handler/releases/tag/2.17.1)

[Compare
Source](https://togithub.com/software-mansion/react-native-gesture-handler/compare/2.17.0...2.17.1)

#### 🐛 Bug fixes

- Don't register `checkIntegrityBetweenArchitectures` task when
installed as a dependency by
[@&#8203;j-piasecki](https://togithub.com/j-piasecki) in
[#&#8203;2953](https://togithub.com/software-mansion/react-native-gesture-handler/issues/2953)

**Full Changelog**:
software-mansion/react-native-gesture-handler@2.17.0...2.17.1

###
[`v2.17.0`](https://togithub.com/software-mansion/react-native-gesture-handler/releases/tag/2.17.0)

[Compare
Source](https://togithub.com/software-mansion/react-native-gesture-handler/compare/2.16.2...2.17.0)

#### ❗ Important changes

- Add `ref` property to `Buttons` by
[@&#8203;m-bert](https://togithub.com/m-bert) in
[software-mansion/react-native-gesture-handler#2903
- Unify touch events callbacks on `web` with respect to `Android` by
[@&#8203;m-bert](https://togithub.com/m-bert) in
[software-mansion/react-native-gesture-handler#2923
- Add `touchType` to `TouchEvent` by
[@&#8203;latekvo](https://togithub.com/latekvo) in
[software-mansion/react-native-gesture-handler#2941

#### 👍 Improvements

- Refactor `GestureDetector` by
[@&#8203;j-piasecki](https://togithub.com/j-piasecki) in
[software-mansion/react-native-gesture-handler#2901
- Remove lodash by [@&#8203;m-bert](https://togithub.com/m-bert) in
[software-mansion/react-native-gesture-handler#2916
- `PointerTracker` refactor. by
[@&#8203;m-bert](https://togithub.com/m-bert) in
[software-mansion/react-native-gesture-handler#2931
- Change `offsetX` and `offsetY` calculations in `PointerEventManager`
by [@&#8203;m-bert](https://togithub.com/m-bert) in
[software-mansion/react-native-gesture-handler#2938

#### 🐛 Bug fixes

- Make `RootViewGestureHandler` handler cancel awaiting gestures by
[@&#8203;j-piasecki](https://togithub.com/j-piasecki) in
[software-mansion/react-native-gesture-handler#2900
- Get right bridge by
[@&#8203;piaskowyk](https://togithub.com/piaskowyk) in
[software-mansion/react-native-gesture-handler#2886
- Fix buttons getting stuck after scrolling on them by
[@&#8203;j-piasecki](https://togithub.com/j-piasecki) in
[software-mansion/react-native-gesture-handler#2693
- Fix Detector creating and attaching all gestures twice on first mount
by [@&#8203;j-piasecki](https://togithub.com/j-piasecki) in
[software-mansion/react-native-gesture-handler#2914
- Fix GestureDetector not working when its children change by
[@&#8203;j-piasecki](https://togithub.com/j-piasecki) in
[software-mansion/react-native-gesture-handler#2921
- Fix GestureDetector not working correctly with suspense and recycling
by [@&#8203;j-piasecki](https://togithub.com/j-piasecki) in
[software-mansion/react-native-gesture-handler#2925
- Fix nested buttons on the new architecture by
[@&#8203;j-piasecki](https://togithub.com/j-piasecki) in
[software-mansion/react-native-gesture-handler#2926
- Unify scaled coordinates between `web` and `native` side. by
[@&#8203;m-bert](https://togithub.com/m-bert) in
[software-mansion/react-native-gesture-handler#2943
- \[Web] Send relative coords in event. by
[@&#8203;m-bert](https://togithub.com/m-bert) in
[software-mansion/react-native-gesture-handler#2944

#### 🔢 Miscellaneous

- Add information about `GestureHandlerRootView` area and change
component name in quick start section in docs. by
[@&#8203;m-bert](https://togithub.com/m-bert) in
[software-mansion/react-native-gesture-handler#2899
- Add Hire us section to docs by
[@&#8203;patrycjakalinska](https://togithub.com/patrycjakalinska) in
[software-mansion/react-native-gesture-handler#2904
- Bump ejs from 3.1.7 to 3.1.10 by
[@&#8203;dependabot](https://togithub.com/dependabot) in
[software-mansion/react-native-gesture-handler#2888
- Bump tar from 6.2.0 to 6.2.1 in /e2e/web-tests by
[@&#8203;dependabot](https://togithub.com/dependabot) in
[software-mansion/react-native-gesture-handler#2859
- docs: add
[@&#8203;swmansion/t-rex-ui](https://togithub.com/swmansion/t-rex-ui) by
[@&#8203;kacperkapusciak](https://togithub.com/kacperkapusciak) in
[software-mansion/react-native-gesture-handler#2895
- Change navbar and footer to a reusable component by
[@&#8203;patrycjakalinska](https://togithub.com/patrycjakalinska) in
[software-mansion/react-native-gesture-handler#2902
- Add dark mode to button in `Hire us` section by
[@&#8203;patrycjakalinska](https://togithub.com/patrycjakalinska) in
[software-mansion/react-native-gesture-handler#2908
- Setup example app to use Expo by
[@&#8203;bohdanprog](https://togithub.com/bohdanprog) in
[software-mansion/react-native-gesture-handler#2905
- Revert "docs: add
[@&#8203;swmansion/t-rex-ui](https://togithub.com/swmansion/t-rex-ui)"
by [@&#8203;kacperkapusciak](https://togithub.com/kacperkapusciak) in
[software-mansion/react-native-gesture-handler#2909
- Bump [@&#8203;sideway/formula](https://togithub.com/sideway/formula)
from 3.0.0 to 3.0.1 by
[@&#8203;dependabot](https://togithub.com/dependabot) in
[software-mansion/react-native-gesture-handler#2910
- docs: add
[@&#8203;swmansion/t-rex-ui](https://togithub.com/swmansion/t-rex-ui) by
[@&#8203;patrycjakalinska](https://togithub.com/patrycjakalinska) in
[software-mansion/react-native-gesture-handler#2911
- Fix symlink loop during pods installation by
[@&#8203;j-piasecki](https://togithub.com/j-piasecki) in
[software-mansion/react-native-gesture-handler#2917
- Bump rexml from 3.2.6 to 3.2.8 in /FabricExample by
[@&#8203;dependabot](https://togithub.com/dependabot) in
[software-mansion/react-native-gesture-handler#2915
- Add animated header to the example app by
[@&#8203;j-piasecki](https://togithub.com/j-piasecki) in
[software-mansion/react-native-gesture-handler#2913
- Add option to auto-open last example in the example app by
[@&#8203;j-piasecki](https://togithub.com/j-piasecki) in
[software-mansion/react-native-gesture-handler#2918
- Replace current theme components with `@swmansion/t-rex-ui` by
[@&#8203;patrycjakalinska](https://togithub.com/patrycjakalinska) in
[software-mansion/react-native-gesture-handler#2906
- Bump rexml from 3.2.6 to 3.2.8 in /MacOSExample by
[@&#8203;dependabot](https://togithub.com/dependabot) in
[software-mansion/react-native-gesture-handler#2924
- Bump rexml from 3.2.6 to 3.2.8 in /example by
[@&#8203;dependabot](https://togithub.com/dependabot) in
[software-mansion/react-native-gesture-handler#2930
- feat: automatically copy codegen artifacts to paper by
[@&#8203;maciekstosio](https://togithub.com/maciekstosio) in
[software-mansion/react-native-gesture-handler#2933
- Add swipeable example rewritten to new API by
[@&#8203;latekvo](https://togithub.com/latekvo) in
[software-mansion/react-native-gesture-handler#2934
- Fix new swipeable using useMemo with incomplete dependency list by
[@&#8203;latekvo](https://togithub.com/latekvo) in
[software-mansion/react-native-gesture-handler#2937
- docs: update Hire us links to directly lead to contact form by
[@&#8203;kacperkapusciak](https://togithub.com/kacperkapusciak) in
[software-mansion/react-native-gesture-handler#2935
- chore(types): add missing pointerType to GestureTouchEvent by
[@&#8203;mgcrea](https://togithub.com/mgcrea) in
[software-mansion/react-native-gesture-handler#2928
- Bump braces from 3.0.2 to 3.0.3 in /example by
[@&#8203;dependabot](https://togithub.com/dependabot) in
[software-mansion/react-native-gesture-handler#2945
- Bump braces from 3.0.2 to 3.0.3 in /e2e/web-tests by
[@&#8203;dependabot](https://togithub.com/dependabot) in
[software-mansion/react-native-gesture-handler#2946
- Bump ws from 6.2.2 to 6.2.3 by
[@&#8203;dependabot](https://togithub.com/dependabot) in
[software-mansion/react-native-gesture-handler#2947
- Bump ws from 6.2.2 to 6.2.3 in /e2e/web-tests by
[@&#8203;dependabot](https://togithub.com/dependabot) in
[software-mansion/react-native-gesture-handler#2948
- Add `relativeCoords` average to `PointerTracker` by
[@&#8203;m-bert](https://togithub.com/m-bert) in
[software-mansion/react-native-gesture-handler#2939
- Add missing import in docs by
[@&#8203;piaskowyk](https://togithub.com/piaskowyk) in
[software-mansion/react-native-gesture-handler#2950

#### New Contributors

- [@&#8203;bohdanprog](https://togithub.com/bohdanprog) made their first
contribution in
[software-mansion/react-native-gesture-handler#2905
- [@&#8203;maciekstosio](https://togithub.com/maciekstosio) made their
first contribution in
[software-mansion/react-native-gesture-handler#2933
- [@&#8203;mgcrea](https://togithub.com/mgcrea) made their first
contribution in
[software-mansion/react-native-gesture-handler#2928

**Full Changelog**:
software-mansion/react-native-gesture-handler@2.16.2...2.17.0

</details>

---

### Configuration

📅 **Schedule**: Branch creation - "after 5pm,every weekend" in timezone
America/Los_Angeles, Automerge - "after 5pm,every weekend" in timezone
America/Los_Angeles.

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/valora-inc/wallet).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy40MTMuMiIsInVwZGF0ZWRJblZlciI6IjM3LjQxMy4yIiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6WyJucG0iLCJyZW5vdmF0ZSJdfQ==-->

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: valora-bot <valorabot@valoraapp.com>
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