Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { mockBranch } from '../../__mocks__/mocks';
jest.mock('../../lib/keyboard_shortcut/keyboard_shortcut', () => ({
isKeyboardShortcut: jest.fn(),
isMac: jest.fn(),
isEscapeKey: jest.fn(),
}));

const mockedIsKeyboardShortcut = jest.mocked(isKeyboardShortcut);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { InspectOverlay } from './inspect_overlay';
import { getElementFromPoint } from '../../../lib/dom/get_element_from_point';
import { findSourceComponent } from '../../../lib/fiber/find_source_component';
import { getInspectedElementData } from '../../../lib/get_inspected_element_data';
import { isEscapeKey } from '../../../lib/keyboard_shortcut/keyboard_shortcut';
import { mockBranch } from '../../../__mocks__/mocks';

jest.mock('../../../lib/dom/get_element_from_point', () => ({
Expand All @@ -30,6 +31,12 @@ jest.mock('../../../lib/get_inspected_element_data', () => ({
getInspectedElementData: jest.fn(),
}));

jest.mock('../../../lib/keyboard_shortcut/keyboard_shortcut', () => ({
isKeyboardShortcut: jest.fn(),
isMac: jest.fn(),
isEscapeKey: jest.fn(),
}));

describe('InspectOverlay', () => {
let mockCoreStart: CoreStart;

Expand Down Expand Up @@ -130,4 +137,23 @@ describe('InspectOverlay', () => {
expect(setFlyoutOverlayRef).not.toHaveBeenCalled();
});
});

it('should close overlay when escape key is pressed', () => {
const setIsInspecting = jest.fn();

(isEscapeKey as jest.Mock).mockReturnValue(true);

renderWithI18n(
<InspectOverlay
core={mockCoreStart}
setFlyoutOverlayRef={jest.fn()}
setIsInspecting={setIsInspecting}
branch={mockBranch}
/>
);

fireEvent.keyDown(window, { key: 'Escape', code: 'Escape' });

expect(setIsInspecting).toHaveBeenCalledWith(false);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { Global } from '@emotion/react';
import type { CoreStart, OverlayRef } from '@kbn/core/public';
import { EuiPortal, EuiWindowEvent, transparentize, useEuiTheme } from '@elastic/eui';
import { toMountPoint } from '@kbn/react-kibana-mount';
import { isEscapeKey } from '../../../lib/keyboard_shortcut/keyboard_shortcut';
import { findFirstFiberWithDebugSource } from '../../../lib/fiber/find_first_fiber_with_debug_source';
import { handleEventPropagation } from '../../../lib/dom/handle_event_propagation';
import { getInspectedElementData } from '../../../lib/get_inspected_element_data';
Expand Down Expand Up @@ -55,6 +56,16 @@ export const InspectOverlay = ({ core, branch, setFlyoutOverlayRef, setIsInspect
[euiTheme.colors.backgroundFilledText, euiTheme.levels.toast]
);

const handleKeydown = useCallback(
(event: KeyboardEvent) => {
if (isEscapeKey(event)) {
event.preventDefault();
setIsInspecting(false);
}
},
[setIsInspecting]
);

const handlePointerMove = useCallback((event: PointerEvent) => {
const target = getElementFromPoint(event);

Expand Down Expand Up @@ -154,6 +165,7 @@ export const InspectOverlay = ({ core, branch, setFlyoutOverlayRef, setIsInspect
id={INSPECT_OVERLAY_ID}
data-test-subj="inspectOverlayContainer"
>
<EuiWindowEvent event="keydown" handler={handleKeydown} />
<EuiWindowEvent event="pointermove" handler={handlePointerMove} />
<InspectHighlight
currentPosition={highlightPosition}
Expand All @@ -162,7 +174,7 @@ export const InspectOverlay = ({ core, branch, setFlyoutOverlayRef, setIsInspect
</div>
</React.Fragment>
),
[overlayCss, highlightPosition, sourceComponent?.type, handlePointerMove]
[overlayCss, highlightPosition, sourceComponent?.type, handlePointerMove, handleKeydown]
);

return <EuiPortal>{overlayContent}</EuiPortal>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,9 @@ export const isMac = (): boolean =>
((navigator as any)?.userAgentData?.platform || navigator.userAgent)
.toLowerCase()
.includes('mac');

/**
* Check if Escape key was pressed.
* @return {boolean} 'true' if Escape key was pressed, 'false' otherwise.
*/
export const isEscapeKey = (event: KeyboardEvent): boolean => event.key === 'Escape';