Skip to content

Commit 421ebf0

Browse files
add data-component attributes for Avatar, AvatarStack, Banner, & BaseStyles (#7734)
Co-authored-by: Marie Lucca <40550942+francinelucca@users.noreply.github.com>
1 parent b288d6e commit 421ebf0

10 files changed

Lines changed: 97 additions & 48 deletions

File tree

.changeset/shaggy-houses-laugh.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@primer/react": patch
3+
---
4+
5+
add data-component attributes for Avatar, AvatarStack, Banner, & BaseStyles

packages/react/src/Avatar/Avatar.test.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ import classes from './Avatar.module.css'
77
describe('Avatar', () => {
88
implementsClassName(Avatar, classes.Avatar)
99

10+
describe('Avatar data-component attribute', () => {
11+
it('renders Avatar with data-component attribute', () => {
12+
render(<Avatar src="primer.png" alt="" data-testid="avatar" />)
13+
const avatar = screen.getByTestId('avatar')
14+
expect(avatar).toHaveAttribute('data-component', 'Avatar')
15+
})
16+
})
17+
1018
it('renders small by default', () => {
1119
const size = 20
1220
render(<Avatar src="primer.png" data-testid="avatar" />)

packages/react/src/AvatarStack/AvatarStack.test.tsx

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,24 @@ const rightAvatarComp = (
2525
describe('AvatarStack', () => {
2626
implementsClassName(AvatarStack, classes.AvatarStack)
2727

28+
describe('AvatarStack data-component attributes', () => {
29+
it('renders AvatarStack with data-component attribute', () => {
30+
const {container} = render(avatarComp)
31+
const root = container.querySelector('[data-component="AvatarStack"]')
32+
expect(root).toBeInTheDocument()
33+
})
34+
35+
it('renders AvatarStack.Body with data-component attribute', () => {
36+
const {container} = render(avatarComp)
37+
const body = container.querySelector('[data-component="AvatarStack.Body"]')
38+
expect(body).toBeInTheDocument()
39+
})
40+
})
41+
2842
it('respects alignRight props', () => {
2943
const {container} = render(rightAvatarComp)
30-
expect(container.firstChild).toMatchSnapshot()
44+
const root = container.querySelector('[data-component="AvatarStack"]')
45+
expect(root).toHaveAttribute('data-align-right', '')
3146
})
3247

3348
it('should have a tabindex of 0 if there are no interactive children', () => {

packages/react/src/AvatarStack/AvatarStack.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const AvatarStackBody = ({
4242
} & React.ComponentPropsWithoutRef<'div'>) => {
4343
return (
4444
<div
45+
data-component="AvatarStack.Body"
4546
data-disable-expand={disableExpand ? '' : undefined}
4647
className={clsx(
4748
{
@@ -158,6 +159,7 @@ const AvatarStack = ({
158159

159160
return (
160161
<span
162+
data-component="AvatarStack"
161163
data-variant={variant}
162164
data-shape={shape}
163165
data-avatar-count={count > 3 ? '3+' : count}

packages/react/src/AvatarStack/__snapshots__/AvatarStack.test.tsx.snap

Lines changed: 0 additions & 40 deletions
This file was deleted.

packages/react/src/Banner/Banner.test.tsx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,36 @@ describe('Banner', () => {
1414
expect(screen.getByRole('heading', {name: 'test'})).toBeInTheDocument()
1515
})
1616

17+
it('renders data-component attributes', () => {
18+
const {container} = render(
19+
<Banner
20+
title="test"
21+
description="test-description"
22+
primaryAction={<Banner.PrimaryAction>test primary action</Banner.PrimaryAction>}
23+
secondaryAction={<Banner.SecondaryAction>test secondary action</Banner.SecondaryAction>}
24+
onDismiss={() => {}}
25+
/>,
26+
)
27+
28+
expect(container.querySelector('[data-component="Banner"]')).toBeInTheDocument()
29+
expect(container.querySelector('[data-component="Banner.Icon"]')).toBeInTheDocument()
30+
expect(container.querySelector('[data-component="Banner.Content"]')).toBeInTheDocument()
31+
expect(container.querySelector('[data-component="Banner.Actions"]')).toBeInTheDocument()
32+
33+
expect(container.querySelector('[data-component="Banner.PrimaryAction"]')).toBeInTheDocument()
34+
expect(container.querySelector('[data-component="Banner.SecondaryAction"]')).toBeInTheDocument()
35+
36+
expect(container.querySelector('[data-component="Banner.Title"]')).toBeInTheDocument()
37+
expect(container.querySelector('[data-component="Banner.Description"]')).toBeInTheDocument()
38+
})
39+
40+
it('renders Banner dismiss IconButton with data-component attribute', () => {
41+
const {container} = render(<Banner title="test" onDismiss={() => {}} />)
42+
43+
const dismissButton = container.querySelector('[data-component="Banner"] [data-component="IconButton"]')
44+
expect(dismissButton).toBeInTheDocument()
45+
})
46+
1747
it('should label the landmark element with the title by default', () => {
1848
render(<Banner title="My Banner Title" />)
1949
const region = screen.getByRole('region', {name: 'My Banner Title'})

packages/react/src/Banner/Banner.tsx

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ export const Banner = React.forwardRef<HTMLElement, BannerProps>(function Banner
157157
return (
158158
<BannerContext.Provider value={{titleId}}>
159159
<section
160+
data-component="Banner"
160161
{...rest}
161162
aria-labelledby={labelledBy ?? (label ? undefined : titleId)}
162163
aria-label={labelledBy ? undefined : label}
@@ -171,9 +172,11 @@ export const Banner = React.forwardRef<HTMLElement, BannerProps>(function Banner
171172
data-layout={rest.layout || 'default'}
172173
data-flush={flush ? '' : undefined}
173174
>
174-
<div className={classes.BannerIcon}>{visual && supportsCustomIcon ? visual : iconForVariant[variant]}</div>
175+
<div data-component="Banner.Icon" className={classes.BannerIcon}>
176+
{visual && supportsCustomIcon ? visual : iconForVariant[variant]}
177+
</div>
175178
<div className={classes.BannerContainer}>
176-
<div className={classes.BannerContent}>
179+
<div data-component="Banner.Content" className={classes.BannerContent}>
177180
{title ? (
178181
hideTitle ? (
179182
<VisuallyHidden>
@@ -215,7 +218,13 @@ export function BannerTitle<As extends HeadingElement>(props: BannerTitleProps<A
215218
const titleId = id ?? context?.titleId
216219

217220
return (
218-
<Heading {...rest} id={titleId} className={clsx(className, classes.BannerTitle)} data-banner-title="">
221+
<Heading
222+
{...rest}
223+
id={titleId}
224+
className={clsx(className, classes.BannerTitle)}
225+
data-component="Banner.Title"
226+
data-banner-title=""
227+
>
219228
{children}
220229
</Heading>
221230
)
@@ -225,7 +234,7 @@ export type BannerDescriptionProps = React.ComponentPropsWithoutRef<'div'>
225234

226235
export function BannerDescription({children, className, ...rest}: BannerDescriptionProps) {
227236
return (
228-
<div {...rest} className={clsx('BannerDescription', className)}>
237+
<div {...rest} className={clsx('BannerDescription', className)} data-component="Banner.Description">
229238
{children}
230239
</div>
231240
)
@@ -238,7 +247,7 @@ export type BannerActionsProps = {
238247

239248
export function BannerActions({primaryAction, secondaryAction}: BannerActionsProps) {
240249
return (
241-
<div className={classes.BannerActions}>
250+
<div className={classes.BannerActions} data-component="Banner.Actions">
242251
<div className={classes.BannerActionsContainer} data-primary-action="trailing">
243252
{secondaryAction ?? null}
244253
{primaryAction ?? null}
@@ -255,7 +264,13 @@ export type BannerPrimaryActionProps = Omit<ButtonProps, 'variant'>
255264

256265
const BannerPrimaryAction = forwardRef(({children, className, ...rest}, forwardedRef) => {
257266
return (
258-
<Button ref={forwardedRef} className={clsx('BannerPrimaryAction', className)} variant="default" {...rest}>
267+
<Button
268+
data-component="Banner.PrimaryAction"
269+
ref={forwardedRef}
270+
className={clsx('BannerPrimaryAction', className)}
271+
variant="default"
272+
{...rest}
273+
>
259274
{children}
260275
</Button>
261276
)
@@ -267,7 +282,13 @@ export type BannerSecondaryActionProps = Omit<ButtonProps, 'variant'>
267282

268283
const BannerSecondaryAction = forwardRef(({children, className, ...rest}, forwardedRef) => {
269284
return (
270-
<Button ref={forwardedRef} className={clsx('BannerPrimaryAction', className)} variant="invisible" {...rest}>
285+
<Button
286+
data-component="Banner.SecondaryAction"
287+
ref={forwardedRef}
288+
className={clsx('BannerPrimaryAction', className)}
289+
variant="invisible"
290+
{...rest}
291+
>
271292
{children}
272293
</Button>
273294
)

packages/react/src/BaseStyles.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ function BaseStyles({children, color, className, as: Component = 'div', style, .
2121

2222
return (
2323
<Component
24+
data-component="BaseStyles"
2425
className={newClassName}
2526
data-portal-root
2627
style={{

packages/react/src/__tests__/BaseStyles.test.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ import {implementsClassName} from '../utils/testing'
66

77
describe('BaseStyles', () => {
88
implementsClassName(BaseStyles, classes.BaseStyles)
9+
10+
it('renders BaseStyles with data-component attribute', () => {
11+
const {container} = render(<BaseStyles>Hello</BaseStyles>)
12+
expect(container.firstElementChild).toHaveAttribute('data-component', 'BaseStyles')
13+
})
14+
915
it('has default styles', () => {
1016
const {container} = render(<BaseStyles>Hello</BaseStyles>)
1117
expect(container).toMatchSnapshot()

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ exports[`BaseStyles > has default styles 1`] = `
44
<div>
55
<div
66
class="prc-src-BaseStyles-9LBd2"
7+
data-component="BaseStyles"
78
data-portal-root="true"
89
>
910
Hello

0 commit comments

Comments
 (0)