6
6
7
7
import isChromatic from "chromatic/isChromatic" ;
8
8
import { type VariantProps , cva } from "class-variance-authority" ;
9
- import type { ReactNode } from "react" ;
9
+ import type { FC , ReactNode } from "react" ;
10
10
import { cn } from "utils/cn" ;
11
11
12
- const leaves = 8 ;
12
+ const SPINNER_LEAF_COUNT = 8 ;
13
13
14
14
const spinnerVariants = cva ( "" , {
15
15
variants : {
@@ -23,19 +23,25 @@ const spinnerVariants = cva("", {
23
23
} ,
24
24
} ) ;
25
25
26
- type SpinnerProps = React . SVGProps < SVGSVGElement > &
27
- VariantProps < typeof spinnerVariants > & {
28
- children ?: ReactNode ;
29
- loading ?: boolean ;
30
- } ;
26
+ type SpinnerProps = Readonly <
27
+ React . SVGProps < SVGSVGElement > &
28
+ VariantProps < typeof spinnerVariants > & {
29
+ children ?: ReactNode ;
30
+ loading ?: boolean ;
31
+ unmountedWhileLoading ?: boolean ;
32
+ spinnerStartDelayMs ?: number ;
33
+ }
34
+ > ;
31
35
32
- export function Spinner ( {
36
+ const leavesIterable = Array . from ( { length : SPINNER_LEAF_COUNT } , ( _ , i ) => i ) ;
37
+
38
+ export const Spinner : FC < SpinnerProps > = ( {
33
39
className,
34
40
size,
35
41
loading,
36
42
children,
37
43
...props
38
- } : SpinnerProps ) {
44
+ } ) => {
39
45
if ( ! loading ) {
40
46
return children ;
41
47
}
@@ -45,33 +51,29 @@ export function Spinner({
45
51
viewBox = "0 0 24 24"
46
52
xmlns = "http://www.w3.org/2000/svg"
47
53
fill = "currentColor"
48
- className = { cn ( spinnerVariants ( { size, className } ) ) }
54
+ className = { cn ( className , spinnerVariants ( { size } ) ) }
49
55
{ ...props }
50
56
>
51
57
< title > Loading spinner</ title >
52
- { [ ...Array ( leaves ) ] . map ( ( _ , i ) => {
53
- const rotation = i * ( 360 / leaves ) ;
54
-
55
- return (
56
- < rect
57
- key = { i }
58
- x = "10.9"
59
- y = "2"
60
- width = "2"
61
- height = "5.5"
62
- rx = "1"
63
- // 0.8 = leaves * 0.1
64
- className = {
65
- isChromatic ( ) ? "" : "animate-[loading_0.8s_ease-in-out_infinite]"
66
- }
67
- style = { {
68
- transform : `rotate(${ rotation } deg)` ,
69
- transformOrigin : "center" ,
70
- animationDelay : `${ - i * 0.1 } s` ,
71
- } }
72
- />
73
- ) ;
74
- } ) }
58
+ { leavesIterable . map ( ( leafIndex ) => (
59
+ < rect
60
+ key = { leafIndex }
61
+ x = "10.9"
62
+ y = "2"
63
+ width = "2"
64
+ height = "5.5"
65
+ rx = "1"
66
+ // 0.8 = leaves * 0.1
67
+ className = {
68
+ isChromatic ( ) ? "" : "animate-[loading_0.8s_ease-in-out_infinite]"
69
+ }
70
+ style = { {
71
+ transform : `rotate(${ leafIndex * ( 360 / SPINNER_LEAF_COUNT ) } deg)` ,
72
+ transformOrigin : "center" ,
73
+ animationDelay : `${ - leafIndex * 0.1 } s` ,
74
+ } }
75
+ />
76
+ ) ) }
75
77
</ svg >
76
78
) ;
77
- }
79
+ } ;
0 commit comments