Skip to content

Commit

Permalink
Merge pull request #26853 from software-mansion-labs/ts-migration/cal…
Browse files Browse the repository at this point in the history
…culateAnchorPosition

[TS migration] Migrate 'calculateAnchorPosition.js' lib to TypeScript
  • Loading branch information
bondydaa authored Sep 15, 2023
2 parents 5fb9a0f + 26dc124 commit 048850b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 26 deletions.
26 changes: 0 additions & 26 deletions src/libs/calculateAnchorPosition.js

This file was deleted.

32 changes: 32 additions & 0 deletions src/libs/calculateAnchorPosition.ts
Original file line number Diff line number Diff line change
@@ -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<typeof CONST.MODAL.ANCHOR_ORIGIN_HORIZONTAL>;
vertical: ValueOf<typeof CONST.MODAL.ANCHOR_ORIGIN_VERTICAL>;
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<AnchorPosition> {
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});
});
});
}

0 comments on commit 048850b

Please sign in to comment.