Skip to content

Commit 404c3e5

Browse files
committed
fitToContent when calling cellHeight()/addWidget/MakeWidget
* partial fix for #2427 * when changing cellHeight, or calling addWidget() | makeWidget() we now call doContentResize() * fixed doContentResize logic to use expected size (using cellHeight) instead of actual DOM values so we don't need to delay until animation is done (unlike column width which does affect calc height for content reflow) TODO: support 1rem type of cellHeight.
1 parent 8358e01 commit 404c3e5

File tree

3 files changed

+38
-9
lines changed

3 files changed

+38
-9
lines changed

demo/fitToContent.html

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,19 @@
1818
<body>
1919
<div class="container">
2020
<h1>Cell FitToContent options demo</h1>
21-
<p>new 9.x feature that size the items to fit their content height as to not have scroll bars (unless `fitToContent:false` in C: case) </p>
21+
<p>new 9.x feature that size the items to fit their content height as to not have scroll bars
22+
(unless `fitToContent:false` in C: case). Defaulting to different initial size (see code) to show grow/shrink behavior.</p>
2223
<div>
2324
column:
2425
<a onClick="column(8)" class="btn btn-primary" href="#">8</a>
2526
<a onClick="column(12)" class="btn btn-primary" href="#">12</a>
27+
cellHeight:
28+
<a onClick="cellHeight(25)" class="btn btn-primary" href="#">25</a>
29+
<a onClick="cellHeight(50)" class="btn btn-primary" href="#">50</a>
30+
<a onClick="cellHeight(75)" class="btn btn-primary" href="#">75</a>
31+
Widget:
32+
<a onClick="addWidget()" class="btn btn-primary" href="#">Add</a>
33+
<a onClick="makeWidget()" class="btn btn-primary" href="#">Make</a>
2634
</div>
2735
<br>
2836
<div class="grid-stack"></div>
@@ -32,22 +40,36 @@ <h1>Cell FitToContent options demo</h1>
3240
margin: 5,
3341
cellHeight: 50,
3442
fitToContent: true, // default to make them all fit
43+
resizable: { handles: 'all'} // do all sides for testing
3544
// cellHeightThrottle: 100, // ms before fitToContent happens
3645
}
3746
let grid = GridStack.init(opts);
3847
let text ='some very large content that will normally not fit in the window.'
3948
text = text + text;
4049
let items = [
41-
{x:0, y:0, w:2, content: `<div>A: ${text}</div>`},
42-
{x:2, y:0, w:1, h:2, content: '<div>B: shrink</div>'}, // make taller than needed upfront
50+
{x:0, y:0, w:2, content: `<div>A no h: ${text}</div>`},
51+
{x:2, y:0, w:1, h:2, content: '<div>B: shrink h=2</div>'}, // make taller than needed upfront
4352
{x:3, y:0, w:2, fitToContent: false, content: `<div>C: WILL SCROLL. ${text}</div>`}, // prevent this from fitting testing
44-
{x:0, y:1, w:3, content: `<div>D: ${text} ${text}</div>`},
53+
{x:0, y:1, w:3, content: `<div>D no h: ${text} ${text}</div>`},
4554
];
4655
grid.load(items);
4756

4857
function column(n) {
4958
grid.column(n, 'none');
5059
}
60+
function cellHeight(n) {
61+
grid.cellHeight(n);
62+
}
63+
function addWidget() {
64+
grid.addWidget({content: `<div>New: ${text}</div>`});
65+
}
66+
function makeWidget() {
67+
let doc = document.implementation.createHTMLDocument();
68+
doc.body.innerHTML = `<div class="item"><div class="grid-stack-item-content"><div>New Make: ${text}</div></div></div>`;
69+
let el = doc.body.children[0];
70+
grid.el.appendChild(el);
71+
grid.makeWidget(el);
72+
}
5173
</script>
5274
</body>
5375
</html>

doc/CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ Change log
9797

9898
## 9.0.1-dev (TBD)
9999
* fix 'resizecontent' event fix not called.
100+
* partial fix [#2427](https://github.com/gridstack/gridstack.js/issues/2427) fitToContent when calling cellHeight()/addWidget()/MakeWidget()
100101

101102
## 9.0.1 (2023-08-27)
102103
* fix [#2413](https://github.com/gridstack/gridstack.js/issues/2413) support touchscreen+mouse devices. Thank you [@Ruslan207](https://github.com/Ruslan207)

src/gridstack.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,8 @@ export class GridStack {
798798
this.opts.cellHeightUnit = data.unit;
799799
this.opts.cellHeight = data.h;
800800

801+
this.doContentResize(false);
802+
801803
if (update) {
802804
this._updateStyles(true); // true = force re-create for current # of rows
803805
}
@@ -996,10 +998,13 @@ export class GridStack {
996998
public makeWidget(els: GridStackElement, options?: GridStackWidget): GridItemHTMLElement {
997999
let el = GridStack.getElement(els);
9981000
this._prepareElement(el, true, options);
1001+
const node = el.gridstackNode;
1002+
9991003
this._updateContainerHeight();
10001004

1005+
this.doContentResize(false, node);
1006+
10011007
// see if there is a sub-grid to create
1002-
const node = el.gridstackNode;
10031008
if (node.subGridOpts) {
10041009
this.makeSubGrid(el, node.subGridOpts, undefined, false); // node.subGrid will be used as option in method, no need to pass
10051010
}
@@ -1266,19 +1271,20 @@ export class GridStack {
12661271
const grid = n.grid;
12671272
if (grid !== this) return grid?.resizeToContent(el);
12681273
if (el.parentElement !== this.el) return; // skip if we are not inside a grid
1269-
let height = el.clientHeight; // getBoundingClientRect().height seem to flicker back and forth
1274+
const cell = this.getCellHeight();
1275+
if (!cell) return;
1276+
let height = n.h ? n.h * cell : el.clientHeight; // getBoundingClientRect().height seem to flicker back and forth
12701277
if (!height) return; // 0 when hidden, skip
12711278
const item = el.querySelector(GridStack.resizeToContentParent);
12721279
if (!item) return;
12731280
const child = item.firstElementChild;
12741281
// NOTE: clientHeight & getBoundingClientRect() is undefined for text and other leaf nodes. use <div> container!
12751282
if (!child) { console.log(`Error: resizeToContent() '${GridStack.resizeToContentParent}'.firstElementChild is null, make sure to have a div like container. Skipping sizing.`); return; }
1276-
const itemH = item.clientHeight; // available height to our child (minus border, padding...)
1283+
const padding = el.clientHeight - item.clientHeight; // full - available height to our child (minus border, padding...)
1284+
const itemH = n.h ? n.h * cell - padding : item.clientHeight; // calculated to what cellHeight is or will become (rather than actual to prevent waiting for animation to finish)
12771285
const wantedH = child.getBoundingClientRect().height || itemH;
12781286
if (itemH === wantedH) return;
12791287
height += wantedH - itemH;
1280-
const cell = this.getCellHeight();
1281-
if (!cell) return;
12821288
let h = Math.ceil(height / cell);
12831289
if (n.minH && h < n.minH) h = n.minH;
12841290
else if (n.maxH && h > n.maxH) h = n.maxH;

0 commit comments

Comments
 (0)