Skip to main contentSkip to docs navigation

Chassis

Nav overflow

View on GitHub

Collapse the nav items that do not fit their container into a "More" menu, following the Priority+ pattern.

@layer components
Requires JavaScript
Depends onNav, Menu

Introduction

Nav overflow, also known as the Priority+ pattern, detects the items of a nav that do not fit within their container and moves them into a menu. One set of markup then serves every container width.

How it works

The plugin measures a wrapper around the nav and decides which items fit:

  • The .nav-overflow wrapper is the element the plugin measures. Collapsing items changes the width of the nav, so measuring the nav itself would feed the result back in as the input.
  • The plugin responds to the size of the wrapper, not of the viewport. It watches the wrapper with a ResizeObserver, so it also works in sidebars, embedded contexts, and resizable containers.
  • The links of the items that overflow are cloned into a "More" menu, and the original items are hidden.
  • Every nav style is supported: default, tabs, segments, and underline.
  • Active and disabled states are preserved in the overflow menu.

Chassis CSS respects user accessibility preferences by automatically disabling animations when the prefers-reduced-motion media query is detected. See the reduced motion guidelines in the accessibility documentation for implementation details.

Basic structure

Wrap the .nav in a .nav-overflow element and add data-cx-toggle="nav-overflow" to that wrapper. Items that do not fit move to a "More" menu at the end of the nav. Resize the example below to preview items moving in and out of the menu as the space changes.

HTML
<div class="nav-overflow" data-cx-toggle="nav-overflow">
  <ul class="nav nav-segments">
    <li class="nav-item">
      <a class="nav-link active" aria-current="page" href="#">Home</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">Dashboard</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">Products</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">Services</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">Analytics</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">Reports</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">Settings</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">Help</a>
    </li>
  </ul>
</div>

The wrapper works with the variant classes of the nav, which keep their styling while items collapse.

Tabs

Add .nav-tabs to the nav to collapse tabbed navigation.

HTML
<div class="nav-overflow" data-cx-toggle="nav-overflow">
  <ul class="nav nav-tabs">
    <li class="nav-item">
      <a class="nav-link active" aria-current="page" href="#">Overview</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">Details</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">History</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">Activity</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">Comments</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">Attachments</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">Related</a>
    </li>
  </ul>
</div>

Underline

Add .nav-underline to the nav to collapse underlined navigation.

HTML
<div class="nav-overflow" data-cx-toggle="nav-overflow">
  <ul class="nav nav-underline">
    <li class="nav-item">
      <a class="nav-link active" aria-current="page" href="#">Home</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">Features</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">Pricing</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">FAQs</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">About</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">Contact</a>
    </li>
  </ul>
</div>

Keep items visible

Add .nav-overflow-keep to the items that must never move to the overflow menu. These items stay visible regardless of the available space, which suits high-priority items such as "Home" or action buttons.

HTML
<div class="nav-overflow" data-cx-toggle="nav-overflow">
  <ul class="nav nav-segments">
    <li class="nav-item nav-overflow-keep">
      <a class="nav-link active" aria-current="page" href="#">Home</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">Products</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">Services</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">About</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">Blog</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">Careers</a>
    </li>
    <li class="nav-item nav-overflow-keep">
      <a class="nav-link" href="#">Contact</a>
    </li>
  </ul>
</div>

Disabled items

A disabled link keeps its disabled state when its item moves to the overflow menu.

HTML
<div class="nav-overflow" data-cx-toggle="nav-overflow">
  <ul class="nav nav-segments">
    <li class="nav-item">
      <a class="nav-link active" aria-current="page" href="#">Active</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">Link</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">Another link</a>
    </li>
    <li class="nav-item">
      <a class="nav-link disabled" aria-disabled="true">Disabled</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">More content</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">Even more</a>
    </li>
  </ul>
</div>

In a navbar

Nav overflow also works inside a navbar, for horizontal navigation that adapts to the available space.

HTML
<nav class="navbar navbar-expand bg-body-tertiary" aria-label="Main navigation">
  <div class="container fluid">
    <a class="navbar-brand" href="#">Brand</a>
    <div class="nav-overflow" data-cx-toggle="nav-overflow">
      <ul class="nav navbar-nav">
        <li class="nav-item">
          <a class="nav-link active" aria-current="page" href="#">Home</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">Features</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">Pricing</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">About</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">Contact</a>
        </li>
      </ul>
    </div>
  </div>
</nav>

Customizing the toggle

The toggle that opens the overflow menu shows an icon followed by the text "More". The options of the plugin change the text, the icon, and their order.

Custom text

Set the moreText option to change the text of the toggle:

JavaScript
import { NavOverflow } from '@chassis-ui/css'

const nav = document.querySelector('.nav-overflow')
new NavOverflow(nav, {
  moreText: 'See all'
})

Custom icon

Set the moreIcon option to change the icon. Any HTML string is accepted, including an inline SVG; the plugin sanitizes it before inserting it:

JavaScript
const nav = document.querySelector('.nav-overflow')
new NavOverflow(nav, {
  moreIcon: '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" viewBox="0 0 16 16"><path d="M3 9.5a1.5 1.5 0 1 1 0-3 1.5 1.5 0 0 1 0 3m5 0a1.5 1.5 0 1 1 0-3 1.5 1.5 0 0 1 0 3m5 0a1.5 1.5 0 1 1 0-3 1.5 1.5 0 0 1 0 3"/></svg>',
  moreText: false // Icon-only toggle; falls back to an aria-label of "More"
})

Alternatively, place an element with data-cx-overflow-icon inside the wrapper. The plugin uses it as the toggle icon and removes the original element from the DOM until dispose puts it back. This element takes priority over the moreIcon option.

HTML
<div class="nav-overflow" data-cx-toggle="nav-overflow">
  <ul class="nav nav-segments">
    <li class="nav-item"><a class="nav-link" href="#">Link 1</a></li>
    <li class="nav-item"><a class="nav-link" href="#">Link 2</a></li>
    <svg data-cx-overflow-icon xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" viewBox="0 0 16 16">
      <path d="M8 4.754a3.246 3.246 0 1 0 0 6.492 3.246 3.246 0 0 0 0-6.492"/>
    </svg>
  </ul>
</div>

Icon placement

Set the iconPlacement option to place the icon before the text ('start', the default) or after it ('end'):

JavaScript
const nav = document.querySelector('.nav-overflow')
new NavOverflow(nav, {
  moreText: 'Menu',
  iconPlacement: 'end'
})

Collapse behavior

By default, items collapse one by one from the end of the nav as the wrapper narrows. Two options change when items collapse.

Minimum visible items

Set the threshold option to keep a minimum number of items visible:

JavaScript
import { NavOverflow } from '@chassis-ui/css'

const nav = document.querySelector('.nav-overflow')
new NavOverflow(nav, {
  threshold: 3 // Always keep at least 3 items visible
})

Collapse all

Set the collapseBelow option to move every item into the overflow menu while the wrapper is narrower than a given width. Pass a breakpoint name to resolve the width from --cx-breakpoint-{name}, or a number for a width in pixels.

HTML
<div class="nav-overflow" data-cx-toggle="nav-overflow" data-cx-collapse-below="md">
  <ul class="nav nav-segments">
    <li class="nav-item"><a class="nav-link" href="#">Link 1</a></li>
    <li class="nav-item"><a class="nav-link" href="#">Link 2</a></li>
    <li class="nav-item"><a class="nav-link" href="#">Link 3</a></li>
  </ul>
</div>

The same option takes a width in pixels through JavaScript:

JavaScript
const nav = document.querySelector('.nav-overflow')
new NavOverflow(nav, {
  collapseBelow: 768
})

At and above that width, items collapse one by one again. Items with .nav-overflow-keep are never collapsed.

Accessibility

The toggle the plugin builds is a <button> with aria-expanded, and the Menu plugin opens the overflow menu and handles its keyboard interaction. An icon-only toggle (moreText: false) gets aria-label="More", so it keeps an accessible name.

An item that overflows is hidden with display: none, and a copy of its link, with its attributes, is added to the menu. Each link is therefore exposed to assistive technologies once, and aria-current and aria-disabled travel with it.

JavaScript API

The NavOverflow plugin measures the wrapper, moves items between the nav and the overflow menu, and fires events as it does. Import the NavOverflow class from the Chassis module:

JavaScript
import { NavOverflow } from '@chassis-ui/css'

Triggers

The data-attribute API enables the overflow behavior without any JavaScript on the page beyond the bundled plugin. The plugin initializes every wrapper with the attribute when the page has loaded:

AttributeDescription
data-cx-toggle="nav-overflow"Enables overflow handling on the .nav-overflow wrapper.
HTML
<div class="nav-overflow" data-cx-toggle="nav-overflow">
  <ul class="nav nav-segments">
    <li class="nav-item"><a class="nav-link" href="#">Link 1</a></li>
    <li class="nav-item"><a class="nav-link" href="#">Link 2</a></li>
    <li class="nav-item"><a class="nav-link" href="#">Link 3</a></li>
    <!-- More items... -->
  </ul>
</div>

Initialization

For programmatic control, or for a wrapper added after the page has loaded, instantiate the wrapper with the NavOverflow class. The wrapper must contain a .nav; the constructor throws a TypeError otherwise.

JavaScript
const wrapperElement = document.querySelector('.nav-overflow')
const navOverflow = new NavOverflow(wrapperElement, {
  moreText: 'More',
  threshold: 2
})

Options

Options are set via data-cx-* attributes or passed to the constructor as an object. Attribute names use the kebab-case form of the option name — data-cx-custom-class, not data-cx-customClass. Attribute values are parsed to their native types:"true" → true, "0" → 0, and valid JSON strings to objects.

Use data-cx-config to pass multiple options as a JSON string:data-cx-config='{"delay":200}'. Individual data-cx-* attributes take precedence over data-cx-config. Individual attributes accept JSON values too, such as data-cx-delay='{"show":100,"hide":200}'.

When initializing components, Chassis merges configurations from multiple sources in this priority order: default settings, data-cx-config values, individual data-cx-*attributes, and finally any JavaScript object options. Values defined later in this sequence override earlier ones.

NameTypeDefaultDescription
collapseBelownumber|string0Width of the wrapper below which all items collapse into the overflow menu. Pass a breakpoint name (e.g., 'md') to resolve from --cx-breakpoint-{name}, or a number for a width in pixels. 0 disables.
iconPlacementstring'start'Position of the icon relative to the text in the toggle button. Use 'start' for before the text or 'end' for after.
menuPlacementstring'bottom-end'Placement of the overflow menu, passed as data-cx-placement to the menu toggle.
moreTextstring|boolean'More'Text label for the overflow toggle button. Pass false for an icon-only toggle; the button falls back to aria-label="More" so it keeps an accessible name.
moreIconstring'<svg>...</svg>'SVG or HTML icon for the overflow toggle button. Overridden by a child element with data-cx-overflow-icon if present.
thresholdnumber0Minimum number of items to keep visible before showing overflow.

Methods

The plugin exposes two instance methods and two static lookup methods for programmatic control:

MethodDescription
disposeDestroys the instance and restores the items to their original positions. A toggle the plugin built is removed; a toggle, menu or icon element written in the markup stays where it was.
getInstanceStatic — returns the existing instance bound to a DOM element, or null. Example: NavOverflow.getInstance(element)
getOrCreateInstanceStatic — returns the existing instance or creates one. Example: NavOverflow.getOrCreateInstance(element)
updateRecalculates which items overflow. The plugin recalculates by itself when the wrapper resizes; call update after a change that alters the width of the items without resizing the wrapper, such as a changed label.

Events

The events fire on the .nav-overflow wrapper and report each overflow calculation.

EventDescription
update.cx.navoverflowFired after update() recalculates the overflow.
overflow.cx.navoverflowFired after a calculation that leaves items in the overflow menu, on resize or on update(). The event has the overflowCount and visibleCount properties.
JavaScript
const myNav = document.getElementById('myNav')

myNav.addEventListener('overflow.cx.navoverflow', event => {
  console.log(`${event.overflowCount} items moved to overflow`)
  console.log(`${event.visibleCount} items still visible`)
})