-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Promote spinner to standalone component (#1476)
* Simplify markup and use colours from Source palette * Reduce circle radius to avoid clipping * Update spinner overrides in button component * Update svg attributes to match other icons * Add theming support to spinner * Use `theme` prop in preference to style overrides * Replace negative stroke dash array value with multiple Using a negative value causes the animation to jump in Safari rather than animating continuously. * Promote spinner to standalone component * Add docs to story * Allow custom sizes * Add examples of size options and theming * Convert attributes to JSX * Simplify theme naming and remove fill colours * Add changeset * Update text for change log * Update import order
- Loading branch information
1 parent
b23249e
commit a275431
Showing
11 changed files
with
192 additions
and
72 deletions.
There are no files selected for viewing
This file contains 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,13 @@ | ||
--- | ||
'@guardian/source': major | ||
--- | ||
|
||
Removes `SvgSpinner` from icon library and replaces with dedicated `Spinner` component. The `size` prop supports the existing set of named icon sizes for backwards compatibility, but also allows setting a custom size in pixels. The default colour scheme can be overridden with the `theme` prop. | ||
|
||
```tsx | ||
<> | ||
<Spinner size="small" /> | ||
<Spinner size={40} /> | ||
<Spinner theme={{ background: 'transparent', color: 'currentColor' }} /> | ||
</> | ||
``` |
This file contains 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 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 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 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
54 changes: 0 additions & 54 deletions
54
libs/@guardian/source/src/react-components/icons/SvgSpinner.tsx
This file was deleted.
Oops, something went wrong.
This file contains 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 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
65 changes: 65 additions & 0 deletions
65
libs/@guardian/source/src/react-components/spinner/Spinner.stories.tsx
This file contains 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,65 @@ | ||
import type { Meta, StoryFn } from '@storybook/react'; | ||
import { palette } from '../../foundations'; | ||
import type { SpinnerProps } from './Spinner'; | ||
import { Spinner } from './Spinner'; | ||
|
||
const meta: Meta<typeof Spinner> = { | ||
title: 'React Components/Spinner', | ||
component: Spinner, | ||
argTypes: { | ||
size: { | ||
options: ['xsmall', 'small', 'medium'], | ||
control: { type: 'select' }, | ||
}, | ||
}, | ||
}; | ||
|
||
export default meta; | ||
|
||
const Template: StoryFn<typeof Spinner> = (args: SpinnerProps) => ( | ||
<Spinner {...args} /> | ||
); | ||
|
||
// ***************************************************************************** | ||
|
||
export const XSmallSizeDefaultTheme: StoryFn<typeof Spinner> = Template.bind( | ||
{}, | ||
); | ||
XSmallSizeDefaultTheme.args = { | ||
size: 'xsmall', | ||
}; | ||
|
||
// ***************************************************************************** | ||
|
||
export const SmallSizeDefaultTheme: StoryFn<typeof Spinner> = Template.bind({}); | ||
SmallSizeDefaultTheme.args = { | ||
size: 'small', | ||
}; | ||
|
||
// ***************************************************************************** | ||
|
||
export const MediumSizeDefaultTheme: StoryFn<typeof Spinner> = Template.bind( | ||
{}, | ||
); | ||
MediumSizeDefaultTheme.args = { | ||
size: 'medium', | ||
}; | ||
|
||
// ***************************************************************************** | ||
|
||
export const CustomSizeDefaultTheme: StoryFn<typeof Spinner> = Template.bind( | ||
{}, | ||
); | ||
CustomSizeDefaultTheme.args = { | ||
size: 40, | ||
}; | ||
|
||
// ***************************************************************************** | ||
|
||
export const CustomTheme: StoryFn<typeof Spinner> = Template.bind({}); | ||
CustomTheme.args = { | ||
theme: { | ||
background: 'transparent', | ||
color: palette.neutral[7], | ||
}, | ||
}; |
86 changes: 86 additions & 0 deletions
86
libs/@guardian/source/src/react-components/spinner/Spinner.tsx
This file contains 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,86 @@ | ||
import { iconSize } from '../../foundations'; | ||
import type { IconSize } from '../@types/Icons'; | ||
import type { ThemeSpinner } from './theme'; | ||
import { themeSpinner } from './theme'; | ||
|
||
export interface SpinnerProps { | ||
/** | ||
* Size of the spinner | ||
*/ | ||
size?: IconSize | number; | ||
/** | ||
* Partial or complete theme to override the spinner's default colour palette. | ||
* The colours which can be changed are: | ||
* | ||
* `background`<br> | ||
* `color`<br> | ||
*/ | ||
theme?: Partial<ThemeSpinner>; | ||
} | ||
|
||
/** | ||
* [Storybook](https://guardian.github.io/storybooks/?path=/story/source_react-components-spinner--docs) • | ||
* [GitHub](https://github.com/guardian/csnx/tree/main/libs/@guardian/source/src/react-components/spinner/Spinner.tsx) • | ||
* [NPM](https://www.npmjs.com/package/@guardian/source) | ||
* | ||
* A spinner conveys to the user that a process is ongoing. ie. a page is | ||
* loading or an action is being processed. The spinner is purely visual and | ||
* does not include any accessibility features. It is the responsibility of the | ||
* consumer to ensure that the spinner is used in a way that is accessible by | ||
* adding an appropriate label (either visually or via `aria-label`) and | ||
* applying `aria-live` to the containing element if the user needs to be | ||
* informed of changes to the spinner's state. | ||
*/ | ||
export const Spinner = ({ size = 'medium', theme }: SpinnerProps) => { | ||
const mergedTheme = { ...themeSpinner, ...theme }; | ||
const spinnerWidth = typeof size === 'number' ? size : iconSize[size]; | ||
|
||
return ( | ||
<svg | ||
width={spinnerWidth} | ||
viewBox="0 0 30 30" | ||
focusable={false} | ||
aria-hidden={true} | ||
> | ||
<g> | ||
<animateTransform | ||
attributeName="transform" | ||
attributeType="XML" | ||
type="rotate" | ||
from="0 15 15" | ||
to="360 15 15" | ||
dur="2.5s" | ||
repeatCount="indefinite" | ||
/> | ||
<circle | ||
cx={15} | ||
cy={15} | ||
r={12.5} | ||
strokeWidth={5} | ||
stroke={mergedTheme.background} | ||
fill="transparent" | ||
/> | ||
<circle | ||
cx={15} | ||
cy={15} | ||
r={12.5} | ||
strokeWidth={5} | ||
strokeDasharray={82} | ||
strokeDashoffset={82} | ||
stroke={mergedTheme.color} | ||
fill="transparent" | ||
> | ||
<animate | ||
attributeName="stroke-dashoffset" | ||
dur="3.5s" | ||
from={ | ||
164 | ||
} /* Multiple of `stroke-dasharray` so animation is continuous */ | ||
to={0} | ||
repeatCount="indefinite" | ||
/> | ||
</circle> | ||
</g> | ||
</svg> | ||
); | ||
}; |
11 changes: 11 additions & 0 deletions
11
libs/@guardian/source/src/react-components/spinner/theme.ts
This file contains 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,11 @@ | ||
import { palette } from '../../foundations'; | ||
|
||
export type ThemeSpinner = { | ||
background: string; | ||
color: string; | ||
}; | ||
|
||
export const themeSpinner: ThemeSpinner = { | ||
background: palette.brand[800], | ||
color: palette.brand[400], | ||
}; |