Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/violet-plants-sip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@primer/react": patch
---

TreeView: Fix unexpected scrolling when focusing child items
13 changes: 7 additions & 6 deletions src/TreeView/TreeView.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -670,19 +670,20 @@ export const NestedScrollContainer: Story = () => {
return (
<Box sx={{p: 3, maxWidth: 400, maxHeight: '50vh', overflow: 'auto'}}>
<TreeView aria-label="Files">
{Array.from({length: 100}).map((_, index) => (
<TreeView.Item key={index}>
{Array.from({length: 100}).map((_, i) => (
<TreeView.Item key={i}>
<TreeView.LeadingVisual>
<TreeView.DirectoryIcon />
</TreeView.LeadingVisual>
Directory {index}
Directory {i}
<TreeView.SubTree>
{Array.from({length: 10}).map((_, index) => (
<TreeView.Item key={index}>
{Array.from({length: 10}).map((_, j) => (
// eslint-disable-next-line no-console
<TreeView.Item key={j} onSelect={() => console.log(`Directory ${i}/File ${j}`)}>
<TreeView.LeadingVisual>
<FileIcon />
</TreeView.LeadingVisual>
File {index}
File {j}
</TreeView.Item>
))}
</TreeView.SubTree>
Expand Down
3 changes: 3 additions & 0 deletions src/TreeView/TreeView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,9 @@ const Item = React.forwardRef<HTMLElement, TreeViewItemProps>(
onFocus={event => {
// Scroll the first child into view when the item receives focus
event.currentTarget.firstElementChild?.scrollIntoView({block: 'nearest', inline: 'nearest'})

// Prevent focus event from bubbling up to parent items
event.stopPropagation()
}}
sx={{
outline: 'none',
Expand Down