import * as React from 'react';
import { Text, View, StyleSheet ,FlatList ,TouchableOpacity } from 'react-native';
import Constants from 'expo-constants';
import {useRef , createRef , useState} from 'react'
export default function App() {
const data = [1,2,3,4,5,6,7,8,9,10];
const [topH,setTop] = useState(0)
const elementsRef = useRef(data.map(() => createRef()));
const onCardPress = (item,newRef) => {
newRef?.current?.measureInWindow( (fx, fy, width, height, px, py) => {
console.log('Component width is: ' + width)
console.log('Component height is: ' + height)
console.log('X offset to frame: ' + fx)
console.log('Y offset to frame: ' + fy)
console.log('X offset to page: ' + px)
console.log('Y offset to page: ' + py)
setTop(fy)
})
}
const renderEach = ({item,index}) => {
return(
<TouchableOpacity onPress={() => onCardPress (item,elementsRef?.current[index])} style={styles.eachCard} ref={elementsRef?.current[index]} >
<Text>{item}</Text>
</TouchableOpacity>
)
}
const Popup = () => {
if(topH === 0 ){
return null
}
return(
<View style={{backgroundColor:'yellow' , position:'absolute',zIndex:3 , height:60,width:60 , right:30 , top:topH}} >
<Text> popup </Text>
</View>
)
}
return (
<View style={styles.container}>
<View style={{flex:1}} >
<Popup />
<FlatList
style={{flex:1}}
data={data}
renderItem={renderEach}
/>
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
// justifyContent: 'center',
paddingTop: Constants.statusBarHeight,
backgroundColor: '#ecf0f1',
padding: 8,
},
eachCard:{
height:100,
margin:10,
justifyContent:'center',
alignItems:'center',
backgroundColor:'#850D5F'
},
paragraph: {
margin: 24,
fontSize: 18,
fontWeight: 'bold',
textAlign: 'center',
},
});