-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Grid interactivity: Improve how grid resizer handles 0-width and 0-height cells #61423
Conversation
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Size Change: +37 B (0%) Total Size: 1.74 MB
ℹ️ View Unchanged
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice subtle fix, and thanks for the explanation about why this is the preferred approach 👍
Tests well for me, with the ability to now drag close to, but not quite onto the empty row, for it to then snap to the row:
2024-05-08.10.22.12.mp4
Just left a question about whether it'd be useful to add unit tests at this stage to cover the logic, but feel free to ignore it aids the iterative development to not lock things in place just yet.
LGTM!
function getGridLines( template, gap ) { | ||
const lines = [ 0 ]; | ||
function getGridTracks( template, gap ) { | ||
const tracks = []; | ||
for ( const size of template.split( ' ' ) ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not to worry about for this PR, but just wondering if we'll ever need to handle templates with more complex values for each of the columns or rows? I.e. if a function is used like minmax
then the space character would be between part of a function value instead of between each column value. We don't yet support complex values, so I don't think there's anything we need to do to handle it just yet, but thought I'd mention it in case we need a safe escape point / bail early if the values aren't what we expect.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That won't be a problem as long as the values we pass to this function come from getComputedCSS
, because computed values will all be in px
. I guess it might be good to make it clear that that's what this function expects 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, excellent. Thanks for explaining!
( closest, line, index ) => | ||
Math.abs( line - position ) < | ||
Math.abs( lines[ closest ] - position ) | ||
function getClosestTrack( tracks, position, edge = 'start' ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it worth adding unit tests for these two functions? Being unfamiliar with this code it took me a little while to wrap my head around what it's doing (grabbing a computed CSS value, splitting it into parts, and then providing values for distances to closest edge). I think I'd struggle a little bit attempting to debug or tweak the logic if I had to, so wondered if a couple of tests might help document the expected behaviour.
Of course, I understand this is still experimental and the feature is in flux, so no worries if you think it's too early to lock the behaviour in place!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unit tests would be good at some point but I think what would also be good for clarity would be a docblock. Too many of our internal functions aren't documented at all, and even just a line describing what it does, parameters and return values can be super helpful!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, a docblock comment would be great! Especially while you're still moving things around, I reckon a little documentation is probably quicker and easier than unit tests at this point 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree with @andrewserong on the docs/future tests; otherwise this LGTM!
a49c929
to
0a0a87c
Compare
What?
Small fix that I've split out of #59490.
Improves the grid item resizer's handling of empty 0-width or 0-height cells.
Why?
It's basically impossible in
trunk
to resize a grid item using the drag handles to span an empty row. This PR improves the logic of how resizing is calculated to fix this.How?
In
trunk
we're using grid lines to calculate the span. A grid line is where the cell starts. The problem with this approach is that you need to drag the resize box to be close to the start of the next cell in order to span a cell. This is fine when the cell is a normal size but is impossible when it has no width or height.So instead of calculating the new span using grid lines which is just where a cell starts, use grid tracks which contains where a cell starts and where it ends. We can then calculate the span by looking for the nearest end track and subtracting the nearest start track. This correctly handles the case where a cell has no width or height.
It's also just more logically correct 😊
Testing Instructions
Screenshots or screencast
Before:
Kapture.2024-05-07.at.16.08.07.mp4
After:
Kapture.2024-05-07.at.16.08.43.mp4