Skip to content

Commit e9b54b8

Browse files
Joost Zöllnerjoostme
authored andcommitted
Allow cell height in cm and mm units
1 parent bbde344 commit e9b54b8

File tree

4 files changed

+14
-3
lines changed

4 files changed

+14
-3
lines changed

doc/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ gridstack.js API
8787
- `auto` - if `false` gridstack will not initialize existing items (default: `true`)
8888
- `cellHeight`- one cell height (default?: 'auto'). Can be:
8989
* an integer (px)
90-
* a string (ex: '100px', '10em', '10rem'). Note: % doesn't right - see [CellHeight](http://gridstackjs.com/demo/cell-height.html)
90+
* a string (ex: '100px', '10em', '10rem', '10cm'). Note: % doesn't right - see [CellHeight](http://gridstackjs.com/demo/cell-height.html)
9191
* 0, in which case the library will not generate styles for rows. Everything must be defined in your own CSS files.
9292
* `auto` - height will be calculated for square cells (width / column) and updated live as you resize the window - also see `cellHeightThrottle`
9393
* `initial` - similar to 'auto' (start at square cells) but stay that size during window resizing.

spec/utils-spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ describe('gridstack utils', function() {
7676
expect(Utils.parseHeight('12.3vh')).toEqual(jasmine.objectContaining({h: 12.3, unit: 'vh'}));
7777
expect(Utils.parseHeight('12.3vw')).toEqual(jasmine.objectContaining({h: 12.3, unit: 'vw'}));
7878
expect(Utils.parseHeight('12.3%')).toEqual(jasmine.objectContaining({h: 12.3, unit: '%'}));
79+
expect(Utils.parseHeight('12.5cm')).toEqual(jasmine.objectContaining({h: 12.5, unit: 'cm'}));
80+
expect(Utils.parseHeight('12.5mm')).toEqual(jasmine.objectContaining({h: 12.5, unit: 'mm'}));
7981
expect(Utils.parseHeight('12.5')).toEqual(jasmine.objectContaining({h: 12.5, unit: 'px'}));
8082
expect(function() { Utils.parseHeight('12.5 df'); }).toThrowError('Invalid height');
8183
});
@@ -89,6 +91,8 @@ describe('gridstack utils', function() {
8991
expect(Utils.parseHeight('-12.3vh')).toEqual(jasmine.objectContaining({h: -12.3, unit: 'vh'}));
9092
expect(Utils.parseHeight('-12.3vw')).toEqual(jasmine.objectContaining({h: -12.3, unit: 'vw'}));
9193
expect(Utils.parseHeight('-12.3%')).toEqual(jasmine.objectContaining({h: -12.3, unit: '%'}));
94+
expect(Utils.parseHeight('-12.3cm')).toEqual(jasmine.objectContaining({h: -12.3, unit: 'cm'}));
95+
expect(Utils.parseHeight('-12.3mm')).toEqual(jasmine.objectContaining({h: -12.3, unit: 'mm'}));
9296
expect(Utils.parseHeight('-12.5')).toEqual(jasmine.objectContaining({h: -12.5, unit: 'px'}));
9397
expect(function() { Utils.parseHeight('-12.5 df'); }).toThrowError('Invalid height');
9498
});

src/gridstack.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -811,13 +811,20 @@ 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
814+
// do rem/em/cm/mm to px conversion
815815
if (this.opts.cellHeightUnit === 'rem') {
816816
return (this.opts.cellHeight as number) * parseFloat(getComputedStyle(document.documentElement).fontSize);
817817
}
818818
if (this.opts.cellHeightUnit === 'em') {
819819
return (this.opts.cellHeight as number) * parseFloat(getComputedStyle(this.el).fontSize);
820820
}
821+
if (this.opts.cellHeightUnit === 'cm') {
822+
// 1cm = 96px/2.54. See https://www.w3.org/TR/css-values-3/#absolute-lengths
823+
return (this.opts.cellHeight as number) * (96 / 2.54);
824+
}
825+
if (this.opts.cellHeightUnit === 'mm') {
826+
return (this.opts.cellHeight as number) * (96 / 2.54) / 10;
827+
}
821828
// else get first cell height
822829
let el = this.el.querySelector('.' + this.opts.itemClass) as HTMLElement;
823830
if (el) {

src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ export class Utils {
219219
if (typeof val === 'string') {
220220
if (val === 'auto' || val === '') h = 0;
221221
else {
222-
let match = val.match(/^(-[0-9]+\.[0-9]+|[0-9]*\.[0-9]+|-[0-9]+|[0-9]+)(px|em|rem|vh|vw|%)?$/);
222+
let match = val.match(/^(-[0-9]+\.[0-9]+|[0-9]*\.[0-9]+|-[0-9]+|[0-9]+)(px|em|rem|vh|vw|%|cm|mm)?$/);
223223
if (!match) {
224224
throw new Error(`Invalid height val = ${val}`);
225225
}

0 commit comments

Comments
 (0)