Skip to content

Commit

Permalink
907 accept additional props for labeling icons (#932)
Browse files Browse the repository at this point in the history
  • Loading branch information
green6erry authored May 25, 2023
1 parent 16b1cd2 commit ea81ec1
Show file tree
Hide file tree
Showing 7 changed files with 4,487 additions and 4,436 deletions.
5 changes: 5 additions & 0 deletions .changeset/happy-knives-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@primer/octicons": minor
---

Add `id`, `title`, and `aria-labelledby` props to icon components
1 change: 1 addition & 0 deletions lib/octicons_react/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
.next
dist/
src/__generated__/
.tool-versions
40 changes: 40 additions & 0 deletions lib/octicons_react/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,43 @@ export default () => (
)
```

### `aria-labelledby`

You have the option of adding accessibility information to the icon with the
[`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.

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

export default () => (
<button>
<PlusIcon aria-labelledby="title" title="Add new item" /> New
</button>
)
```

### `title`

You have the option of adding accessibility information to the icon with the
[`title` attribute][title] via the `title` prop.

### `id`

You have the option of adding information to the icon with the
[`id` attribute][id] via the `id` prop.

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

export default () => (
<button>
<PlusIcon id="unique-plus-icon" /> New
</button>
)
```

### `tabIndex`

You can add the `tabindex` attribute to an SVG element via the `tabIndex` prop if the SVG element is intended to be interactive.
Expand Down Expand Up @@ -142,3 +179,6 @@ can pass it either via the `icon` prop, or as the only child:
[install-npm]: https://docs.npmjs.com/getting-started/installing-node
[tree-shaking]: https://developer.mozilla.org/en-US/docs/Glossary/Tree_shaking
[aria-label]: https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Attributes/aria-label
[aria-labelledby]: https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Attributes/aria-labelledby
[title]: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/title
[id]: https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/id
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.244kB"`)
expect(`${bundleSize}kB`).toMatchInlineSnapshot(`"3.47kB"`)
})
8 changes: 7 additions & 1 deletion lib/octicons_react/src/createIconComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ export function createIconComponent(name, defaultClassName, getSVGData) {
(
{
'aria-label': ariaLabel,
'aria-labelledby': arialabelledby,
tabIndex,
className = defaultClassName,
fill = 'currentColor',
size = 16,
verticalAlign = 'text-bottom'
verticalAlign = 'text-bottom',
id,
title
},
forwardedRef
) => {
Expand All @@ -35,19 +38,22 @@ export function createIconComponent(name, defaultClassName, getSVGData) {
tabIndex={tabIndex}
focusable={tabIndex >= 0 ? 'true' : 'false'}
aria-label={ariaLabel}
aria-labelledby={arialabelledby}
role="img"
className={className}
viewBox={`0 0 ${naturalWidth} ${naturalHeight}`}
width={width}
height={height}
fill={fill}
id={id}
style={{
display: 'inline-block',
userSelect: 'none',
verticalAlign,
overflow: 'visible'
}}
>
{title ? <title>{title}</title> : null}
{path}
</svg>
)
Expand Down
9 changes: 7 additions & 2 deletions lib/octicons_react/src/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
// eslint-disable-next-line import/no-namespace
import * as React from 'react'

import {Icon} from './__generated__/icons'
// eslint-disable-next-line prettier/prettier
import { Icon } from './__generated__/icons'

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

export interface OcticonProps {
'aria-label'?: string
'aria-labelledby'?: string
tabIndex?: number
children?: React.ReactElement<any>
className?: string
title?: string | React.ReactElement<any>
id?: string
fill?: string
icon?: Icon
icon?: Icon | React.ReactNode
size?: number | Size
verticalAlign?: 'middle' | 'text-bottom' | 'text-top' | 'top' | 'unset'
}
Expand Down
Loading

0 comments on commit ea81ec1

Please sign in to comment.