Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 87e0df6

Browse files
authored
Merge pull request #2563 from adumesny/master
rem/em support for sizeToContent
2 parents 7d67c3e + 667fe99 commit 87e0df6

File tree

4 files changed

+15
-6
lines changed

4 files changed

+15
-6
lines changed

demo/sizeToContent.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ <h1>sizeToContent options demo</h1>
3636
<a onClick="column(12)" class="btn btn-primary" href="#">12</a>
3737
cellHeight:
3838
<a onClick="cellHeight(25)" class="btn btn-primary" href="#">25</a>
39+
<a onClick="cellHeight('3rem')" class="btn btn-primary" href="#">3rem</a>
3940
<a onClick="cellHeight(50)" class="btn btn-primary" href="#">50</a>
4041
<a onClick="cellHeight(75)" class="btn btn-primary" href="#">75</a>
4142
Widget:
@@ -74,7 +75,7 @@ <h1>sizeToContent options demo</h1>
7475
items.forEach(n => n.id = String(count++));
7576
let opts = {
7677
margin: 5,
77-
cellHeight: 50,
78+
cellHeight: '3rem', // = 48px
7879
sizeToContent: true, // default to make them all fit
7980
resizable: { handles: 'all'}, // do all sides for testing
8081
acceptWidgets: true,

doc/CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ Change log
109109
## 10.0.0-dev (TBD)
110110
* fix: [#2552](https://github.com/gridstack/gridstack.js/issues/2552) DOM init doesn't sizeToContent
111111
* fix: [#2561](https://github.com/gridstack/gridstack.js/pull/2561) issues with sizeToContent animation, cleanup, etc...
112+
* fix: [#2427](https://github.com/gridstack/gridstack.js/issues/2427) sizeToContent supports rem/em cell height
112113
* fix: [#2558](https://github.com/gridstack/gridstack.js/pull/2558) remove style node in shadow root
113114
* fix: [#2556](https://github.com/gridstack/gridstack.js/pull/2556) make sure 'new GridStack(el)' set el.gridstack=this right away
114115
* cleanup: [#2550](https://github.com/gridstack/gridstack.js/pull/2550) Optimize resize arrow (~88% lighter from 1.82 KB to 225B)

src/gridstack.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -811,11 +811,18 @@ export class GridStack {
811811
(!forcePixel || !this.opts.cellHeightUnit || this.opts.cellHeightUnit === 'px')) {
812812
return this.opts.cellHeight as number;
813813
}
814+
// do rem/em to px conversion
815+
if (this.opts.cellHeightUnit === 'rem') {
816+
return (this.opts.cellHeight as number) * parseFloat(getComputedStyle(document.documentElement).fontSize);
817+
}
818+
if (this.opts.cellHeightUnit === 'em') {
819+
return (this.opts.cellHeight as number) * parseFloat(getComputedStyle(this.el).fontSize);
820+
}
814821
// else get first cell height
815822
let el = this.el.querySelector('.' + this.opts.itemClass) as HTMLElement;
816823
if (el) {
817-
let height = Utils.toNumber(el.getAttribute('gs-h')) || 1; // since we don't write 1 anymore
818-
return Math.round(el.offsetHeight / height);
824+
let h = Utils.toNumber(el.getAttribute('gs-h')) || 1; // since we don't write 1 anymore
825+
return Math.round(el.offsetHeight / h);
819826
}
820827
// else do entire grid and # of rows (but doesn't work if min-height is the actual constrain)
821828
let rows = parseInt(this.el.getAttribute('gs-current-row'));
@@ -1358,7 +1365,7 @@ export class GridStack {
13581365
if (!n) return;
13591366
const grid = n.grid;
13601367
if (!grid || el.parentElement !== grid.el) return; // skip if we are not inside a grid
1361-
const cell = grid.getCellHeight();
1368+
const cell = grid.getCellHeight(true);
13621369
if (!cell) return;
13631370
let height = n.h ? n.h * cell : el.clientHeight; // getBoundingClientRect().height seem to flicker back and forth
13641371
let item: Element;
@@ -1370,7 +1377,7 @@ export class GridStack {
13701377
let wantedH: number;
13711378
if (n.subGrid) {
13721379
// sub-grid - use their actual row count * their cell height
1373-
wantedH = n.subGrid.getRow() * n.subGrid.getCellHeight();
1380+
wantedH = n.subGrid.getRow() * n.subGrid.getCellHeight(true);
13741381
} else {
13751382
// NOTE: clientHeight & getBoundingClientRect() is undefined for text and other leaf nodes. use <div> container!
13761383
const child = item.firstElementChild;

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ export interface GridStackOptions {
138138
/**
139139
* one cell height (default?: 'auto'). Can be:
140140
* an integer (px)
141-
* a string (ex: '100px', '10em', '10rem'). Note: % doesn't right - see demo/cell-height.html
141+
* a string (ex: '100px', '10em', '10rem'). Note: % doesn't work right - see demo/cell-height.html
142142
* 0, in which case the library will not generate styles for rows. Everything must be defined in your own CSS files.
143143
* 'auto' - height will be calculated for square cells (width / column) and updated live as you resize the window - also see `cellHeightThrottle`
144144
* 'initial' - similar to 'auto' (start at square cells) but stay that size during window resizing.

0 commit comments

Comments
 (0)