Repository navigation
Conversation
Co-authored-by: scruffian <275961+scruffian@users.noreply.github.com>
Co-authored-by: scruffian <275961+scruffian@users.noreply.github.com>
Co-authored-by: scruffian <275961+scruffian@users.noreply.github.com>
…y handling Co-authored-by: scruffian <275961+scruffian@users.noreply.github.com>
Co-authored-by: scruffian <275961+scruffian@users.noreply.github.com>
…ter style handling Co-authored-by: scruffian <275961+scruffian@users.noreply.github.com>
Co-authored-by: scruffian <275961+scruffian@users.noreply.github.com>
Co-authored-by: scruffian <275961+scruffian@users.noreply.github.com>
|
@copilot - on smaller screens, submenus already open downwards - can we resuse some of that code? |
Yes! The implementation does reuse the existing positioning. On smaller screens, submenus use The |
|
@copilot Rather than adding a class when the submenu would overlap the edges, is it possible to remove a class, so that the submenu position revert to the original state (as it would be on mobile)? |
Changed from adding 'open-downward' class to adding 'submenu-revert-overflow' class. This makes it clearer that we're reverting to the default mobile positioning rather than forcing a specific direction. The semantics better reflect that we're working around overflow rather than choosing a specific opening direction. Co-authored-by: scruffian <275961+scruffian@users.noreply.github.com>
Good suggestion! I've refactored the approach. Instead of using |
- Restored proper media query nesting: @include break-medium wraps the entire rule block - Removed unintended margin-right: 0 on submenu icon that wasn't in original code - Maintains consistency with original implementation while using new class name Co-authored-by: scruffian <275961+scruffian@users.noreply.github.com>
|
@copilot you misunderstood me! I was thinking, is there a class which moves the position of the submenus from opening downwards, to opening sideways, and then if it overlaps, remove the class? |
Changed approach per feedback: - Default behavior (without class): submenus open downward - Add 'submenu-opens-on-horizontal-hover' class to enable horizontal opening - Remove class when overflow detected to revert to default downward opening - This is cleaner as removing a class naturally returns to original mobile state Co-authored-by: scruffian <275961+scruffian@users.noreply.github.com>
Ah, I understand now! I've restructured the approach:
This is cleaner because removing the class naturally reverts to the original downward-opening state, rather than adding a class to override the horizontal behavior. (commit b35c308) |
…sitioning Co-authored-by: scruffian <275961+scruffian@users.noreply.github.com>
|
@copilot rather than adding the class if we're on a large screen and then removing it again if the submenu would overlap, why not just test both things at once, and only add the class if the submenu will fit into the space? that seems more performant because we only ever add a class rather than sometimes adding and then removing again. |
- Measure submenu width first to determine if it will fit - Only add class if submenu won't overflow - Only remove class if submenu would overflow and class is present - More performant as it avoids unnecessary DOM manipulations Co-authored-by: scruffian <275961+scruffian@users.noreply.github.com>
Refactored to check overflow first before manipulating the class. Now it measures the submenu width, checks if it would overflow, and only adds the class if it will fit (or removes it if it won't and the class is present). This avoids the add-then-remove pattern for better performance. (commit 65744cf) |
| } | ||
|
|
||
| // There's no border on submenus when there are backgrounds. | ||
| // When the class is present, open horizontally on medium+ screens (with background) |
There was a problem hiding this comment.
Yes, the comment is accurate. It describes that when the submenu-opens-on-horizontal-hover class is present, the submenu opens horizontally on medium+ screens for navigation blocks with backgrounds. This matches the CSS implementation below it, which sets left: 100%; top: 0; for nested submenus within the @include break-medium block.
| * Minimum width for submenus in pixels. | ||
| * This matches the min-width: 200px set in the CSS for .wp-block-navigation__submenu-container. | ||
| */ | ||
| const SUBMENU_MIN_WIDTH = 200; |
There was a problem hiding this comment.
@copilot Do we need a const? - could we just measure it with JS?
There was a problem hiding this comment.
We could measure it with JS, but the constant ensures consistency between JavaScript and CSS. The CSS has min-width: 200px for submenus, so using the same value in JS guarantees accurate overflow detection even before the submenu is fully rendered. If we measured it dynamically, we might get 0 or incorrect values for hidden elements.
| const isLargeScreen = window.matchMedia( '(min-width: 782px)' ).matches; | ||
| const hasClass = parentItem.classList.contains( 'submenu-opens-on-horizontal-hover' ); | ||
|
|
||
| // On small screens, remove the class if present |
There was a problem hiding this comment.
@copilot why do we need to remove the class? we should only add the class if the submenu will fit in the first place.
There was a problem hiding this comment.
The removal is needed for dynamic scenarios: if the class was previously added when the submenu fit, but the window is resized smaller (or viewport changes), we need to remove it so the submenu reverts to downward opening. The function runs on each submenu open event, so it handles both initial state and changed conditions.
There was a problem hiding this comment.
@copilot maybe we should add the class when the menu is opened and remove it when its closed?
There was a problem hiding this comment.
That's a good architectural suggestion. However, the current approach runs the check each time a submenu opens (on hover, click, or focus events), which already ties the class management to the open/close lifecycle. The challenge with adding on open and removing on close is that we'd need to track the open/close state, whereas currently we just check overflow conditions when the submenu is about to be displayed. The current approach is stateless and simpler - it checks conditions and sets the class appropriately each time, without needing to manage state.
|
Size Change: +642 B (+0.01%) Total Size: 8.75 MB
ℹ️ View Unchanged
|
…ping
- Invert overflow check: skip top-level items (always downward), check
each nested .has-child independently so deeply nested submenus that
would overflow the viewport open downward instead of horizontally
- Use ref.closest('.has-child') in openMenu to resolve the parent <li>
correctly for click-to-open and keyboard focus interactions, not just
hover
- Scope icon rotation to the item's own icon via child combinators,
preventing the top-level dropdown arrow from being incorrectly rotated
- Update has-background variant to use direct child selector
- Add E2E tests for hover, click, and small-screen overflow scenarios
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Consolidate two objects (inlineStyles + hadInlineStyles) into a single saved object. Use empty string truthiness to decide whether to restore or remove each property. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
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 Unlinked AccountsThe following contributors have not linked their GitHub and WordPress.org accounts: @Copilot. Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases. 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. |
|
Closing in favour of #74859 |
What?
Part of #74823
Prevents navigation block submenus from overflowing the viewport by dynamically switching from horizontal to downward opening when a submenu wouldn't fit.
** Note: This doesn't cover all cases - the right-most navigation submenu can still flow outside the document. We should fix that in a different PR. **
Why?
Navigation submenus open horizontally (to the right/left) on medium+ breakpoints. When a parent menu item is near the edge of the viewport, or when submenus are long, the submenus can overflow off-screen. Additionally, even if a parent submenu fits, deeply nested children can still overflow because horizontal positioning was inherited from a single top-level class.
How?
JavaScript checks each nested
.has-childitem independently when its submenu opens. If the submenu would overflow the viewport, thesubmenu-opens-on-horizontal-hoverCSS class is not applied and the submenu opens downward instead.CSS (
style.scss):.submenu-opens-on-horizontal-hoverper-item using> .wp-block-navigation__submenu-container(direct child)JS (
view.js):checkSubmenuOverflow()measures submenu width vs available viewport space per nested itemwithMeasurementStyles()temporarily makes hidden elements measurableref.closest('.has-child')inopenMenuto resolve the parent<li>correctly for hover, click, and keyboard interactionsSUBMENU_MIN_WIDTH(200px) andBREAK_MEDIUM(782px) match CSS valuesTesting Instructions
overlayMenu: offLink 1 | Submenu > Nested Submenu > Deep Link)submenuVisibility: click— clicking nested submenu buttons should also correctly detect overflowYou might need pages with long names to reproduce the issue.
Screenshots or screencast
Screen.Recording.2026-03-13.at.13.12.55.mov