import { useCallback, useEffect, useRef, useState } from 'react'; import { Dimensions, StyleSheet, Text, View } from 'react-native'; import Animated, { FadeIn, FadeOut } from 'react-native-reanimated'; import { TouchableOpacity } from 'react-native-gesture-handler'; import type { FCCWD, MenuProps } from '../../types'; import { applyDefaults } from '../../core/KitraProvider'; import Divider from '../Divider/Divider'; const HEIGHT = Dimensions.get('window').height; const styles = StyleSheet.create({ container: { zIndex: 1, }, openButton: { alignItems: 'flex-end', marginRight: 10, }, menuContainer: { borderRadius: 5, position: 'absolute', }, menuButton: { margin: 10, flexDirection: 'row', }, buttonContainer: { flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center', flexGrow: 1, }, }); const Menu : FCCWD = ( { typography, theme, items, dividerColor, menuStyle, labelStyle, containerStyle, rowStyle, closeOnPress, button = () => ... }, ) => { const [open, setOpen] = useState(false); const [size, setSize] = useState({ x: 0, y: 0, width: 0, height: 0 }); const [menuHeight, setMenuHeight] = useState({ height: 0 }); const menu = useRef(null); useEffect(() => { menu.current?.measure((x, y, width, height, pageX, pageY) => { setSize({ x: pageX, y: pageY, width, height }); }); }, [open]); const closeMenu = useCallback(() => { if (closeOnPress) { setOpen(false); } }, [closeOnPress]); return ( setOpen(!open)} style={styles.openButton}> {button(open)} {open ? ( = 0 ? { top: size.height + 10 } : { bottom: 30 }, menuStyle, ]} entering={FadeIn.duration(300)} exiting={FadeOut.duration(300)} onLayout={e => setMenuHeight({ height: e.nativeEvent.layout.height })} > {items.map((item, index) => ( { item.onPress && item.onPress(); closeMenu(); }} style={styles.menuButton} > {item.left && item.left} {item.label} {item.right && item.right} {items.length - 1 !== index ? : null } ))} ) : null } ); }; export default applyDefaults(Menu);