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
1 change: 1 addition & 0 deletions doc/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ Change log
* fix: [#3418](https://github.com/gridstack/gridstack.js/pull/3418) preserve saved layout after cancelling a cross-grid drag- #3418 - thank you [sameerdeolalikar](https://github.com/sameerdeolalikar)
* fix: [#2819](https://github.com/gridstack/gridstack.js/issues/2819) item not moved (to alt position) when colliding fails with an object
* fix: [#3230](https://github.com/gridstack/gridstack.js/issues/3230) don't recompute w/h unless side can change (rounding errors)
* fix: [#2208](https://github.com/gridstack/gridstack.js/issues/2208) `cellHeight` in `vw`/`vh` units converts straight to pixels drift issue

## 14.0.0 (2026-09-20)
* feat: [#754](https://github.com/gridstack/gridstack.js/issues/754), [#2866](https://github.com/gridstack/gridstack.js/issues/2866) new `mode?: 'top' | 'float' | 'list' | 'compact'` - items are continuously re-flowed in sequential (row-major) order, like a re-orderable list: dragging, resizing, adding or removing an item re-flows everyone else instead of pushing them down, and dropping an item on another takes its place. See new
Expand Down
39 changes: 39 additions & 0 deletions spec/regression-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -780,4 +780,43 @@ describe('regression >', () => {
grid.engine.endUpdate();
});
});

describe('2208 cellHeight in viewport units drifts items >', () => {
beforeEach(() => {
document.body.insertAdjacentHTML('afterbegin', gridstackEmptyHTML);
});
afterEach(() => {
document.body.removeChild(document.getElementById('gs-cont'));
});

it('converts vw/vh to pixels instead of guessing from the first item', () => {
grid = GridStack.init({column: 12, cellHeight: '1vw', margin: 0,
children: [{x: 0, y: 0, w: 1, h: 1}]});
expect(grid.opts.cellHeightUnit).toBe('vw');
// 1vw = 1% of the viewport width. We used to fall through to measuring the first item, which
// is only approximate - and positions are y*cellHeight, so the error grew with the row index
expect(grid.getCellHeight(true)).toBeCloseTo(window.innerWidth / 100, 6);

grid.cellHeight('2vh');
expect(grid.opts.cellHeightUnit).toBe('vh');
expect(grid.getCellHeight(true)).toBeCloseTo(2 * window.innerHeight / 100, 6);
});

it('still handles the units it already knew', () => {
grid = GridStack.init({column: 12, cellHeight: '10mm', margin: 0});
expect(grid.getCellHeight(true)).toBeCloseTo(10 * (96 / 2.54) / 10, 6);
grid.cellHeight('50px');
expect(grid.getCellHeight(true)).toBe(50);
});

it('a row position is an exact multiple of the cell height, however far down', () => {
grid = GridStack.init({column: 12, cellHeight: '1vw', margin: 0,
children: [{id: 'top', x: 0, y: 0, w: 1, h: 1}, {id: 'low', x: 0, y: 40, w: 1, h: 1}]});
const ch = grid.getCellHeight(true);
// no accumulating drift: row 40 sits at exactly 40 cells down
expect(40 * ch).toBeCloseTo(40 * window.innerWidth / 100, 6);
expect(Number.isFinite(ch)).toBe(true);
expect(ch).toBeGreaterThan(0);
});
});
});
9 changes: 9 additions & 0 deletions src/gridstack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,15 @@ export class GridStack {
if (this.opts.cellHeightUnit === 'mm') {
return (this.opts.cellHeight as number) * (96 / 2.54) / 10;
}
// viewport units: parseHeight() accepts these but we used to fall through to measuring the first
// item below, which is only approximate - and since item positions are y*cellHeight that error
// multiplies by the row index, so items drifted more the further down they were (#2208).
if (this.opts.cellHeightUnit === 'vw' || this.opts.cellHeightUnit === 'vh') {
const view = this.opts.cellHeightUnit === 'vw'
? (window.innerWidth || document.documentElement.clientWidth)
: (window.innerHeight || document.documentElement.clientHeight);
return (this.opts.cellHeight as number) * view / 100;
}
// else get first cell height
const el = this.el.querySelector('.' + this.opts.itemClass) as HTMLElement;
if (el) {
Expand Down