Skip to content

Commit

Permalink
[Discover] Move focus on chart toggle in Discover (elastic#103119)
Browse files Browse the repository at this point in the history
* [Discover] move focus on show chart

* [Discover] set actual moveFocus flag
  • Loading branch information
dimaanj authored Jun 24, 2021
1 parent dd072c3 commit 686ac90
Showing 1 changed file with 18 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import React, { useCallback } from 'react';
import React, { useCallback, useEffect, useRef } from 'react';
import moment from 'moment';
import { EuiFlexGroup, EuiFlexItem, EuiButtonEmpty, EuiSpacer } from '@elastic/eui';
import { IUiSettingsClient } from 'kibana/public';
Expand Down Expand Up @@ -47,8 +47,21 @@ export function DiscoverChart({
stateContainer: GetStateReturn;
timefield?: string;
}) {
const chartRef = useRef<{ element: HTMLElement | null; moveFocus: boolean }>({
element: null,
moveFocus: false,
});

useEffect(() => {
if (chartRef.current.moveFocus && chartRef.current.element) {
chartRef.current.element.focus();
}
}, [state.hideChart]);

const toggleHideChart = useCallback(() => {
stateContainer.setAppState({ hideChart: !state.hideChart });
const newHideChart = !state.hideChart;
stateContainer.setAppState({ hideChart: newHideChart });
chartRef.current.moveFocus = !newHideChart;
}, [state, stateContainer]);

const onChangeInterval = useCallback(
Expand Down Expand Up @@ -102,9 +115,7 @@ export function DiscoverChart({
<EuiButtonEmpty
size="xs"
iconType={!state.hideChart ? 'eyeClosed' : 'eye'}
onClick={() => {
toggleHideChart();
}}
onClick={toggleHideChart}
data-test-subj="discoverChartToggle"
>
{!state.hideChart
Expand All @@ -122,6 +133,8 @@ export function DiscoverChart({
{!state.hideChart && chartData && (
<EuiFlexItem grow={false}>
<section
ref={(element) => (chartRef.current.element = element)}
tabIndex={-1}
aria-label={i18n.translate('discover.histogramOfFoundDocumentsAriaLabel', {
defaultMessage: 'Histogram of found documents',
})}
Expand Down

0 comments on commit 686ac90

Please sign in to comment.