Dropdown
Display a positioned toggleable pop-up
import Dropdown from "@intility/bifrost-react/Dropdown";Basic Dropdown
Renders a dropdown with content when a user clicks (or taps) on its
children. Use a focusable element (like <Button>) to make
sure it's accessible for keyboard users.
Not to be confused with <Select> or
<Tooltip>
<Dropdown content={<div>Dropdown content</div>}> <Button>Click to show dropdown</Button> </Dropdown>
Placement
The dropdown will be aligned to the left ('bottom-start') by default, but you
can place it anywhere you like by using the placement prop supplied by
floating-ui
For example, you can use bottom-end to align the dropdown to the bottom right.
<Dropdown content={<div>Dropdown content</div>}> <Button>Default (bottom-start)</Button> </Dropdown> <Dropdown content={<div>Dropdown content</div>} placement='bottom-end'> <Button>bottom-end</Button> </Dropdown>
Dropdown.Item
You can create a dropdown menu by styling <button> with an optional icon
using <Dropdown.Item>.
For links we recommend placing a <Box.Arrow> (using
<Inline>) on the right-hand side.
Optionally use <hr /> to separate sections.
<Dropdown.Item> supports a state prop to indicate the importance of the
action (new in 6.10):
"default"( orundefined): For neutral to positive actions"warning": Cautionary action"success": Positive action"alert": Destructive action"inactive": For actions that are currently unavailable, typically used with tooltips to explain why.
<Dropdown content={ <> <button type="button"> <Dropdown.Item>Button</Dropdown.Item> </button> <button type="button"> <Dropdown.Item icon={faCat}>With icon</Dropdown.Item> </button> <a href="#path"> <Dropdown.Item icon={faUserCircle}> <Inline align="center"> <Inline.Stretch>Link with arrow</Inline.Stretch> <Box.Arrow /> </Inline> </Dropdown.Item> </a> <a href="https://intility.com" target="_blank"> <Dropdown.Item icon={faGlobe}> <Inline align="center"> <Inline.Stretch>External link</Inline.Stretch> <Box.Arrow external /> </Inline> </Dropdown.Item> </a> <hr /> <button type="button"> <Dropdown.Item state="warning" icon={faStar}> Mark as favorite </Dropdown.Item> </button> <Tooltip placement="left" content="History not available"> <button type="button"> <Dropdown.Item state="inactive" icon={faHistory}> View history </Dropdown.Item> </button> </Tooltip> <button type="button"> <Dropdown.Item state="success" icon={faCheck}> Mark as completed </Dropdown.Item> </button> <hr /> <button type="button"> <Dropdown.Item state="alert" icon={faTrash}> Delete </Dropdown.Item> </button> </> } > <Button>Dropdown.Item examples</Button> </Dropdown>
Dropdown.Group
The <Dropdown.Group> component provides a styled button for nested dropdown
menu hierarchy. It's meant to be used inside content of <Dropdown>.
nameprop for labelling the button (supports JSX)- Optional
iconprop, styled equivalent to<Dropdown.Item icon> childrencontent will be placed inside a toggleable sub-dropdown
<Dropdown content={ <Dropdown.Group name="Group"> <Dropdown.Item>Item 1</Dropdown.Item> <Dropdown.Item>Item 2</Dropdown.Item> </Dropdown.Group> } > <Button>Two levels</Button> </Dropdown>
You can also nest them inside eachother, although we recommend keeping it to as few levels as possible for usability.
<Dropdown content={ <Dropdown.Group name="Group"> <Dropdown.Group name="Nested Group A"> <Dropdown.Item>Item A1</Dropdown.Item> <Dropdown.Item>Item A2</Dropdown.Item> </Dropdown.Group> <Dropdown.Group name="Nested Group B"> <Dropdown.Item>Item B1</Dropdown.Item> <Dropdown.Item>Item B2</Dropdown.Item> </Dropdown.Group> </Dropdown.Group> } > <Button>Three levels</Button> </Dropdown>
Controlled state
By passing a boolean to the visible prop you can control programmatically
whether the dropdown is open. Keep your state in sync with an onHide handler.
import { useState } from "react"; import Dropdown from "@intility/bifrost-react/Dropdown"; import Button from "@intility/bifrost-react/Button"; export default function () { const [open, setOpen] = useState(false); return ( <Dropdown content={<div>Click anywhere outside to close me</div>} visible={open} onHide={() => setOpen(false)} > <Button onClick={() => setOpen(!open)}>Click me to toggle</Button> </Dropdown> ); }