Skip to content
Draft
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/shaggy-pants-remain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@primer/react": minor
---

feat(Spinner): Add a delay prop to set how long to wait before rendering the Spinner
20 changes: 20 additions & 0 deletions packages/react/src/Spinner/Spinner.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type React from 'react'
import {useState, useEffect} from 'react'
import {VisuallyHidden} from '../VisuallyHidden'
import type {HTMLDataAttributes} from '../internal/internal-types'
import {useId} from '../hooks'
Expand All @@ -20,6 +21,8 @@ export type SpinnerProps = {
'aria-label'?: string
className?: string
style?: React.CSSProperties
/** Number of milliseconds to delay the spinner before rendering. */
delay?: number
} & HTMLDataAttributes

function Spinner({
Expand All @@ -28,12 +31,29 @@ function Spinner({
'aria-label': ariaLabel,
className,
style,
delay = 0,
...props
}: SpinnerProps) {
const size = sizeMap[sizeKey]
const hasHiddenLabel = srText !== null && ariaLabel === undefined
const labelId = useId()

const [isVisible, setIsVisible] = useState(delay === 0)

useEffect(() => {
if (delay > 0) {
const timeoutId = setTimeout(() => {
setIsVisible(true)
}, delay)

return () => clearTimeout(timeoutId)
}
}, [delay])

if (!isVisible) {
return null
}

return (
/* inline-flex removes the extra line height */
<span className={classes.Box}>
Expand Down
Loading