Skip to content
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

Update column width calculation logic #3747

Open
wants to merge 40 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 93.33333% with 4 lines in your changes missing coverage. Please review.

Project coverage is 98.63%. Comparing base (58b38d3) to head (45bbadd).

Files with missing lines Patch % Lines
src/hooks/useColumnWidths.ts 92.98% 4 Missing ⚠️
Additional details and impacted files
@@                    Coverage Diff                     @@
##           am-double-double-click    #3747      +/-   ##
==========================================================
- Coverage                   98.75%   98.63%   -0.12%     
==========================================================
  Files                          47       47              
  Lines                        3445     3446       +1     
  Branches                      751      751              
==========================================================
- Hits                         3402     3399       -3     
- Misses                         43       47       +4     
Files with missing lines Coverage Δ
src/HeaderCell.tsx 97.15% <100.00%> (-0.03%) ⬇️
src/HeaderRow.tsx 100.00% <ø> (ø)
src/hooks/useCalculatedColumns.ts 100.00% <100.00%> (ø)
src/types.ts 100.00% <ø> (ø)
src/utils/index.ts 90.47% <ø> (-3.08%) ⬇️
src/hooks/useColumnWidths.ts 96.09% <92.98%> (-3.03%) ⬇️
🚀 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.

// ignore maxWidth if it less than minWidth
if (typeof maxWidth === 'number' && maxWidth >= minWidth) {
return min(width, maxWidth);
// clamp() and max() do not handle all the css grid column width values
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried calc-size but it does not work unfortunately
https://developer.mozilla.org/en-US/docs/Web/CSS/calc-size

@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
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.

2 participants