Skip to content

Commit 4789821

Browse files
committed
Use CSS.supports
1 parent c90685c commit 4789821

1 file changed

Lines changed: 25 additions & 20 deletions

File tree

src/utils/index.ts

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,36 +20,37 @@ export function assertIsValidKeyGetter<R, K extends React.Key>(
2020
export function getColumnWidthForMeasurement<R, SR>(
2121
width: number | string,
2222
{ minWidth, maxWidth }: CalculatedColumn<R, SR>
23-
): string {
24-
if (typeof width === 'number') {
25-
if (maxWidth != null) {
26-
return `clamp(${minWidth}px, ${width}px, ${maxWidth}px)`;
27-
}
28-
return `max(${minWidth}px, ${width}px)`;
23+
) {
24+
if (width === 'max-content' || width === 'min-content') {
25+
// TODO: how to handle minWidth and maxWidth?
26+
return width;
2927
}
3028

31-
const useMinMax = !width.includes('minmax') && !width.includes('fit-content');
29+
const widthWithUnit = typeof width === 'number' ? `${width}px` : width;
30+
const hasMaxWidth = maxWidth != null;
31+
const clampedWidth = hasMaxWidth
32+
? `clamp(${minWidth}px, ${widthWithUnit}, ${maxWidth}px)`
33+
: `max(${minWidth}px, ${widthWithUnit})`;
34+
35+
// clamp() and max() do not handle all the css grid column width values
36+
if (isValidCSSGridColumnWidth(clampedWidth)) {
37+
return clampedWidth;
38+
}
3239

3340
if (
34-
maxWidth != null &&
41+
hasMaxWidth &&
3542
// ignore maxWidth if it less than minWidth
3643
maxWidth >= minWidth
3744
) {
38-
// TODO: how to clamp width in CSS grid?
39-
if (width === 'max-content') {
40-
return width;
41-
}
42-
43-
if (useMinMax) {
44-
return `minmax(${width}, ${maxWidth}px)`;
45+
// We are setting maxWidth on the measuring cell but the browser only applies
46+
// it after all the widths are calculated. This results in left over space in some cases.
47+
const minMaxWidth = `minmax(${widthWithUnit}, ${maxWidth}px)`;
48+
if (isValidCSSGridColumnWidth(minMaxWidth)) {
49+
return minMaxWidth;
4550
}
4651
}
4752

48-
if (useMinMax) {
49-
return `minmax(${minWidth}px, ${width})`;
50-
}
51-
52-
return width;
53+
return isValidCSSGridColumnWidth(widthWithUnit) ? widthWithUnit : 'auto';
5354
}
5455

5556
export function getHeaderCellRowSpan<R, SR>(
@@ -58,3 +59,7 @@ export function getHeaderCellRowSpan<R, SR>(
5859
) {
5960
return column.parent === undefined ? rowIdx : column.level - column.parent.level;
6061
}
62+
63+
function isValidCSSGridColumnWidth(width: string) {
64+
return CSS.supports('grid-template-columns', width);
65+
}

0 commit comments

Comments
 (0)