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
6 changes: 3 additions & 3 deletions addon/src/modifiers/sortable-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { macroCondition, isDevelopingApp } from '@embroider/macros';
import { getX, getY } from '../utils/coordinate.ts';
import ScrollContainer from '../system/scroll-container.ts';
import scrollParent from '../system/scroll-parent.ts';
import { getBorderSpacing } from '../utils/css-calculation.ts';
import { getBorderSpacing, getLayoutSize } from '../utils/css-calculation.ts';
import { buildWaiter } from '@ember/test-waiters';
import * as s from '@ember/service';
import { assert, deprecate } from '@ember/debug';
Expand Down Expand Up @@ -993,7 +993,7 @@ export default class SortableItemModifier<T> extends Modifier<SortableItemModifi

// `offsetWidth` rounds to the nearest integer; summing rounded widths
// across items can drift enough to break the grid direction's wrap check.
let width = el.getBoundingClientRect().width;
let width = getLayoutSize(el, 'width');
const elStyles = getComputedStyle(el);

width += parseFloat(elStyles.marginLeft) + parseFloat(elStyles.marginRight); // equal to jQuery.outerWidth(true)
Expand All @@ -1012,7 +1012,7 @@ export default class SortableItemModifier<T> extends Modifier<SortableItemModifi
const el = this.element;

// See `width` above for why this avoids the rounded `offsetHeight`.
let height = el.getBoundingClientRect().height;
let height = getLayoutSize(el, 'height');
const elStyles = getComputedStyle(el);
const marginTop = parseFloat(elStyles.marginTop);
const marginBottom = parseFloat(elStyles.marginBottom);
Expand Down
37 changes: 37 additions & 0 deletions addon/src/utils/css-calculation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,43 @@ export function getBorderSpacing(el: Element) {
};
}

/**
Gets the fractional border-box width or height of an element in layout
pixels. Unlike `getBoundingClientRect()`, it is not scaled by a CSS transform
on the element or an ancestor, so it matches `offsetTop`, `offsetLeft` and
margins. Falls back to the rounded offset size when the computed size is
not a number (for example `auto` on an element that is not rendered).

@method getLayoutSize
@param {Element} element
@param {String} dimension
@return {Number}
@private
*/
export function getLayoutSize(el: HTMLElement, dimension: 'width' | 'height') {
const css = getComputedStyle(el);
let size = parseFloat(css[dimension]);

if (css.boxSizing === 'content-box') {
size +=
dimension === 'width'
? parseFloat(css.paddingLeft) +
parseFloat(css.paddingRight) +
parseFloat(css.borderLeftWidth) +
parseFloat(css.borderRightWidth)
: parseFloat(css.paddingTop) +
parseFloat(css.paddingBottom) +
parseFloat(css.borderTopWidth) +
parseFloat(css.borderBottomWidth);
}

if (Number.isFinite(size)) {
return size;
}

return dimension === 'width' ? el.offsetWidth : el.offsetHeight;
}

/**
Gets numeric `column-gap`/`row-gap` values for a given element.

Expand Down
32 changes: 32 additions & 0 deletions test-app/tests/integration/modifiers/sortable-item-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,38 @@ module('Integration | Modifier | sortable-item', function (hooks) {
assert.equal(contents('#test-list'), 'Uno');
});

// `getBoundingClientRect()` is scaled by a CSS transform on an ancestor, but
// `offsetTop`, `offsetLeft` and the margins are not, so item sizes have to
// come from layout pixels.
test('Item size ignores a CSS transform on an ancestor', async function (assert) {
this.items = ['Uno', 'Dos'];

await render(hbs`
<div style="transform: scale(0.5); transform-origin: top left;">
<ol id="test-list" style="width: 100px; margin: 0; padding: 0; list-style: none;" {{sortable-group groupName="scaled"}}>
{{#each this.items as |item|}}
<li
data-test-item
style="box-sizing: border-box; width: 60.5px; height: 20.5px; margin: 0 0 4px; padding: 0;"
{{sortable-item groupName="scaled" model=item}}
>{{item}}</li>
{{/each}}
</ol>
</div>
`);

const item = this.owner
.lookup('service:ember-sortable-internal-state')
.fetchGroup('scaled').items[0];

assert.strictEqual(
item.height,
24.5,
'height is the layout height plus the bottom margin',
);
assert.strictEqual(item.width, 60.5, 'width is the layout width');
});

function contents(selector) {
return find(selector)
.textContent.replace(/⇕/g, '')
Expand Down
Loading