Skip to content
Open
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
9 changes: 7 additions & 2 deletions core/src/components/reorder-group/reorder-group.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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(
`
<style>
.row {
height: 20.5px;
}
</style>
<ion-reorder-group disabled="false">
${'<div class="row"><ion-reorder>Item</ion-reorder></div>'.repeat(30)}
</ion-reorder-group>
`,
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)');
});
});
});
});
Loading