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:
NavBrand
Displays the brand name or logo and links to the homepage or any specified URL.NavLinkGroup
A wrapper for organizing multiple navigation links in a structured manner.NavLink
Represents an individual navigation link with customizable text and URL.NavDropdown
A 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 thescrollAmountToShrink
prop to shrink the Navbar. Follow this for more.hideOnScrollDown
- The Navbar disappears when scrolling down and reappears when scrolling up. Use thescrollAmountToHide
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.
<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.
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>
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.