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

Shift focus to accordion content when expanded #4442

Merged
merged 5 commits into from
Feb 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
36 changes: 36 additions & 0 deletions src/components/accordion/__snapshots__/accordion.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,11 @@ exports[`EuiAccordion behavior closes when clicked twice 1`] = `
</button>
</div>
<div
aria-labelledby="generated-id"
className="euiAccordion__childWrapper"
id="12"
role="region"
tabIndex={-1}
>
<EuiResizeObserver
onResize={[Function]}
Expand Down Expand Up @@ -105,8 +108,11 @@ exports[`EuiAccordion behavior opens when clicked once 1`] = `
</button>
</div>
<div
aria-labelledby="generated-id"
className="euiAccordion__childWrapper"
id="11"
role="region"
tabIndex={-1}
>
<EuiResizeObserver
onResize={[Function]}
Expand Down Expand Up @@ -152,8 +158,11 @@ exports[`EuiAccordion is rendered 1`] = `
</button>
</div>
<div
aria-labelledby="generated-id"
class="euiAccordion__childWrapper"
id="0"
role="region"
tabindex="-1"
>
<div>
<div
Expand Down Expand Up @@ -184,8 +193,11 @@ exports[`EuiAccordion props arrowDisplay none is rendered 1`] = `
</button>
</div>
<div
aria-labelledby="generated-id"
class="euiAccordion__childWrapper"
id="6"
role="region"
tabindex="-1"
>
<div>
<div
Expand Down Expand Up @@ -228,8 +240,11 @@ exports[`EuiAccordion props arrowDisplay right is rendered 1`] = `
</button>
</div>
<div
aria-labelledby="generated-id"
class="euiAccordion__childWrapper"
id="5"
role="region"
tabindex="-1"
>
<div>
<div
Expand Down Expand Up @@ -276,8 +291,11 @@ exports[`EuiAccordion props buttonContent is rendered 1`] = `
</button>
</div>
<div
aria-labelledby="generated-id"
class="euiAccordion__childWrapper"
id="2"
role="region"
tabindex="-1"
>
<div>
<div
Expand Down Expand Up @@ -316,8 +334,11 @@ exports[`EuiAccordion props buttonContentClassName is rendered 1`] = `
</button>
</div>
<div
aria-labelledby="generated-id"
class="euiAccordion__childWrapper"
id="1"
role="region"
tabindex="-1"
>
<div>
<div
Expand Down Expand Up @@ -363,8 +384,11 @@ exports[`EuiAccordion props extraAction is rendered 1`] = `
</div>
</div>
<div
aria-labelledby="generated-id"
class="euiAccordion__childWrapper"
id="3"
role="region"
tabindex="-1"
>
<div>
<div
Expand Down Expand Up @@ -403,8 +427,11 @@ exports[`EuiAccordion props forceState is rendered 1`] = `
</button>
</div>
<div
aria-labelledby="generated-id"
class="euiAccordion__childWrapper"
id="7"
role="region"
tabindex="-1"
>
<div>
<div
Expand Down Expand Up @@ -447,8 +474,11 @@ exports[`EuiAccordion props initialIsOpen is rendered 1`] = `
</button>
</div>
<div
aria-labelledby="generated-id"
class="euiAccordion__childWrapper"
id="4"
role="region"
tabindex="-1"
>
<div>
<div
Expand Down Expand Up @@ -498,8 +528,11 @@ exports[`EuiAccordion props isLoading is rendered 1`] = `
</div>
</div>
<div
aria-labelledby="generated-id"
class="euiAccordion__childWrapper"
id="9"
role="region"
tabindex="-1"
>
<div>
<div
Expand Down Expand Up @@ -549,8 +582,11 @@ exports[`EuiAccordion props isLoadingMessage is rendered 1`] = `
</div>
</div>
<div
aria-labelledby="generated-id"
class="euiAccordion__childWrapper"
id="10"
role="region"
tabindex="-1"
>
<div>
<div
Expand Down
14 changes: 14 additions & 0 deletions src/components/accordion/accordion.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -191,5 +191,19 @@ describe('EuiAccordion', () => {
expect(onToggleHandler).toBeCalled();
expect(onToggleHandler).toBeCalledWith(false);
});

it('moves focus to the content when expanded', () => {
const component = mount<EuiAccordion>(<EuiAccordion id={getId()} />);
const accordionClass = component.instance();
const childWrapper = accordionClass.childWrapper;

expect(childWrapper).not.toBeFalsy();
expect(childWrapper).not.toBe(document.activeElement);

// click button
component.find('button').simulate('click');

expect(childWrapper).toBe(document.activeElement);
});
});
});
6 changes: 6 additions & 0 deletions src/components/accordion/accordion.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ export class EuiAccordion extends Component<
isOpen: !prevState.isOpen,
}),
() => {
if (this.state.isOpen && this.childWrapper) {
this.childWrapper.focus();
}
this.props.onToggle && this.props.onToggle(this.state.isOpen);
}
);
Expand Down Expand Up @@ -288,6 +291,9 @@ export class EuiAccordion extends Component<
ref={(node) => {
this.childWrapper = node;
}}
tabIndex={-1}
chandlerprall marked this conversation as resolved.
Show resolved Hide resolved
role="region"
aria-labelledby={buttonId}
id={id}>
<EuiResizeObserver onResize={this.setChildContentHeight}>
{(resizeRef) => (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,11 @@ exports[`EuiCollapsibleNavGroup when isCollapsible is true will render an accord
</button>
</div>
<div
aria-labelledby="generated-id"
class="euiAccordion__childWrapper"
id="id"
role="region"
tabindex="-1"
>
<div>
<div
Expand Down