Skip to content

Commit 8da41df

Browse files
committed
refactor: clean up existing code
1 parent de70ff3 commit 8da41df

File tree

1 file changed

+36
-34
lines changed

1 file changed

+36
-34
lines changed

site/src/components/Spinner/Spinner.tsx

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77
import isChromatic from "chromatic/isChromatic";
88
import { type VariantProps, cva } from "class-variance-authority";
9-
import type { ReactNode } from "react";
9+
import type { FC, ReactNode } from "react";
1010
import { cn } from "utils/cn";
1111

12-
const leaves = 8;
12+
const SPINNER_LEAF_COUNT = 8;
1313

1414
const spinnerVariants = cva("", {
1515
variants: {
@@ -23,19 +23,25 @@ const spinnerVariants = cva("", {
2323
},
2424
});
2525

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+
>;
3135

32-
export function Spinner({
36+
const leavesIterable = Array.from({ length: SPINNER_LEAF_COUNT }, (_, i) => i);
37+
38+
export const Spinner: FC<SpinnerProps> = ({
3339
className,
3440
size,
3541
loading,
3642
children,
3743
...props
38-
}: SpinnerProps) {
44+
}) => {
3945
if (!loading) {
4046
return children;
4147
}
@@ -45,33 +51,29 @@ export function Spinner({
4551
viewBox="0 0 24 24"
4652
xmlns="http://www.w3.org/2000/svg"
4753
fill="currentColor"
48-
className={cn(spinnerVariants({ size, className }))}
54+
className={cn(className, spinnerVariants({ size }))}
4955
{...props}
5056
>
5157
<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+
))}
7577
</svg>
7678
);
77-
}
79+
};

0 commit comments

Comments
 (0)