@atlaskit/tooltip
Version:
A tooltip briefly describes an interactive element on mouse hover or keyboard focus.
69 lines (66 loc) • 2.02 kB
JavaScript
/**
* Returns the trigger's X edge for inline-axis placements, or the
* trigger's horizontal center for block-axis placements.
*
* Note: does not account for RTL writing modes.
*/
function getTriggerEdgeX(_ref) {
var triggerRect = _ref.triggerRect,
placement = _ref.placement;
if (placement.axis === 'inline') {
return placement.edge === 'start' ? triggerRect.left : triggerRect.right;
}
return triggerRect.left + triggerRect.width / 2;
}
/**
* Returns the trigger's Y edge for block-axis placements, or the
* trigger's vertical center for inline-axis placements.
*/
function getTriggerEdgeY(_ref2) {
var triggerRect = _ref2.triggerRect,
placement = _ref2.placement;
if (placement.axis === 'block') {
return placement.edge === 'start' ? triggerRect.top : triggerRect.bottom;
}
return triggerRect.top + triggerRect.height / 2;
}
/**
* Computes the viewport point at which to anchor a cursor-positioned
* tooltip, given the cursor's coordinates and the trigger's bounding rect.
*
* - `mouse`: both axes follow the cursor.
* - `mouse-x`: X follows the cursor; Y locks to the trigger edge selected
* by `placement` (falls back to trigger center if the placement axis
* doesn't have a Y edge).
* - `mouse-y`: Y follows the cursor; X locks to the trigger edge (falls
* back to trigger center similarly).
*/
export function getAnchorPoint(_ref3) {
var cursor = _ref3.cursor,
triggerRect = _ref3.triggerRect,
tooltipPosition = _ref3.tooltipPosition,
placement = _ref3.placement;
if (tooltipPosition === 'mouse-y') {
return {
x: getTriggerEdgeX({
triggerRect: triggerRect,
placement: placement
}),
y: cursor.clientY
};
}
if (tooltipPosition === 'mouse-x') {
return {
x: cursor.clientX,
y: getTriggerEdgeY({
triggerRect: triggerRect,
placement: placement
})
};
}
// tooltipPosition === 'mouse'
return {
x: cursor.clientX,
y: cursor.clientY
};
}