Bootstrap React Dropdown
Bootstrap React Dropdown component allows you to toggle contextual overlays for displaying lists, links, and more html elements.
Overview#
Dropdowns are toggleable, contextual overlays for displaying lists of links and more.
Dropdowns are built on a third party library, Popper.js, which provides dynamic positioning and viewport detection. Popper.js isn't used to position dropdowns in navbars though as dynamic positioning isn't required.
Examples#
Bind the dropdown's toggle and the dropdown menu inside <CDropdown>
, or different element that declares position: relative;
. Dropdowns can be triggered from <a>
or <button>
elements to better fit your possible requirements.
Single button#
Here's how you can put them to work with either <button>
elements:
1<CDropdown>2 <CDropdownToggle color="secondary">Dropdown button</CDropdownToggle>3 <CDropdownMenu>4 <CDropdownItem href="#">Action</CDropdownItem>5 <CDropdownItem href="#">Another action</CDropdownItem>6 <CDropdownItem href="#">Something else here</CDropdownItem>7 </CDropdownMenu>8</CDropdown>
And with <a>
elements:
1<CDropdown>2 <CDropdownToggle href="#" color="secondary">3 Dropdown button4 </CDropdownToggle>5 <CDropdownMenu>6 <CDropdownItem href="#">Action</CDropdownItem>7 <CDropdownItem href="#">Another action</CDropdownItem>8 <CDropdownItem href="#">Something else here</CDropdownItem>9 </CDropdownMenu>10</CDropdown>
The best part is you can do this with any button variant, too:
1<>2 {['primary', 'secondary', 'success', 'info', 'warning', 'danger'].map((color, index) => (3 <CDropdown variant="btn-group" key={index}>4 <CDropdownToggle color={color}>{color}</CDropdownToggle>5 <CDropdownMenu>6 <CDropdownItem href="#">Action</CDropdownItem>7 <CDropdownItem href="#">Another action</CDropdownItem>8 <CDropdownItem href="#">Something else here</CDropdownItem>9 <CDropdownDivider />10 <CDropdownItem href="#">Separated link</CDropdownItem>11 </CDropdownMenu>12 </CDropdown>13 ))}14</>
Split button#
Similarly, create split button dropdowns with virtually the same markup as single button dropdowns, but with the addition of boolean prop split
for proper spacing around the dropdown caret.
We use this extra class to reduce the horizontal padding
on either side of the caret by 25% and remove the margin-left
that's attached for normal button dropdowns. Those additional changes hold the caret centered in the split button and implement a more properly sized hit area next to the main button.
1<>2 {['primary', 'secondary', 'success', 'info', 'warning', 'danger'].map((color, index) => (3 <CDropdown variant="btn-group" key={index}>4 <CButton color={color}>{color}</CButton>5 <CDropdownToggle color={color} split />6 <CDropdownMenu>7 <CDropdownItem href="#">Action</CDropdownItem>8 <CDropdownItem href="#">Another action</CDropdownItem>9 <CDropdownItem href="#">Something else here</CDropdownItem>10 <CDropdownDivider />11 <CDropdownItem href="#">Separated link</CDropdownItem>12 </CDropdownMenu>13 </CDropdown>14 ))}15</>
Sizing#
Button dropdowns work with buttons of all sizes, including default and split dropdown buttons.
1<CDropdown variant="btn-group">2 <CDropdownToggle color="secondary" size="lg">Large button</CDropdownToggle>3 <CDropdownMenu>4 <CDropdownItem href="#">Action</CDropdownItem>5 <CDropdownItem href="#">Another action</CDropdownItem>6 <CDropdownItem href="#">Something else here</CDropdownItem>7 <CDropdownDivider/>8 <CDropdownItem href="#">Separated link</CDropdownItem>9 </CDropdownMenu>10 </CDropdown>1112 <CDropdown variant="btn-group">13 <CButton color="secondary" size="lg">Large split button</CButton>14 <CDropdownToggle color="secondary" size="lg" split/>15 <CDropdownMenu>16 <CDropdownItem href="#">Action</CDropdownItem>17 <CDropdownItem href="#">Another action</CDropdownItem>18 <CDropdownItem href="#">Something else here</CDropdownItem>19 <CDropdownDivider/>20 <CDropdownItem href="#">Separated link</CDropdownItem>21 </CDropdownMenu>22 </CDropdown>
1<CDropdown variant="btn-group">2 <CDropdownToggle color="secondary" size="sm">Small button</CDropdownToggle>3 <CDropdownMenu>4 <CDropdownItem href="#">Action</CDropdownItem>5 <CDropdownItem href="#">Another action</CDropdownItem>6 <CDropdownItem href="#">Something else here</CDropdownItem>7 <CDropdownDivider/>8 <CDropdownItem href="#">Separated link</CDropdownItem>9 </CDropdownMenu>10 </CDropdown>1112 <CDropdown variant="btn-group">13 <CButton color="secondary" size="sm">Small split button</CButton>14 <CDropdownToggle color="secondary" size="sm"split/>15 <CDropdownMenu>16 <CDropdownItem href="#">Action</CDropdownItem>17 <CDropdownItem href="#">Another action</CDropdownItem>18 <CDropdownItem href="#">Something else here</CDropdownItem>19 <CDropdownDivider/>20 <CDropdownItem href="#">Separated link</CDropdownItem>21 </CDropdownMenu>22 </CDropdown>
Dark dropdowns#
Opt into darker dropdowns to match a dark navbar or custom style by set dark
property. No changes are required to the dropdown items.
1<CDropdown dark>2 <CDropdownToggle color="secondary">Dropdown button</CDropdownToggle>3 <CDropdownMenu>4 <CDropdownItem href="#">Action</CDropdownItem>5 <CDropdownItem href="#">Another action</CDropdownItem>6 <CDropdownItem href="#">Something else here</CDropdownItem>7 <CDropdownDivider />8 <CDropdownItem href="#">Separated link</CDropdownItem>9 </CDropdownMenu>10</CDropdown>
And putting it to use in a navbar:
1<CNavbar expand="lg" colorScheme="dark" className="bg-dark">2 <CContainer fluid>3 <CNavbarBrand href="#">Navbar</CNavbarBrand>4 <CNavbarToggler aria-label="Toggle navigation" aria-expanded={true} />5 <CCollapse className="navbar-collapse" visible={true}>6 <CNavbarNav>7 <CDropdown dark component="li" variant="nav-item">8 <CDropdownToggle>Dropdown</CDropdownToggle>9 <CDropdownMenu>10 <CDropdownItem href="#">Action</CDropdownItem>11 <CDropdownItem href="#">Another action</CDropdownItem>12 <CDropdownItem href="#">Something else here</CDropdownItem>13 <CDropdownDivider />14 <CDropdownItem href="#">Separated link</CDropdownItem>15 </CDropdownMenu>16 </CDropdown>17 </CNavbarNav>18 </CCollapse>19 </CContainer>20</CNavbar>
Directions#
RTL
Directions are mirrored when using Bootstrap React in RTL, meaning `.dropstart` will appear on the right side.
Dropup#
Trigger dropdown menus above elements by adding direction="dropup"
to the <CDropdown>
component.
1<CDropdown variant="btn-group" direction="dropup">2 <CDropdownToggle color="secondary">Dropdown</CDropdownToggle>3 <CDropdownMenu>4 <CDropdownItem href="#">Action</CDropdownItem>5 <CDropdownItem href="#">Another action</CDropdownItem>6 <CDropdownItem href="#">Something else here</CDropdownItem>7 <CDropdownDivider/>8 <CDropdownItem href="#">Separated link</CDropdownItem>9 </CDropdownMenu>10 </CDropdown>1112 <CDropdown variant="btn-group" direction="dropup">13 <CButton color="secondary" >Small split button</CButton>14 <CDropdownToggle color="secondary" split/>15 <CDropdownMenu>16 <CDropdownItem href="#">Action</CDropdownItem>17 <CDropdownItem href="#">Another action</CDropdownItem>18 <CDropdownItem href="#">Something else here</CDropdownItem>19 <CDropdownDivider/>20 <CDropdownItem href="#">Separated link</CDropdownItem>21 </CDropdownMenu>22 </CDropdown>
Dropright#
Trigger dropdown menus at the right of the elements by adding direction="dropend"
to the <CDropdown>
component.
1<CDropdown variant="btn-group" direction="dropend">2 <CDropdownToggle color="secondary">Dropdown</CDropdownToggle>3 <CDropdownMenu>4 <CDropdownItem href="#">Action</CDropdownItem>5 <CDropdownItem href="#">Another action</CDropdownItem>6 <CDropdownItem href="#">Something else here</CDropdownItem>7 <CDropdownDivider/>8 <CDropdownItem href="#">Separated link</CDropdownItem>9 </CDropdownMenu>10 </CDropdown>1112 <CDropdown variant="btn-group" direction="dropend">13 <CButton color="secondary" >Small split button</CButton>14 <CDropdownToggle color="secondary" split/>15 <CDropdownMenu>16 <CDropdownItem href="#">Action</CDropdownItem>17 <CDropdownItem href="#">Another action</CDropdownItem>18 <CDropdownItem href="#">Something else here</CDropdownItem>19 <CDropdownDivider/>20 <CDropdownItem href="#">Separated link</CDropdownItem>21 </CDropdownMenu>22 </CDropdown>
Dropleft#
Trigger dropdown menus at the left of the elements by adding direction="dropstart"
to the <CDropdown>
component.
1<CDropdown variant="btn-group" direction="dropstart">2 <CDropdownToggle color="secondary">Dropdown</CDropdownToggle>3 <CDropdownMenu>4 <CDropdownItem href="#">Action</CDropdownItem>5 <CDropdownItem href="#">Another action</CDropdownItem>6 <CDropdownItem href="#">Something else here</CDropdownItem>7 <CDropdownDivider/>8 <CDropdownItem href="#">Separated link</CDropdownItem>9 </CDropdownMenu>10 </CDropdown>1112 <CButtonGroup>13 <CDropdown variant="btn-group" direction="dropstart">14 <CDropdownToggle color="secondary" split/>15 <CDropdownMenu>16 <CDropdownItem href="#">Action</CDropdownItem>17 <CDropdownItem href="#">Another action</CDropdownItem>18 <CDropdownItem href="#">Something else here</CDropdownItem>19 <CDropdownDivider/>20 <CDropdownItem href="#">Separated link</CDropdownItem>21 </CDropdownMenu>22 </CDropdown>23 <CButton color="secondary" >Small split button</CButton>24 </CButtonGroup>
Menu items#
Historically dropdown menu contents had to be links, but that's no longer the case with v4. Now you can optionally use <button>
elements in your dropdowns instead of just <a>
s.
1<CDropdown>2 <CDropdownToggle color="secondary">Dropdown</CDropdownToggle>3 <CDropdownMenu>4 <CDropdownItem component="button">Action</CDropdownItem>5 <CDropdownItem component="button">Another action</CDropdownItem>6 <CDropdownItem component="button">Something else here</CDropdownItem>7 <CDropdownDivider />8 <CDropdownItem component="button">Separated link</CDropdownItem>9 </CDropdownMenu>10</CDropdown>
You can also create non-interactive dropdown items with <CDropdownItemPlain>
.
1<div className="border rounded py-2">2 <CDropdownItemPlain>Dropdown item text</CDropdownItemPlain>3 <CDropdownItem href="#">Action</CDropdownItem>4 <CDropdownItem href="#">Another action</CDropdownItem>5 <CDropdownItem href="#">Something else here</CDropdownItem>6</div>
Active#
Set boolean property active
to style item as active.
In the following example we use div
instead of <CDropdownMenu>
to show <CDropdownMenu>
content.
1<div className="border rounded py-2">2 <CDropdownItem href="#">Regular link</CDropdownItem>3 <CDropdownItem href="#" active>4 Active link5 </CDropdownItem>6 <CDropdownItem href="#">Another link</CDropdownItem>7</div>
Disabled#
Add disabled
boolean property to items in the dropdown to style them as disabled.
In the following example we use div
instead of <CDropdownMenu>
to show <CDropdownMenu>
content.
1<div className="border rounded py-2">2 <CDropdownItem href="#">Regular link</CDropdownItem>3 <CDropdownItem href="#" disabled>4 Disabled link5 </CDropdownItem>6 <CDropdownItem href="#">Another link</CDropdownItem>7</div>
Menu alignment#
By default, a dropdown menu is automatically positioned 100% from the top and along the left side of its parent. Add aligment="end"
to right align the dropdown menu.
1<CDropdown alignment="end">2 <CDropdownToggle color="secondary">Right-aligned menu example</CDropdownToggle>3 <CDropdownMenu>4 <CDropdownItem href="#">Action</CDropdownItem>5 <CDropdownItem href="#">Another action</CDropdownItem>6 <CDropdownItem href="#">Something else here</CDropdownItem>7 <CDropdownDivider />8 <CDropdownItem href="#">Separated link</CDropdownItem>9 </CDropdownMenu>10</CDropdown>
Responsive alignment#
If you use responsive alignment, dynamic positioning is disabled.
To align right the dropdown menu with the given breakpoint or larger, add aligment="xs|sm|md|lg|xl|xxl: end"
.
1<CDropdown alignment={{ lg: 'end' }}>2 <CDropdownToggle color="secondary">3 Left-aligned but right aligned when large screen4 </CDropdownToggle>5 <CDropdownMenu>6 <CDropdownItem href="#">Action</CDropdownItem>7 <CDropdownItem href="#">Another action</CDropdownItem>8 <CDropdownItem href="#">Something else here</CDropdownItem>9 <CDropdownDivider />10 <CDropdownItem href="#">Separated link</CDropdownItem>11 </CDropdownMenu>12</CDropdown>
To align left the dropdown menu with the given breakpoint or larger, add aligment="xs|sm|md|lg|xl|xxl: start"
.
1<CDropdown alignment={{ xs: 'end', lg: 'start' }}>2 <CDropdownToggle color="secondary">3 Right-aligned but left aligned when large screen4 </CDropdownToggle>5 <CDropdownMenu>6 <CDropdownItem href="#">Action</CDropdownItem>7 <CDropdownItem href="#">Another action</CDropdownItem>8 <CDropdownItem href="#">Something else here</CDropdownItem>9 <CDropdownDivider />10 <CDropdownItem href="#">Separated link</CDropdownItem>11 </CDropdownMenu>12</CDropdown>
Menu content#
Headers#
Add a header to label sections of actions in any dropdown menu.
In the following example we use div
instead of <CDropdownMenu>
to show <CDropdownMenu>
content.
1<div className="border rounded py-2">2 <CDropdownHeader>Dropdown header</CDropdownHeader>3 <CDropdownItem href="#">Action</CDropdownItem>4 <CDropdownItem href="#">Another action</CDropdownItem>5</div>
Dividers#
Separate groups of related menu items with a divider.
In the following example we use div
instead of <CDropdownMenu>
to show <CDropdownMenu>
content.
1<div className="border rounded py-2">2 <CDropdownItem href="#">Action</CDropdownItem>3 <CDropdownItem href="#">Another action</CDropdownItem>4 <CDropdownItem href="#">Something else here</CDropdownItem>5 <CDropdownDivider />6 <CDropdownItem href="#">Separated link</CDropdownItem>7</div>
Text#
Place any freeform text within a dropdown menu with text. Note that you'll likely need additional sizing styles to constrain the menu width.
Some example text that's free-flowing within the dropdown menu.
And this is more example text.
1<div className="border rounded p-4 text-medium-emphasis" style={{ maxWidth: '200px' }}>2 <p>Some example text that's free-flowing within the dropdown menu.</p>3 <p className="mb-0">And this is more example text.</p>4</div>
Forms#
Put a form within a dropdown menu, or make it into a dropdown menu.
1<div className="border rounded py-2">2 <CForm className="px-4 py-4">3 <div className="mb-3">4 <CFormLabel htmlFor="exampleDropdownFormEmail1">Email address</CFormLabel>6 </div>7 <div className="mb-3">8 <CFormLabel htmlFor="exampleDropdownFormPassword1">Password</CFormLabel>9 <CFormInput type="password" id="exampleDropdownFormPassword1" placeholder="Password" />10 </div>11 <div className="mb-3">12 <CFormCheck id="dropdownCheck" label="Remember me" />13 </div>14 <CButton type="submit">Sign in</CButton>15 </CForm>16 <CDropdownDivider />17 <CDropdownItem href="#">New around here? Sign up</CDropdownItem>18 <CDropdownItem href="#">Forgot password?</CDropdownItem>19</div>
API#
CDropdown#
1import { CDropdown } from '@coreui/bootstrap-react'2// or3import CDropdown from '@coreui/bootstrap-react/src/components/dropdown/CDropdown'
Property | Description | Type | Default |
---|---|---|---|
alignment | Set aligment of dropdown menu. | 'start' | 'end' | { xs: 'start' | 'end' } | { sm: 'start' | 'end' } | { md: 'start' | 'end' } | { lg: 'start' | 'end' } | { xl: 'start' | 'end'} | { xxl: 'start' | 'end'} | - |
className | A string of all className you want applied to the base component. | string | - |
component | Component used for the root node. Either a string to use a HTML element or a component. | string | ComponentClass<any, any> | FunctionComponent<any> | div |
dark | Sets a darker color scheme to match a dark navbar. | boolean | - |
direction | Sets a specified direction and location of the dropdown menu. | 'dropup' | 'dropend' | 'dropstart' | - |
onHide | Callback fired when the component requests to be hidden. | () => void | - |
onShow | Callback fired when the component requests to be shown. | () => void | - |
placement | Describes the placement of your component after Popper.js has applied all the modifiers that may have flipped or altered the originally provided placement property. | 'auto' | 'top-end' | 'top' | 'top-start' | 'bottom-end' | 'bottom' | 'bottom-start' | 'right-start' | 'right' | 'right-end' | 'left-start' | 'left' | 'left-end' | bottom-start |
popper | If you want to disable dynamic positioning set this property to true . | boolean | true |
variant | Set the dropdown variant to an btn-group, dropdown, input-group, and nav-item. | 'btn-group' | 'dropdown' | 'input-group' | 'nav-item' | btn-group |
visible | Toggle the visibility of dropdown menu component. | boolean | false |
CDropdownDivider#
1import { CDropdownDivider } from '@coreui/bootstrap-react'2// or3import CDropdownDivider from '@coreui/bootstrap-react/src/components/dropdown/CDropdownDivider'
Property | Description | Type | Default |
---|---|---|---|
className | A string of all className you want applied to the component. | string | - |
CDropdownHeader#
1import { CDropdownHeader } from '@coreui/bootstrap-react'2// or3import CDropdownHeader from '@coreui/bootstrap-react/src/components/dropdown/CDropdownHeader'
Property | Description | Type | Default |
---|---|---|---|
className | A string of all className you want applied to the component. | string | - |
component | Component used for the root node. Either a string to use a HTML element or a component. | string | ComponentClass<any, any> | FunctionComponent<any> | - |
CDropdownItem#
1import { CDropdownItem } from '@coreui/bootstrap-react'2// or3import CDropdownItem from '@coreui/bootstrap-react/src/components/dropdown/CDropdownItem'
Property | Description | Type | Default |
---|---|---|---|
active | Toggle the active state for the component. | boolean | - |
className | A string of all className you want applied to the component. | string | - |
component | Component used for the root node. Either a string to use a HTML element or a component. | string | ComponentClass<any, any> | FunctionComponent<any> | a |
disabled | Toggle the disabled state for the component. | boolean | - |
href | The href attribute specifies the URL of the page the link goes to. | string | - |
CDropdownItemPlain#
1import { CDropdownItemPlain } from '@coreui/bootstrap-react'2// or3import CDropdownItemPlain from '@coreui/bootstrap-react/src/components/dropdown/CDropdownItemPlain'
Property | Description | Type | Default |
---|---|---|---|
className | A string of all className you want applied to the component. | string | - |
component | Component used for the root node. Either a string to use a HTML element or a component. | string | ComponentClass<any, any> | FunctionComponent<any> | - |
CDropdownMenu#
1import { CDropdownMenu } from '@coreui/bootstrap-react'2// or3import CDropdownMenu from '@coreui/bootstrap-react/src/components/dropdown/CDropdownMenu'
Property | Description | Type | Default |
---|---|---|---|
className | A string of all className you want applied to the base component. | string | - |
component | Component used for the root node. Either a string to use a HTML element or a component. | string | ComponentClass<any, any> | FunctionComponent<any> | - |
CDropdownToggle#
1import { CDropdownToggle } from '@coreui/bootstrap-react'2// or3import CDropdownToggle from '@coreui/bootstrap-react/src/components/dropdown/CDropdownToggle'
Property | Description | Type | Default |
---|---|---|---|
active | Toggle the active state for the component. | boolean | - |
caret | Enables pseudo element caret on toggler. | boolean | true |
className | A string of all className you want applied to the base component. | string | - |
color | Sets the color context of the component to one of Bootstrap React’s themed colors. | 'primary' | 'secondary' | 'success' | 'danger' | 'warning' | 'info' | 'dark' | 'light' | string | - |
component | Component used for the root node. Either a string to use a HTML element or a component. | string | ComponentClass<any, any> | FunctionComponent<any> | - |
disabled | Toggle the disabled state for the component. | boolean | - |
href | The href attribute specifies the URL of the page the link goes to. | string | - |
role | The role attribute describes the role of an element in programs that can make use of it, such as screen readers or magnifiers. | string | - |
shape | Select the shape of the component. | 'rounded' | 'rounded-top' | 'rounded-end' | 'rounded-bottom' | 'rounded-start' | 'rounded-circle' | 'rounded-pill' | 'rounded-0' | 'rounded-1' | 'rounded-2' | 'rounded-3' | string | - |
size | Size the component small or large. | 'sm' | 'lg' | - |
split | Similarly, create split button dropdowns with virtually the same markup as single button dropdowns, but with the addition of .dropdown-toggle-split className for proper spacing around the dropdown caret. | boolean | - |
trigger | Sets which event handlers you’d like provided to your toggle prop. You can specify one trigger or an array of them. | 'hover' | 'focus' | 'click' | click |
variant | Set the button variant to an outlined button or a ghost button. | 'outline' | - |