Skip to content

Commit

Permalink
Standardize icon creation
Browse files Browse the repository at this point in the history
  • Loading branch information
danielfdsilva committed Jun 24, 2024
1 parent 5e1121b commit f25c883
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 2 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"private": true,
"scripts": {
"icons:react": "./node_modules/@svgr/cli/bin/svgr --out-dir packages/collecticons-react/lib/icons --filename-case camel --typescript --title-prop --ref --svg-props fill=currentColor --svg-props viewBox='0 0 16 16' -- ./icons",
"icons:chakra": "./node_modules/@svgr/cli/bin/svgr --out-dir packages/collecticons-chakra/lib/icons --filename-case camel --ext tsx --template chakra-svco-template.js -- ./icons",
"icons:react": "./node_modules/@svgr/cli/bin/svgr --out-dir packages/collecticons-react/lib/icons --filename-case camel --ext tsx --template svco-template.js -- ./icons",
"icons:chakra": "./node_modules/@svgr/cli/bin/svgr --out-dir packages/collecticons-chakra/lib/icons --filename-case camel --ext tsx --template svco-template.js -- ./icons",
"icons:zip": "zip showcase/static/collecticons.zip icons/*",
"icons": "yarn icons:zip && yarn icons:react && yarn icons:chakra",
"build": "lerna run build",
Expand Down
1 change: 1 addition & 0 deletions packages/collecticons-chakra/lib/collecticon-creator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export function createCollecticon(
};
return (
<Icon {...iconProps} ref={ref}>
{title && (<title>{title}</title>)}
{creatorFn(iconProps)}
</Icon>
);
Expand Down
58 changes: 58 additions & 0 deletions packages/collecticons-react/lib/collecticon-creator.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import React, { SVGProps } from 'react';

interface IconPropsWithTitle extends SVGProps<SVGSVGElement> {
title?: string;
}

interface CollecticonProps extends IconPropsWithTitle {
meaningful?: boolean;
size?: string;
}

export type Collecticon = React.ForwardRefExoticComponent<
CollecticonProps & React.RefAttributes<SVGSVGElement>
>;

/**
* Creates a collecticons component.
*
* @param {function} creatorFn Function to create the collecticon.
*/
export function createCollecticon(
creatorFn: (props: IconPropsWithTitle) => JSX.Element,
iconSvgProps: IconPropsWithTitle = {}
): Collecticon {
const Collecticon = React.forwardRef<SVGSVGElement, CollecticonProps>(
(props, ref) => {
const { title, size, meaningful, ...rest } = props;

const s = typeof size !== 'undefined' ? size : '1rem';

const iconProps: IconPropsWithTitle = {
title,
width: s,
height: s,
fill: 'currentColor',
role: 'img',
viewBox: '0 0 16 16',
xmlns: 'http://www.w3.org/2000/svg',
// By default icons are rendered decoratively (aria hidden true) Using
// "meaningful" prop (together with a descriptive title) will enable the
// icon to be understood by assistive technologies.
'aria-hidden': !meaningful,
...iconSvgProps,
...rest
};
return (
<svg {...iconProps} ref={ref}>
{title && (<title>{title}</title>)}
{creatorFn(iconProps)}
</svg>
);
}
);

Collecticon.displayName = 'Collecticon';

return Collecticon;
}
File renamed without changes.

0 comments on commit f25c883

Please sign in to comment.