From 763e4eed8d88191aee4d20595b96945b3f0944cc Mon Sep 17 00:00:00 2001 From: Andrew Patton Date: Wed, 7 Feb 2024 14:00:25 -0800 Subject: [PATCH] Add use-keyboard-events util isPrimaryModifierPressed --- packages/use-keyboard-events/src/handlers.ts | 7 +++++++ packages/use-keyboard-events/src/useKeyboardEvents.ts | 6 +++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/use-keyboard-events/src/handlers.ts b/packages/use-keyboard-events/src/handlers.ts index 10a5ae9c..f4ea0ced 100644 --- a/packages/use-keyboard-events/src/handlers.ts +++ b/packages/use-keyboard-events/src/handlers.ts @@ -63,6 +63,13 @@ export function addHandler({ }; } +const IS_APPLE_REGEXP = /mac|iphone|ipad|ipod/i; + +export function isPrimaryModifierPressed(event: KeyboardEvent) { + const platform = globalThis.navigator?.platform ?? ''; + return IS_APPLE_REGEXP.test(platform) ? event.metaKey : event.ctrlKey; +} + const NON_TEXT_INPUT_TYPES = new Set([ 'button', 'checkbox', diff --git a/packages/use-keyboard-events/src/useKeyboardEvents.ts b/packages/use-keyboard-events/src/useKeyboardEvents.ts index 410c5a4c..ee2d51ec 100644 --- a/packages/use-keyboard-events/src/useKeyboardEvents.ts +++ b/packages/use-keyboard-events/src/useKeyboardEvents.ts @@ -3,7 +3,11 @@ import { useEffect } from 'react'; import { addHandler, addHandlers } from './handlers.js'; import type { Handler } from './handlers.js'; -export { isEventTargetUsingKeyEvent, usesKeyEvents } from './handlers.js'; +export { + isEventTargetUsingKeyEvent, + isPrimaryModifierPressed, + usesKeyEvents, +} from './handlers.js'; type Props = { ignoreUsedKeyboardEvents?: boolean;