Skip to content

Commit

Permalink
Fix getTargetByDirectionFromElement in target to find NavigableContai…
Browse files Browse the repository at this point in the history
…ners with element rect first (#3216)

Enact-DCO-1.0-Signed-off-by: Juwon Jeong (juwon.jeong@lge.com)
  • Loading branch information
juwonjeong authored Feb 29, 2024
1 parent b7b6f8f commit d43b2c3
Showing 1 changed file with 28 additions and 8 deletions.
36 changes: 28 additions & 8 deletions packages/spotlight/src/target.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,20 +367,14 @@ function getTargetByDirectionFromElement (direction, element) {
}

const elementContainerId = getContainersForNode(element).pop();
let elementRect = null;
if (elementContainerId !== rootContainerId && getContainerConfig(elementContainerId)?.overflow) {
elementRect = getIntersectionRect(getContainerNode(elementContainerId), element);
} else {
elementRect = getRect(element);
}

const next = getNavigableContainersForNode(element)
let next = getNavigableContainersForNode(element)
.reduceRight((result, containerId, index, elementContainerIds) => {
result = result || getTargetInContainerByDirectionFromElement(
direction,
containerId,
element,
elementRect,
getRect(element),
elementContainerIds
);

Expand All @@ -398,6 +392,32 @@ function getTargetByDirectionFromElement (direction, element) {
return result;
}, null);

if (next === element && elementContainerId !== rootContainerId && getContainerConfig(elementContainerId)?.overflow) {
next = getNavigableContainersForNode(element)
.reduceRight((result, containerId, index, elementContainerIds) => {
result = result || getTargetInContainerByDirectionFromElement(
direction,
containerId,
element,
getIntersectionRect(getContainerNode(elementContainerId), element),
elementContainerIds
);

if (!result) {
result = getLeaveForTarget(containerId, direction);

// To support a `leaveFor` configuration with navigation disallowed in the current
// `direction`, we return the current element to prevent further searches for a
// target in this reduction.
if (result === false) {
result = element;
}
}

return result;
}, null);
}

// if the reduce above returns the original element, it means it hit a `leaveFor` config that
// prevents navigation so we enforce that here by returning null.
return next !== element ? next : null;
Expand Down

0 comments on commit d43b2c3

Please sign in to comment.