Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
87c27e0
Revamped WorkletEventHandler
szydlovsky Mar 27, 2024
6b9c8f3
Merge branch 'main' into @szydlovsky/WorkletEventHandler-revamp
szydlovsky Mar 27, 2024
86e762b
fix import
szydlovsky Mar 27, 2024
42adc74
code review changes and fixes
szydlovsky Mar 28, 2024
30caad9
fix lint again
szydlovsky Mar 28, 2024
614c688
fix publicity of handler props
szydlovsky Mar 28, 2024
88583b2
linter give me a break
szydlovsky Mar 28, 2024
89cb76d
Merge branch 'main' into @szydlovsky/WorkletEventHandler-revamp
szydlovsky Mar 28, 2024
20b8d67
improved scroll handler props changes
szydlovsky Mar 29, 2024
b77737d
fix null/undefined check
szydlovsky Mar 29, 2024
9ba75de
further code review improvements
szydlovsky Apr 2, 2024
7de562a
make worklet a public API for now
szydlovsky Apr 3, 2024
f493098
Merge branch main into @szydlovsky/WorkletEventHandler-revamp
szydlovsky Apr 3, 2024
a2de0b7
small post main merge changes
szydlovsky Apr 3, 2024
ad1fe89
add null guard and a warn to scroll tag logic
szydlovsky Apr 4, 2024
4e911b8
cosmetical changes in worklet handler
szydlovsky Apr 5, 2024
faa8ec9
upgrade and clarify some tag logic
szydlovsky Apr 10, 2024
41ec161
add useComposedEventHandler hook with example
szydlovsky Apr 11, 2024
c9a1aae
Upgrade example's reusability
szydlovsky Apr 16, 2024
68bb83b
some cosmetical review changes
szydlovsky Apr 16, 2024
a88e13d
Add docs page
szydlovsky Apr 16, 2024
c43a3cd
fix docs lint
szydlovsky Apr 16, 2024
d736682
Merge branch '@szydlovsky/WorkletEventHandler-revamp' into @szydlovsk…
szydlovsky Apr 17, 2024
d7f1708
Merge branch 'main' into @szydlovsky/composeHandlers/hook
szydlovsky Apr 17, 2024
608c08d
improve typing
szydlovsky Apr 17, 2024
72eb8e5
add interal handler type
szydlovsky Apr 17, 2024
a9944c1
remove unneeded exports
szydlovsky Apr 17, 2024
decf4eb
add missing use strict
szydlovsky Apr 17, 2024
8050176
add typetests
szydlovsky Apr 18, 2024
e5f8200
add more examples for the hook
szydlovsky May 6, 2024
08211a3
update new apis in docs
szydlovsky May 6, 2024
6593bd4
moar code review changes (hopefully final)
szydlovsky May 10, 2024
734358e
Merge branch 'main' into @szydlovsky/composeHandlers/hook
szydlovsky May 10, 2024
1add139
more polishing
szydlovsky May 14, 2024
cad7962
revert unneeded export
szydlovsky May 14, 2024
e666193
small naming change
szydlovsky May 14, 2024
28b98a2
Merge branch 'main' into @szydlovsky/composeHandlers/hook
szydlovsky May 14, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions __typetests__/common/useComposedEventHandlerTest.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/* eslint-disable no-unused-expressions */
/* eslint-disable @typescript-eslint/no-empty-function */
/* eslint-disable @typescript-eslint/ban-ts-comment */
/* eslint-disable @typescript-eslint/no-unused-vars */
import React from 'react';
import Animated, {
useAnimatedScrollHandler,
useComposedEventHandler,
useEvent,
} from '../..';
import { Text } from 'react-native';
import type { ReanimatedEvent } from '../..';

function useComposedEventHandlerTest() {
function useComposedEventHandlerTestDifferentHandlers() {
function useCustomScrollHandler(
handler: (event: ReanimatedEvent<object>) => void
) {
return useEvent(
(event) => {
'worklet';
handler(event);
},
['onScroll']
);
}

const onScrollCustomHandler = useCustomScrollHandler((e) => {
'worklet';
console.log(`custom scroll handler invoked on ${e.eventName}`);
});

const onScrollHandler = useAnimatedScrollHandler((e) => {
console.log(`scroll handler invoked on ${e.eventName}`);
});

const composedHandler = useComposedEventHandler([
onScrollHandler,
onScrollCustomHandler,
]);

return (
<>
<Animated.ScrollView onScroll={composedHandler}>
{[...new Array(20)].map((_each, index) => (
<Text>{index}</Text>
))}
</Animated.ScrollView>
</>
);
}

function useComposedEventHandlerTestDifferentProps() {
const onDragBeginHandler = useAnimatedScrollHandler({
onBeginDrag(e) {
'worklet';
console.log('Drag begin');
},
});

const onDragEndHandler = useAnimatedScrollHandler({
onEndDrag(e) {
'worklet';
console.log('Drag end');
},
});

const composedHandler1 = useComposedEventHandler([
onDragBeginHandler,
onDragEndHandler,
]);

const composedHandler2 = useComposedEventHandler([
onDragBeginHandler,
onDragEndHandler,
]);

return (
<>
<Animated.ScrollView
// This will work well thanks to the way it is filtered in `PropsFilter`.
onMomentumScrollBegin={composedHandler1}
// same as above
onMomentumScrollEnd={composedHandler2}>
{[...new Array(20)].map((_each, index) => (
<Text>{index}</Text>
))}
</Animated.ScrollView>
</>
);
}
}
147 changes: 147 additions & 0 deletions app/src/examples/ComposedHandlerConditionalExample.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import React from 'react';
import { Text, View, StyleSheet, Button } from 'react-native';
import Animated, {
useAnimatedScrollHandler,
useComposedEventHandler,
} from 'react-native-reanimated';

export default function ComposedHandlerConditionalExample() {
const [toggleFirst, setToggleFirst] = React.useState(true);
const [toggleSecond, setToggleSecond] = React.useState(true);
const [toggleThird, setToggleThird] = React.useState(true);

const handlerFunc = React.useCallback(
(handlerName: string, eventName: string) => {
'worklet';
console.log(`${handlerName} handler: ${eventName}`);
},
[]
);

const firstHandler = useAnimatedScrollHandler({
onScroll(e) {
handlerFunc('first', e.eventName);
},
});

const secondHandler = useAnimatedScrollHandler({
onScroll(e) {
handlerFunc('second', e.eventName);
},
});

const thirdHandler = useAnimatedScrollHandler({
onScroll(e) {
handlerFunc('third', e.eventName);
},
});

const composedHandler = useComposedEventHandler([
toggleFirst ? firstHandler : null,
toggleSecond ? secondHandler : null,
toggleThird ? thirdHandler : null,
]);

return (
<View style={styles.container}>
<Text style={styles.infoText}>Check console logs!</Text>
<ToggleButton
name={'first'}
isToggled={toggleFirst}
onPressFunc={() => setToggleFirst(!toggleFirst)}
/>
<ToggleButton
name={'second'}
isToggled={toggleSecond}
onPressFunc={() => setToggleSecond(!toggleSecond)}
/>
<ToggleButton
name={'third'}
isToggled={toggleThird}
onPressFunc={() => setToggleThird(!toggleThird)}
/>
<Animated.FlatList
onScroll={composedHandler}
style={styles.list}
data={items}
renderItem={({ item }) => <Item title={item.title} />}
keyExtractor={(item) => `A:${item.title}`}
/>
</View>
);
}

type ToggleProps = {
name: string;
isToggled: boolean;
onPressFunc: () => void;
};
const ToggleButton = ({ name, isToggled, onPressFunc }: ToggleProps) => (
<View style={styles.toggleContainer}>
<View
style={[
styles.toggleIcon,
isToggled ? styles.toggleON : styles.toggleOFF,
]}
/>
<Button
title={`Toggle ${name} handler ${isToggled ? 'OFF' : 'ON'}`}
onPress={onPressFunc}
/>
</View>
);

type ItemValue = { title: string };
const items: ItemValue[] = [...new Array(101)].map((_each, index) => {
return { title: `${index}` };
});

type ItemProps = { title: string };
const Item = ({ title }: ItemProps) => (
<View style={styles.item}>
<Text style={styles.title}>{title}</Text>
</View>
);

const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'column',
},
infoText: {
fontSize: 19,
alignSelf: 'center',
},
list: {
flex: 1,
},
item: {
backgroundColor: '#883ef0',
alignItems: 'center',
padding: 20,
marginVertical: 8,
marginHorizontal: 16,
borderRadius: 20,
},
title: {
fontSize: 32,
},
toggleContainer: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'center',
},
toggleIcon: {
width: 20,
height: 20,
borderRadius: 10,
borderWidth: 3,
borderColor: 'black',
},
toggleON: {
backgroundColor: '#7CFC00',
},
toggleOFF: {
backgroundColor: 'red',
},
});
96 changes: 96 additions & 0 deletions app/src/examples/ComposedHandlerDifferentEventsExample.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import React from 'react';
import { Text, View, StyleSheet } from 'react-native';
import Animated, {
useAnimatedScrollHandler,
useComposedEventHandler,
} from 'react-native-reanimated';

export default function ComposedHandlerDifferentEventsExample() {
const handlerFunc = React.useCallback(
(handlerName: string, eventName: string) => {
'worklet';
console.log(`${handlerName} handler: ${eventName}`);
},
[]
);

const onScrollHandler = useAnimatedScrollHandler({
onScroll(e) {
handlerFunc('scroll', e.eventName);
},
});

const onDragHandler = useAnimatedScrollHandler({
onBeginDrag(e) {
handlerFunc('drag', e.eventName);
},
onEndDrag(e) {
handlerFunc('drag', e.eventName);
},
});

const onMomentumHandler = useAnimatedScrollHandler({
onMomentumBegin(e) {
handlerFunc('momentum', e.eventName);
},
onMomentumEnd(e) {
handlerFunc('momentum', e.eventName);
},
});

const composedHandler = useComposedEventHandler([
onScrollHandler,
onDragHandler,
onMomentumHandler,
]);

return (
<View style={styles.container}>
<Text style={styles.infoText}>Check console logs!</Text>
<Animated.FlatList
onScroll={composedHandler}
style={styles.list}
data={items}
renderItem={({ item }) => <Item title={item.title} />}
keyExtractor={(item) => `A:${item.title}`}
/>
</View>
);
}

type ItemValue = { title: string };
const items: ItemValue[] = [...new Array(101)].map((_each, index) => {
return { title: `${index}` };
});

type ItemProps = { title: string };
const Item = ({ title }: ItemProps) => (
<View style={styles.item}>
<Text style={styles.title}>{title}</Text>
</View>
);

const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'column',
},
infoText: {
fontSize: 19,
alignSelf: 'center',
},
list: {
flex: 1,
},
item: {
backgroundColor: '#66e3c0',
alignItems: 'center',
padding: 20,
marginVertical: 8,
marginHorizontal: 16,
borderRadius: 20,
},
title: {
fontSize: 32,
},
});
Loading