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

907 accept additional props for labeling icons #932

Merged
merged 19 commits into from
May 25, 2023
Merged
Show file tree
Hide file tree
Changes from 18 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
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.
green6erry marked this conversation as resolved.
Show resolved Hide resolved

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

export default () => (
<button>
<PlusIcon aria-labelledby="title" title="Add new item" /> New
green6erry marked this conversation as resolved.
Show resolved Hide resolved
</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>
)
```
green6erry marked this conversation as resolved.
Show resolved Hide resolved

### `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 {
green6erry marked this conversation as resolved.
Show resolved Hide resolved
'aria-label'?: string
'aria-labelledby'?: string
tabIndex?: number
children?: React.ReactElement<any>
className?: string
title?: string | React.ReactElement<any>
green6erry marked this conversation as resolved.
Show resolved Hide resolved
id?: string
fill?: string
icon?: Icon
icon?: Icon | React.ReactNode
size?: number | Size
verticalAlign?: 'middle' | 'text-bottom' | 'text-top' | 'top' | 'unset'
}
Expand Down
Loading