Skip to content
Merged
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
4 changes: 2 additions & 2 deletions core/src/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2283,7 +2283,7 @@ export namespace Components {
}
interface IonPopover {
/**
* Describes how to align the popover content with the `reference` point. Defaults to `"center"` for `ios` mode, and `"start"` for `md` mode.
* Describes how to align the popover content with the `reference` point. Defaults to `"center"` for `ios` mode, and `"start"` for `md` mode. The `"start"` and `"end"` values are RTL-aware.
*/
"alignment"?: PositionAlign;
/**
Expand Down Expand Up @@ -7393,7 +7393,7 @@ declare namespace LocalJSX {
}
interface IonPopover {
/**
* Describes how to align the popover content with the `reference` point. Defaults to `"center"` for `ios` mode, and `"start"` for `md` mode.
* Describes how to align the popover content with the `reference` point. Defaults to `"center"` for `ios` mode, and `"start"` for `md` mode. The `"start"` and `"end"` values are RTL-aware.
*/
"alignment"?: PositionAlign;
/**
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions core/src/components/popover/popover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ export class Popover implements ComponentInterface, PopoverInterface {
/**
* Describes how to align the popover content with the `reference` point.
* Defaults to `"center"` for `ios` mode, and `"start"` for `md` mode.
* The `"start"` and `"end"` values are RTL-aware.
*/
@Prop({ mutable: true }) alignment?: PositionAlign;

Expand Down
Comment thread
OS-jacobbell marked this conversation as resolved.
Resolved
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
55 changes: 55 additions & 0 deletions core/src/components/popover/test/position/popover.e2e.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expect } from '@playwright/test';
import { configs, test, Viewports } from '@utils/test/playwright';
import type { E2EPage } from '@utils/test/playwright';

import { openPopover } from '../test.utils';

Expand Down Expand Up @@ -35,6 +36,60 @@ configs().forEach(({ title, screenshot, config }) => {

configs({ directions: ['ltr', 'rtl'] }).forEach(({ title, config }) => {
test.describe(title('popover: position'), async () => {
/**
* Opens the popover for the given trigger and returns the
* horizontal edges of both the trigger and the popover content.
*/
const openAndMeasure = async (page: E2EPage, id: string) => {
await page.goto('/src/components/popover/test/position', config);
await page.setViewportSize(Viewports.tablet.portrait);
await openPopover(page, id);

const buttonBox = (await page.locator(`#${id}`).boundingBox())!;
const popoverBox = (await page.locator(`.${id}-popover`).locator('.popover-content').boundingBox())!;

return {
trigger: { left: buttonBox.x, right: buttonBox.x + buttonBox.width },
popover: {
left: popoverBox.x,
right: popoverBox.x + popoverBox.width,
},
};
};

/**
* Allow up to 1px of difference to account for sub-pixel rounding.
*/
const expectWithinOnePx = (actual: number, expected: number) => {
expect(Math.abs(actual - expected)).toBeLessThanOrEqual(1);
};

for (const side of ['top', 'bottom']) {
test(`should align to the start edge of the trigger when side is ${side} and alignment is start`, async ({
page,
}) => {
const { trigger, popover } = await openAndMeasure(page, `${side}-start`);

if (config.direction === 'rtl') {
expectWithinOnePx(popover.right, trigger.right);
} else {
expectWithinOnePx(popover.left, trigger.left);
}
});

test(`should align to the end edge of the trigger when side is ${side} and alignment is end`, async ({
page,
}) => {
const { trigger, popover } = await openAndMeasure(page, `${side}-end`);

if (config.direction === 'rtl') {
expectWithinOnePx(popover.left, trigger.left);
} else {
expectWithinOnePx(popover.right, trigger.right);
}
});
}

test('should be on the left when side is start and an ancestor declares ltr', async ({ page }) => {
await page.goto('/src/components/popover/test/position', config);
await page.setViewportSize(Viewports.tablet.portrait);
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
126 changes: 48 additions & 78 deletions core/src/components/popover/utils.ts
Comment thread
OS-jacobbell marked this conversation as resolved.
Resolved
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,14 @@ export const getPopoverPosition = (
* would allow the popover content
* to have the correct alignment.
*/
const alignedCoordinates = calculatePopoverAlign(align, side, referenceCoordinates, contentWidth, contentHeight);
const alignedCoordinates = calculatePopoverAlignedCoordinates(
align,
side,
referenceCoordinates,
contentWidth,
contentHeight,
isRTL
);

const top = coordinates.top + alignedCoordinates.top;
const left = coordinates.left + alignedCoordinates.left;
Expand Down Expand Up @@ -702,9 +709,9 @@ export const getPopoverPosition = (
const calculatePopoverOrigin = (side: PositionSide, align: PositionAlign, isRTL: boolean) => {
switch (side) {
case 'top':
return { originX: getOriginXAlignment(align), originY: 'bottom' };
return { originX: getOriginXAlignment(align, isRTL), originY: 'bottom' };
case 'bottom':
return { originX: getOriginXAlignment(align), originY: 'top' };
return { originX: getOriginXAlignment(align, isRTL), originY: 'top' };
case 'left':
return { originX: 'right', originY: getOriginYAlignment(align) };
case 'right':
Expand All @@ -716,14 +723,14 @@ const calculatePopoverOrigin = (side: PositionSide, align: PositionAlign, isRTL:
}
};

const getOriginXAlignment = (align: PositionAlign) => {
const getOriginXAlignment = (align: PositionAlign, isRTL: boolean) => {
switch (align) {
case 'start':
return 'left';
return isRTL ? 'right' : 'left';
case 'center':
return 'center';
case 'end':
return 'right';
return isRTL ? 'left' : 'right';
}
};

Expand Down Expand Up @@ -839,89 +846,52 @@ const calculatePopoverSide = (
* correct alignment regardless while taking
* into account the side the popover is on.
*/
const calculatePopoverAlign = (
const calculatePopoverAlignedCoordinates = (
align: PositionAlign,
side: PositionSide,
triggerBoundingBox: ReferenceCoordinates,
contentWidth: number,
contentHeight: number
) => {
switch (align) {
case 'center':
return calculatePopoverCenterAlign(side, triggerBoundingBox, contentWidth, contentHeight);
case 'end':
return calculatePopoverEndAlign(side, triggerBoundingBox, contentWidth, contentHeight);
case 'start':
default:
return { top: 0, left: 0 };
}
};

/**
* Calculate the end alignment for
* the popover. If side is on the x-axis
* then the align values refer to the top
* and bottom margins of the content.
* If side is on the y-axis then the
* align values refer to the left and right
* margins of the content.
*/
const calculatePopoverEndAlign = (
side: PositionSide,
triggerBoundingBox: ReferenceCoordinates,
contentWidth: number,
contentHeight: number
) => {
switch (side) {
case 'start':
case 'end':
case 'left':
case 'right':
return {
top: -(contentHeight - triggerBoundingBox.height),
left: 0,
};
case 'top':
case 'bottom':
default:
return {
top: 0,
left: -(contentWidth - triggerBoundingBox.width),
};
}
};

/**
* Calculate the center alignment for
* the popover. If side is on the x-axis
* then the align values refer to the top
* and bottom margins of the content.
* If side is on the y-axis then the
* align values refer to the left and right
* margins of the content.
*/
const calculatePopoverCenterAlign = (
side: PositionSide,
triggerBoundingBox: ReferenceCoordinates,
contentWidth: number,
contentHeight: number
contentHeight: number,
isRTL: boolean
) => {
switch (side) {
case 'start':
case 'end':
case 'left':
case 'right':
return {
top: -(contentHeight / 2 - triggerBoundingBox.height / 2),
left: 0,
};
case 'right': {
/**
* If side is on the x-axis
* then the align values refer to the top
* and bottom margins of the content.
*/
switch (align) {
case 'end':
return { top: -(contentHeight - triggerBoundingBox.height), left: 0 };
case 'center':
return { top: -(contentHeight / 2 - triggerBoundingBox.height / 2), left: 0 };
case 'start':
default:
return { top: 0, left: 0 };
}
}
case 'top':
case 'bottom':
default:
return {
top: 0,
left: -(contentWidth / 2 - triggerBoundingBox.width / 2),
};
default: {
/**
* If side is on the y-axis then the
* align values refer to the left and right
* margins of the content.
*/
switch (align) {
case 'end':
return { top: 0, left: isRTL ? 0 : -(contentWidth - triggerBoundingBox.width) };
case 'center':
return { top: 0, left: -(contentWidth / 2 - triggerBoundingBox.width / 2) };
case 'start':
default:
return { top: 0, left: isRTL ? -(contentWidth - triggerBoundingBox.width) : 0 };
}
Comment thread
OS-jacobbell marked this conversation as resolved.
Resolved
}
}
};

Expand Down
Loading