Skip to content

Commit

Permalink
Add fill prop
Browse files Browse the repository at this point in the history
Co-authored-by: @macno
  • Loading branch information
colebemis committed Feb 23, 2021
1 parent 89a79bf commit 6efa7c2
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
5 changes: 3 additions & 2 deletions lib/octicons_react/script/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ ${name}.defaultProps = {
key,
name,
octicon,
code
code,
}
})
.sort((a, b) => a.key.localeCompare(b.key))
Expand Down Expand Up @@ -64,6 +64,7 @@ type Size = 'small' | 'medium' | 'large'
interface IconProps {
'aria-label'?: string
className?: string
fill?: string
size?: number | Size
verticalAlign?: 'middle' | 'text-bottom' | 'text-top' | 'top' | 'unset'
}
Expand All @@ -87,7 +88,7 @@ fse
.mkdirs(srcDir)
.then(() => writeIcons(iconsFile))
.then(() => writeTypes(typesFile))
.catch(error => {
.catch((error) => {
console.error(error)
process.exit(1)
})
5 changes: 5 additions & 0 deletions lib/octicons_react/src/__tests__/octicon.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ describe('An icon component', () => {
expect(container.querySelector('svg')).toHaveAttribute('class', 'foo')
})

it('respects the fill prop', () => {
const {container} = render(<AlertIcon fill="#f00" />)
expect(container.querySelector('svg')).toHaveAttribute('fill', '#f00')
})

it('respects the verticalAlign prop', () => {
const {container} = render(<AlertIcon verticalAlign="middle" />)
expect(container.querySelector('svg')).toHaveStyle({verticalAlign: 'middle'})
Expand Down
19 changes: 13 additions & 6 deletions lib/octicons_react/src/get-svg-props.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
const sizeMap = {
small: 16,
medium: 32,
large: 64
large: 64,
}

export default function getSvgProps({'aria-label': ariaLabel, className, size, verticalAlign, svgDataByHeight}) {
export default function getSvgProps({
'aria-label': ariaLabel,
className,
fill = 'currentColor',
size,
verticalAlign,
svgDataByHeight,
}) {
const height = sizeMap[size] || size
const naturalHeight = closestNaturalHeight(Object.keys(svgDataByHeight), height)
const naturalWidth = svgDataByHeight[naturalHeight].width
Expand All @@ -19,18 +26,18 @@ export default function getSvgProps({'aria-label': ariaLabel, className, size, v
viewBox: `0 0 ${naturalWidth} ${naturalHeight}`,
width,
height,
fill: 'currentColor',
fill,
style: {
display: 'inline-block',
userSelect: 'none',
verticalAlign
verticalAlign,
},
dangerouslySetInnerHTML: {__html: path}
dangerouslySetInnerHTML: {__html: path},
}
}

function closestNaturalHeight(naturalHeights, height) {
return naturalHeights
.map(naturalHeight => parseInt(naturalHeight, 10))
.map((naturalHeight) => parseInt(naturalHeight, 10))
.reduce((acc, naturalHeight) => (naturalHeight <= height ? naturalHeight : acc), naturalHeights[0])
}
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 @@ -8,6 +8,7 @@ export interface OcticonProps {
'aria-label'?: string
children?: React.ReactElement<any>
className?: string
fill?: string
icon?: Icon
size?: number | Size
verticalAlign?: 'middle' | 'text-bottom' | 'text-top' | 'top' | 'unset'
Expand Down

0 comments on commit 6efa7c2

Please sign in to comment.