UNPKG

1.21 kBPlain TextView Raw
1'use strict';
2import type { Component } from 'react';
3import { measure } from './measure';
4import type { AnimatedRef } from '../hook/commonTypes';
5
6/**
7 * An object which contains relative coordinates.
8 */
9export interface ComponentCoords {
10 x: number;
11 y: number;
12}
13
14/**
15 * Lets you determines the location on the screen, relative to the given view.
16 *
17 * @param animatedRef - An [animated ref](https://docs.swmansion.com/react-native-reanimated/docs/core/useAnimatedRef#returns) connected to the component you'd want to get the coordinates from.
18 * @param absoluteX - A number which is an absolute x coordinate.
19 * @param absoluteY - A number which is an absolute y coordinate.
20 * @returns An object which contains relative coordinates - {@link ComponentCoords}.
21 * @see https://docs.swmansion.com/react-native-reanimated/docs/utilities/getRelativeCoords
22 */
23export function getRelativeCoords(
24 animatedRef: AnimatedRef<Component>,
25 absoluteX: number,
26 absoluteY: number
27): ComponentCoords | null {
28 'worklet';
29 const parentCoords = measure(animatedRef);
30 if (parentCoords === null) {
31 return null;
32 }
33 return {
34 x: absoluteX - parentCoords.pageX,
35 y: absoluteY - parentCoords.pageY,
36 };
37}