-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Discover] [Unified Doc Viewer] Fix doc viewer not receiving focus on…
… open (#191039) ## Summary This PR fixes an issue where the new resizable doc viewer push flyout was not receiving focus on open, resulting in the flyout having to be manually focused in before users can navigate between docs using the arrow keys. It also re-adds support for closing the flyout when pressing the escape key, improves overall focus management, ensures the proper a11y attributes are used when the push flyout is shown, and introduces a new set of functional tests for doc viewer keyboard navigation and a11y. Fixes #190946. ### Checklist - [x] Any text added follows [EUI's writing guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses sentence case text and includes [i18n support](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md) - [ ] [Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html) was added for features that require explanation or tutorials - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios - [ ] [Flaky Test Runner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was used on any tests changed - [x] Any UI touched in this PR is usable by keyboard only (learn more about [keyboard accessibility](https://webaim.org/techniques/keyboard/)) - [x] Any UI touched in this PR does not create any new axe failures (run axe in browser: [FF](https://addons.mozilla.org/en-US/firefox/addon/axe-devtools/), [Chrome](https://chrome.google.com/webstore/detail/axe-web-accessibility-tes/lhdoppojpmngadmnindnejefpokejbdd?hl=en-US)) - [ ] If a plugin configuration key changed, check if it needs to be allowlisted in the cloud and added to the [docker list](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker) - [x] This renders correctly on smaller devices using a responsive layout. (You can test this [in your browser](https://www.browserstack.com/guide/responsive-testing-on-local-server)) - [x] This was checked for [cross-browser compatibility](https://www.elastic.co/support/matrix#matrix_browsers) ### For maintainers - [ ] This was checked for breaking API changes and was [labeled appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process) --------- Co-authored-by: kibanamachine <[email protected]>
- Loading branch information
1 parent
cf22d73
commit c642b2a
Showing
4 changed files
with
281 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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
53 changes: 53 additions & 0 deletions
53
src/plugins/unified_doc_viewer/public/components/doc_viewer_flyout/use_flyout_a11y.tsx
This file contains 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,53 @@ | ||
/* | ||
* 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 { EuiScreenReaderOnly, useGeneratedHtmlId } from '@elastic/eui'; | ||
import { i18n } from '@kbn/i18n'; | ||
import React, { useEffect, useState } from 'react'; | ||
import useUnmount from 'react-use/lib/useUnmount'; | ||
|
||
export const useFlyoutA11y = ({ isXlScreen }: { isXlScreen: boolean }) => { | ||
const descriptionId = useGeneratedHtmlId(); | ||
const [triggerEl] = useState(document.activeElement); | ||
const [flyoutEl, setFlyoutEl] = useState<HTMLElement>(); | ||
|
||
// Auto-focus push flyout on open or when switching to XL screen | ||
useEffect(() => { | ||
if (isXlScreen && flyoutEl && document.contains(flyoutEl)) { | ||
// Wait a tick before focusing or focus will be stolen by the trigger element when | ||
// switching from an overlay flyout to a push flyout (due to EUI focus lock) | ||
setTimeout(() => flyoutEl.focus()); | ||
} | ||
}, [flyoutEl, isXlScreen]); | ||
|
||
// Return focus to the trigger element when the flyout is closed | ||
useUnmount(() => { | ||
if (triggerEl instanceof HTMLElement && document.contains(triggerEl)) { | ||
triggerEl.focus(); | ||
} | ||
}); | ||
|
||
return { | ||
a11yProps: { | ||
ref: setFlyoutEl, | ||
role: isXlScreen ? 'dialog' : undefined, | ||
tabindex: isXlScreen ? 0 : undefined, | ||
'aria-describedby': isXlScreen ? descriptionId : undefined, | ||
'data-no-focus-lock': isXlScreen || undefined, | ||
}, | ||
screenReaderDescription: isXlScreen && ( | ||
<EuiScreenReaderOnly> | ||
<p id={descriptionId} data-test-subj="unifiedDocViewerScreenReaderDescription"> | ||
{i18n.translate('unifiedDocViewer.flyout.screenReaderDescription', { | ||
defaultMessage: 'You are in a non-modal dialog. To close the dialog, press Escape.', | ||
})} | ||
</p> | ||
</EuiScreenReaderOnly> | ||
), | ||
}; | ||
}; |
This file contains 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 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