Skip to content

Commit 833b52d

Browse files
committed
Move function
1 parent 2522c3d commit 833b52d

File tree

2 files changed

+45
-45
lines changed

2 files changed

+45
-45
lines changed

src/hooks/useColumnWidths.ts

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { useLayoutEffect, useState } from 'react';
22

3-
import { getColumnWidthForMeasurement } from '../utils';
43
import type { CalculatedColumn, ResizedWidth, StateSetter } from '../types';
54
import type { DataGridProps } from '../DataGrid';
65

@@ -130,3 +129,47 @@ function measureColumnWidth(gridRef: React.RefObject<HTMLDivElement | null>, key
130129
const measuringCell = gridRef.current?.querySelector(selector);
131130
return measuringCell?.getBoundingClientRect().width;
132131
}
132+
133+
function getColumnWidthForMeasurement<R, SR>(
134+
width: number | string,
135+
{ minWidth, maxWidth }: CalculatedColumn<R, SR>
136+
) {
137+
const widthWithUnit = typeof width === 'number' ? `${width}px` : width;
138+
139+
// don't break in Node.js (SSR) and jsdom
140+
if (typeof CSS === 'undefined') {
141+
return widthWithUnit;
142+
}
143+
144+
const hasMaxWidth = maxWidth != null;
145+
const clampedWidth = hasMaxWidth
146+
? `clamp(${minWidth}px, ${widthWithUnit}, ${maxWidth}px)`
147+
: `max(${minWidth}px, ${widthWithUnit})`;
148+
149+
// clamp() and max() do not handle all the css grid column width values
150+
if (isValidCSSGridColumnWidth(clampedWidth)) {
151+
return clampedWidth;
152+
}
153+
154+
if (
155+
hasMaxWidth &&
156+
// ignore maxWidth if it less than minWidth
157+
maxWidth >= minWidth &&
158+
// we do not want to use minmax with max-content as it
159+
// can result in width being larger than max-content
160+
widthWithUnit !== 'max-content'
161+
) {
162+
// We are setting maxWidth on the measuring cell but the browser only applies
163+
// it after all the widths are calculated. This results in left over space in some cases.
164+
const minMaxWidth = `minmax(${widthWithUnit}, ${maxWidth}px)`;
165+
if (isValidCSSGridColumnWidth(minMaxWidth)) {
166+
return minMaxWidth;
167+
}
168+
}
169+
170+
return isValidCSSGridColumnWidth(widthWithUnit) ? widthWithUnit : 'auto';
171+
}
172+
173+
function isValidCSSGridColumnWidth(width: string) {
174+
return CSS.supports('grid-template-columns', width);
175+
}

src/utils/index.ts

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { CalculatedColumn, CalculatedColumnOrColumnGroup, Maybe } from '../types';
1+
import type { CalculatedColumnOrColumnGroup, Maybe } from '../types';
22

33
export * from './colSpanUtils';
44
export * from './domUtils';
@@ -17,53 +17,10 @@ export function assertIsValidKeyGetter<R, K extends React.Key>(
1717
throw new Error('Please specify the rowKeyGetter prop to use selection');
1818
}
1919
}
20-
export function getColumnWidthForMeasurement<R, SR>(
21-
width: number | string,
22-
{ minWidth, maxWidth }: CalculatedColumn<R, SR>
23-
) {
24-
const widthWithUnit = typeof width === 'number' ? `${width}px` : width;
25-
26-
// don't break in Node.js (SSR) and jsdom
27-
if (typeof CSS === 'undefined') {
28-
return widthWithUnit;
29-
}
30-
31-
const hasMaxWidth = maxWidth != null;
32-
const clampedWidth = hasMaxWidth
33-
? `clamp(${minWidth}px, ${widthWithUnit}, ${maxWidth}px)`
34-
: `max(${minWidth}px, ${widthWithUnit})`;
35-
36-
// clamp() and max() do not handle all the css grid column width values
37-
if (isValidCSSGridColumnWidth(clampedWidth)) {
38-
return clampedWidth;
39-
}
40-
41-
if (
42-
hasMaxWidth &&
43-
// ignore maxWidth if it less than minWidth
44-
maxWidth >= minWidth &&
45-
// we do not want to use minmax with max-content as it
46-
// can result in width being larger than max-content
47-
widthWithUnit !== 'max-content'
48-
) {
49-
// We are setting maxWidth on the measuring cell but the browser only applies
50-
// it after all the widths are calculated. This results in left over space in some cases.
51-
const minMaxWidth = `minmax(${widthWithUnit}, ${maxWidth}px)`;
52-
if (isValidCSSGridColumnWidth(minMaxWidth)) {
53-
return minMaxWidth;
54-
}
55-
}
56-
57-
return isValidCSSGridColumnWidth(widthWithUnit) ? widthWithUnit : 'auto';
58-
}
5920

6021
export function getHeaderCellRowSpan<R, SR>(
6122
column: CalculatedColumnOrColumnGroup<R, SR>,
6223
rowIdx: number
6324
) {
6425
return column.parent === undefined ? rowIdx : column.level - column.parent.level;
6526
}
66-
67-
function isValidCSSGridColumnWidth(width: string) {
68-
return CSS.supports('grid-template-columns', width);
69-
}

0 commit comments

Comments
 (0)