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

fix(masthead): prevent megamenus from overlaying global banner #11779

Conversation

jkaeser
Copy link
Member

@jkaeser jkaeser commented May 10, 2024

Note: This PR currently relies on changes from #11773, which is why there are extra commits. We'll want to hold off on merging this until that PR has been merged.

Related Ticket(s)

Closes #11775

Description

Currently, <c4d-megamenu> are slotted into a <ul> that is position: fixed;. This is what allows them to extend the full width of the viewport on desktop, but it also means they cannot align themselves relative to other elements using CSS alone. At this stage in the jenga tower, there's not a great CSS-only fix for our issue.

So we turn to our good friend JavaScript for assistance. <c4d-top-nav-menu> items now report the position of their bottom edge via a --c4d-masthead-top-nav-menu-bottom-edge CSS custom property when toggled open. <c4d-megamenu> elements reference this prop to position themselves in the exact right place when they get loaded into the DOM.

Changelog

New

  • Makes <c4d-top-nav-menu> items expose the position of their bottom edge via a --c4d-masthead-top-nav-menu-bottom-edge CSS custom property.

Changed

  • Updates <c4d-megamenu> CSS to reference --c4d-masthead-top-nav-menu-bottom-edge value to position themselves correctly when they load into the DOM.

…e is also a ToC (carbon-design-system#11734)

none

Given the page uses both an L1 menu and a table of contents (ToC),
and the page is displaying at a mobile breakpoint,
and the ToC menu is the sticky element (i.e. stuck to the top of the viewport),
and the user scrolls up slightly to reveal the L1 dropdown,
and the user open the L1's dropdown menu,
then the dropdown menu gets hidden above the viewport, forcing the user to scroll up more to access it.
Instead, the top of L1 dropdown menu should be visible when opened.

This problem is occurring because the dropdown exists in the document flow, and the StickyHeader utility doesn't reset stuck elements' positions when the menu is opened.

The easiest solution I found was to position the dropdown menu absolutely like most other dropdown menus you might encounter. This bypasses the tricky problem of trying to get the StickyHeader to recalculate, and in my opinion is a slight enhancement in both performance (avoids layout shifts and associated repaint costs) and overall UX (gives dropdown a more expected behavior).

**Changed**

- Prevents mobile L1 dropdown menu from becoming hidden above viewport when opened while there is currently a sticky table of contents menu.
- Prevents L1 from scrolling away when a submenu is open on desktop or the dropdown is open on mobile.
- Consolidates StickyHeader utility tracked data to make debugging easier.
- Refactors StickyHeader logic to support all potentially sticky elements being on page at the same time.

To test the main fix:
1. Visit [the Dotcom Shell > With L1 story](https://ibmdotcom-webcomponents.s3.us-east.cloud-object-storage.appdomain.cloud/deploy-previews/11734/index.html?path=/story/components-dotcom-shell--with-l-1) in the deploy preview.
2. Change the preview size to "Large mobile" or "Small mobile"
3. Scroll down the page until the ToC is stuck to the top of the viewport.
4. Scroll back up just enough to see the L1.
5. Open the L1 and verify the top of it doesn't jump back up above the viewport.
6. Scroll down and verify the L1 stays stuck to the top of the viewport.
7. Close the L1 and verify the ToC gets stuck to the top of the viewport as you scroll down.

You should also test the L1 remains stuck when open at desktop widths.

This PR also changes a good amount of the StickyHeader class logic. To support regression testing the existing sticky element behaviors, I've added the [Sticky Element Sandbox story](https://ibmdotcom-webcomponents.s3.us-east.cloud-object-storage.appdomain.cloud/deploy-previews/11734/index.html?path=/story/components-dotcom-shell--sticky-element-sandbox). Test various knob configurations at both desktop and mobile viewport sizes and let me know how you were able to break it 😄

**Note:** If you find the sticky elements aren't working correctly when switching between stories, try reloading the window.
@jkaeser jkaeser added package: styles Work necessary for the Carbon for IBM.com styles package package: web components Work necessary for the IBM.com Library web components package owner: Innovation Team used when the engineering work will be done by Hybrid Cloud with DDS engineers as consultants v2 DO NOT MERGE labels May 10, 2024
@ibmdotcom-bot
Copy link
Contributor

ibmdotcom-bot commented May 10, 2024

@ibmdotcom-bot
Copy link
Contributor

ibmdotcom-bot commented May 10, 2024

@ibmdotcom-bot
Copy link
Contributor

ibmdotcom-bot commented May 10, 2024

@ibmdotcom-bot
Copy link
Contributor

ibmdotcom-bot commented May 10, 2024

@jkaeser jkaeser force-pushed the fix/masthead-megamenu-banner-position branch from 4fbb429 to 0050368 Compare May 10, 2024 13:43
@jkaeser jkaeser force-pushed the fix/masthead-megamenu-banner-position branch from b9357fb to a80ea5c Compare May 14, 2024 15:25
@jkaeser
Copy link
Member Author

jkaeser commented May 28, 2024

Closing in favor of #11837

@jkaeser jkaeser closed this May 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
DO NOT MERGE owner: Innovation Team used when the engineering work will be done by Hybrid Cloud with DDS engineers as consultants package: styles Work necessary for the Carbon for IBM.com styles package package: web components Work necessary for the IBM.com Library web components package v2
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Masthead]: Megamenu positioned incorrectly when global banner is present
2 participants