React Navs & Tabs Components
Navs & Tabs
Documentation and examples for how to use CoreUI's included React navigation components.
Other frameworks
CoreUI components are available as native Angular, Bootstrap (Vanilla JS), and Vue components. To learn more please visit the following pages.
Base nav#
Navigation available in CoreUI for React share general markup and styles, from the base .nav
class to the active and disabled states. Swap modifier classes to switch between each style.
The base <CNav>
component is built with flexbox and provide a strong foundation for building all types of navigation components. It includes some style overrides (for working with lists), some link padding for larger hit areas, and basic disabled styling.
<CNav> <CNavItem> <CNavLink href="#" active> Active </CNavLink> </CNavItem> <CNavItem> <CNavLink href="#">Link</CNavLink> </CNavItem> <CNavItem> <CNavLink href="#">Link</CNavLink> </CNavItem> <CNavItem> <CNavLink href="#" disabled> Disabled </CNavLink> </CNavItem></CNav>
Classes are used throughout, so your markup can be super flexible. Use <ul>
s like above, <ol>
if the order of your items is important, or roll your own with a <nav>
element. Because the .nav uses display: flex, the nav links behave the same as nav items would, but without the extra markup.
<CNav as="nav"> <CNavLink href="#" active> Active </CNavLink> <CNavLink href="#">Link</CNavLink> <CNavLink href="#">Link</CNavLink> <CNavLink href="#" disabled> Disabled </CNavLink></CNav>
Available styles#
Change the style of <CNav>
's component with modifiers and utilities. Mix and match as needed, or build your own.
Horizontal alignment#
Change the horizontal alignment of your nav with flexbox utilities. By default, navs are left-aligned, but you can easily change them to center or right aligned.
Centered with .justify-content-center
:
<CNav className="justify-content-center"> <CNavItem> <CNavLink href="#" active> Active </CNavLink> </CNavItem> <CNavItem> <CNavLink href="#">Link</CNavLink> </CNavItem> <CNavItem> <CNavLink href="#">Link</CNavLink> </CNavItem> <CNavItem> <CNavLink href="#" disabled> Disabled </CNavLink> </CNavItem></CNav>
Right-aligned with .justify-content-end
:
<CNav className="justify-content-end"> <CNavItem> <CNavLink href="#" active> Active </CNavLink> </CNavItem> <CNavItem> <CNavLink href="#">Link</CNavLink> </CNavItem> <CNavItem> <CNavLink href="#">Link</CNavLink> </CNavItem> <CNavItem> <CNavLink href="#" disabled> Disabled </CNavLink> </CNavItem></CNav>
Vertical#
Stack your navigation by changing the flex item direction with the .flex-column
utility. Need to stack them on some viewports but not others? Use the responsive versions (e.g., .flex-sm-column
).
<CNav className="flex-column"> <CNavItem> <CNavLink href="#" active> Active </CNavLink> </CNavItem> <CNavItem> <CNavLink href="#">Link</CNavLink> </CNavItem> <CNavItem> <CNavLink href="#">Link</CNavLink> </CNavItem> <CNavItem> <CNavLink href="#" disabled> Disabled </CNavLink> </CNavItem></CNav>
Tabs#
Takes the basic nav from above and adds the variant="tabs"
class to generate a tabbed interface
<CNav variant="tabs"> <CNavItem> <CNavLink href="#" active> Active </CNavLink> </CNavItem> <CNavItem> <CNavLink href="#">Link</CNavLink> </CNavItem> <CNavItem> <CNavLink href="#">Link</CNavLink> </CNavItem> <CNavItem> <CNavLink href="#" disabled> Disabled </CNavLink> </CNavItem></CNav>
Pills#
Take that same code, but use variant="pills"
instead:
<CNav variant="pills"> <CNavItem> <CNavLink href="#" active> Active </CNavLink> </CNavItem> <CNavItem> <CNavLink href="#">Link</CNavLink> </CNavItem> <CNavItem> <CNavLink href="#">Link</CNavLink> </CNavItem> <CNavItem> <CNavLink href="#" disabled> Disabled </CNavLink> </CNavItem></CNav>
Underline#
Take that same code, but use variant="underline"
instead:
<CNav variant="underline"> <CNavItem> <CNavLink href="#" active> Active </CNavLink> </CNavItem> <CNavItem> <CNavLink href="#">Link</CNavLink> </CNavItem> <CNavItem> <CNavLink href="#">Link</CNavLink> </CNavItem> <CNavItem> <CNavLink href="#" disabled> Disabled </CNavLink> </CNavItem></CNav>
Underline border#
Take that same code, but use variant="underline-border"
instead:
<CNav variant="underline-border"> <CNavItem> <CNavLink href="#" active> Active </CNavLink> </CNavItem> <CNavItem> <CNavLink href="#">Link</CNavLink> </CNavItem> <CNavItem> <CNavLink href="#">Link</CNavLink> </CNavItem> <CNavItem> <CNavLink href="#" disabled> Disabled </CNavLink> </CNavItem></CNav>
Fill and justify#
Force your <CNav>
's contents to extend the full available width one of two modifier classes. To proportionately fill all available space with your .nav-item
s, use layout="fill"
. Notice that all horizontal space is occupied, but not every nav item has the same width.
<CNav variant="pills" layout="fill"> <CNavItem> <CNavLink href="#" active> Active </CNavLink> </CNavItem> <CNavItem> <CNavLink href="#">Link</CNavLink> </CNavItem> <CNavItem> <CNavLink href="#">Link</CNavLink> </CNavItem> <CNavItem> <CNavLink href="#" disabled> Disabled </CNavLink> </CNavItem></CNav>
For equal-width elements, use layout="justified"
. All horizontal space will be occupied by nav links, but unlike the .nav-fill above, every nav item will be the same width.
<CNav variant="pills" layout="justified"> <CNavItem> <CNavLink href="#" active> Active </CNavLink> </CNavItem> <CNavItem> <CNavLink href="#">Link</CNavLink> </CNavItem> <CNavItem> <CNavLink href="#">Link</CNavLink> </CNavItem> <CNavItem> <CNavLink href="#" disabled> Disabled </CNavLink> </CNavItem></CNav>
Working with flex utilities#
If you need responsive nav variations, consider using a series of flexbox utilities. While more verbose, these utilities offer greater customization across responsive breakpoints. In the example below, our nav will be stacked on the lowest breakpoint, then adapt to a horizontal layout that fills the available width starting from the small breakpoint.
<CNav as="nav" variant="pills" className="flex-column flex-sm-row"> <CNavLink href="#" active> Active </CNavLink> <CNavLink href="#">Link</CNavLink> <CNavLink href="#">Link</CNavLink> <CNavLink href="#" disabled> Disabled </CNavLink></CNav>
Regarding accessibility#
If you're using navs to provide a navigation bar, be sure to add a role="navigation"
to the most logical parent container of the <ul>
, or wrap a <nav>
element around the whole navigation. Do not add the role to the <ul>
itself, as this would prevent it from being announced as an actual list by assistive technologies.
Note that navigation bars, even if visually styled as tabs with the .nav-tabs
class, should not be given role="tablist"
, role="tab"
or role="tabpanel"
attributes. These are only appropriate for dynamic tabbed interfaces, as described in the WAI ARIA Authoring Practices. See JavaScript behavior for dynamic tabbed interfaces in this section for an example. The aria-current
attribute is not necessary on dynamic tabbed interfaces since our JavaScript handles the selected state by adding aria-selected="true"
on the active tab.
Using dropdowns#
Add dropdown menus with a little extra HTML.
Tabs with dropdowns#
<CNav> <CNavItem> <CNavLink href="#" active> Active </CNavLink> </CNavItem> <CDropdown variant="nav-item"> <CDropdownToggle color="secondary">Dropdown button</CDropdownToggle> <CDropdownMenu> <CDropdownItem href="#">Action</CDropdownItem> <CDropdownItem href="#">Another action</CDropdownItem> <CDropdownItem href="#">Something else here</CDropdownItem> </CDropdownMenu> </CDropdown> <CNavItem> <CNavLink href="#">Link</CNavLink> </CNavItem> <CNavItem> <CNavLink href="#" disabled> Disabled </CNavLink> </CNavItem></CNav>
Pills with dropdowns#
<CNav variant="pills"> <CNavItem> <CNavLink href="#" active> Active </CNavLink> </CNavItem> <CDropdown variant="nav-item"> <CDropdownToggle color="secondary">Dropdown button</CDropdownToggle> <CDropdownMenu> <CDropdownItem href="#">Action</CDropdownItem> <CDropdownItem href="#">Another action</CDropdownItem> <CDropdownItem href="#">Something else here</CDropdownItem> </CDropdownMenu> </CDropdown> <CNavItem> <CNavLink href="#">Link</CNavLink> </CNavItem> <CNavItem> <CNavLink href="#" disabled> Disabled </CNavLink> </CNavItem></CNav>
Tab panes#
Dynamic tabbed interfaces, as described in the WAI ARIA Authoring Practices, require role="tablist"
, role="tab"
, role="tabpanel"
, and additional aria-
attributes in order to convey their structure, functionality and current state to users of assistive technologies (such as screen readers).
Note that dynamic tabbed interfaces should not contain dropdown menus, as this causes both usability and accessibility issues. From a usability perspective, the fact that the currently displayed tab's trigger element is not immediately visible (as it's inside the closed dropdown menu) can cause confusion. From an accessibility point of view, there is currently no sensible way to map this sort of construct to a standard WAI ARIA pattern, meaning that it cannot be easily made understandable to users of assistive technologies.
const [activeKey, setActiveKey] = useState(1)return ( <> <CNav variant="tabs" role="tablist"> <CNavItem> <CNavLink href="#!" active={activeKey === 1} onClick={() => setActiveKey(1)} > Home </CNavLink> </CNavItem> <CNavItem> <CNavLink href="#!" active={activeKey === 2} onClick={() => setActiveKey(2)} > Profile </CNavLink> </CNavItem> <CNavItem> <CNavLink href="#!" active={activeKey === 3} onClick={() => setActiveKey(3)} > Contact </CNavLink> </CNavItem> </CNav> <CTabContent> <CTabPane role="tabpanel" aria-labelledby="home-tab" visible={activeKey === 1}> Raw denim you probably haven't heard of them jean shorts Austin. Nesciunt tofu stumptown aliqua, retro synth master cleanse. Mustache cliche tempor, williamsburg carles vegan helvetica. Reprehenderit butcher retro keffiyeh dreamcatcher synth. Cosby sweater eu banh mi, qui irure terry richardson ex squid. Aliquip placeat salvia cillum iphone. Seitan aliquip quis cardigan american apparel, butcher voluptate nisi qui. </CTabPane> <CTabPane role="tabpanel" aria-labelledby="profile-tab" visible={activeKey === 2}> Food truck fixie locavore, accusamus mcsweeney's marfa nulla single-origin coffee squid. Exercitation +1 labore velit, blog sartorial PBR leggings next level wes anderson artisan four loko farm-to-table craft beer twee. Qui photo booth letterpress, commodo enim craft beer mlkshk aliquip jean shorts ullamco ad vinyl cillum PBR. Homo nostrud organic, assumenda labore aesthetic magna delectus mollit. Keytar helvetica VHS salvia yr, vero magna velit sapiente labore stumptown. Vegan fanny pack odio cillum wes anderson 8-bit, sustainable jean shorts beard ut DIY ethical culpa terry richardson biodiesel. Art party scenester stumptown, tumblr butcher vero sint qui sapiente accusamus tattooed echo park. </CTabPane> <CTabPane role="tabpanel" aria-labelledby="contact-tab" visible={activeKey === 3}> Etsy mixtape wayfarers, ethical wes anderson tofu before they sold out mcsweeney's organic lomo retro fanny pack lo-fi farm-to-table readymade. Messenger bag gentrify pitchfork tattooed craft beer, iphone skateboard locavore carles etsy salvia banksy hoodie helvetica. DIY synth PBR banksy irony. Leggings gentrify squid 8-bit cred pitchfork. Williamsburg banh mi whatever gluten-free, carles pitchfork biodiesel fixie etsy retro mlkshk vice blog. Scenester cred you probably haven't heard of them, vinyl craft beer blog stumptown. Pitchfork sustainable tofu synth chambray yr. </CTabPane> </CTabContent> </>)
The tabs also works with pills.
const [activeKey, setActiveKey] = useState(1)return ( <> <CNav variant="pills" role="tablist"> <CNavItem> <CNavLink href="#!" active={activeKey === 1} onClick={() => setActiveKey(1)} > Home </CNavLink> </CNavItem> <CNavItem> <CNavLink href="#!" active={activeKey === 2} onClick={() => setActiveKey(2)} > Profile </CNavLink> </CNavItem> <CNavItem> <CNavLink href="#!" active={activeKey === 3} onClick={() => setActiveKey(3)} > Contact </CNavLink> </CNavItem> </CNav> <CTabContent> <CTabPane role="tabpanel" aria-labelledby="home-tab" visible={activeKey === 1}> Raw denim you probably haven't heard of them jean shorts Austin. Nesciunt tofu stumptown aliqua, retro synth master cleanse. Mustache cliche tempor, williamsburg carles vegan helvetica. Reprehenderit butcher retro keffiyeh dreamcatcher synth. Cosby sweater eu banh mi, qui irure terry richardson ex squid. Aliquip placeat salvia cillum iphone. Seitan aliquip quis cardigan american apparel, butcher voluptate nisi qui. </CTabPane> <CTabPane role="tabpanel" aria-labelledby="profile-tab" visible={activeKey === 2}> Food truck fixie locavore, accusamus mcsweeney's marfa nulla single-origin coffee squid. Exercitation +1 labore velit, blog sartorial PBR leggings next level wes anderson artisan four loko farm-to-table craft beer twee. Qui photo booth letterpress, commodo enim craft beer mlkshk aliquip jean shorts ullamco ad vinyl cillum PBR. Homo nostrud organic, assumenda labore aesthetic magna delectus mollit. Keytar helvetica VHS salvia yr, vero magna velit sapiente labore stumptown. Vegan fanny pack odio cillum wes anderson 8-bit, sustainable jean shorts beard ut DIY ethical culpa terry richardson biodiesel. Art party scenester stumptown, tumblr butcher vero sint qui sapiente accusamus tattooed echo park. </CTabPane> <CTabPane role="tabpanel" aria-labelledby="contact-tab" visible={activeKey === 3}> Etsy mixtape wayfarers, ethical wes anderson tofu before they sold out mcsweeney's organic lomo retro fanny pack lo-fi farm-to-table readymade. Messenger bag gentrify pitchfork tattooed craft beer, iphone skateboard locavore carles etsy salvia banksy hoodie helvetica. DIY synth PBR banksy irony. Leggings gentrify squid 8-bit cred pitchfork. Williamsburg banh mi whatever gluten-free, carles pitchfork biodiesel fixie etsy retro mlkshk vice blog. Scenester cred you probably haven't heard of them, vinyl craft beer blog stumptown. Pitchfork sustainable tofu synth chambray yr. </CTabPane> </CTabContent> </>)
Customizing#
CSS variables#
React navs use local CSS variables on .nav
, .nav-tabs
, .nav-pills
, .nav-underline
and .nav-underline-border
for enhanced real-time customization. Values for the CSS variables are set via Sass, so Sass customization is still supported, too.
On the .nav
base class:
--cui-nav-link-padding-x: #{$nav-link-padding-x};--cui-nav-link-padding-y: #{$nav-link-padding-y};@include rfs($nav-link-font-size, --cui-nav-link-font-size);--cui-nav-link-font-weight: #{$nav-link-font-weight};--cui-nav-link-color: #{$nav-link-color};--cui-nav-link-hover-color: #{$nav-link-hover-color};--cui-nav-link-disabled-color: #{$nav-link-disabled-color};
On the .nav-tabs
modifier class:
--cui-nav-tabs-border-width: #{$nav-tabs-border-width};--cui-nav-tabs-border-color: #{$nav-tabs-border-color};--cui-nav-tabs-border-radius: #{$nav-tabs-border-radius};--cui-nav-tabs-link-hover-border-color: #{$nav-tabs-link-hover-border-color};--cui-nav-tabs-link-active-color: #{$nav-tabs-link-active-color};--cui-nav-tabs-link-active-bg: #{$nav-tabs-link-active-bg};--cui-nav-tabs-link-active-border-color: #{$nav-tabs-link-active-border-color};
On the .nav-pills
modifier class:
--cui-nav-pills-border-radius: #{$nav-pills-border-radius};--cui-nav-pills-link-active-color: #{$nav-pills-link-active-color};--cui-nav-pills-link-active-bg: #{$nav-pills-link-active-bg};
On the .nav-underline
modifier class:
--cui-nav-underline-gap: #{$nav-underline-gap};--cui-nav-underline-border-width: #{$nav-underline-border-width};--cui-nav-underline-link-active-color: #{$nav-underline-link-active-color};
On the .nav-underline-border
modifier class:
--cui-nav-underline-border-gap: #{$nav-underline-border-gap};--cui-nav-underline-border-border-color: #{$nav-underline-border-border-color};--cui-nav-underline-border-border-width: #{$nav-underline-border-border-width};--cui-nav-underline-border-link-padding-x: #{$nav-underline-border-link-padding-x};--cui-nav-underline-border-link-padding-y: #{$nav-underline-border-link-padding-y};--cui-nav-underline-border-link-color: #{$nav-underline-border-link-color};--cui-nav-underline-border-link-active-color: #{$nav-underline-border-link-active-color};--cui-nav-underline-border-link-disabled-color: #{$nav-underline-border-link-disabled-color};
How to use CSS variables#
const vars = { '--my-css-var': 10, '--my-another-css-var': "red" }return <CNav style={vars}>...</CNav>
SASS variables#
$nav-link-padding-y: .5rem !default;$nav-link-padding-x: 1rem !default;$nav-link-font-size: null !default;$nav-link-font-weight: null !default;$nav-link-color: var(--cui-link-color) !default;$nav-link-hover-color: var(--cui-link-hover-color) !default;$nav-link-transition: color .15s ease-in-out, background-color .15s ease-in-out, border-color .15s ease-in-out !default;$nav-link-disabled-color: var(--cui-secondary-color) !default;$nav-link-focus-box-shadow: $focus-ring-box-shadow !default;
$nav-tabs-border-color: var(--cui-border-color) !default;$nav-tabs-border-width: var(--cui-border-width) !default;$nav-tabs-border-radius: var(--cui-border-radius) !default;$nav-tabs-link-hover-border-color: var(--cui-secondary-bg) var(--cui-secondary-bg) $nav-tabs-border-color !default;$nav-tabs-link-active-color: var(--cui-emphasis-color) !default;$nav-tabs-link-active-bg: var(--cui-body-bg) !default;$nav-tabs-link-active-border-color: var(--cui-border-color) var(--cui-border-color) $nav-tabs-link-active-bg !default;
$nav-pills-border-radius: var(--cui-border-radius) !default;$nav-pills-link-active-color: $component-active-color !default;$nav-pills-link-active-bg: $component-active-bg !default;
$nav-underline-gap: 1rem !default;$nav-underline-border-width: .125rem !default;$nav-underline-link-active-color: var(--cui-emphasis-color) !default;
$nav-underline-border-gap: .5rem !default;$nav-underline-border-border-color: var(--cui-border-color) !default;$nav-underline-border-border-width: .125rem !default;$nav-underline-border-link-padding-y: .5rem !default;$nav-underline-border-link-padding-x: .5rem !default;$nav-underline-border-link-color: var(--cui-secondary-color) !default;$nav-underline-border-link-active-color: var(--cui-primary) !default;$nav-underline-border-link-disabled-color: var(--cui-tertiary-color) !default;
API#
CNav#
import { CNav } from '@coreui/react'// orimport CNav from '@coreui/react/src/components/nav/CNav'
Property | Description | Type | Default |
---|---|---|---|
as | Component used for the root node. Either a string to use a HTML element or a component. | (ElementType & 'symbol') | (ElementType & 'object') | (ElementType & 'ul') | (ElementType & 'slot') | (ElementType & 'style') | ... 174 more ... | (ElementType & FunctionComponent<...>) | - |
className | A string of all className you want applied to the base component. | string | - |
layout | Specify a layout type for component. | 'fill' | 'justified' | - |
variant | Set the nav variant to tabs or pills. | 'pills' | 'tabs' | 'underline' | 'underline-border' | - |
CNavItem#
import { CNavItem } from '@coreui/react'// orimport CNavItem from '@coreui/react/src/components/nav/CNavItem'
Property | Description | Type | Default |
---|---|---|---|
active | Toggle the active state for the component. | boolean | - |
as 5.0.0+ | Component used for the root node. Either a string to use a HTML element or a component. | (ElementType & 'symbol') | (ElementType & 'object') | (ElementType & 'li') | (ElementType & 'cite') | (ElementType & 'data') | ... 174 more ... | (ElementType & FunctionComponent<...>) | - |
className | A string of all className you want applied to the component. | string | - |
disabled | Toggle the disabled state for the component. | boolean | - |
href | The href attribute specifies the URL of the page the link goes to. | string | - |
CNavLink#
import { CNavLink } from '@coreui/react'// orimport CNavLink from '@coreui/react/src/components/nav/CNavLink'
Property | Description | Type | Default |
---|---|---|---|
active | Toggle the active state for the component. | boolean | - |
as | Component used for the root node. Either a string to use a HTML element or a component. | (ElementType & 'symbol') | (ElementType & 'object') | (ElementType & 'a') | (ElementType & 'cite') | (ElementType & 'data') | ... 174 more ... | (ElementType & FunctionComponent<...>) | - |
className | A string of all className you want applied to the component. | string | - |
disabled | Toggle the disabled state for the component. | boolean | - |
href | The href attribute specifies the URL of the page the link goes to. | string | - |
CTabContent#
import { CTabContent } from '@coreui/react'// orimport CTabContent from '@coreui/react/src/components/tabs/CTabContent'
Property | Description | Type | Default |
---|---|---|---|
className | A string of all className you want applied to the base component. | string | - |
CTabPane#
import { CTabPane } from '@coreui/react'// orimport CTabPane from '@coreui/react/src/components/tabs/CTabPane'
Property | Description | Type | Default |
---|---|---|---|
className | A string of all className you want applied to the base component. | string | - |
onHide | Callback fired when the component requests to be hidden. | () => void | - |
onShow | Callback fired when the component requests to be shown. | () => void | - |
transition 5.1.0+ | Enable fade in and fade out transition. | boolean | true |
visible | Toggle the visibility of component. | boolean | - |