Skip to content

Commit

Permalink
feat(PF4-Dropdown): Enhance keyboard interaction for a11y (#815)
Browse files Browse the repository at this point in the history
* feat(PF4-Dropdown): Enhance keyboard interactions for a11y

- Fixes esc key interaction
- Introduces focus-trap-react

Fixes #557

* Use KEY_CODES constant
  • Loading branch information
michaelkro authored and tlabaj committed Oct 24, 2018
1 parent 2e79a95 commit cc89db6
Show file tree
Hide file tree
Showing 5 changed files with 330 additions and 1,824 deletions.
3 changes: 2 additions & 1 deletion packages/patternfly-4/react-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"dependencies": {
"@patternfly/react-icons": "^2.5.2",
"@patternfly/react-styles": "^2.3.0",
"exenv": "^1.2.2"
"exenv": "^1.2.2",
"focus-trap-react": "^4.0.1"
},
"peerDependencies": {
"prop-types": "^15.6.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { Children, cloneElement } from 'react';
import styles from '@patternfly/patternfly-next/components/Dropdown/dropdown.css';
import { css } from '@patternfly/react-styles';
import PropTypes from 'prop-types';
import FocusTrap from 'focus-trap-react';
import DropdownMenu from './DropdownMenu';

export const DropdownPosition = {
Expand Down Expand Up @@ -71,9 +72,13 @@ class Dropdown extends React.Component {
}}
>
{toggle && Children.map(toggle, oneToggle => cloneElement(oneToggle, { parentRef: this.parentRef, isOpen }))}
<DropdownMenu isOpen={isOpen} aria-labelledby={id} onClick={event => onSelect && onSelect(event)}>
{children}
</DropdownMenu>
{isOpen && (
<FocusTrap>
<DropdownMenu isOpen={isOpen} aria-labelledby={id} onClick={event => onSelect && onSelect(event)}>
{children}
</DropdownMenu>
</FocusTrap>
)}
</div>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { Component } from 'react';
import styles from '@patternfly/patternfly-next/components/Dropdown/dropdown.css';
import { css } from '@patternfly/react-styles';
import PropTypes from 'prop-types';
import { KEY_CODES } from '../../internal/constants';

const propTypes = {
/** Anything which can be rendered as dropdown toggle */
Expand Down Expand Up @@ -51,8 +52,9 @@ class DropdownToggle extends Component {
};

onEscPress = event => {
const { parentRef } = this.props;
const keyCode = event.keyCode || event.which;
if (keyCode === 27) {
if (keyCode === KEY_CODES.ESCAPE_KEY && parentRef && parentRef.contains(event.target)) {
this.props.onToggle && this.props.onToggle(false);
this.toggle.focus();
}
Expand Down
Loading

0 comments on commit cc89db6

Please sign in to comment.