About
The Navbar component is the navigation bar: a brand, one or more link groups, and dropdown menus. It collapses into a toggle menu below the breakpoint you set, and every link answers to the keyboard.
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.NavCollapseWraps the links that should fold into a toggle menu on small screens.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.
<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.
Navbar Scrolling Effects
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 thescrollAmountToShrinkprop to shrink the Navbar. Follow this for more.hideOnScrollDown- The Navbar disappears when scrolling down and reappears when scrolling up. Use thescrollAmountToHideprop 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.
<Navbar scrollBehavior="default"> ... </Navbar>
<Navbar scrollBehavior="fixed"> ... </Navbar>
<Navbar scrollBehavior="shrinkOnScrollDown"> ... </Navbar>
<Navbar scrollBehavior="hideOnScrollDown"> ... </Navbar>
<Navbar scrollBehavior="shrinkAndHide"> ... </Navbar>The Navbar also checks the scroll position when the page loads, so a page reloaded halfway down starts shrunk or hidden as it should.
Every behavior except default makes the Navbar fixed, so it no longer takes up space in the page. Add top padding to your page content, about the height of the Navbar, so nothing is hidden behind it.
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. Change them to suit how tall your navbar is.
Brand and Links
NavBrand
The NavBrand component shows your logo or site name as a link. Its href prop defaults to "/". Put text or an image inside it. Screen readers read its content, so give a logo image an alt text. If the brand has no readable content, such as an SVG logo, set the ariaLabel prop.
<Navbar>
<NavBrand href="/">BRAND</NavBrand>
</Navbar>NavLink
A NavLink renders a link when it has an href, a button when it has an onclick instead, and a plain text element otherwise. Write the content inside the component, or pass it as the text prop, which renders as plain text. Set active on the link of the current page: it gets the active styles and aria-current="page". Clicking a link also closes the mobile menu.
<NavLinkGroup>
<NavLink href="/" active>Home</NavLink>
<NavLink href="/about" text="About" />
<NavLink href="/contact" text="Contact" />
</NavLinkGroup>Link Alignment
The align prop in the NavLinkGroup component controls how navigation links are positioned within the group. By default, links are aligned to the "start", keeping them on the left. If you want the links to be centered, set align to "center". To push the links to the right, use "end". In the example below, different NavLinkGroup elements demonstrate these alignments inside NavCollapse.
<Navbar>
<NavBrand href="#">BRAND</NavBrand>
<NavCollapse>
<NavLinkGroup align="start"> ... </NavLinkGroup> <!-- Default -->
<NavLinkGroup align="center"> ... </NavLinkGroup>
<NavLinkGroup align="end"> ... </NavLinkGroup>
</NavCollapse>
</Navbar>Animation Speed
The animationSpeed prop of the Navbar sets the transition speed of the Navbar and everything inside it: the shrink and hide effects, the links and the dropdowns. Available values are "slower", "slow", "normal", "fast" (default), "faster" and "none".
<Navbar animationSpeed="slower"> ... </Navbar>Rounded
The rounded prop of the Navbar sets the corner radius of its links, its dropdown menus and the bottom corners of the mobile menu. Available values are "none", "sm", "md" (default), "lg", "xl", "2xl" and "full".
<Navbar rounded="none"> ... </Navbar>Customization
The Navbar is styled from the Navbar component, so one set of props styles every part of it:
class- classes for the<nav>element, merged with the defaults.navInnerClasses- classes for the inner container that holds the brand and the links.navCollapseClasses- classes for the mobile menu created byNavCollapse.scrollShrinkClasses- classes added while the Navbar is shrunk by theshrinkOnScrollDownorshrinkAndHidebehavior.linkClasses- classes for everyNavLinkand dropdown trigger.activeLinkClasses- classes added to anactiveNavLink.dropdownLinkClasses- classes for the NavLinks inside aNavDropdown.
Each part also takes a class of its own. On a NavDropdown, class styles the menu panel, and linkClasses styles its trigger.
The inner row stops at the same width as a Container, so the brand and the links line up with the content below them. Both read the --max-width variable, and changing it moves them together.
<Navbar
class="bg-blue-50 dark:bg-blue-950"
linkClasses="text-blue-700 dark:text-blue-300"
activeLinkClasses="font-bold"
dropdownLinkClasses="text-sm"
>
...
</Navbar>Accessibility
The Navbar ships with the roles and the keyboard behavior a navigation region needs.
Use the ariaLabel prop on the Navbar to set an aria-label. It tells a screen reader which navigation region this is, which matters as soon as a page has more than one.
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 and moves focus back to the dropdown button. The mobile toggle button and each dropdown button point to the content they open with aria-controls.
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.