Skip to content
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

Add alt option to Spinner #4608

Closed
wants to merge 1 commit into from
Closed
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
8 changes: 7 additions & 1 deletion packages/react/src/Spinner/Spinner.docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@
"name": "size",
"type": "'small' | 'medium' | 'large'",
"description": "Sets the width and height of the spinner."
},
{
"name": "alt",
"type": "string",
"description": "Sets the alt text for the spinner image. For no alt, set to '', defaults to 'Loading...'",
"defaultValue": "Loading..."
}
],
"subcomponents": []
}
}
4 changes: 4 additions & 0 deletions packages/react/src/Spinner/Spinner.features.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ export default {
export const Small = () => <Spinner size="small" />

export const Large = () => <Spinner size="large" />

export const CustomAlt = () => <Spinner alt="Non-default loading message..." />

export const NoAlt = () => <Spinner alt="" />
3 changes: 3 additions & 0 deletions packages/react/src/Spinner/Spinner.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,7 @@
},
options: ['small', 'medium', 'large'],
},
alt: {
type: 'string',
}

Check failure on line 27 in packages/react/src/Spinner/Spinner.stories.tsx

View workflow job for this annotation

GitHub Actions / lint

Insert `,`
}
17 changes: 14 additions & 3 deletions packages/react/src/Spinner/Spinner.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react'
import React, { useId } from 'react'

Check failure on line 1 in packages/react/src/Spinner/Spinner.tsx

View workflow job for this annotation

GitHub Actions / lint

Replace `·useId·` with `useId`
import styled from 'styled-components'
import type {SxProp} from '../sx'
import sx from '../sx'
Expand All @@ -13,13 +13,24 @@
export interface SpinnerInternalProps {
/** Sets the width and height of the spinner. */
size?: keyof typeof sizeMap
alt?: string
}

function Spinner({size: sizeKey = 'medium', ...props}: SpinnerInternalProps) {
function Spinner({size: sizeKey = 'medium', alt = 'Loading...', ...props}: SpinnerInternalProps) {
const size = sizeMap[sizeKey]

let title: React.JSX.Element | null = null
let titleId: string | undefined

if (alt && alt.trim() !== "") {

Check failure on line 25 in packages/react/src/Spinner/Spinner.tsx

View workflow job for this annotation

GitHub Actions / lint

Replace `""` with `''`
// Get an id for the title
titleId = useId()

Check failure on line 27 in packages/react/src/Spinner/Spinner.tsx

View workflow job for this annotation

GitHub Actions / lint

React Hook "useId" is called conditionally. React Hooks must be called in the exact same order in every component render. Did you accidentally call a React Hook after an early return?
title = <title id={titleId}>{alt}</title>
}

return (
<svg height={size} width={size} viewBox="0 0 16 16" fill="none" {...props}>
<svg height={size} width={size} aria-labelledby={titleId} viewBox="0 0 16 16" fill="none" {...props}>
{title}
<circle
cx="8"
cy="8"
Expand Down
Loading