diff --git a/packages/react-core/src/components/Dropdown/examples/Dropdown.md b/packages/react-core/src/components/Dropdown/examples/Dropdown.md index 43383bf8ba9..8b2dad2ed74 100644 --- a/packages/react-core/src/components/Dropdown/examples/Dropdown.md +++ b/packages/react-core/src/components/Dropdown/examples/Dropdown.md @@ -444,11 +444,7 @@ const SecondaryDropdown = () => { onFocus(); }} toggle={ - setIsOpen(next)} - toggleIndicator={CaretDownIcon} - id="toggle-id-plain-text" - > + setIsOpen(next)} toggleIndicator={CaretDownIcon} id="toggle-id-plain-text"> Dropdown } @@ -1489,53 +1485,95 @@ class ImageTextDropdown extends React.Component { } ``` -### Append menu document body +### Appending document body vs parent + +Avoid passing in `document.body` when passing a value to the `menuAppendTo` prop on the Dropdown component, as it can cause accessibility issues. These issues can include, but are not limited to, being unable to enter the contents of the Dropdown options via assistive technologies (like keyboards or screen readers). + +Instead append to `"parent"` to achieve the same result without sacrificing accessibility. + +In this example, while, after making a selection, both variants retain focus on their respective Dropdown component, the options for the `document.body` variant cannot be navigated to via Voice Over. ```js import React from 'react'; -import { Dropdown, DropdownToggle, DropdownItem } from '@patternfly/react-core'; +import { Dropdown, DropdownToggle, DropdownItem, Flex } from '@patternfly/react-core'; import CaretDownIcon from '@patternfly/react-icons/dist/esm/icons/caret-down-icon'; -class MenuOnDocumentBodyDropdown extends React.Component { +class DropdownDocumentBodyVsParent extends React.Component { constructor(props) { super(props); this.state = { - isOpen: false + isBodyOpen: false, + isParentOpen: false }; - this.onToggle = isOpen => { + this.onBodyToggle = isBodyOpen => { this.setState({ - isOpen + isBodyOpen }); }; - this.onSelect = event => { + this.onBodySelect = event => { this.setState({ - isOpen: !this.state.isOpen + isBodyOpen: !this.state.isBodyOpen }); - this.onFocus(); + this.onBodyFocus(); }; - this.onFocus = () => { - const element = document.getElementById('toggle-id-menu-document-body'); + this.onBodyFocus = () => { + const element = document.getElementById('toggle-id-document-body'); + element.focus(); + }; + + this.onParentToggle = isParentOpen => { + this.setState({ + isParentOpen + }); + }; + this.onParentSelect = event => { + this.setState({ + isParentOpen: !this.state.isParentOpen + }); + this.onParentFocus(); + }; + this.onParentFocus = () => { + const element = document.getElementById('toggle-id-parent'); element.focus(); }; } render() { - const { isOpen } = this.state; - const dropdownItems = [1]; + const { isBodyOpen, isParentOpen } = this.state; + const dropdownItems = [ + Link, + + Action + , + + Disabled link + + ]; return ( -
+ - Dropdown + + Dropdown - Document Body } - isOpen={isOpen} + isOpen={isBodyOpen} dropdownItems={dropdownItems} menuAppendTo={() => document.body} /> -
+ + Dropdown - Parent +
+ } + isOpen={isParentOpen} + dropdownItems={dropdownItems} + menuAppendTo="parent" + /> + ); } }