-
Notifications
You must be signed in to change notification settings - Fork 861
Create a status component and use it in EUI Docs to announce page loads to screen readers #5991
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
772b22d
Announce route changes to screen readers consistently.
215dc57
Refactoring for a true status component.
79fe9b5
Adding documentation for status component.
6d99efc
Adding CHANGELOG entry.
04863d8
Tweaking docs and skip link style.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import React from 'react'; | ||
|
|
||
| import { EuiScreenReaderStatus, EuiText } from '../../../../src/components'; | ||
|
|
||
| export default () => ( | ||
| <EuiText> | ||
| <p>This is the first paragraph. It is visible to all.</p> | ||
| <EuiScreenReaderStatus statusMessage="User-defined status message" /> | ||
| <p> | ||
| This is the third paragraph. If you turn on a screen reader, there is a | ||
| status message between the paragraphs. | ||
| </p> | ||
| </EuiText> | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ import { | |
| EuiPageBody, | ||
| EuiSkipLink, | ||
| } from '../../../src/components'; | ||
| import { EuiScreenReaderStatus } from '../../../src/components/accessibility/screen_reader_status'; | ||
|
|
||
| import { keys } from '../../../src/services'; | ||
|
|
||
|
|
@@ -69,6 +70,10 @@ export const AppView = ({ children, currentRoute }) => { | |
|
|
||
| return ( | ||
| <LinkWrapper> | ||
| <EuiScreenReaderStatus | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tested in Safari and Firefox with VoiceOver (MacOS Monterey). Request is out for more screen reader testing, but I have 💯 confidence this technique will work. |
||
| statusMessage={`${currentRoute.name} - Elastic UI Framework`} | ||
| shouldReceiveFocus | ||
| /> | ||
| <EuiSkipLink | ||
| destinationId="start-of-content" | ||
| position="fixed" | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
...nents/accessibility/screen_reader_status/__snapshots__/screen_reader_status.test.tsx.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| // Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
|
||
| exports[`EuiScreenReaderStatus it renders 1`] = ` | ||
| <div | ||
| aria-live="polite" | ||
| class="euiScreenReaderOnly" | ||
| role="status" | ||
| /> | ||
| `; | ||
|
|
||
| exports[`EuiScreenReaderStatus it sets \`statusMessage\` using document title 1`] = ` | ||
| <EuiScreenReaderStatus> | ||
| <EuiScreenReaderOnly> | ||
| <div | ||
| aria-live="polite" | ||
| className="euiScreenReaderOnly" | ||
| role="status" | ||
| > | ||
| Default title | ||
| </div> | ||
| </EuiScreenReaderOnly> | ||
| </EuiScreenReaderStatus> | ||
| `; | ||
|
|
||
| exports[`EuiScreenReaderStatus it sets \`statusMessage\` with user defined string 1`] = ` | ||
| <EuiScreenReaderStatus | ||
| statusMessage="User-defined title" | ||
| > | ||
| <EuiScreenReaderOnly> | ||
| <div | ||
| aria-live="polite" | ||
| className="euiScreenReaderOnly" | ||
| role="status" | ||
| > | ||
| User-defined title | ||
| </div> | ||
| </EuiScreenReaderOnly> | ||
| </EuiScreenReaderStatus> | ||
| `; | ||
|
|
||
| exports[`EuiScreenReaderStatus it sets aria-live and tabindex correctly when \`shouldReceiveFocus\` is passed 1`] = ` | ||
| <EuiScreenReaderStatus | ||
| shouldReceiveFocus={true} | ||
| statusMessage="User-defined title" | ||
| > | ||
| <EuiScreenReaderOnly> | ||
| <div | ||
| aria-live="off" | ||
| className="euiScreenReaderOnly" | ||
| role="status" | ||
| tabIndex={-1} | ||
| > | ||
| User-defined title | ||
| </div> | ||
| </EuiScreenReaderOnly> | ||
| </EuiScreenReaderStatus> | ||
| `; |
10 changes: 10 additions & 0 deletions
10
src/components/accessibility/screen_reader_status/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
| * in compliance with, at your election, the Elastic License 2.0 or the Server | ||
| * Side Public License, v 1. | ||
| */ | ||
|
|
||
| export type { EuiScreenReaderStatusProps } from './screen_reader_status'; | ||
| export { EuiScreenReaderStatus } from './screen_reader_status'; |
84 changes: 84 additions & 0 deletions
84
src/components/accessibility/screen_reader_status/screen_reader_status.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
| * in compliance with, at your election, the Elastic License 2.0 or the Server | ||
| * Side Public License, v 1. | ||
| */ | ||
|
|
||
| import React from 'react'; | ||
| import { mount, render } from 'enzyme'; | ||
|
|
||
| import { EuiScreenReaderStatus } from './screen_reader_status'; | ||
|
|
||
| describe('EuiScreenReaderStatus', () => { | ||
| test('it renders', () => { | ||
| const component = render(<EuiScreenReaderStatus />); | ||
| expect(component).toMatchSnapshot(); | ||
| }); | ||
|
|
||
| test('it sets `statusMessage` using document title', () => { | ||
| document.title = 'Default title'; | ||
| const component = mount(<EuiScreenReaderStatus />); | ||
| expect(component).toMatchSnapshot(); | ||
| }); | ||
|
|
||
| test('it sets `statusMessage` with user defined string', () => { | ||
| const component = mount( | ||
| <EuiScreenReaderStatus statusMessage="User-defined title" /> | ||
| ); | ||
| expect(component).toMatchSnapshot(); | ||
| }); | ||
|
|
||
| test('it sets aria-live and tabindex correctly when `shouldReceiveFocus` is passed', () => { | ||
| const component = mount( | ||
| <EuiScreenReaderStatus | ||
| statusMessage="User-defined title" | ||
| shouldReceiveFocus | ||
| /> | ||
| ); | ||
| expect(component).toMatchSnapshot(); | ||
| }); | ||
|
|
||
| test('it creates a non-focusable live region by default', () => { | ||
| const component = mount( | ||
| <EuiScreenReaderStatus statusMessage="User-defined title" /> | ||
| ); | ||
| const statusDiv = component.find('div'); | ||
| expect(statusDiv.prop('tabindex')).toBe(undefined); | ||
| expect(statusDiv.prop('aria-live')).toBe('polite'); | ||
| expect(statusDiv.text()).toBe('User-defined title'); | ||
| }); | ||
|
|
||
| test('it creates a focusable region when `shouldReceiveFocus` is passed', () => { | ||
| const component = mount( | ||
| <EuiScreenReaderStatus | ||
| statusMessage="User-defined title" | ||
| shouldReceiveFocus | ||
| /> | ||
| ); | ||
| const statusDiv = component.find('div'); | ||
| expect(statusDiv.prop('tabIndex')).toBe(-1); | ||
| expect(statusDiv.prop('aria-live')).toBe('off'); | ||
| expect(statusDiv.text()).toBe('User-defined title'); | ||
| }); | ||
|
|
||
| test('it does not set focus by default', () => { | ||
| const component = mount( | ||
| <EuiScreenReaderStatus statusMessage="User-defined title" /> | ||
| ); | ||
| const statusDiv = component.find('div'); | ||
| expect(statusDiv.is(':focus')).toBe(false); | ||
| }); | ||
|
|
||
| test('it sets focus when `shouldReceiveFocus` is passed', () => { | ||
| const component = mount( | ||
| <EuiScreenReaderStatus | ||
| statusMessage="User-defined title" | ||
| shouldReceiveFocus | ||
| /> | ||
| ); | ||
| const statusDiv = component.find('div'); | ||
| expect(statusDiv.is(':focus')).toBe(true); | ||
| }); | ||
| }); |
55 changes: 55 additions & 0 deletions
55
src/components/accessibility/screen_reader_status/screen_reader_status.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
| * in compliance with, at your election, the Elastic License 2.0 or the Server | ||
| * Side Public License, v 1. | ||
| */ | ||
|
|
||
| import React, { FunctionComponent, useEffect, useRef, useState } from 'react'; | ||
| import { EuiScreenReaderOnly } from '../screen_reader_only'; | ||
|
|
||
| export interface EuiScreenReaderStatusProps { | ||
| /** | ||
| * Set a custom status message to be announced to screen readers. | ||
| * Defaults to `document.title` and will announce client-side route change. | ||
| */ | ||
| statusMessage?: string; | ||
| /** | ||
| * Focuses the status message and removes the live region when set to true. | ||
| */ | ||
| shouldReceiveFocus?: boolean; | ||
| } | ||
|
|
||
| export const EuiScreenReaderStatus: FunctionComponent<EuiScreenReaderStatusProps> = ({ | ||
| statusMessage = document.title, | ||
| shouldReceiveFocus = false, | ||
| }) => { | ||
| const [statusMessageState, setStatusMessageState] = useState(''); | ||
| const statusRef = useRef<HTMLDivElement>(null); | ||
|
|
||
| useEffect(() => { | ||
| setStatusMessageState(statusMessage); | ||
|
|
||
| if (statusRef.current !== null && shouldReceiveFocus) { | ||
| statusRef.current.focus(); | ||
| } | ||
|
|
||
| return () => { | ||
| setStatusMessageState(''); | ||
| }; | ||
| }, [statusMessage, shouldReceiveFocus]); | ||
|
|
||
| return ( | ||
| <EuiScreenReaderOnly> | ||
| <div | ||
| aria-live={shouldReceiveFocus ? 'off' : 'polite'} | ||
| ref={statusRef} | ||
| role="status" | ||
| tabIndex={shouldReceiveFocus ? -1 : undefined} | ||
| > | ||
| {statusMessageState} | ||
| </div> | ||
| </EuiScreenReaderOnly> | ||
| ); | ||
| }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added this SR-only span to cue that some links in the docs left nav won't load a new route or announce themselves past the "click" sound.