refactor(useKeyPress): update preventDefault handling
This commit is contained in:
@@ -8,6 +8,7 @@ type KeyOrCode = 'key' | 'code';
|
|||||||
export type UseKeyPressOptions = {
|
export type UseKeyPressOptions = {
|
||||||
target?: Window | Document | HTMLElement | ShadowRoot | null;
|
target?: Window | Document | HTMLElement | ShadowRoot | null;
|
||||||
actInsideInputWithModifier?: boolean;
|
actInsideInputWithModifier?: boolean;
|
||||||
|
preventDefault?: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
const defaultDoc = typeof document !== 'undefined' ? document : null;
|
const defaultDoc = typeof document !== 'undefined' ? document : null;
|
||||||
@@ -88,7 +89,7 @@ export function useKeyPress(
|
|||||||
|
|
||||||
if (keyCode !== null) {
|
if (keyCode !== null) {
|
||||||
const downHandler = (event: KeyboardEvent) => {
|
const downHandler = (event: KeyboardEvent) => {
|
||||||
modifierPressed.current = event.ctrlKey || event.metaKey || event.shiftKey;
|
modifierPressed.current = event.ctrlKey || event.metaKey || event.shiftKey || event.altKey;
|
||||||
const preventAction =
|
const preventAction =
|
||||||
(!modifierPressed.current || (modifierPressed.current && !options.actInsideInputWithModifier)) &&
|
(!modifierPressed.current || (modifierPressed.current && !options.actInsideInputWithModifier)) &&
|
||||||
isInputDOMNode(event);
|
isInputDOMNode(event);
|
||||||
@@ -100,7 +101,16 @@ export function useKeyPress(
|
|||||||
pressedKeys.current.add(event[keyOrCode]);
|
pressedKeys.current.add(event[keyOrCode]);
|
||||||
|
|
||||||
if (isMatchingKey(keyCodes, pressedKeys.current, false)) {
|
if (isMatchingKey(keyCodes, pressedKeys.current, false)) {
|
||||||
event.preventDefault();
|
const target = (event.composedPath?.()?.[0] || event.target) as Element | null;
|
||||||
|
const isInteractiveElement = target?.nodeName === 'BUTTON' || target?.nodeName === 'A';
|
||||||
|
|
||||||
|
if (
|
||||||
|
(options.preventDefault || typeof options.preventDefault === 'undefined') &&
|
||||||
|
(modifierPressed.current || !isInteractiveElement)
|
||||||
|
) {
|
||||||
|
event.preventDefault();
|
||||||
|
}
|
||||||
|
|
||||||
setKeyPressed(true);
|
setKeyPressed(true);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ export const getDimensions = (node: HTMLDivElement): Dimensions => ({
|
|||||||
export const getHostForElement = (element: HTMLElement | EventTarget | null): Document | ShadowRoot =>
|
export const getHostForElement = (element: HTMLElement | EventTarget | null): Document | ShadowRoot =>
|
||||||
((element as Partial<HTMLElement> | null)?.getRootNode?.() as Document | ShadowRoot) || window?.document;
|
((element as Partial<HTMLElement> | null)?.getRootNode?.() as Document | ShadowRoot) || window?.document;
|
||||||
|
|
||||||
const inputTags = ['INPUT', 'SELECT', 'TEXTAREA', 'BUTTON'];
|
const inputTags = ['INPUT', 'SELECT', 'TEXTAREA'];
|
||||||
|
|
||||||
export function isInputDOMNode(event: KeyboardEvent): boolean {
|
export function isInputDOMNode(event: KeyboardEvent): boolean {
|
||||||
// using composed path for handling shadow dom
|
// using composed path for handling shadow dom
|
||||||
|
|||||||
Reference in New Issue
Block a user