Skip to content

Commit

Permalink
Add use-keyboard-events util isPrimaryModifierPressed
Browse files Browse the repository at this point in the history
  • Loading branch information
acusti committed Feb 7, 2024
1 parent 85cbd00 commit 763e4ee
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
7 changes: 7 additions & 0 deletions packages/use-keyboard-events/src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
6 changes: 5 additions & 1 deletion packages/use-keyboard-events/src/useKeyboardEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 763e4ee

Please sign in to comment.