
React-Bootstrap Dropdown Component
React-Bootstrap is a front-end framework that was designed keeping react in mind. Dropdown Componentprovides a way to displaying lists of links or more actions within a menu when clicked over it. We can use the following approach in ReactJS to use the react-bootstrap Dropdown Component.
Dropdown Props:
- alignRight: It is used to align the menu to the right side of the Dropdown toggle.
- as: It can be used as a custom element type for this component.
- drop: It is used to determine the location and direction of the Menu in relation to its toggle.
- flip: It is used to flip the dropdown in case of overlapping on the reference element.
- focusFirstItemOnShow: When the dropdown is opened, it is used to control the focus behavior for it.
- navbar: It is the attribute that is by default false and indicates whether dropdown is navbar related or not.
- onSelect: It is a callback function that is triggered when a menu item is selected.
- onToggle: It is used to trigger a callback when the visibility of the dropdown needs to be changed.
- show: It is used to indicate whether the dropdown is visible or not.
- bsPrefix:It is an escape hatch for working with strongly customized bootstrap CSS.
SplitButton Props:
- disabled: It is used to disable the button.
- href: It is used to pass the href attribute to the underlying non-toggle button.
- id: It is the general HTML id attribute for the toggle button.
- menuAlign: It is used to responsively align the dropdown menu.
- menuRole: It is used for the ARIA accessible role applied which is applied to the menu component.
- onClick: It is the callback function that is passed as a handler for the non-toggle button.
- renderMenuOnMount: It is used to indicate whether to render the dropdown menu before the first time it is shown in the DOM.
- rootCloseEvent: It is used to close the component when which event is triggered outside this component.
- size: It denotes the size of the component.
- target: For the non-toggle Button, it is an anchor target passed to it.
- title: It is used to define the content of non-toggle Button.
- toggleLabel: For the toggle button, it is the accessible label.
- type: It is used to pass the type for the non-toggle button.
- variant: It is used to indicate the style variant for it.
- bsPrefix:It is an escape hatch for working with strongly customized bootstrap CSS.
Dropdown.Toggle Props:
- as: It can be used as a custom element type for this component.
- childBsPrefix: It is used for the DropdownButtonto pass through to the underlying button or whatever from it.
- eventKey: It is used to uniquely identify the dropdown toggle component.
- id: It is used to pass the HTML id attribute to this element.
- split: It is used to pass the split attribute to this element.
- bsPrefix:It is an escape hatch for working with strongly customized bootstrap CSS.
Dropdown.Menu Props:
- align: It is used for the dropdown menu to align it to the specified side of the container.
- alignRight: It is used for the dropdown menu to align it to the right side of the container.
- as: It can be used as a custom element type for this component.
- flip: It is used to flip the dropdown to its opposite placement.
- onSelect: It is a callback function that is triggered when the menu item is selected.
- popperConfig: It is used to pass the set of popper options to the popper directly.
- renderOnMount: It is used to indicate whether to render the dropdown menu before the first time it is shown in the DOM.
- rootCloseEvent: It is used to close the component when which event is triggered outside this component.
- show: It is used to indicate whether the dropdown menu is visible or not.
- bsPrefix:It is an escape hatch for working with strongly customized bootstrap CSS.
Dropdown.Item Props:
- active: It can be used to mark the menu item as active.
- as: It can be used as a custom element type for this component.
- disabled: It is used to make the menu item disabled.
- eventKey: It is used to uniquely identify the selected menu item.
- href: It is used to pass the href attribute to this element.
- onClick: It is a callback function that is triggered when the menu item is clicked.
- onSelect: It is a callback function that is triggered when the menu item is selected.
- bsPrefix:It is an escape hatch for working with strongly customized bootstrap CSS.
Dropdown.Header Props:
- as: It can be used as a custom element type for this component.
- bsPrefix:It is an escape hatch for working with strongly customized bootstrap CSS.
Dropdown.Divider Props:
- as: It can be used as a custom element type for this component.
- bsPrefix:It is an escape hatch for working with strongly customized bootstrap CSS.
Creating React Application And Installing Module:
Step 1: Create a React application using the following command:
npx create-react-app foldernameStep 2: After creating your project folder i.e. foldername, move to it using the following command:
cd foldernameStep 3: After creating the ReactJS application, Install therequiredmodule using the following command:
npm install react-bootstrap npm install bootstrap
Project Structure: It will look like the following.

Project Structure
Example: Now write down the following code in the App.js file. Here, App is our default component where we have written our code.
App.js
Step to Run Application: Run the application using the following command from the root directory of the project:
npm startOutput: Now open your browser and go to http://localhost/, you will see the following output:
Reference:https://react-bootstrap.github.io/components/dropdowns/
- Burlington county times obituaries 2019
- Paul mitchell near me
- Is borderlands 3 fun solo
- Eddie hall bench press
Dropdown
const MenuContainer =styled("div")`
display: ${(p)=>(p.show ?"flex":"none")};
min-width: px;
position: absolute;
z-index: ;
flex-direction: column;
border: 1px solid #e5e5e5;
background-color: white;
box-shadow: 0 5px 15px rgba(0, 0, 0, );
`;
constMenu=({ role })=>{
const{ show, onClose, props }=useDropdownMenu({
flip:true,
offset:[0,8],
});
return(
<MenuContainer{props}role={role}show={show}>
<button
type="button"
onClick={onClose}
className="text-left hover:bg-brand px-6 py-2"
>
Item 1
</button>
<button
type="button"
onClick={onClose}
className="text-left hover:bg-brand px-6 py-2"
>
Item 2
</button>
</MenuContainer>
);
};
constToggle=({ id, children })=>{
const[props,{ show, toggle }]=useDropdownToggle();
return(
<button
type="button"
className="btn"
id={id}
{props}
onClick={toggle}
>
{children}
</button>
);
};
constDropdownButton=({
show,
onToggle,
drop,
alignEnd,
title,
role,
})=>(
<Dropdown
show={show}
onToggle={onToggle}
drop={drop}
alignEnd={alignEnd}
itemSelector="button:not(:disabled)"
>
{({ props })=>(
<div{props}className="relative inline-block">
<Toggleid="example-toggle">{title}</Toggle>
<Menurole={role}/>
</div>
)}
</Dropdown>
);
const ButtonToolbar =styled("div")`
& > * + * {
margin-left: 12px;
}
`;
const[show, setShow]=useState(false);
<ButtonToolbarclassName="dropdown-example">
<DropdownButton
show={show}
onToggle={(nextShow)=>setShow(nextShow)}
title={`${show ?"Close":"Open"} Dropdown`}
/>
<DropdownButtonalignEndtitle="Align right"/>
<DropdownButtondrop="up"title="Drop up"/>
<DropdownButtonrole="menu"title="Role 'menu'"/>
</ButtonToolbar>;
Best JavaScript code snippets using react-bootstrap.DropdownButton(Showing top 15 results out of )
Bootstrap dropdown react
Dropdowns
Toggle contextual overlays for displaying lists of links and more with the Bootstrap dropdown plugin
Overview#
Dropdowns are toggleable, contextual overlays for displaying lists of links and more. Like overlays, Dropdowns are built using a third-party library Popper.js, which provides dynamic positioning and viewport detection.
Accessibility#
The WAIARIA standard defines a widget, but it's very specific to a certain kind of menu. ARIA menus, must only contain , , or .
On the other hand, Bootstrap's dropdowns are designed to more generic and application in a variety of situations. For this reason we don't automatically add the menu roles to the markup. We do implement some basic keyboard navigation, and if you do provide the "menu" role, react-bootstrap will do its best to ensure the focus management is compliant with the ARIA authoring guidelines for menus.
Examples#
Single button dropdowns#
The basic Dropdown is composed of a wrapping and inner , and . By default the will render a component and accepts all the same props.
<Dropdown>
<Dropdown.Togglevariant="success"id="dropdown-basic">
Dropdown Button
</Dropdown.Toggle>
<Dropdown.Menu>
<Dropdown.Itemhref="#/action-1">Action</Dropdown.Item>
<Dropdown.Itemhref="#/action-2">Another action</Dropdown.Item>
<Dropdown.Itemhref="#/action-3">Something else</Dropdown.Item>
</Dropdown.Menu>
</Dropdown>
Since the above is such a common configuration react-bootstrap provides the component to help reduce typing. Provide a prop and some s and you're ready to go.
<DropdownButtonid="dropdown-basic-button"title="Dropdown button">
<Dropdown.Itemhref="#/action-1">Action</Dropdown.Item>
<Dropdown.Itemhref="#/action-2">Another action</Dropdown.Item>
<Dropdown.Itemhref="#/action-3">Something else</Dropdown.Item>
</DropdownButton>
DropdownButton will forward Button props to the underlying Toggle component
<>
{['Primary','Secondary','Success','Info','Warning','Danger'].map(
(variant)=>(
<DropdownButton
as={ButtonGroup}
key={variant}
id={`dropdown-variants-${variant}`}
variant={variant.toLowerCase()}
title={variant}
>
<Dropdown.ItemeventKey="1">Action</Dropdown.Item>
<Dropdown.ItemeventKey="2">Another action</Dropdown.Item>
<Dropdown.ItemeventKey="3"active>
Active Item
</Dropdown.Item>
<Dropdown.Divider/>
<Dropdown.ItemeventKey="4">Separated link</Dropdown.Item>
</DropdownButton>
),
)}
</>
Split button dropdowns#
Similarly, You create a split dropdown by combining the Dropdown components with another Button and a ButtonGroup.
<Dropdownas={ButtonGroup}>
<Buttonvariant="success">Split Button</Button>
<Dropdown.Togglesplitvariant="success"id="dropdown-split-basic"/>
<Dropdown.Menu>
<Dropdown.Itemhref="#/action-1">Action</Dropdown.Item>
<Dropdown.Itemhref="#/action-2">Another action</Dropdown.Item>
<Dropdown.Itemhref="#/action-3">Something else</Dropdown.Item>
</Dropdown.Menu>
</Dropdown>
As with DropdownButton, is provided as convenience component.
<>
{['Primary','Secondary','Success','Info','Warning','Danger'].map(
(variant)=>(
<SplitButton
key={variant}
id={`dropdown-split-variants-${variant}`}
variant={variant.toLowerCase()}
title={variant}
>
<Dropdown.ItemeventKey="1">Action</Dropdown.Item>
<Dropdown.ItemeventKey="2">Another action</Dropdown.Item>
<Dropdown.ItemeventKey="3"active>
Active Item
</Dropdown.Item>
<Dropdown.Divider/>
<Dropdown.ItemeventKey="4">Separated link</Dropdown.Item>
</SplitButton>
),
)}
</>
Sizing#
Dropdowns work with buttons of all sizes.
<>
<divclassName="mb-2">
{[DropdownButton, SplitButton].map((DropdownType, idx)=>(
<DropdownType
as={ButtonGroup}
key={idx}
id={`dropdown-button-drop-${idx}`}
size="lg"
title="Drop large"
>
<Dropdown.ItemeventKey="1">Action</Dropdown.Item>
<Dropdown.ItemeventKey="2">Another action</Dropdown.Item>
<Dropdown.ItemeventKey="3">Something else here</Dropdown.Item>
<Dropdown.Divider/>
<Dropdown.ItemeventKey="4">Separated link</Dropdown.Item>
</DropdownType>
))}
</div>
<div>
{[DropdownButton, SplitButton].map((DropdownType, idx)=>(
<DropdownType
as={ButtonGroup}
key={idx}
id={`dropdown-button-drop-${idx}`}
size="sm"
variant="secondary"
title="Drop small"
>
<Dropdown.ItemeventKey="1">Action</Dropdown.Item>
<Dropdown.ItemeventKey="2">Another action</Dropdown.Item>
<Dropdown.ItemeventKey="3">Something else here</Dropdown.Item>
<Dropdown.Divider/>
<Dropdown.ItemeventKey="4">Separated link</Dropdown.Item>
</DropdownType>
))}
</div>
</>
Dark dropdowns#
Opt into darker dropdowns to match a dark navbar or custom style by adding onto an existing . Alternatively, use when using the component.
<>
<Dropdown>
<Dropdown.Toggleid="dropdown-button-dark-example1"variant="secondary">
Dropdown Button
</Dropdown.Toggle>
<Dropdown.Menuvariant="dark">
<Dropdown.Itemhref="#/action-1"active>
Action
</Dropdown.Item>
<Dropdown.Itemhref="#/action-2">Another action</Dropdown.Item>
<Dropdown.Itemhref="#/action-3">Something else</Dropdown.Item>
<Dropdown.Divider/>
<Dropdown.Itemhref="#/action-4">Separated link</Dropdown.Item>
</Dropdown.Menu>
</Dropdown>
<DropdownButton
id="dropdown-button-dark-example2"
variant="secondary"
menuVariant="dark"
title="Dropdown button"
className="mt-2"
>
<Dropdown.Itemhref="#/action-1"active>
Action
</Dropdown.Item>
<Dropdown.Itemhref="#/action-2">Another action</Dropdown.Item>
<Dropdown.Itemhref="#/action-3">Something else</Dropdown.Item>
<Dropdown.Divider/>
<Dropdown.Itemhref="#/action-4">Separated link</Dropdown.Item>
</DropdownButton>
</>
Using in a :
<Navbarvariant="dark"bg="dark"expand="lg">
<Containerfluid>
<Navbar.Brandhref="#home">React-Bootstrap</Navbar.Brand>
<Navbar.Togglearia-controls="navbar-dark-example"/>
<Navbar.Collapseid="navbar-dark-example">
<Nav>
<NavDropdown
id="nav-dropdown-dark-example"
title="Dropdown"
menuVariant="dark"
>
<NavDropdown.Itemhref="#action/">Action</NavDropdown.Item>
<NavDropdown.Itemhref="#action/">Another action</NavDropdown.Item>
<NavDropdown.Itemhref="#action/">Something</NavDropdown.Item>
<NavDropdown.Divider/>
<NavDropdown.Itemhref="#action/">Separated link</NavDropdown.Item>
</NavDropdown>
</Nav>
</Navbar.Collapse>
</Container>
</Navbar>
Drop directions#
Trigger dropdown menus above, below, left, or to the right of their toggle elements, with the prop.
<>
<divclassName="mb-2">
{['up','down','start','end'].map((direction)=>(
<DropdownButton
as={ButtonGroup}
key={direction}
id={`dropdown-button-drop-${direction}`}
drop={direction}
variant="secondary"
title={` Drop ${direction}`}
>
<Dropdown.ItemeventKey="1">Action</Dropdown.Item>
<Dropdown.ItemeventKey="2">Another action</Dropdown.Item>
<Dropdown.ItemeventKey="3">Something else here</Dropdown.Item>
<Dropdown.Divider/>
<Dropdown.ItemeventKey="4">Separated link</Dropdown.Item>
</DropdownButton>
))}
</div>
<div>
{['up','down','start','end'].map((direction)=>(
<SplitButton
key={direction}
id={`dropdown-button-drop-${direction}`}
drop={direction}
variant="secondary"
title={`Drop ${direction}`}
>
<Dropdown.ItemeventKey="1">Action</Dropdown.Item>
<Dropdown.ItemeventKey="2
npm
A simple Dropdown Menu for React. Commonly used as a 'User Settings' menu on websites where users login.
Installation
Include bootstrap in your project(if not already included)
Usage
A simple example using OnClick events. Download and run the demo for more examples, or browse the examples.
DropdownMenu props
Name | Type | Required | Default | Possible Values | Description |
---|---|---|---|---|---|
userName | String | No | undefined | Any string(e.g. 'John Doe') | When provided, will render 'Logged in as: {userName}' in the top MenuItem |
css | Object | No | See Css below | Object as with attributes as described below | CSS as JS variables to be overridden |
position | String | No | right | left, center, right | Changes the menu's horizontal drop position relative to the trigger |
triggerType | String | No | icon | icon, text, image | The Type of drop trigger |
trigger | String | No | glyphicon glyphicon-cog | Depends on triggerType | For 'icon' triggerType: Any bootstrap glyphicon(http://getbootstrap.com/components/) For 'text' triggerType: Any String can be used For 'image' triggerType: Path to image(e.g. "/images/myimage.png") |
triggerWidth | String | No | 50px (only applies to triggerType='image') | Any numeric value with 'px' appended(e.g. '45px') | The width to render the image trigger image. |
triggerHeight | String | No | 50px (only applies to triggerType='image') | Any numeric value with 'px' appended(e.g. '45px') | The height to render the image trigger image. |
caratColor | String | No | # (Black) | Any 6 digit hex value (e.g. #F4E3A2) | The color of the DropDown carat (for triggerType 'image' and 'text' only) |
iconColor | String | No | # (Black) | Any 6 digit hex value (e.g. #F4E3A2) | The color of the Bootstrap icon |
fadeIn | Boolean | No | false | true or false | Dropdown menu will fade in when set to true |
onMouseover | Function | No | undefined | A Function | A function that will fire when the mouse pointer hovers over the DropdownMenu trigger |
onMouseout | Function | No | undefined | A Function | A function that will fire when the mouse pointer moves away from the DropdownMenu trigger |
MenuItem props
Name | Type | Required | Default | Possible Values | Description |
---|---|---|---|---|---|
type | String | No | undefined | separator | A horizontal rule. text prop is not required when using this type. |
text | String | Yes | undefined | Any String value | Text value of the Link in the MenuItem |
disabled | Boolean | No | false | true or false | Disables a MenuItem |
location | String | No | undefined | Any String value | An absolute or relative path |
linkStyle | Object | No | inherited | Hash containing javascript styles(not CSS). See examples for more info | |
onClick | Function | No | undefined | A Function | A function that will fire when the MenuItem Link is clicked |
Css Override
The following CSS(as JS) can be passed to the via the prop to override color, padding etc. NOTE: only applies to the child component.
Download Examples
Backlog
- More Error handling.
- Extend CSS styling so that user defined styles can be passed in as props.
You will also be interested:
- Harbor freight service wrench set
- 2020 ford transit accessories
- Marriage license washington county ohio
- Brothers in arms lyrics
- The repo store military trail
- 2020 nfl draft class
- 7/16 glue sticks
- Who to follow on goodreads
Also exported as from .
#
alignEnd
Aligns the dropdown menu to the 'end' of it's placement position. Generally this is provided by the parent component, but may also be specified as a prop directly.
type:boolean
#
childrenrequired
A render prop that returns a Menu element. The argument should spread through to a component that can accept a ref.
type:{Function ({ show: boolean, alignEnd: boolean, close: (?SyntheticEvent) => void, placement: Placement, update: () => void, forceUpdate: () => void, props: { ref: (?HTMLElement) => void, style: { [string]: string | number }, aria-labelledby: ?string }, arrowProps: { ref: (?HTMLElement) => void, style: { [string]: string | number }, }, }) => React.Element}
#
flip
Enables the Popper.js modifier, allowing the Dropdown to automatically adjust it's placement in case of overlap with the viewport or toggle. Refer to the flip docs for more info
type:boolean
#
offset
type:Offset
#
popperConfig
A set of popper options and props passed directly to react-popper's Popper component.
type:Omit<UsePopperOptions, 'enabled''placement'>
#
rootCloseEvent
Override the default event used by RootCloseWrapper.
type:RootCloseOptions['clickTrigger']
#
show
Controls the visible state of the menu, generally this is provided by the parent component, but may also be specified as a prop directly.
type:boolean
#
usePopper