Skip to content

Commit

Permalink
TreeView: Remove loading indicator delay (#2518)
Browse files Browse the repository at this point in the history
* Update async count story

* Remove loading indicator delay

* Fix focus style

* Create wise-dogs-play.md
  • Loading branch information
colebemis committed Nov 2, 2022
1 parent 862f93c commit 60c36f5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changeset/wise-dogs-play.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@primer/react": patch
---

TreeView: Remove loading indicator delay
8 changes: 4 additions & 4 deletions src/TreeView/TreeView.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ export const AsyncWithCount: Story = args => {
))}
</TreeView.SubTree>
</TreeView.Item>
<TreeView.LinkItem href="#src">
<TreeView.Item>
<TreeView.LeadingVisual>
<TreeView.DirectoryIcon />
</TreeView.LeadingVisual>
Expand All @@ -452,7 +452,7 @@ export const AsyncWithCount: Story = args => {
</TreeView.LeadingVisual>
Avatar.tsx
</TreeView.LinkItem>
<TreeView.LinkItem href="#button" current>
<TreeView.Item current>
<TreeView.LeadingVisual>
<TreeView.DirectoryIcon />
</TreeView.LeadingVisual>
Expand All @@ -471,15 +471,15 @@ export const AsyncWithCount: Story = args => {
Button.test.tsx
</TreeView.LinkItem>
</TreeView.SubTree>
</TreeView.LinkItem>
</TreeView.Item>
<TreeView.Item>
<TreeView.LeadingVisual>
<FileIcon />
</TreeView.LeadingVisual>
ReallyLongFileNameThatShouldBeTruncated.tsx
</TreeView.Item>
</TreeView.SubTree>
</TreeView.LinkItem>
</TreeView.Item>
</TreeView>
</nav>
</Box>
Expand Down
18 changes: 7 additions & 11 deletions src/TreeView/TreeView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ const Item = React.forwardRef<HTMLElement, TreeViewItemProps>(
sx={{
outline: 'none',
'&:focus-visible > div': {
boxShadow: (theme: Theme) => `inset 0 0 0 2px ${theme.colors.accent.emphasis}`,
boxShadow: (theme: Theme) => `inset 0 0 0 2px ${theme.colors.accent.fg}`,
'@media (forced-colors: active)': {
outline: '2px solid HighlightText',
outlineOffset: -2
Expand Down Expand Up @@ -428,8 +428,7 @@ const SubTree: React.FC<TreeViewSubTreeProps> = ({count, state, children}) => {
const {announceUpdate} = React.useContext(RootContext)
const {itemId, isExpanded, isSubTreeEmpty, setIsSubTreeEmpty} = React.useContext(ItemContext)
const [isLoadingItemVisible, setIsLoadingItemVisible] = React.useState(false)
const {safeSetTimeout, safeClearTimeout} = useSafeTimeout()
const timeoutId = React.useRef<number>(0)
const {safeSetTimeout} = useSafeTimeout()
const loadingItemRef = React.useRef<HTMLElement>(null)
const ref = React.useRef<HTMLElement>(null)

Expand Down Expand Up @@ -467,26 +466,23 @@ const SubTree: React.FC<TreeViewSubTreeProps> = ({count, state, children}) => {
}
}, [state, itemId, announceUpdate, safeSetTimeout])

// Show loading indicator after a short delay
// Manage loading indicator state
React.useEffect(() => {
// If we're in the loading state, but not showing the loading indicator yet,
// start a timer to show the loading indicator after a short delay.
// show the loading indicator
if (state === 'loading' && !isLoadingItemVisible) {
timeoutId.current = safeSetTimeout(() => {
setIsLoadingItemVisible(true)
}, 300)
setIsLoadingItemVisible(true)
}

// If we're not in the loading state, but we're still showing a loading indicator,
// hide the loading indicator and move focus if necessary
if (state !== 'loading' && isLoadingItemVisible) {
const isLoadingItemFocused = document.activeElement === loadingItemRef.current

safeClearTimeout(timeoutId.current)
setIsLoadingItemVisible(false)

if (isLoadingItemFocused) {
setTimeout(() => {
safeSetTimeout(() => {
const parentElement = document.getElementById(itemId)
if (!parentElement) return

Expand All @@ -500,7 +496,7 @@ const SubTree: React.FC<TreeViewSubTreeProps> = ({count, state, children}) => {
})
}
}
}, [state, safeSetTimeout, safeClearTimeout, isLoadingItemVisible, itemId])
}, [state, safeSetTimeout, isLoadingItemVisible, itemId])

if (!isExpanded) {
return null
Expand Down

0 comments on commit 60c36f5

Please sign in to comment.