-
Notifications
You must be signed in to change notification settings - Fork 0
feat(Spinner): add accessible SVG spinner component #63
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
Open
Arnab-Mandal1
wants to merge
1
commit into
main
Choose a base branch
from
feature/spinner
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import type {Meta, StoryObj} from "@storybook/react-vite"; | ||
| import {Spinner} from "./Spinner"; | ||
|
|
||
| const meta = { | ||
| title: "components/atoms/Spinner", | ||
| component: Spinner, | ||
| } satisfies Meta<typeof Spinner>; | ||
|
|
||
| export default meta; | ||
|
|
||
| type Story = StoryObj<typeof meta>; | ||
|
|
||
| /** | ||
| * Default spinner | ||
| */ | ||
| export const Default: Story = { | ||
| args: { | ||
| size: "md", | ||
| color: "#2563eb", | ||
| }, | ||
| }; | ||
|
|
||
| /** | ||
| * Spinner sizes | ||
| */ | ||
| export const Sizes: Story = { | ||
| render: () => ( | ||
| <div style={{display: "flex", gap: 16, alignItems: "center"}}> | ||
| <Spinner size="sm" color="#2563eb"/> | ||
| <Spinner size="md" color="#2563eb"/> | ||
| <Spinner size="lg" color="#2563eb"/> | ||
| </div> | ||
| ), | ||
| }; | ||
|
|
||
| /** | ||
| * Spinner with different colors | ||
| */ | ||
| export const Colors: Story = { | ||
| render: () => ( | ||
| <div style={{display: "flex", gap: 16, alignItems: "center"}}> | ||
| <Spinner color="#ef4444"/> {/* red */} | ||
| <Spinner color="#22c55e"/> {/* green */} | ||
| <Spinner color="#f59e0b"/> {/* amber */} | ||
| <Spinner color="#0ea5e9"/> {/* sky */} | ||
| </div> | ||
| ), | ||
| }; | ||
|
|
||
| /** | ||
| * Spinner inheriting parent color (currentColor) | ||
| */ | ||
| export const InheritColor: Story = { | ||
| render: () => ( | ||
| <div style={{color: "#7c3aed"}}> | ||
| <Spinner/> | ||
| </div> | ||
| ), | ||
| }; |
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,76 @@ | ||
| import type {SpinnerProps, SpinnerSize} from "./SpinnerProps"; | ||
|
|
||
| const sizeMap: Record<SpinnerSize, number> = { | ||
| sm: 16, | ||
| md: 24, | ||
| lg: 32, | ||
| }; | ||
|
|
||
| /** | ||
| * Renders a visually appealing and accessible SVG spinner component. | ||
| * This component is typically used to indicate a loading state or ongoing process. | ||
| * | ||
| * The spinner's size and color can be customized via props. | ||
| * It includes accessibility attributes (`role="status"`, `aria-label="Loading"`) | ||
| * to ensure screen readers can convey its purpose. | ||
| * | ||
| * @component | ||
| * @param {Readonly<SpinnerProps>} props - The properties for the Spinner component. | ||
| * @param {("sm" | "md" | "lg")} [props.size="md"] - The predefined size of the spinner. | ||
| * - "sm": Small (16px) | ||
| * - "md": Medium (24px) | ||
| * - "lg": Large (32px) | ||
| * @param {string} [props.color="currentColor"] - The color of the spinner's stroke. | ||
| * Can be any valid CSS color string (e.g., "red", "#FF0000", "var(--primary-color)"). | ||
| * Defaults to "currentColor", which inherits the text color of its parent. | ||
| * @returns {JSX.Element} A React element representing the SVG spinner. | ||
| * | ||
| * @example | ||
| * // Basic usage with default size and color | ||
| * <Spinner /> | ||
| * @example | ||
| * // Large spinner with a specific color | ||
| * <Spinner size="lg" color="#007bff" /> | ||
| */ | ||
| export function Spinner(props: Readonly<SpinnerProps>) { | ||
| const { | ||
| size = "md", | ||
| color = "currentColor", | ||
| className, | ||
| } = props; | ||
|
|
||
| const pixelSize = sizeMap[size]; | ||
| const strokeWidth = Math.max(2, Math.floor(pixelSize / 6)); | ||
|
|
||
| return ( | ||
| <svg | ||
| width={pixelSize} | ||
| height={pixelSize} | ||
| viewBox="0 0 50 50" | ||
| className={className} | ||
| focusable="false" | ||
| > | ||
| <title>Loading</title> | ||
|
|
||
| <circle | ||
| cx="25" | ||
| cy="25" | ||
| r="20" | ||
| fill="none" | ||
| stroke={color} | ||
| strokeWidth={strokeWidth} | ||
| strokeLinecap="round" | ||
| strokeDasharray="31.4 31.4" | ||
| > | ||
| <animateTransform | ||
| attributeName="transform" | ||
| type="rotate" | ||
| from="0 25 25" | ||
| to="360 25 25" | ||
| dur="0.8s" | ||
| repeatCount="indefinite" | ||
| /> | ||
| </circle> | ||
| </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,7 @@ | ||
| export type SpinnerSize = "sm" | "md" | "lg"; | ||
|
|
||
| export interface SpinnerProps { | ||
| size?: SpinnerSize; | ||
| color?: string; | ||
| className?: string; | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add the advertised accessibility attributes to the SVG.
The docs state
role="status"andaria-label="Loading", but the SVG omits them, weakening accessibility and contradicting the comment.🔧 Proposed fix
<svg width={pixelSize} height={pixelSize} viewBox="0 0 50 50" className={className} + role="status" + aria-label="Loading" focusable="false" >📝 Committable suggestion
🤖 Prompt for AI Agents