Skip to content

Commit 134dacd

Browse files
fix: avatar: tooltip accessibility fix (#949)
* fix: avatar: tooltip accessibility fix * fix: avatar: added aria attributes as props * fix: avatar: added props to list story avatar group * test: update: updated test cases --------- Co-authored-by: ypatadia-eightfold <[email protected]>
1 parent 920ad63 commit 134dacd

File tree

5 files changed

+57
-1
lines changed

5 files changed

+57
-1
lines changed

src/components/Avatar/Avatar.tsx

+20
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,11 @@ const AvatarFallback: FC<AvatarFallbackProps> = React.forwardRef(
257257
style,
258258
theme,
259259
tabIndex = 0,
260+
id = 'avatar-id',
261+
ariaLabel,
262+
roleSet,
263+
ariaDescribedBy,
264+
ariaOwns,
260265
},
261266
ref: Ref<HTMLDivElement>
262267
) => {
@@ -290,6 +295,11 @@ const AvatarFallback: FC<AvatarFallbackProps> = React.forwardRef(
290295

291296
return (
292297
<div
298+
id={id}
299+
aria-label={ariaLabel}
300+
aria-owns={ariaOwns}
301+
aria-describedby={ariaDescribedBy}
302+
role={roleSet}
293303
ref={ref}
294304
className={avatarClasses}
295305
onClick={onClick}
@@ -369,6 +379,11 @@ export const Avatar: FC<AvatarProps> = React.forwardRef(
369379
tooltipProps = undefined,
370380
type = 'square',
371381
tabIndex = 0,
382+
id = 'avatar-id',
383+
ariaLabel,
384+
roleSet,
385+
ariaDescribedBy,
386+
ariaOwns,
372387
},
373388
ref: Ref<HTMLDivElement>
374389
) => {
@@ -582,6 +597,11 @@ export const Avatar: FC<AvatarProps> = React.forwardRef(
582597
)}
583598
>
584599
<AvatarFallback
600+
id={id}
601+
ariaLabel={ariaLabel}
602+
ariaOwns={ariaOwns}
603+
ariaDescribedBy={ariaDescribedBy}
604+
roleSet={roleSet}
585605
classNames={wrapperClassNames}
586606
hashingFunction={hashingFunction}
587607
onClick={onClick}

src/components/Avatar/Avatar.types.ts

+11
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ export interface BaseAvatarProps extends OcBaseProps<HTMLDivElement> {
160160
* @default 'square'
161161
*/
162162
type?: 'round' | 'square';
163+
ariaLabel?: string;
164+
roleSet?: string;
165+
ariaDescribedBy?: string;
166+
ariaOwns?: string;
163167
}
164168

165169
export interface AvatarPopupProps extends PopupProps {}
@@ -188,6 +192,10 @@ export interface AvatarProps
188192
* Hover tooltip
189193
*/
190194
tooltipProps?: TooltipProps;
195+
ariaLabel?: string;
196+
roleSet?: string;
197+
ariaDescribedBy?: string;
198+
ariaOwns?: string;
191199
}
192200

193201
interface MaxAvatarProps extends BaseAvatarProps {
@@ -267,4 +275,7 @@ export interface AvatarGroupProps extends OcBaseProps<HTMLDivElement> {
267275
* @default 'square'
268276
*/
269277
type?: 'round' | 'square';
278+
ariaLabel?: string;
279+
roleSet?: string;
280+
tooltipId?: string;
270281
}

src/components/Avatar/AvatarGroup.stories.tsx

+6
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ const sampleList: User[] = [
7272

7373
const Basic_Story: ComponentStory<typeof AvatarGroup> = (args) => (
7474
<AvatarGroup
75+
tooltipId="avatar-group-tooltip-id"
76+
ariaLabel="interviewers"
77+
roleSet="button"
7578
animateOnHover
7679
maxProps={{
7780
count: 4,
@@ -200,6 +203,9 @@ const Basic_Story: ComponentStory<typeof AvatarGroup> = (args) => (
200203

201204
const List_Story: ComponentStory<typeof AvatarGroup> = (args) => (
202205
<AvatarGroup
206+
tooltipId="avatar-group-tooltip-id"
207+
ariaLabel="interviewers"
208+
roleSet="button"
203209
animateOnHover
204210
avatarListProps={{
205211
items: sampleList,

src/components/Avatar/AvatarGroup.tsx

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use client';
22

3-
import React, { Ref } from 'react';
3+
import React, { Ref, useEffect } from 'react';
44
import { Avatar } from './Avatar';
55
import { AvatarGroupProps, AvatarGroupVariant } from './Avatar.types';
66
import { List } from '../List';
@@ -27,6 +27,9 @@ export const AvatarGroup: React.FC<AvatarGroupProps> = React.forwardRef(
2727
size,
2828
style,
2929
type,
30+
ariaLabel,
31+
roleSet,
32+
tooltipId,
3033
...rest
3134
},
3235
ref: Ref<HTMLDivElement>
@@ -69,6 +72,11 @@ export const AvatarGroup: React.FC<AvatarGroupProps> = React.forwardRef(
6972
string | React.JSXElementConstructor<any>
7073
> = (
7174
<Avatar
75+
ariaLabel={`${numChildren - maxCount} more ${ariaLabel}`}
76+
ariaOwns={tooltipId}
77+
ariaDescribedBy={tooltipId}
78+
roleSet={roleSet}
79+
id="avatar-group-max-count"
7280
size={size}
7381
type={type}
7482
fontSize={styles.maxCountFontSize}
@@ -96,6 +104,8 @@ export const AvatarGroup: React.FC<AvatarGroupProps> = React.forwardRef(
96104
>
97105
): JSX.Element => (
98106
<Tooltip
107+
tabIndex={-1}
108+
id={tooltipId}
99109
{...maxProps.tooltipProps}
100110
classNames={mergeClasses([
101111
styles.avatarGroupTooltip,

src/components/Avatar/__snapshots__/Avatar.test.tsx.snap

+9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ LoadedCheerio {
55
"0": Node {
66
"attribs": Object {
77
"class": "wrapper-style avatar image-style",
8+
"id": "avatar-id",
89
"style": "width: 40px; height: 40px; min-width: 40px; min-height: 40px; font-size: 18px;",
910
"tabindex": "0",
1011
},
@@ -34,11 +35,13 @@ LoadedCheerio {
3435
"type": "tag",
3536
"x-attribsNamespace": Object {
3637
"class": undefined,
38+
"id": undefined,
3739
"style": undefined,
3840
"tabindex": undefined,
3941
},
4042
"x-attribsPrefix": Object {
4143
"class": undefined,
44+
"id": undefined,
4245
"style": undefined,
4346
"tabindex": undefined,
4447
},
@@ -133,6 +136,7 @@ LoadedCheerio {
133136
"0": Node {
134137
"attribs": Object {
135138
"class": "wrapper-style avatar image-style round",
139+
"id": "avatar-id",
136140
"style": "width: 32px; height: 32px; min-width: 32px; min-height: 32px; font-size: 18px;",
137141
"tabindex": "0",
138142
},
@@ -162,11 +166,13 @@ LoadedCheerio {
162166
"type": "tag",
163167
"x-attribsNamespace": Object {
164168
"class": undefined,
169+
"id": undefined,
165170
"style": undefined,
166171
"tabindex": undefined,
167172
},
168173
"x-attribsPrefix": Object {
169174
"class": undefined,
175+
"id": undefined,
170176
"style": undefined,
171177
"tabindex": undefined,
172178
},
@@ -261,6 +267,7 @@ LoadedCheerio {
261267
"0": Node {
262268
"attribs": Object {
263269
"class": "wrapper-style avatar image-style",
270+
"id": "avatar-id",
264271
"style": "width: 32px; height: 32px; min-width: 32px; min-height: 32px; font-size: 18px;",
265272
"tabindex": "0",
266273
},
@@ -290,11 +297,13 @@ LoadedCheerio {
290297
"type": "tag",
291298
"x-attribsNamespace": Object {
292299
"class": undefined,
300+
"id": undefined,
293301
"style": undefined,
294302
"tabindex": undefined,
295303
},
296304
"x-attribsPrefix": Object {
297305
"class": undefined,
306+
"id": undefined,
298307
"style": undefined,
299308
"tabindex": undefined,
300309
},

0 commit comments

Comments
 (0)