Skip to content

Commit 3b7c5d6

Browse files
committed
update toggle function with useAnimatedValue hook
1 parent a6670a6 commit 3b7c5d6

File tree

4 files changed

+40
-29
lines changed

4 files changed

+40
-29
lines changed

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ tsconfig.json
2222
.yarnrc.yml
2323

2424
# Yarn
25+
.yarn/
2526
.yarn/install-state.gz
2627
.yarn/patches
2728
.yarn/plugins

README.md

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,30 +35,38 @@
3535

3636
```tsx
3737
import React from 'react';
38-
import { Animated } from 'react-native';
38+
import { Button } from 'react-native';
39+
40+
// Step 1: Import ThumbnailSelector.
3941
import ThumbnailSelector from 'react-native-thumbnail-selector';
4042

41-
const thumbnails = [
43+
// Step 2: Define one or more thumbnails like below.
44+
const Thumbnails = [
4245
{
43-
caption: 'react-native',
46+
caption: 'Caption 1',
4447
imageSrc: { uri: 'https://reactnative.dev/img/pwa/manifest-icon-512.png' },
4548
},
4649
{
47-
caption: 'Dolore do magna ullamco nisi quis.',
48-
imageSrc: { uri: 'https://reactnative.dev/img/pwa/manifest-icon-512.png' },
50+
caption: 'Caption 2',
51+
imageSrc: { uri: 'https://prettier.io/icon.png' },
4952
},
5053
];
5154

5255
export default function App(): React.JSX.Element {
53-
// use toggle to show and hide ThumbnailSelector
54-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
55-
let toggle = () => new Promise<Animated.EndResult>(res => res);
56+
// Step 3: Define the ref variable to hold toggle action.
57+
const toggleRef = React.useRef(() => {});
5658

5759
return (
58-
<ThumbnailSelector
59-
thumbnails={thumbnails}
60-
toggle={func => (toggle = func)}
61-
/>
60+
<>
61+
{/* Step 4: Depending on your use case, use toggleRef to open and close the ThumbnailSelector. */}
62+
<Button title={'Toggle'} onPress={() => toggleRef.current()} />
63+
{/* Step 5: Add ThumbnailSelector at last position in document tree so it overlaps other components. */}
64+
{/* Step 6: Define the thumbnails and toggle props like below. */}
65+
<ThumbnailSelector
66+
thumbnails={Thumbnails}
67+
toggle={func => (toggleRef.current = func)}
68+
/>
69+
</>
6270
);
6371
}
6472
```

ThumbnailSelector.tsx

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
TouchableOpacityProps,
1313
ViewStyle,
1414
LayoutChangeEvent,
15+
useAnimatedValue,
1516
} from 'react-native';
1617

1718
export type ThumbnailItem = {
@@ -26,7 +27,7 @@ export type ThumbnailItemIndex = {
2627

2728
export type ThumbnailSelectorProps = {
2829
thumbnails: ThumbnailItem[];
29-
toggle?: (func: () => Promise<Animated.EndResult>) => void;
30+
toggle?: (func: () => void) => void;
3031
renderThumbnail?: (
3132
item: ThumbnailItem,
3233
index: number,
@@ -84,25 +85,26 @@ const ThumbnailSelector: React.FunctionComponent<ThumbnailSelectorProps> = ({
8485
animatedViewTestID = 'ThumbnailSelector',
8586
}) => {
8687
const window = useWindowDimensions();
87-
const [itemIndex, setItemIndex] = React.useState(initialIndex);
88-
const [animViewHeight, setAnimViewHeight] = React.useState(0);
89-
const animatedValue = React.useRef(new Animated.Value(0));
90-
const toValue = React.useRef(animationConfig.toValue);
88+
const animatedValue = useAnimatedValue(
89+
Number(animationConfig.toValue),
90+
animationConfig,
91+
);
92+
const [itemIndex, setItemIndex] = React.useState<number>(initialIndex);
93+
const [animViewHeight, setAnimViewHeight] = React.useState<number>(0);
94+
const [hasCalledToggle, setHasCalledToggle] = React.useState<boolean>(false);
9195

92-
function _toggle(): Promise<Animated.EndResult> {
93-
if (toValue.current) {
94-
animationConfig.toValue = 0;
95-
} else {
96-
animationConfig.toValue = 1;
97-
}
98-
toValue.current = animationConfig.toValue;
99-
return new Promise<Animated.EndResult>(resolve => {
100-
Animated.spring(animatedValue.current, animationConfig).start(resolve);
101-
});
96+
function _toggle(): void {
97+
const value = Number(JSON.stringify(animatedValue));
98+
const config: Animated.SpringAnimationConfig = {
99+
...animationConfig,
100+
toValue: value == 1 ? 0 : 1,
101+
};
102+
Animated.spring(animatedValue, config).start();
102103
}
103104

104-
if (toggle) {
105+
if (toggle != undefined && !hasCalledToggle) {
105106
toggle(_toggle);
107+
setHasCalledToggle(true);
106108
}
107109

108110
function _renderItem(obj: ThumbnailItemIndex): React.JSX.Element {
@@ -158,7 +160,7 @@ const ThumbnailSelector: React.FunctionComponent<ThumbnailSelectorProps> = ({
158160
{
159161
transform: [
160162
{
161-
translateY: animatedValue.current.interpolate({
163+
translateY: animatedValue.interpolate({
162164
inputRange: [0, 1],
163165
outputRange: [start, end],
164166
}),

assets/demo.gif

-5.31 MB
Loading

0 commit comments

Comments
 (0)