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 7 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
65 changes: 65 additions & 0 deletions lib/octicons_react/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,70 @@ 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.

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

export default () => (
<button>
<PlusIcon title="Add new item" /> New
green6erry marked this conversation as resolved.
Show resolved Hide resolved
</button>
)
```

### `ref`

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

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

export default () => (
<button>
<PlusIcon ref="Add new item" /> New
</button>
)
```
green6erry marked this conversation as resolved.
Show resolved Hide resolved

### `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 @@ -124,6 +188,7 @@ export default () => (
### `Octicon` (DEPRECATED)

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

```diff
- <Octicon icon={AlertIcon} />
+ <AlertIcon />
Expand Down
9 changes: 9 additions & 0 deletions lib/octicons_react/src/createIconComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,31 @@ export function createIconComponent(name, defaultClassName, getSVGData) {

function Icon({
'aria-label': ariaLabel,
'aria-labelledby': arialabelledby,
tabIndex,
className = defaultClassName,
fill = 'currentColor',
size = 16,
ref,
green6erry marked this conversation as resolved.
Show resolved Hide resolved
id,
title,
verticalAlign = 'text-bottom'
}) {
const height = sizeMap[size] || size
const naturalHeight = closestNaturalHeight(heights, height)
const naturalWidth = svgDataByHeight[naturalHeight].width
const width = height * (naturalWidth / naturalHeight)
const path = svgDataByHeight[naturalHeight].path
const svgProps = {...(id ? {id} : {}), ...(ref ? {ref} : {})}
green6erry marked this conversation as resolved.
Show resolved Hide resolved

return (
<svg
aria-hidden={ariaLabel ? 'false' : 'true'}
tabIndex={tabIndex}
ref={ref}
focusable={tabIndex >= 0 ? 'true' : 'false'}
aria-label={ariaLabel}
aria-labelledby={arialabelledby}
role="img"
className={className}
viewBox={`0 0 ${naturalWidth} ${naturalHeight}`}
Expand All @@ -42,7 +49,9 @@ export function createIconComponent(name, defaultClassName, getSVGData) {
verticalAlign,
overflow: 'visible'
}}
{...svgProps}
>
{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.SVGAttributes<SVGElement>
green6erry marked this conversation as resolved.
Show resolved Hide resolved
size?: number | Size
verticalAlign?: 'middle' | 'text-bottom' | 'text-top' | 'top' | 'unset'
}
Expand Down
Loading