Skip to content

Commit

Permalink
[8.15] [Discover] [Unified Doc Viewer] Fix doc viewer not receiving f…
Browse files Browse the repository at this point in the history
…ocus on open (#191039) (#194780)

# Backport

This will backport the following commits from `main` to `8.15`:
- [[Discover] [Unified Doc Viewer] Fix doc viewer not receiving focus on
open (#191039)](#191039)

<!--- Backport version: 8.9.8 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Davis
McPhee","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-09-30T03:25:26Z","message":"[Discover]
[Unified Doc Viewer] Fix doc viewer not receiving focus on open
(#191039)\n\n## Summary\r\n\r\nThis PR fixes an issue where the new
resizable doc viewer push flyout\r\nwas not receiving focus on open,
resulting in the flyout having to be\r\nmanually focused in before users
can navigate between docs using the\r\narrow keys. It also re-adds
support for closing the flyout when pressing\r\nthe escape key, improves
overall focus management, ensures the proper\r\na11y attributes are used
when the push flyout is shown, and introduces a\r\nnew set of functional
tests for doc viewer keyboard navigation and a11y.\r\n\r\nFixes
#190946.\r\n\r\n### Checklist\r\n\r\n- [x] Any text added follows [EUI's
writing\r\nguidelines](https://elastic.github.io/eui/#/guidelines/writing),
uses\r\nsentence case text and includes
[i18n\r\nsupport](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md)\r\n-
[
]\r\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\r\nwas
added for features that require explanation or tutorials\r\n- [x] [Unit
or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common scenarios\r\n- [ ] [Flaky
Test\r\nRunner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1)
was\r\nused on any tests changed\r\n- [x] Any UI touched in this PR is
usable by keyboard only (learn more\r\nabout [keyboard
accessibility](https://webaim.org/techniques/keyboard/))\r\n- [x] Any UI
touched in this PR does not create any new axe failures\r\n(run axe in
browser:\r\n[FF](https://addons.mozilla.org/en-US/firefox/addon/axe-devtools/),\r\n[Chrome](https://chrome.google.com/webstore/detail/axe-web-accessibility-tes/lhdoppojpmngadmnindnejefpokejbdd?hl=en-US))\r\n-
[ ] If a plugin configuration key changed, check if it needs to
be\r\nallowlisted in the cloud and added to the
[docker\r\nlist](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)\r\n-
[x] This renders correctly on smaller devices using a
responsive\r\nlayout. (You can test this [in
your\r\nbrowser](https://www.browserstack.com/guide/responsive-testing-on-local-server))\r\n-
[x] This was checked for
[cross-browser\r\ncompatibility](https://www.elastic.co/support/matrix#matrix_browsers)\r\n\r\n###
For maintainers\r\n\r\n- [ ] This was checked for breaking API changes
and was
[labeled\r\nappropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)\r\n\r\n---------\r\n\r\nCo-authored-by:
kibanamachine
<[email protected]>","sha":"0ac69658bff4e02b8c477673e091b4e86bddf7b5","branchLabelMapping":{"^v9.0.0$":"main","^v8.16.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:fix","v9.0.0","Team:DataDiscovery","backport:prev-minor","backport:prev-major","v8.16.0"],"number":191039,"url":"https://github.com/elastic/kibana/pull/191039","mergeCommit":{"message":"[Discover]
[Unified Doc Viewer] Fix doc viewer not receiving focus on open
(#191039)\n\n## Summary\r\n\r\nThis PR fixes an issue where the new
resizable doc viewer push flyout\r\nwas not receiving focus on open,
resulting in the flyout having to be\r\nmanually focused in before users
can navigate between docs using the\r\narrow keys. It also re-adds
support for closing the flyout when pressing\r\nthe escape key, improves
overall focus management, ensures the proper\r\na11y attributes are used
when the push flyout is shown, and introduces a\r\nnew set of functional
tests for doc viewer keyboard navigation and a11y.\r\n\r\nFixes
#190946.\r\n\r\n### Checklist\r\n\r\n- [x] Any text added follows [EUI's
writing\r\nguidelines](https://elastic.github.io/eui/#/guidelines/writing),
uses\r\nsentence case text and includes
[i18n\r\nsupport](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md)\r\n-
[
]\r\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\r\nwas
added for features that require explanation or tutorials\r\n- [x] [Unit
or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common scenarios\r\n- [ ] [Flaky
Test\r\nRunner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1)
was\r\nused on any tests changed\r\n- [x] Any UI touched in this PR is
usable by keyboard only (learn more\r\nabout [keyboard
accessibility](https://webaim.org/techniques/keyboard/))\r\n- [x] Any UI
touched in this PR does not create any new axe failures\r\n(run axe in
browser:\r\n[FF](https://addons.mozilla.org/en-US/firefox/addon/axe-devtools/),\r\n[Chrome](https://chrome.google.com/webstore/detail/axe-web-accessibility-tes/lhdoppojpmngadmnindnejefpokejbdd?hl=en-US))\r\n-
[ ] If a plugin configuration key changed, check if it needs to
be\r\nallowlisted in the cloud and added to the
[docker\r\nlist](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)\r\n-
[x] This renders correctly on smaller devices using a
responsive\r\nlayout. (You can test this [in
your\r\nbrowser](https://www.browserstack.com/guide/responsive-testing-on-local-server))\r\n-
[x] This was checked for
[cross-browser\r\ncompatibility](https://www.elastic.co/support/matrix#matrix_browsers)\r\n\r\n###
For maintainers\r\n\r\n- [ ] This was checked for breaking API changes
and was
[labeled\r\nappropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)\r\n\r\n---------\r\n\r\nCo-authored-by:
kibanamachine
<[email protected]>","sha":"0ac69658bff4e02b8c477673e091b4e86bddf7b5"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","labelRegex":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/191039","number":191039,"mergeCommit":{"message":"[Discover]
[Unified Doc Viewer] Fix doc viewer not receiving focus on open
(#191039)\n\n## Summary\r\n\r\nThis PR fixes an issue where the new
resizable doc viewer push flyout\r\nwas not receiving focus on open,
resulting in the flyout having to be\r\nmanually focused in before users
can navigate between docs using the\r\narrow keys. It also re-adds
support for closing the flyout when pressing\r\nthe escape key, improves
overall focus management, ensures the proper\r\na11y attributes are used
when the push flyout is shown, and introduces a\r\nnew set of functional
tests for doc viewer keyboard navigation and a11y.\r\n\r\nFixes
#190946.\r\n\r\n### Checklist\r\n\r\n- [x] Any text added follows [EUI's
writing\r\nguidelines](https://elastic.github.io/eui/#/guidelines/writing),
uses\r\nsentence case text and includes
[i18n\r\nsupport](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md)\r\n-
[
]\r\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\r\nwas
added for features that require explanation or tutorials\r\n- [x] [Unit
or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common scenarios\r\n- [ ] [Flaky
Test\r\nRunner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1)
was\r\nused on any tests changed\r\n- [x] Any UI touched in this PR is
usable by keyboard only (learn more\r\nabout [keyboard
accessibility](https://webaim.org/techniques/keyboard/))\r\n- [x] Any UI
touched in this PR does not create any new axe failures\r\n(run axe in
browser:\r\n[FF](https://addons.mozilla.org/en-US/firefox/addon/axe-devtools/),\r\n[Chrome](https://chrome.google.com/webstore/detail/axe-web-accessibility-tes/lhdoppojpmngadmnindnejefpokejbdd?hl=en-US))\r\n-
[ ] If a plugin configuration key changed, check if it needs to
be\r\nallowlisted in the cloud and added to the
[docker\r\nlist](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)\r\n-
[x] This renders correctly on smaller devices using a
responsive\r\nlayout. (You can test this [in
your\r\nbrowser](https://www.browserstack.com/guide/responsive-testing-on-local-server))\r\n-
[x] This was checked for
[cross-browser\r\ncompatibility](https://www.elastic.co/support/matrix#matrix_browsers)\r\n\r\n###
For maintainers\r\n\r\n- [ ] This was checked for breaking API changes
and was
[labeled\r\nappropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)\r\n\r\n---------\r\n\r\nCo-authored-by:
kibanamachine
<[email protected]>","sha":"0ac69658bff4e02b8c477673e091b4e86bddf7b5"}},{"branch":"8.x","label":"v8.16.0","labelRegex":"^v8.16.0$","isSourceBranch":false,"url":"https://github.com/elastic/kibana/pull/194368","number":194368,"state":"MERGED","mergeCommit":{"sha":"1ee91ba1b1b0e5f11cfba210b3aa332723f98352","message":"[8.x]
[Discover] [Unified Doc Viewer] Fix doc viewer not receiving focus on
open (#191039) (#194368)\n\n# Backport\n\nThis will backport the
following commits from `main` to `8.x`:\n- [[Discover] [Unified Doc
Viewer] Fix doc viewer not receiving focus on\nopen
(#191039)](https://github.com/elastic/kibana/pull/191039)\n\n<!---
Backport version: 9.4.3 -->\n\n### Questions ?\nPlease refer to the
[Backport
tool\ndocumentation](https://github.com/sqren/backport)\n\n<!--BACKPORT
[{\"author\":{\"name\":\"Davis\nMcPhee\",\"email\":\"[email protected]\"},\"sourceCommit\":{\"committedDate\":\"2024-09-30T03:25:26Z\",\"message\":\"[Discover]\n[Unified
Doc Viewer] Fix doc viewer not receiving focus on
open\n(#191039)\\n\\n## Summary\\r\\n\\r\\nThis PR fixes an issue where
the new\nresizable doc viewer push flyout\\r\\nwas not receiving focus
on open,\nresulting in the flyout having to be\\r\\nmanually focused in
before users\ncan navigate between docs using the\\r\\narrow keys. It
also re-adds\nsupport for closing the flyout when pressing\\r\\nthe
escape key, improves\noverall focus management, ensures the
proper\\r\\na11y attributes are used\nwhen the push flyout is shown, and
introduces a\\r\\nnew set of functional\ntests for doc viewer keyboard
navigation and a11y.\\r\\n\\r\\nFixes\n#190946.\\r\\n\\r\\n###
Checklist\\r\\n\\r\\n- [x] Any text added follows
[EUI's\nwriting\\r\\nguidelines](https://elastic.github.io/eui/#/guidelines/writing),\nuses\\r\\nsentence
case text and
includes\n[i18n\\r\\nsupport](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md)\\r\\n-\n[\n]\\r\\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\\r\\nwas\nadded
for features that require explanation or tutorials\\r\\n- [x]
[Unit\nor\nfunctional\\r\\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\\r\\nwere\nupdated
or added to match the most common scenarios\\r\\n- [ ]
[Flaky\nTest\\r\\nRunner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1)\nwas\\r\\nused
on any tests changed\\r\\n- [x] Any UI touched in this PR is\nusable by
keyboard only (learn more\\r\\nabout
[keyboard\naccessibility](https://webaim.org/techniques/keyboard/))\\r\\n-
[x] Any UI\ntouched in this PR does not create any new axe
failures\\r\\n(run axe
in\nbrowser:\\r\\n[FF](https://addons.mozilla.org/en-US/firefox/addon/axe-devtools/),\\r\\n[Chrome](https://chrome.google.com/webstore/detail/axe-web-accessibility-tes/lhdoppojpmngadmnindnejefpokejbdd?hl=en-US))\\r\\n-\n[
] If a plugin configuration key changed, check if it needs
to\nbe\\r\\nallowlisted in the cloud and added to
the\n[docker\\r\\nlist](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)\\r\\n-\n[x]
This renders correctly on smaller devices using
a\nresponsive\\r\\nlayout. (You can test this
[in\nyour\\r\\nbrowser](https://www.browserstack.com/guide/responsive-testing-on-local-server))\\r\\n-\n[x]
This was checked
for\n[cross-browser\\r\\ncompatibility](https://www.elastic.co/support/matrix#matrix_browsers)\\r\\n\\r\\n###\nFor
maintainers\\r\\n\\r\\n- [ ] This was checked for breaking API
changes\nand
was\n[labeled\\r\\nappropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)\\r\\n\\r\\n---------\\r\\n\\r\\nCo-authored-by:\nkibanamachine\n<[email protected]>\",\"sha\":\"0ac69658bff4e02b8c477673e091b4e86bddf7b5\",\"branchLabelMapping\":{\"^v9.0.0$\":\"main\",\"^v8.16.0$\":\"8.x\",\"^v(\\\\d+).(\\\\d+).\\\\d+$\":\"$1.$2\"}},\"sourcePullRequest\":{\"labels\":[\"release_note:fix\",\"v9.0.0\",\"Team:DataDiscovery\",\"backport:prev-minor\",\"backport:prev-major\"],\"title\":\"[Discover]\n[Unified
Doc Viewer] Fix doc viewer not receiving focus
on\nopen\",\"number\":191039,\"url\":\"https://github.com/elastic/kibana/pull/191039\",\"mergeCommit\":{\"message\":\"[Discover]\n[Unified
Doc Viewer] Fix doc viewer not receiving focus on
open\n(#191039)\\n\\n## Summary\\r\\n\\r\\nThis PR fixes an issue where
the new\nresizable doc viewer push flyout\\r\\nwas not receiving focus
on open,\nresulting in the flyout having to be\\r\\nmanually focused in
before users\ncan navigate between docs using the\\r\\narrow keys. It
also re-adds\nsupport for closing the flyout when pressing\\r\\nthe
escape key, improves\noverall focus management, ensures the
proper\\r\\na11y attributes are used\nwhen the push flyout is shown, and
introduces a\\r\\nnew set of functional\ntests for doc viewer keyboard
navigation and a11y.\\r\\n\\r\\nFixes\n#190946.\\r\\n\\r\\n###
Checklist\\r\\n\\r\\n- [x] Any text added follows
[EUI's\nwriting\\r\\nguidelines](https://elastic.github.io/eui/#/guidelines/writing),\nuses\\r\\nsentence
case text and
includes\n[i18n\\r\\nsupport](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md)\\r\\n-\n[\n]\\r\\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\\r\\nwas\nadded
for features that require explanation or tutorials\\r\\n- [x]
[Unit\nor\nfunctional\\r\\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\\r\\nwere\nupdated
or added to match the most common scenarios\\r\\n- [ ]
[Flaky\nTest\\r\\nRunner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1)\nwas\\r\\nused
on any tests changed\\r\\n- [x] Any UI touched in this PR is\nusable by
keyboard only (learn more\\r\\nabout
[keyboard\naccessibility](https://webaim.org/techniques/keyboard/))\\r\\n-
[x] Any UI\ntouched in this PR does not create any new axe
failures\\r\\n(run axe
in\nbrowser:\\r\\n[FF](https://addons.mozilla.org/en-US/firefox/addon/axe-devtools/),\\r\\n[Chrome](https://chrome.google.com/webstore/detail/axe-web-accessibility-tes/lhdoppojpmngadmnindnejefpokejbdd?hl=en-US))\\r\\n-\n[
] If a plugin configuration key changed, check if it needs
to\nbe\\r\\nallowlisted in the cloud and added to
the\n[docker\\r\\nlist](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)\\r\\n-\n[x]
This renders correctly on smaller devices using
a\nresponsive\\r\\nlayout. (You can test this
[in\nyour\\r\\nbrowser](https://www.browserstack.com/guide/responsive-testing-on-local-server))\\r\\n-\n[x]
This was checked
for\n[cross-browser\\r\\ncompatibility](https://www.elastic.co/support/matrix#matrix_browsers)\\r\\n\\r\\n###\nFor
maintainers\\r\\n\\r\\n- [ ] This was checked for breaking API
changes\nand
was\n[labeled\\r\\nappropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)\\r\\n\\r\\n---------\\r\\n\\r\\nCo-authored-by:\nkibanamachine\n<[email protected]>\",\"sha\":\"0ac69658bff4e02b8c477673e091b4e86bddf7b5\"}},\"sourceBranch\":\"main\",\"suggestedTargetBranches\":[],\"targetPullRequestStates\":[{\"branch\":\"main\",\"label\":\"v9.0.0\",\"branchLabelMappingKey\":\"^v9.0.0$\",\"isSourceBranch\":true,\"state\":\"MERGED\",\"url\":\"https://github.com/elastic/kibana/pull/191039\",\"number\":191039,\"mergeCommit\":{\"message\":\"[Discover]\n[Unified
Doc Viewer] Fix doc viewer not receiving focus on
open\n(#191039)\\n\\n## Summary\\r\\n\\r\\nThis PR fixes an issue where
the new\nresizable doc viewer push flyout\\r\\nwas not receiving focus
on open,\nresulting in the flyout having to be\\r\\nmanually focused in
before users\ncan navigate between docs using the\\r\\narrow keys. It
also re-adds\nsupport for closing the flyout when pressing\\r\\nthe
escape key, improves\noverall focus management, ensures the
proper\\r\\na11y attributes are used\nwhen the push flyout is shown, and
introduces a\\r\\nnew set of functional\ntests for doc viewer keyboard
navigation and a11y.\\r\\n\\r\\nFixes\n#190946.\\r\\n\\r\\n###
Checklist\\r\\n\\r\\n- [x] Any text added follows
[EUI's\nwriting\\r\\nguidelines](https://elastic.github.io/eui/#/guidelines/writing),\nuses\\r\\nsentence
case text and
includes\n[i18n\\r\\nsupport](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md)\\r\\n-\n[\n]\\r\\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\\r\\nwas\nadded
for features that require explanation or tutorials\\r\\n- [x]
[Unit\nor\nfunctional\\r\\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\\r\\nwere\nupdated
or added to match the most common scenarios\\r\\n- [ ]
[Flaky\nTest\\r\\nRunner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1)\nwas\\r\\nused
on any tests changed\\r\\n- [x] Any UI touched in this PR is\nusable by
keyboard only (learn more\\r\\nabout
[keyboard\naccessibility](https://webaim.org/techniques/keyboard/))\\r\\n-
[x] Any UI\ntouched in this PR does not create any new axe
failures\\r\\n(run axe
in\nbrowser:\\r\\n[FF](https://addons.mozilla.org/en-US/firefox/addon/axe-devtools/),\\r\\n[Chrome](https://chrome.google.com/webstore/detail/axe-web-accessibility-tes/lhdoppojpmngadmnindnejefpokejbdd?hl=en-US))\\r\\n-\n[
] If a plugin configuration key changed, check if it needs
to\nbe\\r\\nallowlisted in the cloud and added to
the\n[docker\\r\\nlist](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)\\r\\n-\n[x]
This renders correctly on smaller devices using
a\nresponsive\\r\\nlayout. (You can test this
[in\nyour\\r\\nbrowser](https://www.browserstack.com/guide/responsive-testing-on-local-server))\\r\\n-\n[x]
This was checked
for\n[cross-browser\\r\\ncompatibility](https://www.elastic.co/support/matrix#matrix_browsers)\\r\\n\\r\\n###\nFor
maintainers\\r\\n\\r\\n- [ ] This was checked for breaking API
changes\nand
was\n[labeled\\r\\nappropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)\\r\\n\\r\\n---------\\r\\n\\r\\nCo-authored-by:\nkibanamachine\n<[email protected]>\",\"sha\":\"0ac69658bff4e02b8c477673e091b4e86bddf7b5\"}}]}]\nBACKPORT-->\n\nCo-authored-by:
Davis McPhee <[email protected]>"}}]}] BACKPORT-->

---------

Co-authored-by: kibanamachine <[email protected]>
  • Loading branch information
davismcphee and kibanamachine authored Oct 4, 2024
1 parent e6b279f commit 9d7cc69
Show file tree
Hide file tree
Showing 4 changed files with 282 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*/

import React, { useMemo, useCallback, type ComponentType } from 'react';
import { get } from 'lodash';
import { i18n } from '@kbn/i18n';
import { css } from '@emotion/react';
import type { DataView } from '@kbn/data-views-plugin/public';
Expand All @@ -34,6 +33,7 @@ import useLocalStorage from 'react-use/lib/useLocalStorage';
import type { ToastsStart } from '@kbn/core-notifications-browser';
import type { DocViewFilterFn, DocViewRenderProps } from '@kbn/unified-doc-viewer/types';
import { UnifiedDocViewer } from '../lazy_doc_viewer';
import { useFlyoutA11y } from './use_flyout_a11y';

export interface UnifiedDocViewerFlyoutProps {
'data-test-subj'?: string;
Expand Down Expand Up @@ -71,6 +71,7 @@ function getIndexByDocId(hits: DataTableRecord[], id: string) {
}

export const FLYOUT_WIDTH_KEY = 'unifiedDocViewer:flyoutWidth';

/**
* Flyout displaying an expanded row details
*/
Expand Down Expand Up @@ -129,24 +130,29 @@ export function UnifiedDocViewerFlyout({

const onKeyDown = useCallback(
(ev: React.KeyboardEvent) => {
const nodeClasses = get(ev, 'target.className', '');
if (typeof nodeClasses === 'string' && nodeClasses.includes('euiDataGrid')) {
if (ev.target instanceof HTMLElement && ev.target.closest('.euiDataGrid__content')) {
// ignore events triggered from the data grid
return;
}

const nodeName = get(ev, 'target.nodeName', null);
if (typeof nodeName === 'string' && nodeName.toLowerCase() === 'input') {
if (ev.key === keys.ESCAPE) {
ev.preventDefault();
ev.stopPropagation();
onClose();
}

if (ev.target instanceof HTMLInputElement) {
// ignore events triggered from the search input
return;
}

if (ev.key === keys.ARROW_LEFT || ev.key === keys.ARROW_RIGHT) {
ev.preventDefault();
ev.stopPropagation();
setPage(activePage + (ev.key === keys.ARROW_RIGHT ? 1 : -1));
}
},
[activePage, setPage]
[activePage, onClose, setPage]
);

const addColumn = useCallback(
Expand Down Expand Up @@ -231,6 +237,7 @@ export function UnifiedDocViewerFlyout({
defaultMessage: 'Document',
});
const currentFlyoutTitle = flyoutTitle ?? defaultFlyoutTitle;
const { a11yProps, screenReaderDescription } = useFlyoutA11y({ isXlScreen });

return (
<EuiPortal>
Expand All @@ -250,7 +257,9 @@ export function UnifiedDocViewerFlyout({
maxWidth: `${isXlScreen ? `calc(100vw - ${DEFAULT_WIDTH}px)` : '90vw'} !important`,
}}
paddingSize="m"
{...a11yProps}
>
{screenReaderDescription}
<EuiFlyoutHeader hasBorder>
<EuiFlexGroup
direction="row"
Expand Down
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>
),
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,9 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) {
it('should allow paginating docs in the flyout by clicking in the doc table', async function () {
await retry.try(async function () {
await dataGrid.clickRowToggle({ rowIndex: rowToInspect - 1 });
await testSubjects.exists(`docViewerFlyoutNavigationPage0`);
await testSubjects.existOrFail('docViewerFlyoutNavigationPage-0');
await dataGrid.clickRowToggle({ rowIndex: rowToInspect });
await testSubjects.exists(`docViewerFlyoutNavigationPage1`);
await testSubjects.existOrFail('docViewerFlyoutNavigationPage-1');
await dataGrid.closeFlyout();
});
});
Expand Down
212 changes: 212 additions & 0 deletions test/functional/apps/discover/group3/_doc_viewer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Side Public License, v 1.
*/

import expect from '@kbn/expect';
import { FtrProviderContext } from '../ftr_provider_context';

export default function ({ getService, getPageObjects }: FtrProviderContext) {
Expand All @@ -16,6 +17,7 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) {
const find = getService('find');
const retry = getService('retry');
const dataGrid = getService('dataGrid');
const browser = getService('browser');

describe('discover doc viewer', function describeIndexTests() {
before(async function () {
Expand Down Expand Up @@ -97,5 +99,215 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) {
});
});
});

describe('flyout', () => {
let originalScreenSize = { width: 0, height: 0 };

const reduceScreenWidth = async () => {
await browser.setWindowSize(800, originalScreenSize.height);
};

const restoreScreenWidth = async () => {
await browser.setWindowSize(originalScreenSize.width, originalScreenSize.height);
};

before(async () => {
originalScreenSize = await browser.getWindowSize();
});

beforeEach(async () => {
// open the flyout once initially to ensure table is the default tab
await dataGrid.clickRowToggle();
await PageObjects.discover.isShowingDocViewer();
await dataGrid.closeFlyout();
});

afterEach(async () => {
await restoreScreenWidth();
});

describe('keyboard navigation', () => {
it('should navigate between documents with arrow keys', async () => {
await dataGrid.clickRowToggle();
await PageObjects.discover.isShowingDocViewer();
await testSubjects.existOrFail(`docViewerFlyoutNavigationPage-0`);
await browser.pressKeys(browser.keys.ARROW_RIGHT);
await testSubjects.existOrFail(`docViewerFlyoutNavigationPage-1`);
await browser.pressKeys(browser.keys.ARROW_RIGHT);
await testSubjects.existOrFail(`docViewerFlyoutNavigationPage-2`);
await browser.pressKeys(browser.keys.ARROW_LEFT);
await testSubjects.existOrFail(`docViewerFlyoutNavigationPage-1`);
await browser.pressKeys(browser.keys.ARROW_LEFT);
await testSubjects.existOrFail(`docViewerFlyoutNavigationPage-0`);
});

it('should not navigate between documents with arrow keys when the search input is focused', async () => {
await dataGrid.clickRowToggle();
await PageObjects.discover.isShowingDocViewer();
await testSubjects.existOrFail(`docViewerFlyoutNavigationPage-0`);
await browser.pressKeys(browser.keys.ARROW_RIGHT);
await testSubjects.existOrFail(`docViewerFlyoutNavigationPage-1`);
await testSubjects.click('unifiedDocViewerFieldsSearchInput');
await browser.pressKeys(browser.keys.ARROW_RIGHT);
await testSubjects.existOrFail(`docViewerFlyoutNavigationPage-1`);
await browser.pressKeys(browser.keys.TAB);
await browser.pressKeys(browser.keys.TAB);
await browser.pressKeys(browser.keys.ARROW_RIGHT);
await testSubjects.existOrFail(`docViewerFlyoutNavigationPage-2`);
});

it('should not navigate between documents with arrow keys when the data grid is focused', async () => {
await dataGrid.clickRowToggle();
await PageObjects.discover.isShowingDocViewer();
await testSubjects.existOrFail(`docViewerFlyoutNavigationPage-0`);
await browser.pressKeys(browser.keys.ARROW_RIGHT);
await testSubjects.existOrFail(`docViewerFlyoutNavigationPage-1`);
await testSubjects.click('dataGridHeaderCell-name');
await browser.pressKeys(browser.keys.ARROW_RIGHT);
await testSubjects.existOrFail(`docViewerFlyoutNavigationPage-1`);
await browser.pressKeys(browser.keys.TAB);
await browser.pressKeys(browser.keys.ARROW_RIGHT);
await testSubjects.existOrFail(`docViewerFlyoutNavigationPage-2`);
});

it('should close the flyout with the escape key', async () => {
await dataGrid.clickRowToggle();
expect(await PageObjects.discover.isShowingDocViewer()).to.be(true);
await browser.pressKeys(browser.keys.ESCAPE);
expect(await PageObjects.discover.isShowingDocViewer()).to.be(false);
});

it('should close the flyout with the escape key when the search input is focused', async () => {
await dataGrid.clickRowToggle();
expect(await PageObjects.discover.isShowingDocViewer()).to.be(true);
await testSubjects.click('unifiedDocViewerFieldsSearchInput');
await browser.pressKeys(browser.keys.ESCAPE);
expect(await PageObjects.discover.isShowingDocViewer()).to.be(false);
});

it('should not close the flyout with the escape key when the data grid is focused', async () => {
await dataGrid.clickRowToggle();
expect(await PageObjects.discover.isShowingDocViewer()).to.be(true);
await testSubjects.click('dataGridHeaderCell-name');
await browser.pressKeys(browser.keys.ESCAPE);
expect(await PageObjects.discover.isShowingDocViewer()).to.be(true);
await browser.pressKeys(browser.keys.TAB);
await browser.pressKeys(browser.keys.ESCAPE);
expect(await PageObjects.discover.isShowingDocViewer()).to.be(false);
});
});

describe('accessibility', () => {
it('should focus the flyout on open, and retain focus when resizing between push and overlay flyouts', async () => {
// push -> overlay -> push
await dataGrid.clickRowToggle();
await PageObjects.discover.isShowingDocViewer();
let activeElement = await find.activeElement();
expect(await activeElement.getAttribute('data-test-subj')).to.be('docViewerFlyout');
await reduceScreenWidth();
activeElement = await find.activeElement();
expect(await activeElement.getAttribute('data-test-subj')).to.be('docViewerFlyout');
await restoreScreenWidth();
activeElement = await find.activeElement();
expect(await activeElement.getAttribute('data-test-subj')).to.be('docViewerFlyout');
// overlay -> push -> overlay
await browser.pressKeys(browser.keys.ESCAPE);
await reduceScreenWidth();
await dataGrid.clickRowToggle();
await PageObjects.discover.isShowingDocViewer();
activeElement = await find.activeElement();
expect(await activeElement.getAttribute('data-test-subj')).to.be('docViewerFlyout');
await restoreScreenWidth();
activeElement = await find.activeElement();
expect(await activeElement.getAttribute('data-test-subj')).to.be('docViewerFlyout');
await reduceScreenWidth();
activeElement = await find.activeElement();
expect(await activeElement.getAttribute('data-test-subj')).to.be('docViewerFlyout');
});

it('should return focus to the trigger element when the flyout is closed', async () => {
// push
await dataGrid.clickRowToggle();
await PageObjects.discover.isShowingDocViewer();
await browser.pressKeys(browser.keys.ESCAPE);
let activeElement = await find.activeElement();
expect(await activeElement.getAttribute('data-test-subj')).to.be(
'docTableExpandToggleColumn'
);
// push -> overlay
await dataGrid.clickRowToggle();
await PageObjects.discover.isShowingDocViewer();
await reduceScreenWidth();
await browser.pressKeys(browser.keys.ESCAPE);
activeElement = await find.activeElement();
expect(await activeElement.getAttribute('data-test-subj')).to.be(
'docTableExpandToggleColumn'
);
// overlay
await dataGrid.clickRowToggle();
await PageObjects.discover.isShowingDocViewer();
await browser.pressKeys(browser.keys.ESCAPE);
activeElement = await find.activeElement();
expect(await activeElement.getAttribute('data-test-subj')).to.be(
'docTableExpandToggleColumn'
);
// overlay -> push
await dataGrid.clickRowToggle();
await PageObjects.discover.isShowingDocViewer();
await restoreScreenWidth();
await browser.pressKeys(browser.keys.ESCAPE);
activeElement = await find.activeElement();
expect(await activeElement.getAttribute('data-test-subj')).to.be(
'docTableExpandToggleColumn'
);
});

it('should show custom screen reader description push flyout is active', async () => {
await dataGrid.clickRowToggle();
await PageObjects.discover.isShowingDocViewer();
await testSubjects.existOrFail('unifiedDocViewerScreenReaderDescription', {
allowHidden: true,
});
});

it('should not show custom screen reader description when overlay flyout active', async () => {
await dataGrid.clickRowToggle();
await PageObjects.discover.isShowingDocViewer();
await reduceScreenWidth();
expect(
await testSubjects.exists('unifiedDocViewerScreenReaderDescription', {
allowHidden: true,
})
).to.be(false);
});

it('should use expected a11y attributes', async () => {
// push flyout
await dataGrid.clickRowToggle();
await PageObjects.discover.isShowingDocViewer();
let role = await testSubjects.getAttribute('docViewerFlyout', 'role');
let tabindex = await testSubjects.getAttribute('docViewerFlyout', 'tabindex');
let describedBy = await testSubjects.getAttribute('docViewerFlyout', 'aria-describedby');
let noFocusLock = await testSubjects.getAttribute(
'docViewerFlyout',
'data-no-focus-lock'
);
expect(role).to.be('dialog');
expect(tabindex).to.be('0');
expect(await find.existsByCssSelector(`#${describedBy}`)).to.be(true);
expect(noFocusLock).to.be('true');
// overlay flyout
await reduceScreenWidth();
role = await testSubjects.getAttribute('docViewerFlyout', 'role');
tabindex = await testSubjects.getAttribute('docViewerFlyout', 'tabindex');
describedBy = await testSubjects.getAttribute('docViewerFlyout', 'aria-describedby');
noFocusLock = await testSubjects.getAttribute('docViewerFlyout', 'data-no-focus-lock');
expect(role).to.be('dialog');
expect(tabindex).to.be('0');
expect(await find.existsByCssSelector(`#${describedBy}`)).to.be(true);
expect(noFocusLock).to.be(null);
});
});
});
});
}

0 comments on commit 9d7cc69

Please sign in to comment.