Skip to content

Commit ea81ec1

Browse files
authored
907 accept additional props for labeling icons (#932)
1 parent 16b1cd2 commit ea81ec1

File tree

7 files changed

+4487
-4436
lines changed

7 files changed

+4487
-4436
lines changed

.changeset/happy-knives-exist.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@primer/octicons": minor
3+
---
4+
5+
Add `id`, `title`, and `aria-labelledby` props to icon components

lib/octicons_react/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
.next
33
dist/
44
src/__generated__/
5+
.tool-versions

lib/octicons_react/README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,43 @@ export default () => (
6262
)
6363
```
6464

65+
### `aria-labelledby`
66+
67+
You have the option of adding accessibility information to the icon with the
68+
[`aria-labelledby` attribute][aria-labelledby] via the `aria-labelledby` prop. Using aria-labelledby referencing the id values of the title element provides the accessible name.
69+
70+
```js
71+
// Example usage
72+
import {PlusIcon} from '@primer/octicons-react'
73+
74+
export default () => (
75+
<button>
76+
<PlusIcon aria-labelledby="title" title="Add new item" /> New
77+
</button>
78+
)
79+
```
80+
81+
### `title`
82+
83+
You have the option of adding accessibility information to the icon with the
84+
[`title` attribute][title] via the `title` prop.
85+
86+
### `id`
87+
88+
You have the option of adding information to the icon with the
89+
[`id` attribute][id] via the `id` prop.
90+
91+
```js
92+
// Example usage
93+
import {PlusIcon} from '@primer/octicons-react'
94+
95+
export default () => (
96+
<button>
97+
<PlusIcon id="unique-plus-icon" /> New
98+
</button>
99+
)
100+
```
101+
65102
### `tabIndex`
66103

67104
You can add the `tabindex` attribute to an SVG element via the `tabIndex` prop if the SVG element is intended to be interactive.
@@ -142,3 +179,6 @@ can pass it either via the `icon` prop, or as the only child:
142179
[install-npm]: https://docs.npmjs.com/getting-started/installing-node
143180
[tree-shaking]: https://developer.mozilla.org/en-US/docs/Glossary/Tree_shaking
144181
[aria-label]: https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Attributes/aria-label
182+
[aria-labelledby]: https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Attributes/aria-labelledby
183+
[title]: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/title
184+
[id]: https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/id

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.244kB"`)
53+
expect(`${bundleSize}kB`).toMatchInlineSnapshot(`"3.47kB"`)
5454
})

lib/octicons_react/src/createIconComponent.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,14 @@ export function createIconComponent(name, defaultClassName, getSVGData) {
1414
(
1515
{
1616
'aria-label': ariaLabel,
17+
'aria-labelledby': arialabelledby,
1718
tabIndex,
1819
className = defaultClassName,
1920
fill = 'currentColor',
2021
size = 16,
21-
verticalAlign = 'text-bottom'
22+
verticalAlign = 'text-bottom',
23+
id,
24+
title
2225
},
2326
forwardedRef
2427
) => {
@@ -35,19 +38,22 @@ export function createIconComponent(name, defaultClassName, getSVGData) {
3538
tabIndex={tabIndex}
3639
focusable={tabIndex >= 0 ? 'true' : 'false'}
3740
aria-label={ariaLabel}
41+
aria-labelledby={arialabelledby}
3842
role="img"
3943
className={className}
4044
viewBox={`0 0 ${naturalWidth} ${naturalHeight}`}
4145
width={width}
4246
height={height}
4347
fill={fill}
48+
id={id}
4449
style={{
4550
display: 'inline-block',
4651
userSelect: 'none',
4752
verticalAlign,
4853
overflow: 'visible'
4954
}}
5055
>
56+
{title ? <title>{title}</title> : null}
5157
{path}
5258
</svg>
5359
)

lib/octicons_react/src/index.d.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
1+
// eslint-disable-next-line import/no-namespace
12
import * as React from 'react'
23

3-
import {Icon} from './__generated__/icons'
4+
// eslint-disable-next-line prettier/prettier
5+
import { Icon } from './__generated__/icons'
46

57
type Size = 'small' | 'medium' | 'large'
68

79
export interface OcticonProps {
810
'aria-label'?: string
11+
'aria-labelledby'?: string
912
tabIndex?: number
1013
children?: React.ReactElement<any>
1114
className?: string
15+
title?: string | React.ReactElement<any>
16+
id?: string
1217
fill?: string
13-
icon?: Icon
18+
icon?: Icon | React.ReactNode
1419
size?: number | Size
1520
verticalAlign?: 'middle' | 'text-bottom' | 'text-top' | 'top' | 'unset'
1621
}

0 commit comments

Comments
 (0)