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 useComposedEventHandler hook #5890

Open
wants to merge 31 commits into
base: main
Choose a base branch
from

Conversation

szydlovsky
Copy link
Contributor

@szydlovsky szydlovsky commented Apr 11, 2024

Summary

There have already been some requests for a way to merge our event handlers. Inspired by #5854, I created a hook that can work with any handlers made using useEvent. I will happily accept any comments and suggestions regarding typing since I am not so sure about it.

composeExample.mov

The PR can be merged only ater #5845 (it is also based on it).

Test plan

Open useComposedEventHandler example from Example app and watch event callbacks in console.

@szydlovsky szydlovsky force-pushed the @szydlovsky/composeHandlers/hook branch from a7b3a18 to 41ec161 Compare April 11, 2024 13:51
@tjzel tjzel changed the base branch from main to @szydlovsky/WorkletEventHandler-revamp April 15, 2024 12:28
Copy link
Contributor

@tjzel tjzel left a comment

Choose a reason for hiding this comment

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

Haven't read the example yet, but there are a few steps that should land in this PR.

  1. TypeScript tests. Since this is a new hook, we should come-up with some example usages and see if they don't generate TS errors for the users. See current TypeScript tests for reference.

  2. Docs page. It should be a standard to ship relevant documentation for new features ASAP. Since this is a relatively small PR I see no reason not to include it here.

src/reanimated2/hook/useComposedEventHandler.ts Outdated Show resolved Hide resolved
src/reanimated2/hook/useComposedEventHandler.ts Outdated Show resolved Hide resolved
@szydlovsky szydlovsky changed the title Add useComposedEventHandler hook Add useComposedEventHandler hook Apr 16, 2024
Base automatically changed from @szydlovsky/WorkletEventHandler-revamp to main April 17, 2024 11:48
@szydlovsky szydlovsky requested a review from tjzel April 18, 2024 13:54
Comment on lines +80 to +86
<Animated.ScrollView
onMomentumScrollBegin={composedHandler1}
onMomentumScrollEnd={composedHandler2}>
{[...new Array(20)].map((_each, index) => (
<Text>{index}</Text>
))}
</Animated.ScrollView>
Copy link
Contributor

Choose a reason for hiding this comment

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

Will this work properly in runtime? If not, annotate it here.

### Returns

The hook returns a handler object that can be hooked into any [`Animated component`](/docs/fundamentals/glossary#animated-component).
The handler should be passed to any `onEvent` (e.g. `onScroll`) parameter regardless of what events it handles.
Copy link
Contributor

Choose a reason for hiding this comment

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

This sentence is confusing. I think we should encourage more intuitive usage here, passing the handler to an event handler property that is related to the actual composed handler, e.g. merged onScroll handlers should go to onScroll property.

Copy link
Contributor

Choose a reason for hiding this comment

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

It would be nice to have more of a showcase example here. Also the typical usage would be to merge several handlers with the same expected events, e.g. multiple onScroll.

docs/src/theme/DocSidebarItems/index.js Outdated Show resolved Hide resolved
app/src/examples/ComposedHandlerExample.tsx Outdated Show resolved Hide resolved
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.

None yet

3 participants