From 6efa7c2a55a0f776c04328bd551fdeaf3cfa2da1 Mon Sep 17 00:00:00 2001 From: Cole Bemis Date: Tue, 23 Feb 2021 10:52:31 -0800 Subject: [PATCH] Add fill prop Co-authored-by: @macno --- lib/octicons_react/script/build.js | 5 +++-- lib/octicons_react/src/__tests__/octicon.js | 5 +++++ lib/octicons_react/src/get-svg-props.js | 19 +++++++++++++------ lib/octicons_react/src/index.d.ts | 1 + 4 files changed, 22 insertions(+), 8 deletions(-) diff --git a/lib/octicons_react/script/build.js b/lib/octicons_react/script/build.js index 5e2f26c31..171406c38 100755 --- a/lib/octicons_react/script/build.js +++ b/lib/octicons_react/script/build.js @@ -32,7 +32,7 @@ ${name}.defaultProps = { key, name, octicon, - code + code, } }) .sort((a, b) => a.key.localeCompare(b.key)) @@ -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' } @@ -87,7 +88,7 @@ fse .mkdirs(srcDir) .then(() => writeIcons(iconsFile)) .then(() => writeTypes(typesFile)) - .catch(error => { + .catch((error) => { console.error(error) process.exit(1) }) diff --git a/lib/octicons_react/src/__tests__/octicon.js b/lib/octicons_react/src/__tests__/octicon.js index 332b09930..fa7dcfe2d 100644 --- a/lib/octicons_react/src/__tests__/octicon.js +++ b/lib/octicons_react/src/__tests__/octicon.js @@ -53,6 +53,11 @@ describe('An icon component', () => { expect(container.querySelector('svg')).toHaveAttribute('class', 'foo') }) + it('respects the fill prop', () => { + const {container} = render() + expect(container.querySelector('svg')).toHaveAttribute('fill', '#f00') + }) + it('respects the verticalAlign prop', () => { const {container} = render() expect(container.querySelector('svg')).toHaveStyle({verticalAlign: 'middle'}) diff --git a/lib/octicons_react/src/get-svg-props.js b/lib/octicons_react/src/get-svg-props.js index 9a245a6c6..7c447f6a3 100644 --- a/lib/octicons_react/src/get-svg-props.js +++ b/lib/octicons_react/src/get-svg-props.js @@ -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 @@ -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]) } diff --git a/lib/octicons_react/src/index.d.ts b/lib/octicons_react/src/index.d.ts index b91239af9..0c733cb8d 100644 --- a/lib/octicons_react/src/index.d.ts +++ b/lib/octicons_react/src/index.d.ts @@ -8,6 +8,7 @@ export interface OcticonProps { 'aria-label'?: string children?: React.ReactElement className?: string + fill?: string icon?: Icon size?: number | Size verticalAlign?: 'middle' | 'text-bottom' | 'text-top' | 'top' | 'unset'