-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Block Selection Toolbar: Support fixed blocks by flipping toolbar when block goes out of view #46085
Conversation
Open in CodeSandbox Web Editor | VS Code | VS Code Insiders |
Note: the logic here isn't quite right, as it currently alters the behaviour on |
Updated, and simplified the logic a little, so that it doesn't require a scroll handler. It is a bit of a departure from behaviour on |
85a7be5
to
b5c311b
Compare
b5c311b
to
e3b8c9c
Compare
It turns out this appears to be because in #44770 the use of the |
@talldan I've just added you as a review in case you get a chance to look at this, since you previously improved the block selection toolbar logic back in #42887. Also @jasmussen just a heads-up, that I believe this PR (or one like it) will be a pre-requisite to landing sticky position support as in #46142. This PR makes a few changes to attempt to deal with the use case of sticky / fixed position blocks, and the behaviour deviates a bit from what's on |
Thank you for thinking ahead, this is an important piece to get right. It's already partially at play for top-aligned blocks in the site editor, such as a header, and it would be nice to have more of a clear system for when this invokes. Fixed-to-top blocks, for example, feels like an obvious use case for this. Two things about the current implementation, though: I think that in most cases we should not be flipping the position of the toolbar if it's able to start out above the block. Specifically, the act of flipping the toolbar feels a bit jarring, especially for editing text, as shown in this GIF: So in the case where it starts above, I'd keep the "sticky" behavior. Secondly as is slightly evident in the GIF above as well, it feels like the position of the toolbar is a bit more jittery and elastic in this PR compared to trunk, where the toolbar is locked solidly in place even when scrolling: In other words, for any blocks that are normal flow blocks, I think we should keep the current block toolbar behavior. Issues of it covering content, I think we can improve by fading out or hiding the toolbar in more cases. At the moment, it disappears when typing, but it could also fade out when hovering various elements, such as sibling inserters, items in the inspector or toolbar, or otherwise in performing actions that do not require the toolbar. And of course, improvements to the top toolbar behavior will bring additional options. But then for blocks that are fixed, or which start at the top edge of the screen and the block toolbar isn't able to be shown above, I think we can embrace this flipping behavior. Figjam is a good example of how this might work: What do you think? |
Thanks for the comments @talldan and @jasmussen, that's just the kind of feedback and help I was looking for! This points to needing to fine-tune the logic, and add a little more smarts.
Oh, I totally forgot about the widget editor, I'll check it out and see if it too needs a
This might be a good clue as to how to get the logic to work. I don't think we can reliably test whether or not we're currently dealing with a fixed/sticky block, because we could be dealing with a direct child (or nested child) of a fixed/sticky block, however seeing if there's space when the toolbar is initially rendered could be a good way to identify when to switch on the
Good catch, I'll see if I can figure out what's going on there and why the changes here introduce that jitter (and whether it's our code in Gutenberg or a peculiarity of floating UI).
Great feedback! I think overall, it sounds like, "yes, flipping for fixed/sticky blocks is a good idea, but let's make it less greedy and fix the jittery-ness". I'll dig in 🙂 |
Update: the widgets header was a simple fix (turns out it didn't have a background color set for the widgets page, so I've added one in f52091e). The other issues will need a little more digging, so I'll continue exploring through the rest of the week, and play around with the logic. I haven't been able to reliably reproduce the jittery movement when scrolling the page, but it does appear to happen sometimes, and then also sometimes resolves itself when I click away from a block and then back to it. So it's a little challenging to figure out the cause, but I'll do some more digging! |
|
I haven't gotten much further to a solution yet, but I can reliably reproduce it within the first few seconds of selecting a block directly after the block editor loads. After a little while of scrolling the page, it seems to then stick appropriately to the block position for me. I initially wondered if there was a conflict with framer motion's animation of the popover position, but after trying skipping that in the Popover component, it doesn't look like that was the culprit. I'll continue digging in next week! |
I'm wrapping up for the year shortly, but I thought I'd push an alternate PR as another option to this one. I've opened up a separate PR over in #46565 that attempts a slightly simpler approach to see if that would be viable instead. Some dot points:
I'll leave that there for now, but will pick all this up again in the new year. Thanks again for all the feedback! |
Have a good break! |
It turns out the larger changes in this PR are no longer required thanks to an improvement to how the popover component is rendered that landed in #46187. We still need to update the flipping logic, so I've reduced the scope of #46565 to solve just that part of the problem. I'll close out this PR now. |
What?
This PR explores whether we can update the Popover component and the block selection toolbar to support the toolbar displaying appropriately when using fixed or sticky positioned blocks (as in the exploration in #46142). This is achieved by flipping the toolbar to be below the block when a block scrolls out of view, to prevent the block toolbar from obscuring the block.
Why?
When support for sticky or fixed position is added to blocks, the block toolbar is not currently positioned correctly — it continues to scroll off the viewport as if the block were not sticky or fixed. Once the block toolbar is fixed in position, it then needs to be moved so that it does not obscure the fixed/sticky block content.
How?
strategy
in floating UI, which allows the consumer to setfixed
or the defaultabsolute
fixed
strategy, which appears to play better with fixed elements as mentioned in the floating UI docs: https://floating-ui.com/docs/computeposition#strategypadding
onshift
andflip
, and use this value to flip and shift the toolbar, while factoring in the height of the toolbaruseBlockToolbarPopoverProps
to simplify things a little — here, we default to havingflip
always switched on, unless the block fills the viewport — this is a departure fromtrunk
, but as described below, appears to be required in order to support sticky or fixed position blocks. I'm very happy for feedback on this PR, as it is quite a change — unfortunately I couldn't quite work out a way to support sticky/fixed blocks without making this change to toolbar positioning.Testing Instructions
Screenshots or screencast
In the below screenshot, a Group block that is set to "sticky" is selected, and its toolbar is flipped to be displayed below the block. It stays there while the viewport is scrolled. (Note: to test this, you also need #46142 applied)
Ignoring sticky or fixed position for the moment, the main change in this PR is that if there is no room above a block, however there is room below the block, then we flip the controls to display underneath. For blocks that fill the height of the viewport, then we retain the sticky behaviour from before. Overall, this is a change from the behaviour on
trunk
— I couldn't quite work out a way to support sticky/fixed blocks without this change, particularly since we also need to support the children of those blocks, so we can't directly inspect if the current block is sticky or fixed in order to enable the behaviour. Here are a couple more screenshots of the behaviour in this PR:Screengrab (note that toolbar flips to be below the block when scrolling past the edge of the viewport, so that it doesn't obscure the currently selected block):
block-selection-toolbar-position.mp4