UNPKG

react-dates

Version:

A responsive and accessible date range picker component built with React

44 lines (39 loc) 1.74 kB
Object.defineProperty(exports, "__esModule", { value: true }); exports['default'] = getDetachedContainerStyles; var _constants = require('../constants'); /** * Calculate and return a CSS transform style to position a detached element * next to a reference element. The open and anchor direction indicate wether * it should be positioned above/below and/or to the left/right of the * reference element. * * Assuming r(0,0), r(1,1), d(0,0), d(1,1) for the bottom-left and top-right * corners of the reference and detached elements, respectively: * - openDirection = DOWN, anchorDirection = LEFT => d(0,1) == r(0,1) * - openDirection = UP, anchorDirection = LEFT => d(0,0) == r(0,0) * - openDirection = DOWN, anchorDirection = RIGHT => d(1,1) == r(1,1) * - openDirection = UP, anchorDirection = RIGHT => d(1,0) == r(1,0) * * By using a CSS transform, we allow to further position it using * top/bottom CSS properties for the anchor gutter. * * @param {string} openDirection The vertical positioning of the popup * @param {string} anchorDirection The horizontal position of the popup * @param {HTMLElement} referenceEl The reference element */ function getDetachedContainerStyles(openDirection, anchorDirection, referenceEl) { var referenceRect = referenceEl.getBoundingClientRect(); var offsetX = referenceRect.left; var offsetY = referenceRect.top; if (openDirection === _constants.OPEN_UP) { offsetY = -(window.innerHeight - referenceRect.bottom); } if (anchorDirection === _constants.ANCHOR_RIGHT) { offsetX = -(window.innerWidth - referenceRect.right); } return { transform: 'translate3d(' + String(Math.round(offsetX)) + 'px, ' + String(Math.round(offsetY)) + 'px, 0)' }; }