-
Notifications
You must be signed in to change notification settings - Fork 983
chore: add spinner component #16014
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
chore: add spinner component #16014
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
07f6e53
Deprecate Spinner
BrunoQuaresma 2fc5748
chore: add Spinner component
BrunoQuaresma 5353da2
Avoid animation on Chromatic
BrunoQuaresma 962c0dd
Fix lint error
BrunoQuaresma 7d97bc3
Fix chromatic import
BrunoQuaresma 9561e20
Run make fmt
BrunoQuaresma a901ab6
Make Spinner to looks more closer to the design
BrunoQuaresma 07c8a7f
Merge branch 'bq/add-spinner-component' of https://github.com/coder/c…
BrunoQuaresma c8466e0
Improve loading animation
BrunoQuaresma a36e017
Merge branch 'bq/add-spinner-component' of https://github.com/coder/c…
BrunoQuaresma File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import type { Meta, StoryObj } from "@storybook/react"; | ||
import { PlusIcon } from "lucide-react"; | ||
import { Spinner } from "./Spinner"; | ||
|
||
const meta: Meta<typeof Spinner> = { | ||
title: "components/Spinner", | ||
component: Spinner, | ||
args: { | ||
children: <PlusIcon className="size-icon-lg" />, | ||
}, | ||
}; | ||
|
||
export default meta; | ||
type Story = StoryObj<typeof Spinner>; | ||
|
||
export const Idle: Story = {}; | ||
|
||
export const Loading: Story = { | ||
args: { loading: true }, | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,77 @@ | ||
import CircularProgress, { | ||
type CircularProgressProps, | ||
} from "@mui/material/CircularProgress"; | ||
import isChromatic from "chromatic/isChromatic"; | ||
import type { FC } from "react"; | ||
|
||
/** | ||
* Spinner component used to indicate loading states. This component abstracts | ||
* the MUI CircularProgress to provide better control over its rendering, | ||
* especially in snapshot tests with Chromatic. | ||
* This component was inspired by | ||
* https://www.radix-ui.com/themes/docs/components/spinner and developed using | ||
* https://v0.dev/ help. | ||
*/ | ||
export const Spinner: FC<CircularProgressProps> = (props) => { | ||
/** | ||
* During Chromatic snapshots, we render the spinner as determinate to make it | ||
* static without animations, using a deterministic value (75%). | ||
*/ | ||
if (isChromatic()) { | ||
props.variant = "determinate"; | ||
props.value = 75; | ||
|
||
import { type VariantProps, cva } from "class-variance-authority"; | ||
import type { ReactNode } from "react"; | ||
import { cn } from "utils/cn"; | ||
import isChromatic from "chromatic/isChromatic"; | ||
|
||
const leaves = 8; | ||
|
||
const spinnerVariants = cva("", { | ||
variants: { | ||
size: { | ||
lg: "size-icon-lg", | ||
sm: "size-icon-sm", | ||
}, | ||
}, | ||
defaultVariants: { | ||
size: "lg", | ||
}, | ||
}); | ||
|
||
type SpinnerProps = React.SVGProps<SVGSVGElement> & | ||
VariantProps<typeof spinnerVariants> & { | ||
children?: ReactNode; | ||
loading?: boolean; | ||
}; | ||
|
||
export function Spinner({ | ||
className, | ||
size, | ||
loading, | ||
children, | ||
...props | ||
}: SpinnerProps) { | ||
if (!loading) { | ||
return children; | ||
} | ||
return <CircularProgress {...props} />; | ||
}; | ||
|
||
return ( | ||
<svg | ||
viewBox="0 0 24 24" | ||
xmlns="http://www.w3.org/2000/svg" | ||
fill="currentColor" | ||
className={cn(spinnerVariants({ size, className }))} | ||
{...props} | ||
> | ||
<title>Loading spinner</title> | ||
{[...Array(leaves)].map((_, i) => { | ||
const rotation = i * (360 / leaves); | ||
|
||
return ( | ||
<rect | ||
key={i} | ||
x="10.9" | ||
y="2" | ||
width="2" | ||
height="5.5" | ||
rx="1" | ||
// 0.8 = leaves * 0.1 | ||
className={ | ||
isChromatic() ? "" : "animate-[loading_0.8s_ease-in-out_infinite]" | ||
} | ||
style={{ | ||
transform: `rotate(${rotation}deg)`, | ||
transformOrigin: "center", | ||
animationDelay: `${-i * 0.1}s`, | ||
}} | ||
/> | ||
); | ||
})} | ||
</svg> | ||
); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import CircularProgress, { | ||
type CircularProgressProps, | ||
} from "@mui/material/CircularProgress"; | ||
import isChromatic from "chromatic/isChromatic"; | ||
import type { FC } from "react"; | ||
|
||
/** | ||
* Spinner component used to indicate loading states. This component abstracts | ||
* the MUI CircularProgress to provide better control over its rendering, | ||
* especially in snapshot tests with Chromatic. | ||
* | ||
* @deprecated prefer `components.Spinner` | ||
*/ | ||
export const Spinner: FC<CircularProgressProps> = (props) => { | ||
/** | ||
* During Chromatic snapshots, we render the spinner as determinate to make it | ||
* static without animations, using a deterministic value (75%). | ||
*/ | ||
if (isChromatic()) { | ||
props.variant = "determinate"; | ||
props.value = 75; | ||
} | ||
return <CircularProgress {...props} />; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.