diff --git a/core/src/components/reorder-group/reorder-group.tsx b/core/src/components/reorder-group/reorder-group.tsx index 918beebd99a..d3f19c06de5 100644 --- a/core/src/components/reorder-group/reorder-group.tsx +++ b/core/src/components/reorder-group/reorder-group.tsx @@ -161,7 +161,12 @@ export class ReorderGroup implements ComponentInterface { let sum = 0; for (let i = 0; i < children.length; i++) { const child = children[i]; - sum += child.offsetHeight; + /** + * offsetHeight is rounded to a whole number. Summing it would add + * an error for every item with a fractional height, moving the swap + * point further from the item boundary further down the list. + */ + sum += child.getBoundingClientRect().height; heights.push(sum); child.$ionIndex = i; } @@ -182,7 +187,7 @@ export class ReorderGroup implements ComponentInterface { } this.lastToIndex = indexForItem(item); - this.selectedItemHeight = item.offsetHeight; + this.selectedItemHeight = item.getBoundingClientRect().height; this.state = ReorderGroupState.Active; item.classList.add(ITEM_REORDER_SELECTED); diff --git a/core/src/components/reorder-group/test/basic/reorder-group.e2e.ts b/core/src/components/reorder-group/test/basic/reorder-group.e2e.ts index 38fc40873ea..f9348817fae 100644 --- a/core/src/components/reorder-group/test/basic/reorder-group.e2e.ts +++ b/core/src/components/reorder-group/test/basic/reorder-group.e2e.ts @@ -1,5 +1,5 @@ import { expect } from '@playwright/test'; -import { configs, test } from '@utils/test/playwright'; +import { configs, dragElementBy, test } from '@utils/test/playwright'; /** * Reorder group does not have per-mode styles @@ -36,5 +36,68 @@ configs({ modes: ['md'], directions: ['ltr'] }).forEach(({ title, config }) => { const reorder = page.locator('ion-reorder'); await expect(reorder).toBeHidden(); }); + + test.describe('fractional item heights', () => { + test.beforeEach(async ({ page }, testInfo) => { + testInfo.annotations.push({ + type: 'issue', + description: 'https://github.com/ionic-team/ionic-framework/issues/31670', + }); + + await page.setContent( + ` + + + ${'
Item
'.repeat(30)} +
+ `, + config + ); + }); + + test('should swap at the item boundaries further down the list', async ({ page }) => { + const rows = page.locator('.row'); + const ionReorderEnd = await page.spyOnEvent('ionReorderEnd'); + + const firstRow = (await rows.nth(0).boundingBox())!; + const targetRow = (await rows.nth(25).boundingBox())!; + const x = firstRow.x + firstRow.width / 2; + const targetY = targetRow.y + targetRow.height / 2; + + // Drag the center of the first row to the center of the target row. + await dragElementBy( + page.locator('ion-reorder').first(), + page, + 0, + targetRow.y - firstRow.y, + x, + undefined, + false + ); + + // The gesture ignores a move that arrives before the previous one was handled, so repeat the last one. + await page.evaluate(() => new Promise(requestAnimationFrame)); + await page.mouse.move(x, targetY); + await page.evaluate(() => new Promise(requestAnimationFrame)); + + await page.mouse.up(); + await page.waitForChanges(); + + await expect(ionReorderEnd).toHaveReceivedEventDetail({ from: 0, to: 25, complete: undefined }); + }); + + test('should move the displaced items by the exact height of the selected item', async ({ page }) => { + const rows = page.locator('.row'); + + await dragElementBy(page.locator('ion-reorder').first(), page, 0, 40, undefined, undefined, false); + await page.waitForChanges(); + + await expect(rows.nth(1)).toHaveCSS('transform', 'matrix(1, 0, 0, 1, 0, -20.5)'); + }); + }); }); });