@nguyentc21/react-native-tooltip
Version:
Simple tooltip for React native app
53 lines • 1.5 kB
JavaScript
import { StyleSheet } from 'react-native';
import calculateCaretWidth from './calculateCaretWidth';
export default args => {
const {
placement,
caretSize,
targetContentLayout,
tooltipContentLayout
} = args;
if (!tooltipContentLayout) return;
const {
pageX,
pageY
} = targetContentLayout;
const halfTargetHeight = targetContentLayout.height * 0.5;
const halfTargetWidth = targetContentLayout.width * 0.5;
const _pageX = pageX - tooltipContentLayout.x;
const _pageY = pageY - tooltipContentLayout.y;
const caretWidth = calculateCaretWidth(caretSize);
const caretHeight = caretWidth * 0.5;
const diffCaretSizeAfterTransform = (caretWidth - caretSize) * 0.5;
const caretOffset = -caretHeight + diffCaretSizeAfterTransform - StyleSheet.hairlineWidth;
const offsetX = halfTargetWidth - caretHeight + diffCaretSizeAfterTransform;
const offsetY = halfTargetHeight - caretHeight + diffCaretSizeAfterTransform;
let top, left, bottom, right;
switch (placement) {
case 'top':
left = _pageX + offsetX;
top = caretOffset;
break;
case 'bottom':
left = _pageX + offsetX;
bottom = caretOffset;
break;
case 'left':
top = _pageY + offsetY;
left = caretOffset;
break;
case 'right':
top = _pageY + offsetY;
right = caretOffset;
break;
default:
break;
}
return {
top,
left,
bottom,
right
};
};
//# sourceMappingURL=generateCaretPosition.js.map