Skip to content

Commit 573ae51

Browse files
randall-krauskopfhussam-i-amjoshblackjonrohan
authored
feat(Spinner): Convert Spinner to CSS module behind feature flag (#5188)
* Convert spinner component to CSS modules Co-authored-by: Hussam Ghazzi <hussam-i-am@github.com> Co-authored-by: Josh Black <joshblack@github.com> Co-authored-by: Jon Rohan <yes@jonrohan.codes> * add changeset * update snapshots * patch -> minor --------- Co-authored-by: Hussam Ghazzi <hussam-i-am@github.com> Co-authored-by: Josh Black <joshblack@github.com> Co-authored-by: Jon Rohan <yes@jonrohan.codes>
1 parent 461ae57 commit 573ae51

File tree

7 files changed

+625
-754
lines changed

7 files changed

+625
-754
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@primer/react': minor
3+
---
4+
5+
Migrated `Spinner` component to use support CSS modules
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import React from 'react'
2+
import type {Meta} from '@storybook/react'
3+
import Spinner from './Spinner'
4+
5+
export default {
6+
title: 'Components/Spinner/Dev',
7+
component: Spinner,
8+
} as Meta<typeof Spinner>
9+
10+
export const Default = () => <Spinner sx={{border: '1px solid red'}} />
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
.Box {
2+
display: inline-flex;
3+
}
4+
5+
@keyframes rotate-keyframes {
6+
100% {
7+
transform: rotate(360deg);
8+
}
9+
}
10+
11+
.SpinnerAnimation {
12+
animation: rotate-keyframes 1s linear infinite;
13+
}

packages/react/src/Spinner/Spinner.tsx

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ import styled from 'styled-components'
33
import sx, {type SxProp} from '../sx'
44
import {VisuallyHidden} from '../VisuallyHidden'
55
import type {HTMLDataAttributes} from '../internal/internal-types'
6-
import Box from '../Box'
76
import {useId} from '../hooks'
7+
import {useFeatureFlag} from '../FeatureFlags'
8+
import classes from './Spinner.module.css'
9+
import Box from '../Box'
810

911
const sizeMap = {
1012
small: '16px',
@@ -20,6 +22,7 @@ export type SpinnerProps = {
2022
/** @deprecated Use `srText` instead. */
2123
'aria-label'?: string
2224
className?: string
25+
style?: React.CSSProperties
2326
} & HTMLDataAttributes &
2427
SxProp
2528

@@ -28,6 +31,7 @@ function Spinner({
2831
srText = 'Loading',
2932
'aria-label': ariaLabel,
3033
className,
34+
style,
3135
...props
3236
}: SpinnerProps) {
3337
const size = sizeMap[sizeKey]
@@ -36,7 +40,7 @@ function Spinner({
3640

3741
return (
3842
/* inline-flex removes the extra line height */
39-
<Box as="span" sx={{display: 'inline-flex'}}>
43+
<span className={classes.Box}>
4044
<svg
4145
height={size}
4246
width={size}
@@ -46,6 +50,7 @@ function Spinner({
4650
aria-label={ariaLabel ?? undefined}
4751
aria-labelledby={hasHiddenLabel ? labelId : undefined}
4852
className={className}
53+
style={style}
4954
{...props}
5055
>
5156
<circle
@@ -66,11 +71,11 @@ function Spinner({
6671
/>
6772
</svg>
6873
{hasHiddenLabel ? <VisuallyHidden id={labelId}>{srText}</VisuallyHidden> : null}
69-
</Box>
74+
</span>
7075
)
7176
}
7277

73-
const StyledSpinner = styled(Spinner)`
78+
const StyledComponentSpinner = styled(Spinner)`
7479
@keyframes rotate-keyframes {
7580
100% {
7681
transform: rotate(360deg);
@@ -82,6 +87,19 @@ const StyledSpinner = styled(Spinner)`
8287
${sx}
8388
`
8489

90+
function StyledSpinner({sx, ...props}: SpinnerProps) {
91+
const enabled = useFeatureFlag('primer_react_css_modules_team')
92+
if (enabled) {
93+
if (sx) {
94+
return <Box sx={sx} as={Spinner} className={classes.SpinnerAnimation} {...props} />
95+
}
96+
97+
return <Spinner className={classes.SpinnerAnimation} {...props} />
98+
}
99+
100+
return <StyledComponentSpinner sx={sx} {...props} />
101+
}
102+
85103
StyledSpinner.displayName = 'Spinner'
86104

87105
export default StyledSpinner

packages/react/src/__tests__/__snapshots__/Autocomplete.test.tsx.snap

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -327,14 +327,7 @@ exports[`snapshots renders a loading state 1`] = `
327327
justify-content: center;
328328
}
329329
330-
.c2 {
331-
display: -webkit-inline-box;
332-
display: -webkit-inline-flex;
333-
display: -ms-inline-flexbox;
334-
display: inline-flex;
335-
}
336-
337-
.c4:not(:focus):not(:active):not(:focus-within) {
330+
.c3:not(:focus):not(:active):not(:focus-within) {
338331
-webkit-clip-path: inset(50%);
339332
clip-path: inset(50%);
340333
height: 1px;
@@ -344,7 +337,7 @@ exports[`snapshots renders a loading state 1`] = `
344337
width: 1px;
345338
}
346339
347-
.c3 {
340+
.c2 {
348341
-webkit-animation: rotate-keyframes 1s linear infinite;
349342
animation: rotate-keyframes 1s linear infinite;
350343
}
@@ -374,12 +367,12 @@ exports[`snapshots renders a loading state 1`] = `
374367
display="flex"
375368
>
376369
<span
377-
className="c2"
370+
className="Box"
378371
>
379372
<svg
380373
aria-hidden={true}
381374
aria-labelledby=":r2h:"
382-
className="c3"
375+
className="c2"
383376
fill="none"
384377
height="32px"
385378
viewBox="0 0 16 16"
@@ -403,7 +396,7 @@ exports[`snapshots renders a loading state 1`] = `
403396
/>
404397
</svg>
405398
<span
406-
className="c4"
399+
className="c3"
407400
id=":r2h:"
408401
>
409402
Loading

0 commit comments

Comments
 (0)