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

[WIP] Fix calculation for center aligned popover #2362

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions UNRELEASED.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Use [the changelog guidelines](https://git.io/polaris-changelog-guidelines) to f

### Bug fixes

- Fix alignment of center aligned popovers that overlap the page ([#2362](https://github.com/Shopify/polaris-react/pull/2362))

### Documentation

### Development workflow
Expand Down
8 changes: 2 additions & 6 deletions src/components/Popover/Popover.scss
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ $content-max-width: rem(400px);

.Popover {
max-width: calc(100vw - #{2 * spacing()});
margin: $visible-portion-of-arrow spacing(tight) spacing();
margin: $visible-portion-of-arrow 0 0;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why the popover needed margin on the horizontal axis as it is positioned absolutely.

box-shadow: shadow(deep);
border-radius: border-radius();
will-change: left, top;
Expand Down Expand Up @@ -53,11 +53,7 @@ $content-max-width: rem(400px);
}

.positionedAbove {
margin: spacing() spacing(tight) $visible-portion-of-arrow;

&.fullWidth {
margin: 0 0 $visible-portion-of-arrow;
}
margin: 0 0 $visible-portion-of-arrow;
}

.Wrapper {
Expand Down
14 changes: 2 additions & 12 deletions src/components/PositionedOverlay/PositionedOverlay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ export interface PositionedOverlayProps {
interface State {
measuring: boolean;
activatorRect: Rect;
right?: number;
left: number;
top: number;
height: number;
Expand Down Expand Up @@ -122,18 +121,12 @@ export class PositionedOverlay extends React.PureComponent<
const {top, zIndex, width} = this.state;
const {render, fixed, classNames: propClassNames} = this.props;

const right =
this.state.right == null || isNaN(this.state.right)
? undefined
: this.state.right;

const left =
right != null || this.state.left == null || isNaN(this.state.left)
this.state.left == null || isNaN(this.state.left)
? undefined
: this.state.left;

const style = {
right,
left,
top: top == null || isNaN(top) ? undefined : top,
width: width == null || isNaN(width) ? undefined : width,
Expand Down Expand Up @@ -238,17 +231,14 @@ export class PositionedOverlay extends React.PureComponent<
activatorRect,
overlayRect,
containerRect,
overlayMargins,
preferredAlignment,
);

this.setState(
{
measuring: false,
activatorRect: getRectForNode(activator),
right:
preferredAlignment === 'right' ? horizontalPosition : undefined,
left: preferredAlignment === 'right' ? 0 : horizontalPosition,
left: horizontalPosition,
Copy link
Member Author

@alex-page alex-page Oct 25, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the right value and only positioning the popover with the left value. This is how it previously worked.

This simplifies the math when the popover overlaps the page. It now doesn't have to calculate a three right values it can just switch to a previously defined left value.

top: lockPosition ? top : verticalPosition.top,
lockPosition: Boolean(fixed),
height: verticalPosition.height || 0,
Expand Down
48 changes: 18 additions & 30 deletions src/components/PositionedOverlay/utilities/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,40 +87,28 @@ export function calculateHorizontalPosition(
activatorRect: Rect,
overlayRect: Rect,
containerRect: Rect,
overlayMargins: Margins,
preferredAlignment: PreferredAlignment,
) {
const maximum = containerRect.width - overlayRect.width;
const borderWidth = 2;

if (preferredAlignment === 'left') {
return Math.min(
maximum,
Math.max(0, activatorRect.left - overlayMargins.horizontal),
);
} else if (preferredAlignment === 'right') {
const activatorRight = activatorRect.left + activatorRect.width;

return (
maximum -
Math.max(
0,
activatorRight -
(overlayRect.width - overlayMargins.horizontal) -
borderWidth,
)
);
const borderWidth = 1;
const rightHorizontalPosition =
activatorRect.left + activatorRect.width - overlayRect.width - borderWidth;
const leftHorizontalPosition = activatorRect.left + borderWidth;
const centerHorizontalPosition =
activatorRect.center.x - overlayRect.width / 2;

// If right aligned or the popover goes over the right side of the page
if (
preferredAlignment === 'right' ||
centerHorizontalPosition + overlayRect.width > containerRect.width
) {
return rightHorizontalPosition;
}
// If left aligned or the popover goes over the left side of the page
else if (preferredAlignment === 'left' || centerHorizontalPosition < 0) {
return leftHorizontalPosition;
}

return Math.min(
maximum,
Math.max(
0,
activatorRect.center.x -
overlayRect.width / 2 -
overlayMargins.horizontal,
),
);
return centerHorizontalPosition;
}

export function rectIsOutsideOfRect(inner: Rect, outer: Rect) {
Expand Down
1 change: 0 additions & 1 deletion src/components/TopBar/components/UserMenu/UserMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ export function UserMenu({

return (
<Menu
preferredAlignment="right"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should work with default middle, left or right alignment.

If the popover overlaps the right hand side of the page it should use a different left value.

activatorContent={activatorContentMarkup}
open={open}
onOpen={onToggle}
Expand Down