Skip to content

Commit 79b9395

Browse files
authored
fix(octicons_react): remove role if aria-hidden (#1007)
* chore(project): set yarn version * fix(octicons_react): remove role if aria-hidden * chore: add changeset * test: update snapshots
1 parent dc333cc commit 79b9395

File tree

6 files changed

+34
-7
lines changed

6 files changed

+34
-7
lines changed

.changeset/spicy-actors-act.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@primer/octicons-react': patch
3+
---
4+
5+
Update octicons in React to no longer set role="img" if the icon is aria-hidden

lib/octicons_react/__tests__/tree-shaking.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,5 @@ test('tree shaking single export', async () => {
5050
})
5151

5252
const bundleSize = Buffer.byteLength(output[0].code.trim()) / 1000
53-
expect(`${bundleSize}kB`).toMatchInlineSnapshot(`"3.47kB"`)
53+
expect(`${bundleSize}kB`).toMatchInlineSnapshot(`"3.563kB"`)
5454
})

lib/octicons_react/src/__tests__/__snapshots__/octicon.js.snap

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ exports[`An icon component matches snapshot 1`] = `
77
fill="currentColor"
88
focusable="false"
99
height="16"
10-
role="img"
1110
style="display: inline-block; user-select: none; vertical-align: text-bottom; overflow: visible;"
1211
viewBox="0 0 16 16"
1312
width="16"

lib/octicons_react/src/__tests__/octicon.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import '@testing-library/jest-dom'
2-
import {render} from '@testing-library/react'
2+
import {render, screen} from '@testing-library/react'
33
import React from 'react'
44
import {AlertIcon} from '../index'
55

@@ -9,9 +9,29 @@ describe('An icon component', () => {
99
expect(container.querySelector('svg')).toMatchSnapshot()
1010
})
1111

12+
it('defaults to `aria-hidden="true"` if no label is present', () => {
13+
const {container} = render(<AlertIcon />)
14+
expect(container.querySelector('svg')).toHaveAttribute('aria-hidden', 'true')
15+
})
16+
17+
it('sets `role="img"` if `aria-label` is provided', () => {
18+
render(<AlertIcon aria-label="Alert" />)
19+
expect(screen.getByLabelText('Alert')).toHaveAttribute('role', 'img')
20+
})
21+
22+
it('sets `role="img"` if `aria-labelledby` is provided', () => {
23+
render(
24+
<>
25+
<span id="label">Alert</span>
26+
<AlertIcon aria-labelledby="label" />
27+
</>
28+
)
29+
expect(screen.getByLabelText('Alert')).toHaveAttribute('role', 'img')
30+
})
31+
1232
it('sets aria-hidden="false" if ariaLabel prop is present', () => {
1333
const {container} = render(<AlertIcon aria-label="icon" />)
14-
expect(container.querySelector('svg')).toHaveAttribute('aria-hidden', 'false')
34+
expect(container.querySelector('svg')).not.toHaveAttribute('aria-hidden')
1535
expect(container.querySelector('svg')).toHaveAttribute('aria-label', 'icon')
1636
})
1737

lib/octicons_react/src/createIconComponent.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,19 @@ export function createIconComponent(name, defaultClassName, getSVGData) {
3030
const naturalWidth = svgDataByHeight[naturalHeight].width
3131
const width = height * (naturalWidth / naturalHeight)
3232
const path = svgDataByHeight[naturalHeight].path
33+
const labelled = ariaLabel || arialabelledby
34+
const role = labelled ? 'img' : undefined
3335

3436
return (
3537
<svg
3638
ref={forwardedRef}
37-
aria-hidden={ariaLabel ? 'false' : 'true'}
39+
aria-hidden={labelled ? undefined : 'true'}
3840
tabIndex={tabIndex}
3941
focusable={tabIndex >= 0 ? 'true' : 'false'}
4042
aria-label={ariaLabel}
4143
aria-labelledby={arialabelledby}
42-
role="img"
4344
className={className}
45+
role={role}
4446
viewBox={`0 0 ${naturalWidth} ${naturalHeight}`}
4547
width={width}
4648
height={height}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,6 @@
4949
"github/no-then": 0,
5050
"eslint-comments/no-use": 0
5151
}
52-
}
52+
},
53+
"packageManager": "[email protected]"
5354
}

0 commit comments

Comments
 (0)