Skip to content
Merged
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
27 changes: 16 additions & 11 deletions ui/packages/shared/components/src/KeyDownContext/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@ import {ReactNode, createContext, useContext, useEffect, useMemo, useState} from

export interface KeyDownState {
isShiftDown: boolean;
keys: string[];
}

const DEFAULT_VALUE = {
isShiftDown: false,
keys: [],
};

const KeyDownContext = createContext<KeyDownState>(DEFAULT_VALUE);
Expand All @@ -31,25 +29,32 @@ export const KeyDownProvider = ({
children: ReactNode;
value?: KeyDownState;
}): JSX.Element => {
const [keys, setKeys] = useState<string[]>([]);
// Shift requires special handling because it is a shortcut key
const isShiftDown = keys.includes('Shift') && !keys.includes('Meta');
// Shift requires special handling because it is a shortcut key and
// keydown and keyup is not recognized when the window begins taking a screenshot
const [isShiftDown, setIsShiftDown] = useState<boolean>(false);

useEffect(() => {
if (typeof window === 'undefined') {
return;
}

const handleKeyDown = (event: {key: string}): void => {
setKeys(keys => [...keys, event.key]);
};
const handleKeyDown = (event: {key: string; preventDefault: any}): void => {
if (event.key === 'Shift') {
setIsShiftDown(true);
return;
}

window.addEventListener('keydown', handleKeyDown);
// if any other key is pressed, reset the shift state
setIsShiftDown(false);
};

const handleKeyUp = (event: {key: string}): void => {
setKeys(keys => [...keys.filter(key => key !== event.key)]);
if (event.key === 'Shift') {
setIsShiftDown(false);
}
};

window.addEventListener('keydown', handleKeyDown);
window.addEventListener('keyup', handleKeyUp);

return () => {
Expand All @@ -58,7 +63,7 @@ export const KeyDownProvider = ({
};
}, []);

const value = useMemo(() => ({keys, isShiftDown}), [keys, isShiftDown]);
const value = useMemo(() => ({isShiftDown}), [isShiftDown]);

return <KeyDownContext.Provider value={value}>{children}</KeyDownContext.Provider>;
};
Expand Down