Skip to content

Commit 313d9f2

Browse files
authored
fix: poll option animation breaking (#3115)
* fix: poll option animation breaking * fix: add container animation * chore: remove console.log * fix: revert not needed prop drilling
1 parent 79e27bd commit 313d9f2

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

package/src/components/Poll/CreatePollContent.tsx

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,22 +66,18 @@ export const CreatePollContent = () => {
6666

6767
useEffect(() => {
6868
if (!createPollOptionHeight) return;
69+
const newCurrentOptionPositions: CurrentOptionPositionsCache = {
70+
inverseIndexCache: {},
71+
positionCache: {},
72+
};
6973
options.forEach((option, index) => {
70-
currentOptionPositions.value = {
71-
...currentOptionPositions.value,
72-
inverseIndexCache: {
73-
...currentOptionPositions.value.inverseIndexCache,
74-
[index]: option.id,
75-
},
76-
positionCache: {
77-
...currentOptionPositions.value.positionCache,
78-
[option.id]: {
79-
updatedIndex: index,
80-
updatedTop: index * createPollOptionHeight,
81-
},
82-
},
74+
newCurrentOptionPositions.inverseIndexCache[index] = option.id;
75+
newCurrentOptionPositions.positionCache[option.id] = {
76+
updatedIndex: index,
77+
updatedTop: index * createPollOptionHeight,
8378
};
8479
});
80+
currentOptionPositions.value = newCurrentOptionPositions;
8581
}, [createPollOptionHeight, currentOptionPositions, options]);
8682

8783
const onBackPressHandler = useCallback(() => {

package/src/components/Poll/components/CreatePollOptions.tsx

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useCallback, useMemo } from 'react';
1+
import React, { useCallback, useEffect, useMemo } from 'react';
22
import { StyleSheet, Text, TextInput, View } from 'react-native';
33
import { Gesture, GestureDetector } from 'react-native-gesture-handler';
44
import Animated, {
@@ -11,6 +11,7 @@ import Animated, {
1111
useSharedValue,
1212
withDelay,
1313
withSpring,
14+
withTiming,
1415
} from 'react-native-reanimated';
1516

1617
import { PollComposerOption, PollComposerState } from 'stream-chat';
@@ -295,6 +296,19 @@ export const CreatePollOptions = ({ currentOptionPositions }: CreatePollOptionsP
295296
// this will hold id for item which user started dragging
296297
const draggedItemId = useSharedValue<string | null>(null);
297298

299+
// holds the animated height of the option container
300+
const animatedOptionsContainerHeight = useSharedValue(createPollOptionHeight * options.length);
301+
302+
useEffect(() => {
303+
animatedOptionsContainerHeight.value = withTiming(createPollOptionHeight * options.length, {
304+
duration: 200,
305+
});
306+
}, [animatedOptionsContainerHeight, createPollOptionHeight, options.length]);
307+
308+
const animatedOptionsContainerStyle = useAnimatedStyle(() => ({
309+
height: animatedOptionsContainerHeight.value,
310+
}));
311+
298312
const boundaries = useMemo(
299313
() => ({ maxBound: (options.length - 1) * createPollOptionHeight, minBound: 0 }),
300314
[createPollOptionHeight, options.length],
@@ -332,7 +346,7 @@ export const CreatePollOptions = ({ currentOptionPositions }: CreatePollOptionsP
332346
return (
333347
<View style={[styles.container, container]}>
334348
<Text style={[styles.text, { color: black }, title]}>{t<string>('Options')}</Text>
335-
<View style={{ height: createPollOptionHeight * options.length }}>
349+
<Animated.View style={animatedOptionsContainerStyle}>
336350
{options.map((option, index) => (
337351
<MemoizedCreatePollOption
338352
boundaries={boundaries}
@@ -348,7 +362,7 @@ export const CreatePollOptions = ({ currentOptionPositions }: CreatePollOptionsP
348362
option={option}
349363
/>
350364
))}
351-
</View>
365+
</Animated.View>
352366
</View>
353367
);
354368
};

0 commit comments

Comments
 (0)