Mirror: React hooks for accessible, common web interactions. UI super powers without the UI.

Prevent focus in useDialogFocus even if hasPriority is false

Changed files
+15 -4
src
+7
src/__tests__/useDialogFocus.tsx
···
+
import React, { useRef } from 'react';
+
import { mount } from '@cypress/react';
+
+
import { useDialogFocus } from '../useDialogFocus';
+
+
it('allows dialogs to be navigated', () => {
+
});
+8 -4
src/useDialogFocus.ts
···
const ownerRef = options && options.ownerRef;
const disabled = !!(options && options.disabled);
const hasPriority = usePriority(ref, disabled);
-
const isActive = !disabled && !!hasPriority;
useLayoutEffect(() => {
-
if (!ref.current || !isActive) return;
+
if (!ref.current || disabled) return;
let selection = snapshotSelection(ownerRef && ownerRef.current);
let willReceiveFocus = false;
···
const owner =
(ownerRef && ownerRef.current) || (selection && selection.element);
-
if (willReceiveFocus || (owner && event.target === owner)) {
+
if (
+
willReceiveFocus ||
+
(hasPriority && owner && event.target === owner)
+
) {
if (!contains(ref.current, active))
selection = snapshotSelection(owner);
willReceiveFocus = false;
···
// Mark whether focus is moving forward for the `onFocus` handler
if (event.code === 'Tab') {
focusMovesForward = !event.shiftKey;
+
} else if (!hasPriority) {
+
return;
}
const active = document.activeElement as HTMLElement;
···
document.body.removeEventListener('focusin', onFocus);
document.removeEventListener('keydown', onKey);
};
-
}, [ref, isActive]);
+
}, [ref, disabled, hasPriority]);
}