Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13319 from storybookjs/13296-event-key
UI: Fix keybindings on non-US keyboard layouts
- Loading branch information
Showing
5 changed files
with
67 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
const codeToKeyMap = { | ||
// event.code => event.key | ||
Space: ' ', | ||
Slash: '/', | ||
ArrowLeft: 'ArrowLeft', | ||
ArrowUp: 'ArrowUp', | ||
ArrowRight: 'ArrowRight', | ||
ArrowDown: 'ArrowDown', | ||
Escape: 'Escape', | ||
Enter: 'Enter', | ||
}; | ||
|
||
interface Modifiers { | ||
alt?: boolean; | ||
ctrl?: boolean; | ||
meta?: boolean; | ||
shift?: boolean; | ||
} | ||
|
||
const allFalse = { alt: false, ctrl: false, meta: false, shift: false }; | ||
|
||
export const matchesModifiers = (modifiers: Modifiers | false, event: KeyboardEvent) => { | ||
const { alt, ctrl, meta, shift } = modifiers === false ? allFalse : modifiers; | ||
if (typeof alt === 'boolean' && alt !== event.altKey) return false; | ||
if (typeof ctrl === 'boolean' && ctrl !== event.ctrlKey) return false; | ||
if (typeof meta === 'boolean' && meta !== event.metaKey) return false; | ||
if (typeof shift === 'boolean' && shift !== event.shiftKey) return false; | ||
return true; | ||
}; | ||
|
||
export const matchesKeyCode = (code: keyof typeof codeToKeyMap, event: KeyboardEvent) => { | ||
// event.code is preferable but not supported in IE | ||
return event.code ? event.code === code : event.key === codeToKeyMap[code]; | ||
}; |
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