import { StyleSheet, Text, TouchableOpacity, View } from 'react-native';
import Animated, { useAnimatedStyle, useSharedValue, withSpring } from 'react-native-reanimated';
import { useEffect } from 'react';
import type { FCCWD, SpeedDialProps } from '../../types';
import MaterialCommunityIcon from '../Icons/MaterialCommunity';
import { applyDefaults } from '../../core/KitraProvider';
import Icon from '../Icons/Icon';
const styles = StyleSheet.create({
childrenContainer: {
width: 50,
height: 50,
borderRadius: 50,
position: 'absolute',
flexDirection: 'row',
},
childrenText: {
right: 60,
top: 10,
position: 'absolute',
fontSize: 12,
fontWeight: '500',
paddingVertical: 5,
paddingHorizontal: 12,
textAlign: 'center',
},
childrenButton: {
width: '100%',
height: '100%',
borderRadius: 50,
justifyContent: 'center',
alignItems: 'center',
},
container: {
width: 40,
position: 'absolute',
bottom: '5%',
},
innerContainer: {
justifyContent: 'center',
alignItems: 'center',
},
nodeContainer: {
width: 60,
height: 60,
borderRadius: 50,
},
nodeButton: {
width: '100%',
height: '100%',
borderRadius: 50,
justifyContent: 'center',
alignItems: 'center',
},
});
const ChildItem = ({ item, index, offset, onPress, variant, typography }:{item:any, typography:any, index:number, offset:any, onPress:any, variant:'flat'|'spread'}) => {
const childRotate = useAnimatedStyle(() => ({
transform: [{ translateY: -offset.value * ((index + 1) * 65) }],
opacity: offset.value,
}));
const childRotete1 = useAnimatedStyle(() => ({
transform: [{ translateX: -offset.value * 80 }],
opacity: offset.value,
}));
const childRotete2 = useAnimatedStyle(() => ({
transform: [{ translateX: -offset.value * 60 }, { translateY: -offset.value * 60 }],
opacity: offset.value,
}));
const childRotete3 = useAnimatedStyle(() => ({
transform: [{ translateY: -offset.value * 80 }],
opacity: offset.value,
}));
return (
{variant !== 'spread' && item.title ? (
{item.title}
)
: null
}
{item.icon}
);
};
const SpeedDial: FCCWD = (
{ theme,
typography,
baseItemBackground = theme?.tertiary,
items,
onChange,
variant = 'flat',
baseItemIcon = },
) => {
const offset = useSharedValue(0);
const rotate = useAnimatedStyle(() => ({
transform: [{ rotateZ: `${(offset.value - 1) * 45}deg` }],
}));
const animationStartConfig = {
damping: 10,
mass: 1,
};
const animationEndConfig = {
mass: 1,
stiffness: 100,
};
useEffect(() => {
if (variant === 'spread') { items = items.slice(0, 3); }
}, [variant]);
const onPress = () => {
if (offset.value > 0) {
offset.value = withSpring(0, animationStartConfig);
onChange && onChange(false);
} else {
offset.value = withSpring(1, animationEndConfig);
onChange && onChange(true);
}
};
return (
<>
{items.map((item, index) => (
))}
>
)
? rotate
: null, styles.nodeContainer, { backgroundColor: baseItemBackground }]}
>
{baseItemIcon}
);
};
export default applyDefaults(SpeedDial);