Skip to content

Commit 32337b6

Browse files
author
Alain Dumesny
authored
Merge pull request #2433 from adumesny/master
sizeToContent number support
2 parents 038eea7 + 1bd5764 commit 32337b6

File tree

7 files changed

+37
-9
lines changed

7 files changed

+37
-9
lines changed

demo/sizeToContent.html

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ <h1>Cell sizeToContent options demo</h1>
5151
{x:2, y:0, w:1, h:2, content: '<div>B: shrink h=2</div>'}, // make taller than needed upfront
5252
{x:3, y:0, w:2, sizeToContent: false, content: `<div>C: WILL SCROLL. ${text}</div>`}, // prevent this from fitting testing
5353
{x:0, y:1, w:3, content: `<div>D no h: ${text} ${text}</div>`},
54-
];
54+
{x:3, y:1, w:2, sizeToContent:3, content: `<div>E sizeToContent=3 <button onClick="more()">more</button><button onClick="less()">less</button><div id="dynContent">${text} ${text} ${text}</div></div>`} ];
5555
grid.load(items);
5656

5757
function column(n) {
@@ -70,6 +70,21 @@ <h1>Cell sizeToContent options demo</h1>
7070
grid.el.appendChild(el);
7171
grid.makeWidget(el);
7272
}
73+
function more() {
74+
let cont = document.getElementById('dynContent');
75+
if (!cont) return;
76+
cont.innerHTML += cont.innerHTML;
77+
let el = cont.parentElement.parentElement.parentElement;
78+
grid.resizeToContent(el)
79+
}
80+
function less() {
81+
let cont = document.getElementById('dynContent');
82+
if (!cont) return;
83+
let content = cont.innerHTML;
84+
cont.innerHTML = content.substring(0, content.length/2);
85+
let el = cont.parentElement.parentElement.parentElement;
86+
grid.resizeToContent(el);
87+
}
7388
</script>
7489
</body>
7590
</html>

doc/CHANGES.md

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

9999
## 9.0.2-dev (TBD)
100100
* renamed fitToContent to sizeToContent (API BREAK)
101+
* feat: `sizeToContent` now supports being `boolean|number` to limit the height but user can resize past that, unlike maxH.
101102

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

doc/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ gridstack.js API
9999
- `draggable` - allows to override draggable options - see `DDDragOpt`. (default: `{handle: '.grid-stack-item-content', appendTo: 'body', scroll: true}`)
100100
- `dragOut` to let user drag nested grid items out of a parent or not (default false) See [example](http://gridstackjs.com/demo/nested.html)
101101
- `engineClass` - the type of engine to create (so you can subclass) default to GridStackEngine
102-
- `sizeToContent` - make gridItems size themselves to their content, calling `resizeToContent(el)` whenever the grid or item is resized.
102+
- `sizeToContent`: boolean - make gridItems size themselves to their content, calling `resizeToContent(el)` whenever the grid or item is resized.
103103
- `float` - enable floating widgets (default: `false`) See [example](http://gridstackjs.com/demo/float.html)
104104
- `handle` - draggable handle selector (default: `'.grid-stack-item-content'`)
105105
- `handleClass` - draggable handle class (e.g. `'grid-stack-item-content'`). If set `handle` is ignored (default: `null`)
@@ -159,7 +159,8 @@ You need to add `noResize` and `noMove` attributes to completely lock the widget
159159
- `noMove` - disable element moving
160160
- `id`- (number | string) good for quick identification (for example in change event)
161161
- `content` - (string) html content to be added when calling `grid.load()/addWidget()` as content inside the item
162-
- `sizeToContent` - make gridItem size itself to the content, calling `GridStack.resizeToContent(el)` whenever the grid or item is resized.
162+
- `sizeToContent`?: boolean | number - make gridItem size itself to the content, calling `GridStack.resizeToContent(el)` whenever the grid or item is resized.
163+
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)
163164
- `subGrid`?: GridStackOptions - optional nested grid options and list of children
164165
- `subGridDynamic`?: boolean - enable/disable the creation of sub-grids on the fly by dragging items completely over others (nest) vs partially (push). Forces `DDDragOpt.pause=true` to accomplish that.
165166

src/gridstack.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ $animation_speed: .3s !default;
5151
overflow-x: hidden;
5252
overflow-y: auto;
5353
}
54-
&.fit-to-content > .grid-stack-item-content {
54+
&.size-to-content > .grid-stack-item-content {
5555
overflow-y: hidden;
5656
}
5757
}

src/gridstack.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,6 +1286,13 @@ export class GridStack {
12861286
if (itemH === wantedH) return;
12871287
height += wantedH - itemH;
12881288
let h = Math.ceil(height / cell);
1289+
// 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;
1293+
el.classList.remove('size-to-content'); // get v-scroll back
1294+
} else el.classList.add('size-to-content');
1295+
}
12891296
if (n.minH && h < n.minH) h = n.minH;
12901297
else if (n.maxH && h > n.maxH) h = n.maxH;
12911298
if (h !== n.h) {
@@ -1507,7 +1514,7 @@ export class GridStack {
15071514
if (!Utils.same(node, copy)) {
15081515
this._writeAttr(el, node);
15091516
}
1510-
if (Utils.shouldSizeToContent(node)) el.classList.add('fit-to-content');
1517+
if (Utils.shouldSizeToContent(node)) el.classList.add('size-to-content');
15111518
this._prepareDragDropByNode(node);
15121519
return this;
15131520
}
@@ -2235,7 +2242,10 @@ export class GridStack {
22352242

22362243
this.engine.endUpdate();
22372244

2238-
if (event.type === 'resizestop') this.doContentResize(false, node);
2245+
if (event.type === 'resizestop') {
2246+
if (Number.isInteger(node.sizeToContent)) node.sizeToContent = node.h; // new soft limit
2247+
this.doContentResize(false, node);
2248+
}
22392249
}
22402250

22412251
dd.draggable(el, {

src/types.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,8 +321,9 @@ export interface GridStackWidget extends GridStackPosition {
321321
id?: string;
322322
/** html to append inside as content */
323323
content?: string;
324-
/** local (grid) override - see GridStackOptions */
325-
sizeToContent?: boolean;
324+
/** local (vs grid) override - see GridStackOptions.
325+
* 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) */
326+
sizeToContent?: boolean | number;
326327
/** optional nested grid options and list of children, which then turns into actual instance at runtime to get options from */
327328
subGridOpts?: GridStackOptions;
328329
}

src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ export class Utils {
104104

105105
/** true if we should resize to content */
106106
static shouldSizeToContent(n: GridStackNode | undefined): boolean {
107-
return n?.grid && (n.sizeToContent || (n.grid.opts.sizeToContent && n.sizeToContent !== false));
107+
return n?.grid && (!!n.sizeToContent || (n.grid.opts.sizeToContent && n.sizeToContent !== false));
108108
}
109109

110110
/** returns true if a and b overlap */

0 commit comments

Comments
 (0)