Skip to content

Commit eef48dc

Browse files
committed
fix: Tree scrolling elements into view
The tree will now correctly scroll the active item into view when using the left and right arrow keys to jump to the expandable parent with the left arrow key or to the first child with the right arrow key.
1 parent a9a0902 commit eef48dc

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

packages/tree/src/useTreeMovement.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,12 @@ export default function useTreeMovement({
238238
if (!expandedIds.includes(itemId)) {
239239
onItemExpansion(itemId, true);
240240
} else {
241-
setFocusedIndex(focusedIndex + 1);
241+
const nextIndex = focusedIndex + 1;
242+
const nextItem =
243+
itemIdRefs[visibleItems[nextIndex]?.itemId]?.ref.current;
244+
245+
setFocusedIndex(nextIndex);
246+
scrollIntoView(event.currentTarget, nextItem);
242247
}
243248
break;
244249
case "ArrowLeft":
@@ -248,7 +253,11 @@ export default function useTreeMovement({
248253
const parentIndex = visibleItems.findIndex(
249254
(item) => item.itemId === parentId
250255
);
256+
const parentItem =
257+
itemIdRefs[visibleItems[parentIndex]?.itemId]?.ref.current;
258+
251259
setFocusedIndex(parentIndex);
260+
scrollIntoView(event.currentTarget, parentItem);
252261
}
253262
break;
254263
case "a": {

0 commit comments

Comments
 (0)