Skip to content

Commit 66d4905

Browse files
committed
refactor: move avatar size styles to css
1 parent fa2fc97 commit 66d4905

6 files changed

Lines changed: 72 additions & 62 deletions

File tree

src/avatar/avatar.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,9 @@ component appearance. The values shown below are the default values.
159159

160160
### Component-owned variables
161161

162-
Avatar sets these variables at render time from the `size`, `shape`, and
163-
`name` props. They are listed for completeness, but consumers should prefer
164-
the component props instead of overriding them directly.
162+
Avatar's size classes set these variables from the `size` prop. They are
163+
listed for completeness, but consumers should prefer the component props
164+
instead of overriding them directly.
165165

166166
```css
167167
.avatar {

src/avatar/avatar.module.css

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,71 @@
5858
outline-offset: -2px;
5959
}
6060

61+
.size-80 {
62+
--reactist-avatar-size: 80px;
63+
--reactist-avatar-rounded-radius: 10px;
64+
}
65+
66+
.size-72 {
67+
--reactist-avatar-size: 72px;
68+
--reactist-avatar-rounded-radius: 10px;
69+
}
70+
71+
.size-62 {
72+
--reactist-avatar-size: 62px;
73+
--reactist-avatar-rounded-radius: 8.5px;
74+
}
75+
76+
.size-50 {
77+
--reactist-avatar-size: 50px;
78+
--reactist-avatar-rounded-radius: 7px;
79+
}
80+
81+
.size-40 {
82+
--reactist-avatar-size: 40px;
83+
--reactist-avatar-rounded-radius: 5.5px;
84+
}
85+
86+
.size-36 {
87+
--reactist-avatar-size: 36px;
88+
--reactist-avatar-rounded-radius: 5px;
89+
}
90+
91+
.size-30 {
92+
--reactist-avatar-size: 30px;
93+
--reactist-avatar-rounded-radius: 5px;
94+
}
95+
96+
.size-28 {
97+
--reactist-avatar-size: 28px;
98+
--reactist-avatar-rounded-radius: 5px;
99+
}
100+
101+
.size-24 {
102+
--reactist-avatar-size: 24px;
103+
--reactist-avatar-rounded-radius: 3.2px;
104+
}
105+
106+
.size-20 {
107+
--reactist-avatar-size: 20px;
108+
--reactist-avatar-rounded-radius: 3px;
109+
}
110+
111+
.size-18 {
112+
--reactist-avatar-size: 18px;
113+
--reactist-avatar-rounded-radius: 3px;
114+
}
115+
116+
.size-16 {
117+
--reactist-avatar-size: 16px;
118+
--reactist-avatar-rounded-radius: 2px;
119+
}
120+
121+
.size-12 {
122+
--reactist-avatar-size: 12px;
123+
--reactist-avatar-rounded-radius: 1.6px;
124+
}
125+
61126
.avatar:has(.initials) {
62127
background-color: var(--reactist-avatar-meta-fill);
63128
}

src/avatar/avatar.test.tsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ describe('Avatar', () => {
1919
render(<Avatar data-testid="avatar" size={36} name="Jane Doe" image="avatar.png" />)
2020

2121
expect(screen.getByRole('img', { name: 'Jane Doe' })).toHaveAttribute('src', 'avatar.png')
22-
expect(screen.getByTestId('avatar')).toHaveStyle({
23-
'--reactist-avatar-size': '36px',
24-
})
22+
expect(screen.getByTestId('avatar')).toHaveClass('size-36')
2523
})
2624

2725
it('does not apply meta color classes while rendering an image', () => {
@@ -223,13 +221,11 @@ describe('Avatar', () => {
223221
expect(invalidRefElement).toBeTruthy()
224222
})
225223

226-
it('supports rounded shape with size-aware radius', () => {
224+
it('supports rounded shape with size-driven CSS classes', () => {
227225
render(<Avatar data-testid="avatar" size={50} shape="rounded" name="Design" />)
228226

229227
expect(screen.getByTestId('avatar')).toHaveClass('shape-rounded')
230-
expect(screen.getByTestId('avatar')).toHaveStyle({
231-
'--reactist-avatar-rounded-radius': '7px',
232-
})
228+
expect(screen.getByTestId('avatar')).toHaveClass('size-50')
233229
})
234230

235231
it('defaults to circle shape', () => {

src/avatar/avatar.tsx

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {
1212
getInitials,
1313
getSources,
1414
normalizeAvatarName,
15-
ROUNDED_AVATAR_RADIUS_BY_SIZE,
1615
} from './utils'
1716

1817
import styles from './avatar.module.css'
@@ -21,11 +20,6 @@ import type { ObfuscatedClassName } from '../utils/common-types'
2120
import type { PolymorphicComponentProps } from '../utils/polymorphism'
2221
import type { AvatarImage, AvatarShape, AvatarSize, ImageSources } from './utils'
2322

24-
type AvatarStyle = React.CSSProperties & {
25-
'--reactist-avatar-size': string
26-
'--reactist-avatar-rounded-radius': string
27-
}
28-
2923
/**
3024
* Props for the `Avatar` component.
3125
*/
@@ -118,12 +112,12 @@ const AvatarContent = polymorphicComponent<'div', AvatarOwnProps, 'omitClassName
118112
ref={ref}
119113
className={classNames(
120114
styles.avatar,
115+
styles[`size-${size}`],
121116
styles[`shape-${shape}`],
122117
metaColorIndex !== undefined && styles[`meta-color-${metaColorIndex}`],
123118
!availableImageSources && !hasInitials && styles.empty,
124119
exceptionallySetClassName,
125120
)}
126-
style={getAvatarStyle(size)}
127121
data-testid={testId}
128122
aria-hidden={isDecorative || undefined}
129123
display="inlineFlex"
@@ -190,13 +184,6 @@ const Avatar = polymorphicComponent<'div', AvatarOwnProps, 'omitClassName'>(func
190184
)
191185
})
192186

193-
function getAvatarStyle(size: AvatarSize): AvatarStyle {
194-
return {
195-
'--reactist-avatar-size': `${size}px`,
196-
'--reactist-avatar-rounded-radius': ROUNDED_AVATAR_RADIUS_BY_SIZE[size],
197-
}
198-
}
199-
200187
function getAbsoluteImageSource(src: string, image: HTMLImageElement) {
201188
try {
202189
return new URL(src, image.ownerDocument.baseURI).href

src/avatar/utils.test.ts

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
getInitials,
77
getSources,
88
normalizeAvatarName,
9-
ROUNDED_AVATAR_RADIUS_BY_SIZE,
109
} from './utils'
1110

1211
describe('Avatar utils', () => {
@@ -216,24 +215,4 @@ describe('Avatar utils', () => {
216215
expect(index).toBeLessThan(AVATAR_META_COLOR_COUNT)
217216
})
218217
})
219-
220-
describe('ROUNDED_AVATAR_RADIUS_BY_SIZE', () => {
221-
it('contains the exclusive rounded radius mapping', () => {
222-
expect(ROUNDED_AVATAR_RADIUS_BY_SIZE).toEqual({
223-
80: '10px',
224-
72: '10px',
225-
62: '8.5px',
226-
50: '7px',
227-
40: '5.5px',
228-
36: '5px',
229-
30: '5px',
230-
28: '5px',
231-
24: '3.2px',
232-
20: '3px',
233-
18: '3px',
234-
16: '2px',
235-
12: '1.6px',
236-
})
237-
})
238-
})
239218
})

src/avatar/utils.ts

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,6 @@ type ImageSources = {
3232

3333
const AVATAR_META_COLOR_COUNT = 20
3434

35-
const ROUNDED_AVATAR_RADIUS_BY_SIZE: Record<AvatarSize, string> = {
36-
80: '10px',
37-
72: '10px',
38-
62: '8.5px',
39-
50: '7px',
40-
40: '5.5px',
41-
36: '5px',
42-
30: '5px',
43-
28: '5px',
44-
24: '3.2px',
45-
20: '3px',
46-
18: '3px',
47-
16: '2px',
48-
12: '1.6px',
49-
}
50-
5135
const WHITESPACE_REGEXP = new RegExp('\\p{White_Space}+', 'gu')
5236
const GRAPHEME_SEGMENTER =
5337
typeof Intl !== 'undefined' && 'Segmenter' in Intl
@@ -179,6 +163,5 @@ export {
179163
getInitials,
180164
getSources,
181165
normalizeAvatarName,
182-
ROUNDED_AVATAR_RADIUS_BY_SIZE,
183166
}
184167
export type { AvatarImage, AvatarImageSource, AvatarShape, AvatarSize, ImageSources }

0 commit comments

Comments
 (0)