0

I am looking to find the top/left position of each Text component. This will sound a bit odd but I tried this and it was giving me left & top keys for each Text component. I was testing it on a Snack.

A few hours later, I try this again & now the top & left are missing from this. Checked the documentation as well. They don't mention top & left for onLayout callback.

Actually I need to show a tooltip & need to position it. Any pointers, please

Edit Not looking for a Modal based implementation for my usecase

<View style={{ flexDirection: 'row', flexWrap: 'wrap'}}>
 {textArr.map((text, idx) => {
   return (
    <Text
      onLayout={event => {
        // this was giving me x,y,width,height,top,left values
        console.log(event.nativeLayout.layout);
      }}
    >
      {text}
    </Text>
   );
 })}
</View>

1 Answer 1

0

Super simple and simplified Tooltip implementation with TouchableOpacity and Modal. Uses pressed location for placement.

const Tooltip = ({children, text}) => {
  const [show, setShow] = useState(false);
  const [location, setLocation] = useState([0, 0])

  return (
    <TouchableOpacity
      onPress={(e) => {
        setLocation([e.nativeEvent.pageX, e.nativeEvent.pageY])
        setShow(!show)
      }}>
      <Modal visible={show} transparent={true} onRequestClose={() => {setShow(false)}}>
        <View style={{backgroundColor: 'lightgray', width: 160, padding: 8, justifyContent: 'center', alignItems: 'center', left: location[0], top: location[1]}}>
          <Text>{text}</Text>
        </View>
      </Modal>
      {children}
    </TouchableOpacity>
  );
}

...

<Tooltip text="Tooltip text">
  <Text>
    Show tooltip for this text
  </Text> 
</Tooltip>
Sign up to request clarification or add additional context in comments.

1 Comment

For my usecase, I am not looking for a Modal based solution. thanks for your time, btw

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.