Sub topic 8
The priority menu
This chapter assumes you can write semantic nav markup, use flexbox, and select elements and handle events in JavaScript. It also assumes you know the aria-expanded pattern from the hamburger chapters.
What a priority menu does
A priority menu (often called "Priority+") shows as many links as fit in the available width. Links that do not fit move into a "More" disclosure. The most important links stay visible at every width, so users do not have to open a menu to reach them.
This suits sites with a handful of top-level links, roughly four to eight, where a few matter most. It works on tablets and small laptop windows, and on phones when only two or three links are essential. With many equally important links, a hamburger menu is usually simpler.
How it works
The script measures the list instead of guessing at breakpoints. A fixed breakpoint assumes a label length, a font and a language. Measuring handles translated labels, user font-size settings and links added later.
The routine resets first and then moves links out one at a time. It never tries to work out which links to restore when the nav grows. This keeps the logic short and avoids stale state.
The markup
The links are an ordinary list inside a labelled nav. The "More" control is a native details element, which supplies the toggle button, keyboard support and expanded state without extra ARIA. Put the links in priority order, because the last one is always the first to move.
The complete example
<nav class="priority" aria-label="Main">
<ul class="priority-list">
<li><a href="/products">Products</a></li>
<li><a href="/pricing">Pricing</a></li>
<li><a href="/docs">Docs</a></li>
<li><a href="/blog">Blog</a></li>
<li><a href="/about">About</a></li>
</ul>
<details class="priority-more" hidden>
<summary>More</summary>
<ul class="priority-overflow"></ul>
</details>
</nav>
<style>
.priority { display: flex; align-items: center; }
.priority-list {
display: flex;
flex: 1;
min-width: 0;
overflow: hidden;
white-space: nowrap;
list-style: none;
margin: 0;
padding: 0;
}
.priority-list a, .priority-overflow a { display: block; padding: 0.75rem 1rem; }
.priority-overflow { list-style: none; margin: 0; padding: 0; }
</style>
<script>
const nav = document.querySelector('.priority');
const list = nav.querySelector('.priority-list');
const more = nav.querySelector('.priority-more');
const overflow = nav.querySelector('.priority-overflow');
function update() {
while (overflow.firstChild) list.appendChild(overflow.firstChild);
more.hidden = true;
while (list.scrollWidth > list.clientWidth && list.children.length > 1) {
more.hidden = false;
overflow.prepend(list.lastElementChild);
}
}
new ResizeObserver(update).observe(nav);
update();
</script>Why each CSS rule is there
flex: 1andmin-width: 0let the list shrink below its content width. Withoutmin-width: 0, a flex child refuses to get smaller than its content, so nothing ever overflows and the script has nothing to measure.overflow: hiddenmakesscrollWidthlarger thanclientWidthwhen links do not fit. That difference is the script's overflow test.white-space: nowrapstops links from wrapping onto a second line, which would hide the overflow from the width test.
Why the script is written this way
overflow.prepend(...)keeps the original order inside the More list. Moving the last link and prepending it means the links appear in the same order in both places.list.children.length > 1guarantees that at least one link stays visible, and it stops the loop if a single link is wider than the nav.more.hidden = falseis set before moving a link, so the More button takes its space before the next measurement. Because the list shrinks to make room, the nav's own size does not change, and the observer does not trigger itself in a loop.ResizeObserverreruns the routine on window resizes, device rotation and font-size changes. It also runs once on load because observers fire initially, though the explicitupdate()call makes the first run obvious.
Common errors
- Fonts load late. A web font that arrives after the first measurement changes link widths, and the menu may overflow. Call
update()again whendocument.fonts.readyresolves. - Links removed from the DOM order. Moving links into the
detailslist changes their position in the tab order. This is correct here, because the More control sits after the visible links and its contents follow it, but check it with the keyboard. - More stays open after resizing. If the overflow list becomes empty,
detailsis hidden but may still carry theopenattribute. Remove it insideupdate()when you hide More. - Dropdown pushes content down. An open
detailsexpands in normal flow. Position.priority-morerelatively and.priority-overflowabsolutely if you want a floating panel. - Nothing closes the panel. Native
detailsignores Escape and outside clicks. If your design needs them, add a small handler that removesopenand returns focus to thesummary.
Priority menu versus the other patterns
A hamburger hides every link behind one tap, and a tab bar fixes a short list of destinations at the bottom of the screen. The priority menu is the only one of the three that adjusts continuously to the width, so it fits best where the nav is a horizontal bar at most sizes.
Exercise
A priority menu on your site never shows the More button, even in a very narrow window, and the last links are cut off. Which CSS rule is the most likely cause, and what does the script need to see?
Show answer
The list is probably missing min-width: 0 (or flex: 1), so it cannot shrink below its content width and the nav itself overflows instead. The script compares list.scrollWidth with list.clientWidth, so the list must be narrower than its content, with overflow: hidden set, for the difference to appear.
Exercise
Why does the script move every overflow link back into the main list at the start of each update() call, instead of moving links one at a time as the window grows?
Show answer
Starting from a known state, with all links in the list, means the loop only needs one rule: move the last link out while the list overflows. It needs no record of which links were moved and no extra width measurements for each hidden link.