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

Fix small discrepancies in ownerRef handling and selection logic

Changed files
+21 -8
src
+11 -1
src/useDialogFocus.ts
···
focusIndex > 0 ? focusIndex - 1 : focusTargets.length - 1;
willReceiveFocus = true;
focusTargets[nextIndex].focus();
+
} else if (event.code === 'Home') {
+
// Implement Home => first item
+
event.preventDefault();
+
willReceiveFocus = true;
+
focusTargets[0].focus();
+
} else if (event.code === 'End') {
+
// Implement End => last item
+
event.preventDefault();
+
willReceiveFocus = true;
+
focusTargets[focusTargets.length - 1].focus();
} else if (
owner &&
!contains(ref.current, owner) &&
···
event.preventDefault();
const newTarget = getFirstFocusTarget(ref.current);
if (newTarget) {
-
selection = snapshotSelection(owner);
+
willReceiveFocus = true;
newTarget.focus();
}
} else if (
+3 -5
src/useMenuFocus.ts
···
selection = snapshotSelection(owner);
} else if (
contains(ref.current, target) &&
-
!contains(ref.current, relatedTarget)
+
!contains(ref.current, relatedTarget) &&
+
(!ownerRef || contains(relatedTarget, ownerRef.current))
) {
// Check whether focus is about to move into the container and snapshot last focus
selection = snapshotSelection(owner);
···
// Move focus to first target when enter is pressed
event.preventDefault();
const newTarget = getFirstFocusTarget(ref.current);
-
if (newTarget) {
-
selection = snapshotSelection(owner);
-
newTarget.focus();
-
}
+
if (newTarget) newTarget.focus();
} else if (
owner &&
!contains(ref.current, owner) &&
+7 -2
src/utils/element.ts
···
);
export const contains = (
-
owner: Element | null,
+
owner: Element | EventTarget | null,
node: Element | EventTarget | null
-
) => !!(node && owner && (owner === node || owner.contains(node as Element)));
+
) =>
+
!!(
+
node &&
+
owner &&
+
(owner === node || (owner as Element).contains(node as Element))
+
);