diff --git a/packages/react-shortcuts/CHANGELOG.md b/packages/react-shortcuts/CHANGELOG.md index dc9021437f..e371a267fc 100644 --- a/packages/react-shortcuts/CHANGELOG.md +++ b/packages/react-shortcuts/CHANGELOG.md @@ -16,3 +16,7 @@ and adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). ### Added - This CHANGELOG [(#508)](https://github.com/Shopify/quilt/pull/508) + +### Fixed + +- Added a check for event.getModifierState method before calling it. ([#1578](https://github.com/Shopify/quilt/pull/1578)) diff --git a/packages/react-shortcuts/src/ShortcutManager/ShortcutManager.tsx b/packages/react-shortcuts/src/ShortcutManager/ShortcutManager.tsx index b873be7a87..d767ec7a74 100644 --- a/packages/react-shortcuts/src/ShortcutManager/ShortcutManager.tsx +++ b/packages/react-shortcuts/src/ShortcutManager/ShortcutManager.tsx @@ -67,7 +67,10 @@ export default class ShortcutManager { } function keyGroupIsHeld(keyGroup: ModifierKey[]) { - return keyGroup.every((key: ModifierKey) => event.getModifierState(key)); + return keyGroup.every( + (key: ModifierKey) => + event.getModifierState && event.getModifierState(key), + ); } return hasKeyGroups(heldKeys) diff --git a/packages/react-shortcuts/src/tests/ShortcutManager.test.tsx b/packages/react-shortcuts/src/tests/ShortcutManager.test.tsx index 6f97a612cc..3fd6ca439a 100644 --- a/packages/react-shortcuts/src/tests/ShortcutManager.test.tsx +++ b/packages/react-shortcuts/src/tests/ShortcutManager.test.tsx @@ -297,6 +297,22 @@ describe('ShortcutManager', () => { expect(fooSpy).not.toHaveBeenCalled(); }); + + it('doesn’t fire on non-keyboard events', () => { + const matchSpy = jest.fn(); + const heldGroup: ModifierKey[] = ['Control', 'Shift']; + const heldToCheck: HeldKey = [[...heldGroup], ['Alt', 'Meta']]; + + mount( + + + , + ); + + document.dispatchEvent(new UIEvent('keydown')); + + expect(matchSpy).not.toHaveBeenCalled(); + }); }); });