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

Add tabIndex prop to React Icon component #849

Merged
merged 8 commits into from
Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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/sixty-nails-juggle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/octicons': minor
---

Add focusable prop to react icons
29 changes: 26 additions & 3 deletions docs/content/packages/react.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,23 @@ export default () => (
)
```

### `focusable`

You can add the `focusable` attribute to the SVG element via the `focusable` prop, which can be either `true`, `false`, or `auto`.
Copy link
Member Author

Choose a reason for hiding this comment

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

@ericwbailey I would appreciate if you could please review the docs here 🙏🏼 Feel free to suggest changes, if there is anything doesn't make sense. Thank you!!

broccolinisoup marked this conversation as resolved.
Show resolved Hide resolved
If there is no `aria-label` prop presents, `focusable` prop will be set to false and this helps prevent the decorative
SVG from being announced by some specialized assistive technology browsing modes which get delayed while trying to parse SVG's markup

```js
// Example usage
import {PlusIcon} from '@primer/octicons-react'

export default () => (
<button>
<PlusIcon aria-label="Add new item" focusable="auto" /> New
broccolinisoup marked this conversation as resolved.
Show resolved Hide resolved
</button>
)
```

### Sizes

The `size` prop takes `small`, `medium`, and `large` values that can be used to
Expand Down Expand Up @@ -110,18 +127,24 @@ export default () => (
### `Octicon` (DEPRECATED)

> ⚠️ The `Octicon` component is deprecated. Use icon components on their own instead:
>
> ```diff
>
> ```

- <Octicon icon={AlertIcon} />
+ <AlertIcon />
```

* <AlertIcon />

````

The `Octicon` component is wrapper that passes props to its icon component. To render a specific icon, you
can pass it either via the `icon` prop, or as the only child:

```jsx
<Octicon icon={Icon} />
<Octicon><Icon x={10}/></Octicon>
```
````

[octicons]: https://primer.style/octicons/
[primer]: https://github.com/primer/primer
Expand Down
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(`"2.484kB"`)
expect(`${bundleSize}kB`).toMatchInlineSnapshot(`"2.645kB"`)
})
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ exports[`An icon component matches snapshot 1`] = `
aria-hidden="true"
class="octicon octicon-alert"
fill="currentColor"
focusable="false"
height="16"
role="img"
style="display: inline-block; user-select: none; vertical-align: text-bottom; overflow: visible;"
Expand Down
10 changes: 10 additions & 0 deletions lib/octicons_react/src/__tests__/octicon.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ describe('An icon component', () => {
expect(container.querySelector('svg')).toHaveAttribute('aria-label', 'icon')
})

it('set the focusable prop to false if ariaLabel prop is not present', () => {
const {container} = render(<AlertIcon focusable={true} />)
expect(container.querySelector('svg')).toHaveAttribute('focusable', 'false')
})

it('sets focusable prop to given value if ariaLabel prop is present', () => {
const {container} = render(<AlertIcon aria-label="icon" focusable="auto" />)
expect(container.querySelector('svg')).toHaveAttribute('focusable', 'auto')
})

it('respects the className prop', () => {
const {container} = render(<AlertIcon className="foo" />)
expect(container.querySelector('svg')).toHaveAttribute('class', 'foo')
Expand Down
3 changes: 2 additions & 1 deletion lib/octicons_react/src/createIconComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export function createIconComponent(name, defaultClassName, getSVGData) {
const svgDataByHeight = getSVGData()
const heights = Object.keys(svgDataByHeight)

function Icon({'aria-label': ariaLabel, className, fill = 'currentColor', size, verticalAlign}) {
function Icon({'aria-label': ariaLabel, focusable = false, className, fill = 'currentColor', size, verticalAlign}) {
const height = sizeMap[size] || size
const naturalHeight = closestNaturalHeight(heights, height)
const naturalWidth = svgDataByHeight[naturalHeight].width
Expand All @@ -20,6 +20,7 @@ export function createIconComponent(name, defaultClassName, getSVGData) {
return (
<svg
aria-hidden={ariaLabel ? 'false' : 'true'}
focusable={ariaLabel ? focusable : false}
broccolinisoup marked this conversation as resolved.
Show resolved Hide resolved
aria-label={ariaLabel}
role="img"
className={className}
Expand Down
1 change: 1 addition & 0 deletions lib/octicons_react/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ type Size = 'small' | 'medium' | 'large'

export interface OcticonProps {
'aria-label'?: string
focusable?: boolean | 'auto'
joshblack marked this conversation as resolved.
Show resolved Hide resolved
children?: React.ReactElement<any>
className?: string
fill?: string
Expand Down