Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(octicons_react): remove role if aria-hidden #1007

Merged
merged 5 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/spicy-actors-act.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/octicons-react': patch
---

Update octicons in React to no longer set role="img" if the icon is aria-hidden
2 changes: 1 addition & 1 deletion lib/octicons_react/__tests__/tree-shaking.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,5 @@ test('tree shaking single export', async () => {
})

const bundleSize = Buffer.byteLength(output[0].code.trim()) / 1000
expect(`${bundleSize}kB`).toMatchInlineSnapshot(`"3.47kB"`)
expect(`${bundleSize}kB`).toMatchInlineSnapshot(`"3.563kB"`)
})
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ exports[`An icon component matches snapshot 1`] = `
fill="currentColor"
focusable="false"
height="16"
role="img"
style="display: inline-block; user-select: none; vertical-align: text-bottom; overflow: visible;"
viewBox="0 0 16 16"
width="16"
Expand Down
24 changes: 22 additions & 2 deletions lib/octicons_react/src/__tests__/octicon.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import '@testing-library/jest-dom'
import {render} from '@testing-library/react'
import {render, screen} from '@testing-library/react'
import React from 'react'
import {AlertIcon} from '../index'

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

it('defaults to `aria-hidden="true"` if no label is present', () => {
const {container} = render(<AlertIcon />)
expect(container.querySelector('svg')).toHaveAttribute('aria-hidden', 'true')
})

it('sets `role="img"` if `aria-label` is provided', () => {
render(<AlertIcon aria-label="Alert" />)
expect(screen.getByLabelText('Alert')).toHaveAttribute('role', 'img')
})

it('sets `role="img"` if `aria-labelledby` is provided', () => {
render(
<>
<span id="label">Alert</span>
<AlertIcon aria-labelledby="label" />
</>
)
expect(screen.getByLabelText('Alert')).toHaveAttribute('role', 'img')
})

it('sets aria-hidden="false" if ariaLabel prop is present', () => {
const {container} = render(<AlertIcon aria-label="icon" />)
expect(container.querySelector('svg')).toHaveAttribute('aria-hidden', 'false')
expect(container.querySelector('svg')).not.toHaveAttribute('aria-hidden')
expect(container.querySelector('svg')).toHaveAttribute('aria-label', 'icon')
})

Expand Down
6 changes: 4 additions & 2 deletions lib/octicons_react/src/createIconComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,19 @@ export function createIconComponent(name, defaultClassName, getSVGData) {
const naturalWidth = svgDataByHeight[naturalHeight].width
const width = height * (naturalWidth / naturalHeight)
const path = svgDataByHeight[naturalHeight].path
const labelled = ariaLabel || arialabelledby
const role = labelled ? 'img' : undefined

return (
<svg
ref={forwardedRef}
aria-hidden={ariaLabel ? 'false' : 'true'}
aria-hidden={labelled ? undefined : 'true'}
tabIndex={tabIndex}
focusable={tabIndex >= 0 ? 'true' : 'false'}
aria-label={ariaLabel}
aria-labelledby={arialabelledby}
role="img"
className={className}
role={role}
viewBox={`0 0 ${naturalWidth} ${naturalHeight}`}
width={width}
height={height}
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,6 @@
"github/no-then": 0,
"eslint-comments/no-use": 0
}
}
},
"packageManager": "yarn@1.22.1"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏻

}
Loading