-
Notifications
You must be signed in to change notification settings - Fork 0
Dropdown
Windmill dropdowns are completely accessible. As soon as you open it, you'll notice that the close first element of the dropdown is already focused. This is because the focus is traped inside the dropdown, so you can navigate with keyboard without the focus leaving the dialog.
Once it is closed, through a click or Esc
key press, focus is restored to the element that opened the dropdown.
import { Dropdown, DropdownItem } from '@narimanb/wreactui'
Code
<Dropdown isOpen={true} onClose={() => {}}>
<DropdownItem tag="a" href="#" className="justify-between">
<span>Messages</span>
<Badge type="danger">13</Badge>
</DropdownItem>
<DropdownItem onClick={() => alert('Alerts!')}>
<span>Alerts</span>
</DropdownItem>
</Dropdown>
There are two alignment options, applied by the align
prop: left
(default) and right
.
It defines to which side the dropdown will be rendered. Note that it is absolute
positioned to the parent element, so the parent must be relative
positioned.
Code
<Dropdown align="right" isOpen={true} onClose={() => {}}>
<DropdownItem tag="a" href="#" className="justify-between">
<span>Messages</span>
<Badge type="danger">13</Badge>
</DropdownItem>
<DropdownItem onClick={() => alert('Alerts!')}>
<span>Alerts</span>
</DropdownItem>
</Dropdown>
The isOpen
prop defines if the dropdown is visible.
Code
<Dropdown isOpen={true} onClose={() => {}}>
...
</Dropdown>
The onClose
prop is responsible for the function that will change the value of isOpen
, closing the dropdown.
It is important that the close function sets isOpen to false, and not just toggle the value, to avoid opening the dropdown using Esc. See complete example below.
<Dropdown isOpen={true} onClose={() => {}}>
...
</Dropdown>
The DropdownItem
is in fact a Button
, styled differently and you're limited to the tag
prop.
Note that every other prop, that is not part of the Button
API exclusively, like onClick
(things you would normally do in React) are still available.
Code
<DropdownItem tag="a" href="#" className="justify-between">
<span>Messages</span>
<Badge type="danger">13</Badge>
</DropdownItem>
<DropdownItem onClick={() => alert('Alerts!')}>
<span>Alerts</span>
</DropdownItem>
This is what a complete example component looks like, with handlers, state, etc.
Code
import React, { useState } from 'react'
import { Dropdown, DropdownItem, Badge, Button } from '@narimanb/wreactui'
function DropdownExample() {
const [isOpen, setIsOpen] = useState(false)
function toggleDropdown() {
setIsOpen(!isOpen)
}
return (
<div className="relative">
<Button onClick={toggleDropdown} aria-label="Notifications" aria-haspopup="true">
Open dropdown
</Button>
<Dropdown isOpen={isOpen} onClose={() => setIsOpen(false)}>
<DropdownItem tag="a" href="#" className="justify-between">
<span>Messages</span>
<Badge type="danger">13</Badge>
</DropdownItem>
<DropdownItem onClick={() => alert('Alerts!')}>
<span>Alerts</span>
</DropdownItem>
</Dropdown>
</div>
)
}
export default DropdownExample
Prop | Description | Type | Default |
---|---|---|---|
align |
define the alignment of the dropdown, relative to it's parent |
left , right
|
left |
isOpen |
required define if the dropdown is visible |
boolean |
|
onClose |
required closes the dropdown |
function |
Original source: Estevan Maito https://windmillui.com/react-ui