Skip to content

Commit 9915ba0

Browse files
author
Alain Dumesny
authored
Merge pull request #2436 from adumesny/master
more resizeToContent fixes
2 parents 32337b6 + e7c3203 commit 9915ba0

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

doc/CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ Change log
9999
## 9.0.2-dev (TBD)
100100
* renamed fitToContent to sizeToContent (API BREAK)
101101
* feat: `sizeToContent` now supports being `boolean|number` to limit the height but user can resize past that, unlike maxH.
102+
* feat: `resizeToContentParent` now on GridStackWidget for those widgets that need to resize differently.
102103

103104
## 9.0.2 (2023-08-29)
104105
* fix 'resizecontent' event fix not called.

src/gridstack.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1266,6 +1266,7 @@ export class GridStack {
12661266
Note: this assumes only 1 child under '.grid-stack-item-content' (sized to gridItem minus padding) that is at the entire content size wanted */
12671267
public resizeToContent(els: GridStackElement) {
12681268
GridStack.getElements(els).forEach(el => {
1269+
if (!el.clientHeight) return; // 0 when hidden, skip
12691270
let n = el?.gridstackNode;
12701271
if (!n) return;
12711272
const grid = n.grid;
@@ -1274,8 +1275,9 @@ export class GridStack {
12741275
const cell = this.getCellHeight();
12751276
if (!cell) return;
12761277
let height = n.h ? n.h * cell : el.clientHeight; // getBoundingClientRect().height seem to flicker back and forth
1277-
if (!height) return; // 0 when hidden, skip
1278-
const item = el.querySelector(GridStack.resizeToContentParent);
1278+
let item: Element;
1279+
if (n.resizeToContentParent) item = el.querySelector(n.resizeToContentParent);
1280+
if (!item) item = el.querySelector(GridStack.resizeToContentParent);
12791281
if (!item) return;
12801282
const child = item.firstElementChild;
12811283
// NOTE: clientHeight & getBoundingClientRect() is undefined for text and other leaf nodes. use <div> container!
@@ -1287,9 +1289,10 @@ export class GridStack {
12871289
height += wantedH - itemH;
12881290
let h = Math.ceil(height / cell);
12891291
// check for min/max and special sizing
1290-
if (Number.isInteger(n.sizeToContent)) {
1291-
if (h > (n.sizeToContent as number)) {
1292-
h = n.sizeToContent as number;
1292+
const softMax = Number.isInteger(n.sizeToContent) ? n.sizeToContent as number : 0;
1293+
if (softMax) {
1294+
if (h > softMax) {
1295+
h = softMax;
12931296
el.classList.remove('size-to-content'); // get v-scroll back
12941297
} else el.classList.add('size-to-content');
12951298
}

src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,8 @@ export interface GridStackWidget extends GridStackPosition {
324324
/** local (vs grid) override - see GridStackOptions.
325325
* Note: This also allow you to set a maximum h value (but user changeable during normal resizing) to prevent unlimited content from taking too much space (get scrollbar) */
326326
sizeToContent?: boolean | number;
327+
/** local override of GridStack.resizeToContentParent that specify the class to use for the parent (actual) vs child (wanted) height */
328+
resizeToContentParent?: string;
327329
/** optional nested grid options and list of children, which then turns into actual instance at runtime to get options from */
328330
subGridOpts?: GridStackOptions;
329331
}

0 commit comments

Comments
 (0)