import Animated, {
type SharedValue,
useAnimatedStyle,
useDerivedValue,
useSharedValue,
withSpring,
} from 'react-native-reanimated';
import {
type LayoutEachProps,
type LayoutProps,
SWIPE_POSITIONS_INDEX,
} from './types';
import React, { useEffect } from 'react';
import { StyleSheet } from 'react-native';
const Layout = (props: LayoutEachProps) => {
const { children, distance, position, segment } = props;
const derivedOpacity = useDerivedValue(() => {
return segment.value === position ? distance.value : 0;
}, [segment.value]);
const layoutStyle = useAnimatedStyle(() => {
return {
opacity: derivedOpacity.value,
};
});
return (
{children}
);
};
const Layouts = (props: LayoutProps) => {
const keys = Object.keys(props.layouts ?? {});
const opacity = keys.reduce(
(acc, key) => {
// eslint-disable-next-line react-hooks/rules-of-hooks
acc[key] = useSharedValue(0);
return acc;
},
{} as Record>
);
useEffect(() => {
keys.forEach((key) => {
const position = SWIPE_POSITIONS_INDEX[key as keyof typeof props.layouts];
const opacitySelf = opacity[key];
opacitySelf!.value = withSpring(
props.segment.value === position ? props.distance.value : 0
);
});
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
return (
{keys.map((key) => {
const opacitySelf = opacity[key];
const position =
SWIPE_POSITIONS_INDEX[key as keyof typeof props.layouts];
return (
{props.layouts[key as keyof typeof props.layouts]}
);
})}
);
};
export default Layouts;