Navbar

A responsive navigation bar with support for dropdown menus, keyboard navigation, and customizable styling. It's ideal for building primary site navigation or complex menu systems.

About

The Navbar component provides a fully responsive and accessible navigation bar for your application. It supports branding, grouped links, and dropdown menus, making it suitable for both simple and complex navigation structures. With built-in keyboard navigation and customizable styling options, the Navbar ensures a seamless user experience across all devices.

Example

The Navbar component is built using multiple elements, each serving a specific role:

  • NavBrandDisplays the brand name or logo and links to the homepage or any specified URL.
  • NavLinkGroupA wrapper for organizing multiple navigation links in a structured manner.
  • NavLinkRepresents an individual navigation link with customizable text and URL.
  • NavDropdownA dropdown menu inside the Navbar that contains additional navigation links.
Svelte
<script>
  import {Navbar, NavBrand, NavLinkGroup, NavDropdown, NavLink} from "theui-svelte";
</script>

<Navbar>
  <NavBrand href="#">BRAND</NavBrand>
  <NavLinkGroup>
    <NavLink href="#" text="Link 1" />
    <NavLink href="#" text="Link 2" />
    <NavLink href="#" text="Link 3" />
    <NavDropdown label="Dropdown">
      <NavLink href="#" text="Dropdown Link 1" />
      <NavLink href="#" text="Dropdown Link 2" />
      <NavLink href="#" text="Dropdown Link 3" />
    </NavDropdown>
  </NavLinkGroup>
</Navbar>

Scroll Behavior

The scrollBehavior prop controls how the Navbar behaves when the user scrolls. It provides flexibility in handling visibility and size adjustments based on scrolling direction.

  • default - The Navbar remains static and does not react to scrolling.
  • fixed - The Navbar stays fixed at the top of the page, even when scrolling.
  • shrinkOnScrollDown - The Navbar reduces in height when scrolling down and restores its size when scrolling up. You can control the scroll amount with the scrollAmountToShrink prop to shrink the Navbar. Follow this for more.
  • hideOnScrollDown - The Navbar disappears when scrolling down and reappears when scrolling up. Use the scrollAmountToHide prop to change the scroll amount to hide the Navbar. Follow this for more.
  • shrinkAndHide - The Navbar first shrinks on scroll down and then hides completely, reappearing at full size when scrolling up.
Svelte
<Navbar scrollBehavior="default"> ... </Navbar>
<Navbar scrollBehavior="fixed"> ... </Navbar>
<Navbar scrollBehavior="shrinkOnScrollDown"> ... </Navbar>
<Navbar scrollBehavior="hideOnScrollDown"> ... </Navbar>
<Navbar scrollBehavior="shrinkAndHide"> ... </Navbar>

Hide/Shrink Scroll Amount

The scrollAmountToShrink and scrollAmountToHide props allow fine-tuned control over how the Navbar reacts to scrolling. These values determine the scroll distance (in pixels) required to trigger shrinking or hiding effects, making the Navbar more adaptive to different layouts.

By default, scrollAmountToShrink is set to 32px, meaning the Navbar will shrink after scrolling this distance when shrinkOnScrollDown or shrinkAndHide is enabled. Similarly, scrollAmountToHide is 128px, which controls when the Navbar fully disappears when hideOnScrollDown or shrinkAndHide is used. Adjusting these values ensures a smooth and customized scroll behavior.

Responsive Navbar

The Navbar is fully responsive and adapts to different screen sizes with the help of the NavCollapse component. Any navigation elements placed inside NavCollapse will automatically collapse into a toggleable menu on smaller screens, ensuring a clean and accessible navigation experience. This is particularly useful for mobile and tablet views where space is limited.

The NavCollapse component acts as a wrapper for collapsible navigation items. When used inside the Navbar, it ensures that the contained elements are hidden on smaller screens and can be revealed through a toggle button. Without NavCollapse, all navigation items will remain visible, even on small screens, potentially affecting layout and usability.

The navBreakpoint prop in the Navbar component determines at which screen size the navigation should collapse. Available values are 'sm', 'md', 'lg' (default), and 'xl'. When the screen width is smaller than the defined breakpoint, NavCollapse will activate, hiding its content until toggled. Adjusting this value allows you to fine-tune when the menu should collapse based on your design needs.

Svelte
<Navbar navBreakpoint="md">
  <NavBrand href="#">BRAND</NavBrand>
  <NavCollapse>
    <NavLinkGroup>
      ...
    </NavLinkGroup>
  </NavCollapse>
</Navbar>

In this example, the navigation items inside NavCollapse will collapse into a toggleable menu when the screen width is smaller than the md breakpoint.

Mega Menu

The Navbar is designed to be highly versatile, making it suitable for both simple navigation and complex menu structures. Whether you need a minimal navigation bar or a feature-rich menu system, it adapts to your design needs effortlessly.

For more advanced layouts, the Navbar includes a Mega Menu option. When the width prop of a NavDropdown is set to "full", the dropdown expands to take the full width of the Navbar, creating a spacious layout for organizing multiple links, sections, or even embedded content. This makes the Mega Menu ideal for websites that require a detailed navigation structure, such as e-commerce platforms, dashboards, and content-heavy websites.

Svelte
<NavDropdown label="Mega menu" width="full">
  <div class="...">
    ...
  </div>
</NavDropdown>

Mega Menu

The Navbar is designed to be highly versatile, making it suitable for both simple navigation and complex menu structures. Whether you need a minimal navigation bar or a feature-rich menu system, it adapts to your design needs effortlessly.

For more advanced layouts, the Navbar includes a Mega Menu option. When the width prop of a NavDropdown is set to "full", the dropdown expands to take the full width of the Navbar, creating a spacious layout for organizing multiple links, sections, or even embedded content. This makes the Mega Menu ideal for websites that require a detailed navigation structure, such as e-commerce platforms, dashboards, and content-heavy websites.

Svelte
<NavDropdown label="Mega menu" width="full">
  <div class="...">
    ...
  </div>
</NavDropdown>

Accessibility

The Navbar is designed with accessibility in mind, ensuring smooth navigation for all users, including those using screen readers and keyboard navigation.

To enhance accessibility, use the ariaLabel prop in the Navbar component to define an appropriate aria-label value. This helps screen readers identify the purpose of the navigation.

All interactive elements, including links and dropdowns, are fully keyboard-accessible. Users can open dropdowns with Enter or Space, navigate through links and move between dropdown items using the Tab key. The Escape key closes open dropdowns.

For better accessibility, ensure that all links have clear, descriptive text. If using a NavDropdown, the aria-expanded attribute updates dynamically to reflect its open or closed state, improving the experience for screen reader users.

By following these guidelines and utilizing the provided accessibility features, you can create a navigation system that is inclusive and user-friendly.

Configuration