Skip to content

Update column width calculation logic #3747

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 46 commits into
base: main
Choose a base branch
from

Conversation

amanmahajan7
Copy link
Contributor

@amanmahajan7 amanmahajan7 commented Mar 21, 2025

#3746 (comment)

I have removed clampColumnWidth helper and now the browser handle the minWidth/maxWidth using the following logic

  • Use clamp/max when possible
  • Use minmax when possible
  • Fallback to minWidth/maxWidth on the measuring cell

@amanmahajan7 amanmahajan7 self-assigned this Mar 21, 2025
Copy link

codecov bot commented Mar 21, 2025

Codecov Report

Attention: Patch coverage is 90.00000% with 4 lines in your changes missing coverage. Please review.

Project coverage is 98.69%. Comparing base (56a420e) to head (96887bb).

Files with missing lines Patch % Lines
src/hooks/useColumnWidths.ts 89.18% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3747      +/-   ##
==========================================
- Coverage   98.80%   98.69%   -0.12%     
==========================================
  Files          47       47              
  Lines        3424     3436      +12     
  Branches      743      750       +7     
==========================================
+ Hits         3383     3391       +8     
- Misses         41       45       +4     
Files with missing lines Coverage Δ
src/HeaderCell.tsx 97.23% <100.00%> (-0.03%) ⬇️
src/hooks/useCalculatedColumns.ts 100.00% <100.00%> (ø)
src/utils/index.ts 90.47% <ø> (-3.08%) ⬇️
src/hooks/useColumnWidths.ts 96.32% <89.18%> (-2.77%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@amanmahajan7 amanmahajan7 marked this pull request as ready for review March 24, 2025 14:49
@amanmahajan7 amanmahajan7 requested a review from nstepien as a code owner March 24, 2025 14:49
@amanmahajan7 amanmahajan7 changed the title Refactor column width calculation logic Update column width calculation logic Mar 24, 2025

export function clampColumnWidth<R, SR>(
width: number,
export function getColumnWidthForMeasurement<R, SR>(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that this function is not needed at all if we pass min-width and max-width to columns styles; then setting any width will not break the grid layout

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is needed to fix this bug. We already set min/max width on the measuring cell

@amanmahajan7 amanmahajan7 marked this pull request as draft April 3, 2025 13:38
@@ -177,9 +177,7 @@ export function useCalculatedColumns<R, SR>({
for (const column of columns) {
let width = getColumnWidth(column);

if (typeof width === 'number') {
width = clampColumnWidth(width, column);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is possible that a column has a width less than minWidth or greater than maxWidth so we may have more or less viewport columns initially but they will be updated once the actual width is measured. I think this scenario is unlikely so I removed clampColumnWidth. getColumnWidthForMeasurement handles it using css clamp function now. I am okay with adding it back if you prefer

prevGridWidthRef.current = gridWidth;
updateMeasuredAndResizedWidths();
});
useLayoutEffect(updateMeasuredAndResizedWidths);
Copy link
Contributor Author

@amanmahajan7 amanmahajan7 Apr 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cannot inline this function as exhaustive deps rule complains. I can add useMemo but not sure it is worth

@amanmahajan7 amanmahajan7 marked this pull request as ready for review April 3, 2025 17:08
Base automatically changed from am-double-double-click to main April 8, 2025 21:02
@amanmahajan7 amanmahajan7 marked this pull request as draft April 9, 2025 14:24
@amanmahajan7 amanmahajan7 marked this pull request as ready for review April 9, 2025 18:59
return widthWithUnit;
}

const hasMaxWidth = maxWidth != null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to handle maxWidth here if we set it on measuring cells?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not 100% sure. My understanding is that grid computes the column widths and then apply min/max style from the measuring cell. This is the reason there was some left over space in the bug you found. I will check again

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, resizing does not work properly without this as clampColumnWidth helper was removed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found another edge case.

nstepien
nstepien previously approved these changes Apr 10, 2025
@amanmahajan7 amanmahajan7 marked this pull request as draft April 10, 2025 22:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants