Skip to content
Open
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/rotten-mugs-tan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/react': minor
---

feat: make Spinner's delay customizable
4 changes: 2 additions & 2 deletions packages/react/src/Spinner/Spinner.docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
},
{
"defaultValue": "false",
"description": "Whether to delay the spinner before rendering by the defined 1000ms.",
"description": "Controls whether and how long to delay rendering the spinner. Set to `true` to delay by 1000ms, or provide a custom number of milliseconds.",
"name": "delay",
"type": "boolean"
"type": "boolean | number"
}
],
"status": "alpha",
Expand Down
28 changes: 28 additions & 0 deletions packages/react/src/Spinner/Spinner.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,33 @@ describe('Spinner', () => {
// No errors should occur
expect(true).toBe(true)
})

it('should render after custom ms when delay is a number', () => {
const {container} = render(<Spinner delay={500} />)

// Not visible initially
expect(container.querySelector('svg')).not.toBeInTheDocument()

// Advance timers by 500ms
act(() => {
vi.advanceTimersByTime(500)
})

// Now it should be visible
expect(container.querySelector('svg')).toBeInTheDocument()
})

it('should cleanup timeout on unmount when delay is a number', () => {
const {unmount} = render(<Spinner delay={500} />)

// Unmount before the delay completes
unmount()

// Advance timers to see if there are any side effects
vi.advanceTimersByTime(500)

// No errors should occur
expect(true).toBe(true)
})
})
})
7 changes: 4 additions & 3 deletions packages/react/src/Spinner/Spinner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ export type SpinnerProps = {
'aria-label'?: string
className?: string
style?: React.CSSProperties
/** Whether to delay the spinner before rendering by the defined 1000ms. */
delay?: boolean
/** Controls whether and how long to delay rendering the spinner. Set to `true` to delay by 1000ms, or provide a custom number of milliseconds. */
delay?: boolean | number
} & HTMLDataAttributes

function Spinner({
Expand All @@ -46,9 +46,10 @@ function Spinner({

useEffect(() => {
if (delay) {
const delayDuration = typeof delay === 'number' ? delay : 1000
const timeoutId = setTimeout(() => {
setIsVisible(true)
}, 1000)
}, delayDuration)

return () => clearTimeout(timeoutId)
}
Expand Down
Loading