Skip to content
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

[EuiPopover] Allow adjusting buffer for individual window sides. #4417

Merged
merged 5 commits into from
Jan 11, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## [`master`](https://github.com/elastic/eui/tree/master)

- Added support for adjusting `buffer` for individual window sides of `EuiPopover`. ([#4417](https://github.com/elastic/eui/pull/4417))
- Added `'full'` option to the `height` prop of `EuiMarkdownEditor`. Added `autoExpandPreview` and `maxHeight` props to `EuiMarkdownEditor` ([#4245](https://github.com/elastic/eui/pull/4245))

## [`31.1.0`](https://github.com/elastic/eui/tree/v31.1.0)
Expand Down
3 changes: 2 additions & 1 deletion src/components/popover/popover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,10 @@ export interface EuiPopoverProps {
offset?: number;
/**
* Minimum distance between the popover and the bounding container;
* Pass an array of 4 values to adjust each side differently: `[top, right, bottom, left]`
* Default is 16
*/
buffer?: number;
buffer?: number | number[];
ashikmeerankutty marked this conversation as resolved.
Show resolved Hide resolved
/**
* Element to pass as the child element of the arrow;
* Use case is typically limited to an accompanying `EuiBeacon`
Expand Down
39 changes: 27 additions & 12 deletions src/services/popover/popover_positioning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ interface FindPopoverPositionArgs {
align?: EuiPopoverPosition;
position: EuiPopoverPosition;
forcePosition?: boolean;
buffer?: number;
buffer?: number | number[];
offset?: number;
allowCrossAxis?: boolean;
container?: HTMLElement;
Expand All @@ -98,6 +98,14 @@ interface FindPopoverPositionResult {
anchorBoundingBox?: EuiClientRect;
}

const getBufferValues = (buffer: number | number[]): number[] => {
if (Array.isArray(buffer)) {
const [topBuffer, rightBuffer, bottomBuffer, leftBuffer] = buffer;
return [topBuffer, rightBuffer, bottomBuffer, leftBuffer];
}
return [buffer, buffer, buffer, buffer];
};

/**
* Calculates the absolute positioning (relative to document.body) to place a popover element
*
Expand Down Expand Up @@ -259,7 +267,7 @@ interface GetPopoverScreenCoordinatesArgs {
containerBoundingBox: EuiClientRect;
arrowConfig?: { arrowWidth: number; arrowBuffer: number };
offset?: number;
buffer?: number;
buffer?: number | number[];
}

interface GetPopoverScreenCoordinatesResult {
Expand Down Expand Up @@ -339,6 +347,10 @@ export function getPopoverScreenCoordinates({
const crossAxisSecondSide = positionComplements[crossAxisFirstSide]; // "left" -> "right"
const crossAxisDimension = relatedDimension[crossAxisFirstSide]; // "left" -> "width"

const [topBuffer, rightBuffer, bottomBuffer, leftBuffer] = getBufferValues(
buffer
);

const { crossAxisPosition, crossAxisArrowPosition } = getCrossAxisPosition({
crossAxisFirstSide,
crossAxisSecondSide,
Expand Down Expand Up @@ -383,10 +395,10 @@ export function getPopoverScreenCoordinates({

// shrink the visible bounding box by `buffer`
// to compute a fit value
combinedBoundingBox.top += buffer;
combinedBoundingBox.right -= buffer;
combinedBoundingBox.bottom -= buffer;
combinedBoundingBox.left += buffer;
combinedBoundingBox.top += topBuffer;
combinedBoundingBox.right -= rightBuffer;
combinedBoundingBox.bottom -= bottomBuffer;
combinedBoundingBox.left += leftBuffer;

const fit = getVisibleFit(
{
Expand Down Expand Up @@ -422,7 +434,7 @@ interface GetCrossAxisPositionArgs {
crossAxisDimension: Dimension;
position: EuiPopoverPosition;
align?: EuiPopoverPosition;
buffer: number;
buffer: number | number[];
offset: number;
windowBoundingBox: EuiClientRect;
containerBoundingBox: EuiClientRect;
Expand Down Expand Up @@ -638,30 +650,33 @@ export function getElementBoundingBox(element: HTMLElement): EuiClientRect {
export function getAvailableSpace(
anchorBoundingBox: BoundingBox,
containerBoundingBox: BoundingBox,
buffer: number,
buffer: number | number[],
offset: number,
offsetSide: EuiPopoverPosition
): BoundingBox {
const [topBuffer, rightBuffer, bottomBuffer, leftBuffer] = getBufferValues(
buffer
);
return {
top:
anchorBoundingBox.top -
containerBoundingBox.top -
buffer -
topBuffer -
(offsetSide === 'top' ? offset : 0),
right:
containerBoundingBox.right -
anchorBoundingBox.right -
buffer -
rightBuffer -
(offsetSide === 'right' ? offset : 0),
bottom:
containerBoundingBox.bottom -
anchorBoundingBox.bottom -
buffer -
bottomBuffer -
(offsetSide === 'bottom' ? offset : 0),
left:
anchorBoundingBox.left -
containerBoundingBox.left -
buffer -
leftBuffer -
(offsetSide === 'left' ? offset : 0),
};
}
Expand Down