From fa0a9a109c6e81b2601f7c4111016455f5477940 Mon Sep 17 00:00:00 2001 From: David Lechner Date: Thu, 12 May 2022 16:14:48 -0500 Subject: [PATCH] editor: improve a18y and visual appearance --- src/editor/Editor.test.tsx | 30 +++++++++++++++-- src/editor/Editor.tsx | 29 ++++++++++++++--- src/editor/editor.scss | 67 +++++++++++++++++++++++++++++--------- 3 files changed, 105 insertions(+), 21 deletions(-) diff --git a/src/editor/Editor.test.tsx b/src/editor/Editor.test.tsx index 9d6541fc..eb8fc371 100644 --- a/src/editor/Editor.test.tsx +++ b/src/editor/Editor.test.tsx @@ -13,7 +13,7 @@ import { editorActivateFile, editorCloseFile } from './actions'; describe('Editor', () => { describe('tabs', () => { - it('should dispatch action when tab is clicked', async () => { + it('should dispatch activate action when tab is clicked', async () => { const [editor, dispatch] = testRender(, { editor: { openFiles: ['test.file'] }, }); @@ -23,7 +23,23 @@ describe('Editor', () => { expect(dispatch).toHaveBeenCalledWith(editorActivateFile('test.file')); }); - it('should dispatch action when close button is clicked', async () => { + it.each(['enter', 'space'])( + 'should dispatch activate action when % button is pressed', + async (button) => { + const [editor, dispatch] = testRender(, { + editor: { openFiles: ['test.file'] }, + }); + + userEvent.type( + editor.getByRole('tab', { name: 'test.file' }), + `{${button}}`, + ); + + expect(dispatch).toHaveBeenCalledWith(editorActivateFile('test.file')); + }, + ); + + it('should dispatch close action when close button is clicked', async () => { const [editor, dispatch] = testRender(, { editor: { openFiles: ['test.file'] }, }); @@ -32,6 +48,16 @@ describe('Editor', () => { expect(dispatch).toHaveBeenCalledWith(editorCloseFile('test.file')); }); + + it('should dispatch close action when delete button is pressed', async () => { + const [editor, dispatch] = testRender(, { + editor: { openFiles: ['test.file'] }, + }); + + userEvent.type(editor.getByRole('tab', { name: 'test.file' }), '{delete}'); + + expect(dispatch).toHaveBeenCalledWith(editorCloseFile('test.file')); + }); }); describe('context menu', () => { diff --git a/src/editor/Editor.tsx b/src/editor/Editor.tsx index 9d4ca65c..73c6da7a 100644 --- a/src/editor/Editor.tsx +++ b/src/editor/Editor.tsx @@ -13,6 +13,7 @@ import { Tab, TabId, Tabs, + Text, } from '@blueprintjs/core'; import { ContextMenu2, ResizeSensor2 } from '@blueprintjs/popover2'; import { I18n, useI18n } from '@shopify/react-i18n'; @@ -202,25 +203,44 @@ const EditorTabs: React.VoidFunctionComponent = ({ const labelId = useUniqueId('pb-editor'); + // close tab when delete key is pressed + const handleKeyDown = useCallback( + (e: React.KeyboardEvent, fileName: string) => { + if (e.key === 'Delete') { + dispatch(editorCloseFile(fileName)); + e.preventDefault(); + e.stopPropagation(); + } + }, + [dispatch], + ); + return ( {openFiles.map((fileName, i) => ( handleKeyDown(e, fileName)} > - {fileName} + + {fileName} +