Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[EuiAccordion] Improve keyboard accessibility of tabbable children #7064

Merged
merged 6 commits into from
Aug 10, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 62 additions & 2 deletions src/components/accordion/accordion.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import React, { Component, HTMLAttributes, ReactNode } from 'react';
import classNames from 'classnames';
import { tabbable, FocusableElement } from 'tabbable';

import { CommonProps } from '../common';

Expand Down Expand Up @@ -108,9 +109,13 @@ export type EuiAccordionProps = CommonProps &
isDisabled?: boolean;
};

type EuiAccordionState = {
isOpen: boolean;
};

export class EuiAccordionClass extends Component<
WithEuiThemeProps & EuiAccordionProps,
{ isOpen: boolean }
EuiAccordionState
> {
static defaultProps = {
initialIsOpen: false,
Expand All @@ -125,6 +130,7 @@ export class EuiAccordionClass extends Component<

childContent: HTMLDivElement | null = null;
childWrapper: HTMLDivElement | null = null;
tabbableChildren: FocusableElement[] | null = null;

state = {
isOpen: this.props.forceState
Expand Down Expand Up @@ -152,10 +158,27 @@ export class EuiAccordionClass extends Component<

componentDidMount() {
this.setChildContentHeight();
if (!this.isOpen) this.preventTabbing();
}

componentDidUpdate() {
componentDidUpdate(
prevProps: EuiAccordionProps,
prevState: EuiAccordionState
) {
this.setChildContentHeight();

if (
(prevProps.forceState === 'open' && this.props.forceState === 'closed') ||
(prevState.isOpen === true && this.state.isOpen === false)
) {
this.preventTabbing();
}
if (
(prevProps.forceState === 'closed' && this.props.forceState === 'open') ||
(prevState.isOpen === false && this.state.isOpen === true)
) {
this.enableTabbing();
}
}

onToggle = () => {
Expand All @@ -177,6 +200,43 @@ export class EuiAccordionClass extends Component<
}
};

// When accordions are closed, tabbable children should not be present in the tab order
preventTabbing = () => {
if (this.childContent) {
// Re-check for children on every close - content can change dynamically
this.tabbableChildren = tabbable(this.childContent);

this.tabbableChildren.forEach((element) => {
// If the element has an existing `tabIndex` set, make sure we can restore it
const originalTabIndex = element.getAttribute('tabIndex');
if (originalTabIndex) {
element.setAttribute('data-original-tabindex', originalTabIndex);
}
Comment on lines +210 to +214
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is good! I'm not sure I would have thought to retain the original tab index.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current EuiDataGridCell usage doesn't do this although it probably should 😂 but I figured I'd add that extra flexibility I'm in here, since accordion content is much more variable than datagrid cell content.


element.setAttribute('tabIndex', '-1');
});
}
};

enableTabbing = () => {
// If no tabbable children were set, we don't need to re-enable anything
if (this.tabbableChildren) {
this.tabbableChildren.forEach((element) => {
const originalTabIndex = element.getAttribute('data-original-tabindex');
if (originalTabIndex) {
// If the element originally had an existing `tabIndex` set, restore it
element.setAttribute('tabIndex', originalTabIndex);
element.removeAttribute('data-original-tabindex');
} else {
// If not, remove the tabIndex property
element.removeAttribute('tabIndex');
}
});
// Cleanup - unset the list of children
this.tabbableChildren = null;
}
};

setChildContentRef = (node: HTMLDivElement | null) => {
this.childContent = node;
};
Expand Down