-
Notifications
You must be signed in to change notification settings - Fork 843
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update calculatePopoverPosition to seek the position with the largest amount of visible surface area of the popover. #550
Merged
cjcenizal
merged 6 commits into
elastic:master
from
cjcenizal:calculate-popover-position
Mar 21, 2018
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
263dd95
Update calculatePopoverPosition to seek the position with the largest…
cjcenizal c376e4b
Return position data from calculatePopoverPosition.
cjcenizal 36a890c
Put comment back where it belongs. Fix EuiIconTip example.
cjcenizal 2de8f3e
Rename popver_calculate_position to calculate_popover_position.
cjcenizal 18f5657
Update CHANGELOG.
cjcenizal b72356f
Clarify calculatePopoverPosition comments and use array reduce() method.
cjcenizal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,5 +64,4 @@ export { | |
|
||
export { | ||
calculatePopoverPosition, | ||
calculatePopoverStyles, | ||
} from './popover'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
export { calculatePopoverPosition } from './popover_calculate_position'; | ||
export { calculatePopoverStyles } from './popover_calculate_styles'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,83 @@ | ||
|
||
/** | ||
* Determine the best position for a popup that avoids clipping by the window view port. | ||
* Determine the best position for a popover that avoids clipping by the window view port. | ||
* | ||
* @param {native DOM Element} wrapperRect - getBoundingClientRect() of wrapping node around the popover. | ||
* @param {native DOM Element} popupRect - getBoundingClientRect() of the popup node. | ||
* @param {native DOM Element} anchorBounds - getBoundingClientRect() of the node the popover is tethered to (e.g. a button). | ||
* @param {native DOM Element} popoverBounds - getBoundingClientRect() of the popover node (e.g. the tooltip). | ||
* @param {string} requestedPosition - Position the user wants. One of ["top", "right", "bottom", "left"] | ||
* @param {number} buffer - The space between the wrapper and the popup. Also the minimum space between the popup and the window. | ||
* @param {number} buffer - The space between the wrapper and the popover. Also the minimum space between the popover and the window. | ||
* | ||
* @returns {string} One of ["top", "right", "bottom", "left"] that ensures no window overflow. | ||
* @returns {string} One of ["top", "right", "bottom", "left"] that ensures the least amount of window overflow. | ||
*/ | ||
export function calculatePopoverPosition(wrapperRect, popupRect, requestedPosition, buffer = 16) { | ||
|
||
// determine popup overflow in each direction | ||
// negative values signal window overflow, large values signal lots of free space | ||
const popupOverflow = { | ||
top: wrapperRect.top - (popupRect.height + (2 * buffer)), | ||
right: window.innerWidth - wrapperRect.right - (popupRect.width + (2 * buffer)), | ||
left: wrapperRect.left - (popupRect.width + (2 * buffer)), | ||
bottom: window.innerHeight - wrapperRect.bottom - (popupRect.height + (2 * buffer)), | ||
|
||
const getVisibleArea = (bounds, windowWidth, windowHeight) => { | ||
const { left, top, width, height } = bounds; | ||
// This is a common algorithm for finding the intersected area among two rectangles. | ||
const dx = Math.min(left + width, windowWidth) - Math.max(left, 0); | ||
const dy = Math.min(top + height, windowHeight) - Math.max(top, 0); | ||
return dx * dy; | ||
}; | ||
|
||
const positionAtTop = (anchorBounds, width, height, buffer) => { | ||
const widthDifference = width - anchorBounds.width; | ||
const left = anchorBounds.left - widthDifference * 0.5; | ||
const top = anchorBounds.top - height - buffer; | ||
return { left, top, width, height }; | ||
}; | ||
|
||
const positionAtRight = (anchorBounds, width, height, buffer) => { | ||
const left = anchorBounds.right + buffer; | ||
const heightDifference = height - anchorBounds.height; | ||
const top = anchorBounds.top - heightDifference * 0.5; | ||
return { left, top, width, height }; | ||
}; | ||
|
||
const positionAtBottom = (anchorBounds, width, height, buffer) => { | ||
const widthDifference = width - anchorBounds.width; | ||
const left = anchorBounds.left - widthDifference * 0.5; | ||
const top = anchorBounds.bottom + buffer; | ||
return { left, top, width, height }; | ||
}; | ||
|
||
const positionAtLeft = (anchorBounds, width, height, buffer) => { | ||
const left = anchorBounds.left - width - buffer; | ||
const heightDifference = height - anchorBounds.height; | ||
const top = anchorBounds.top - heightDifference * 0.5; | ||
return { left, top, width, height }; | ||
}; | ||
|
||
export function calculatePopoverPosition(anchorBounds, popoverBounds, requestedPosition, buffer = 16) { | ||
const windowWidth = window.innerWidth; | ||
const windowHeight = window.innerHeight; | ||
const { width: popoverWidth, height: popoverHeight } = popoverBounds; | ||
|
||
const positionToBoundsMap = { | ||
top: positionAtTop(anchorBounds, popoverWidth, popoverHeight, buffer), | ||
right: positionAtRight(anchorBounds, popoverWidth, popoverHeight, buffer), | ||
bottom: positionAtBottom(anchorBounds, popoverWidth, popoverHeight, buffer), | ||
left: positionAtLeft(anchorBounds, popoverWidth, popoverHeight, buffer), | ||
}; | ||
|
||
function hasCrossDimensionOverflow(key) { | ||
if (key === 'left' || key === 'right') { | ||
const domNodeCenterY = wrapperRect.top + (wrapperRect.height / 2); | ||
const tooltipTop = domNodeCenterY - ((popupRect.height / 2) + buffer); | ||
if (tooltipTop <= 0) { | ||
return true; | ||
} | ||
const tooltipBottom = domNodeCenterY + (popupRect.height / 2) + buffer; | ||
if (tooltipBottom >= window.innerHeight) { | ||
return true; | ||
} | ||
} else { | ||
const domNodeCenterX = wrapperRect.left + (wrapperRect.width / 2); | ||
const tooltipLeft = domNodeCenterX - ((popupRect.width / 2) + buffer); | ||
if (tooltipLeft <= 0) { | ||
return true; | ||
} | ||
const tooltipRight = domNodeCenterX + (popupRect.width / 2) + buffer; | ||
if (tooltipRight >= window.innerWidth) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
const positions = Object.keys(positionToBoundsMap); | ||
|
||
// Calculate how much area of the popover is visible at each position. | ||
const positionToVisibleAreaMap = {}; | ||
|
||
positions.forEach((position) => { | ||
positionToVisibleAreaMap[position] = getVisibleArea(positionToBoundsMap[position], windowWidth, windowHeight); | ||
}); | ||
|
||
// Default to use the requested position. | ||
let calculatedPopoverPosition = requestedPosition; | ||
if (popupOverflow[requestedPosition] <= 0 || hasCrossDimensionOverflow(requestedPosition)) { | ||
// requested position overflows window bounds | ||
// select direction what has the most free space | ||
Object.keys(popupOverflow).forEach((key) => { | ||
if (popupOverflow[key] > popupOverflow[calculatedPopoverPosition] && !hasCrossDimensionOverflow(key)) { | ||
calculatedPopoverPosition = key; | ||
} | ||
}); | ||
} | ||
|
||
return calculatedPopoverPosition; | ||
|
||
// If the requested position clips the popover, find the position which clips the popover the least. | ||
positions.forEach((position) => { | ||
if (positionToVisibleAreaMap[position] > positionToVisibleAreaMap[calculatedPopoverPosition]) { | ||
calculatedPopoverPosition = position; | ||
} | ||
}); | ||
|
||
return { | ||
position: calculatedPopoverPosition, | ||
...positionToBoundsMap[calculatedPopoverPosition], | ||
}; | ||
} |
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like this comment get detached from the function it describes.