diff --git a/src/libs/calculateAnchorPosition.js b/src/libs/calculateAnchorPosition.js deleted file mode 100644 index c886c9ac3712..000000000000 --- a/src/libs/calculateAnchorPosition.js +++ /dev/null @@ -1,26 +0,0 @@ -import lodashGet from 'lodash/get'; -import CONST from '../CONST'; - -/** - * Gets the x,y position of the passed in component for the purpose of anchoring another component to it. - * - * @param {Element} anchorComponent - * @param {{horizontal: string, vertical: string}} anchorOriginValue - Optional parameter - * @return {Promise} - */ -export default function calculateAnchorPosition(anchorComponent, anchorOriginValue) { - return new Promise((resolve) => { - if (!anchorComponent) { - return resolve({horizontal: 0, vertical: 0}); - } - anchorComponent.measureInWindow((x, y, width, height) => { - if ( - lodashGet(anchorOriginValue, 'vertical') === CONST.MODAL.ANCHOR_ORIGIN_VERTICAL.TOP && - lodashGet(anchorOriginValue, 'horizontal') === CONST.MODAL.ANCHOR_ORIGIN_HORIZONTAL.LEFT - ) { - return resolve({horizontal: x, vertical: y + height + lodashGet(anchorOriginValue, 'shiftVertical', 0)}); - } - return resolve({horizontal: x + width, vertical: y}); - }); - }); -} diff --git a/src/libs/calculateAnchorPosition.ts b/src/libs/calculateAnchorPosition.ts new file mode 100644 index 000000000000..39fb3032ee09 --- /dev/null +++ b/src/libs/calculateAnchorPosition.ts @@ -0,0 +1,32 @@ +/* eslint-disable no-console */ +import {ValueOf} from 'type-fest'; +import {View} from 'react-native'; +import CONST from '../CONST'; + +type AnchorOrigin = { + horizontal: ValueOf; + vertical: ValueOf; + shiftVertical?: number; +}; + +type AnchorPosition = { + horizontal: number; + vertical: number; +}; + +/** + * Gets the x,y position of the passed in component for the purpose of anchoring another component to it. + */ +export default function calculateAnchorPosition(anchorComponent: View, anchorOrigin?: AnchorOrigin): Promise { + return new Promise((resolve) => { + if (!anchorComponent) { + return resolve({horizontal: 0, vertical: 0}); + } + anchorComponent.measureInWindow((x, y, width, height) => { + if (anchorOrigin?.vertical === CONST.MODAL.ANCHOR_ORIGIN_VERTICAL.TOP && anchorOrigin?.horizontal === CONST.MODAL.ANCHOR_ORIGIN_HORIZONTAL.LEFT) { + return resolve({horizontal: x, vertical: y + height + (anchorOrigin?.shiftVertical ?? 0)}); + } + return resolve({horizontal: x + width, vertical: y}); + }); + }); +}