-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: Fullview Flyte Deck modal (#764)
* chore: full viewport modal Signed-off-by: Frank Flitton <[email protected]> * chore: close modal on escape Signed-off-by: Frank Flitton <[email protected]> * chore: key events as hook Signed-off-by: Frank Flitton <[email protected]> * chore: add close on esc key to other modals Signed-off-by: Frank Flitton <[email protected]> --------- Signed-off-by: Frank Flitton <[email protected]>
- Loading branch information
1 parent
333940f
commit c69f010
Showing
8 changed files
with
123 additions
and
20 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
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
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
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
23 changes: 23 additions & 0 deletions
23
packages/console/src/components/hooks/test/useKeyListener.test.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,23 @@ | ||
import React from 'react'; | ||
import { fireEvent, getByText, render } from '@testing-library/react'; | ||
import { useEscapeKey } from '../useKeyListener'; | ||
|
||
it('calls the callback on pressing the ESC key', () => { | ||
const callbackSpy = jest.fn(); | ||
const callback = (_?: KeyboardEvent) => { | ||
callbackSpy(); | ||
}; | ||
|
||
const TestComponent = () => { | ||
useEscapeKey(callback); | ||
return <div>test</div>; | ||
}; | ||
|
||
render(<TestComponent />); | ||
|
||
fireEvent.keyDown(getByText(global.document.body, 'test'), { | ||
key: 'Escape', | ||
}); | ||
|
||
expect(callbackSpy).toHaveBeenCalled(); | ||
}); |
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,29 @@ | ||
import { useEffect } from 'react'; | ||
|
||
/** | ||
* Safely register and unregister a key listener on the document. | ||
* @param onKeyPress | ||
* @param keycode | ||
*/ | ||
export const useKeyListener = ( | ||
onKeyPress: (e: KeyboardEvent) => void, | ||
keycode = 'Escape', | ||
) => { | ||
useEffect(() => { | ||
const eventCallback = e => { | ||
if (e.key === keycode) { | ||
onKeyPress(e); | ||
} | ||
}; | ||
document.addEventListener('keydown', eventCallback); | ||
return () => document.removeEventListener('keydown', eventCallback); | ||
}, []); | ||
}; | ||
|
||
/** | ||
* Register a key listener for the Escape key. | ||
* @param onKeyPress | ||
*/ | ||
export const useEscapeKey = (onKeyPress: (e: KeyboardEvent) => void) => { | ||
useKeyListener(onKeyPress, 'Escape'); | ||
}; |