question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[NavigationMenu] Expose position of active <NavigationMenu.Item />

See original GitHub issue

Feature request

Can we expose the position of the active <NavigationMenu.Item />, and maybe it’s bounding box so we can position the <NavigationMenu.Viewport /> directly below the active item?

It could be exposed through CSS custom properties as --radix-navigation-menu-indicator-position, which should work for both horizontal and vertical menu’s.

Even better would be a hook or render prop so we can wait until the indicator knows its position before we show the <NavigationMenu.Viewport />

Overview

The NavigationMenu works really well if your foldout content is wider than your list of navigation menu items.

Screenshot_10_06_2022__13_02

However, if you add (much) more NavigationMenu.Item components the layout will start to look awkward.

image

Example on codesandbox: https://02xv7f.csb.app/

Who does this impact? Who is this for?

Everyone who wants to use NavigationMenu with a wide list of items.

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:4
  • Comments:6

github_iconTop GitHub Comments

3reactions
andy-hookcommented, Sep 29, 2022

Would you mind explaining why you set the list ref with setState rather than useRef in your example?

@elisehein It ensures the ref callbacks are updated with the list node when it’s available, there are likely more efficient ways to build this but I wanted to provide a quick example to show that this is possible in user code with some additional measurement.

Another approach might be to get and sync the currently active trigger via effect and update the positioning accordingly:

https://codesandbox.io/s/navigation-menu-track-position-effect-sync-q20tjd?file=/src/App.js

It depends on your design and the types of animation effects you’re trying to achieve, this example is dependent on central alignment so a different approach or more generic implementation might be necessary depending on the need.

It also doesn’t account for responsive changes in element dimensions, for that you’d need to observe for element resize etc.

Hope this helps!

2reactions
andy-hookcommented, Jun 14, 2022

Hey @janhoogeveen , I agree this is a bit of a pain point at the moment, providing some way to track the content against the active trigger would be very useful for folks 👍

I realize another solution is to wrap <NavigationMenu.Viewport /> in <NavigationMenu.Indicator /> but I’m not sure if that’s frowned upon?

As you noted, the indicator wasn’t designed for this purpose. In addition to the visual glitch there is also an accessibility problem as aria-hidden is set on the indicator part by default so it would effectively hide all menu content from screen readers.

Thanks for raising, I’m going to mark it for improvement. In the meantime it is possible to implement your own tracking logic fairly easily depending on your design requirements, here is a (really) rough example of how it could work with a centrally aligned navigation:

https://codesandbox.io/s/navigation-menu-track-position-fhh9so

Read more comments on GitHub >

github_iconTop Results From Across the Web

Navigation Menu - Radix UI
Optional active item indicator. Full keyboard navigation. Exposes CSS variables for advanced animation.
Read more >
Navigation Menu Button Example | APG | WAI - W3C
This example demonstrates the menu button design pattern for a button that displays a menu of link targets. The menu items are made...
Read more >
NavigationMenu | Metadata API Developer Guide
Represents the navigation menu in an Experience Builder site. A navigation menu consists of items that users can click to go to other...
Read more >
community - Specify the Navigation Menu when extending the ...
I have answered with a solution to a similar question with the same goal here: https://salesforce.stackexchange.com/a/289509/77440.
Read more >
Navigation Menu in WordPress and How to Create It - Gutenix
After you complete editing in a page builder, your last step would be to replace the default menu of your current theme with...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found