Replies: 3 comments 1 reply
-
|
Hey, you may need to whitelist react-native-reanimated/src/ConfigHelper.ts Lines 115 to 134 in 1a292f7 |
Beta Was this translation helpful? Give feedback.
1 reply
-
|
Did you ever find a solution? |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
Here's my solution to this: import { cn } from '@/lib/utils';
import { LinearGradient, LinearGradientProps } from 'expo-linear-gradient';
import React, { useEffect, useRef, useState } from 'react';
import { ColorValue, StyleProp, View, ViewStyle } from 'react-native';
import {
useSharedValue,
withTiming,
interpolateColor,
useDerivedValue,
useAnimatedReaction,
runOnJS,
} from 'react-native-reanimated';
const GradientView = ({
children,
style,
className,
colors,
}: {
children: React.ReactNode;
style?: StyleProp<LinearGradientProps>;
className?: string;
colors?: readonly [ColorValue, ColorValue, ...ColorValue[]];
}) => {
const defaultColors: [ColorValue, ColorValue] = [
'hsla(204, 8%, 24%, 1)',
'hsla(204, 6%, 43%, 1)',
];
const currentColors = colors ?? defaultColors;
const colorProgress = useSharedValue(1);
const fromColorsRef = useRef<readonly ColorValue[]>(currentColors);
const previousColorsRef = useRef<readonly ColorValue[]>(currentColors);
useEffect(() => {
if (JSON.stringify(previousColorsRef.current) !== JSON.stringify(currentColors)) {
fromColorsRef.current = previousColorsRef.current;
previousColorsRef.current = currentColors;
colorProgress.value = 0;
colorProgress.value = withTiming(1, { duration: 500 });
}
}, [currentColors, colorProgress]);
const [displayColors, setDisplayColors] =
useState<readonly [ColorValue, ColorValue, ...ColorValue[]]>(currentColors);
const animatedColors = useDerivedValue(() => {
const fromColors = fromColorsRef.current.length > 0 ? fromColorsRef.current : defaultColors;
const toColors = currentColors;
const interpolatedColors = toColors.map((toColor, index) => {
const fromColor = fromColors[index] ?? fromColors[0] ?? defaultColors[0];
return interpolateColor(
colorProgress.value,
[0, 1],
[fromColor as string, toColor as string]
);
});
return [
interpolatedColors[0] ?? defaultColors[0],
interpolatedColors[1] ?? defaultColors[1],
...interpolatedColors.slice(2),
] as [ColorValue, ColorValue, ...ColorValue[]];
});
useAnimatedReaction(
() => animatedColors.value,
(colors) => {
const mutableColors: [ColorValue, ColorValue, ...ColorValue[]] = [
colors[0],
colors[1],
...colors.slice(2),
];
runOnJS(setDisplayColors)(mutableColors);
}
);
return (
<View className={cn('border border-border', className)}>
<LinearGradient colors={displayColors} style={style as StyleProp<ViewStyle>}>
{children}
</LinearGradient>
</View>
);
};
export default GradientView; |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I want to animate the
colorsofexpo-linear-gradientbut it doesn't work. In fact: nothing changes.Minimal reproducible example (Snack):
What am I doing wrong here? Any help would be highly appreciated.
Beta Was this translation helpful? Give feedback.
All reactions