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

Forward ref for generated icons #895

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/react-icons/scripts/task_all.ts
Expand Up @@ -30,11 +30,11 @@

await write(
[icon.id, "index.js"],
"// THIS FILE IS AUTO GENERATED\nvar GenIcon = require('../lib').GenIcon\n",
"// THIS FILE IS AUTO GENERATED\nvar IconWithRef = require('../lib').IconWithRef\nvar React = require('react')\n",
);
await write(
[icon.id, "index.mjs"],
"// THIS FILE IS AUTO GENERATED\nimport { GenIcon } from '../lib/index.mjs';\n",
"// THIS FILE IS AUTO GENERATED\nimport { IconWithRef } from '../lib/index.mjs';\nimport React from 'react';\n",
);
await write(
[icon.id, "index.d.ts"],
Expand All @@ -60,7 +60,7 @@
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export async function writeIconModule(
icon: IconDefinition,
{ DIST, LIB, rootDir }: TaskContext,

Check warning on line 63 in packages/react-icons/scripts/task_all.ts

View workflow job for this annotation

GitHub Actions / build

'LIB' is defined but never used

Check warning on line 63 in packages/react-icons/scripts/task_all.ts

View workflow job for this annotation

GitHub Actions / build

'rootDir' is defined but never used
) {
const exists = new Set(); // for remove duplicate
for (const content of icon.contents) {
Expand Down
11 changes: 5 additions & 6 deletions packages/react-icons/scripts/templates.ts
Expand Up @@ -10,15 +10,14 @@ export function iconRowTemplate(
switch (type) {
case "module":
return (
`export function ${formattedName} (props) {\n` +
` return GenIcon(${JSON.stringify(iconData)})(props);\n` +
`};\n`
`export const ${formattedName} = React.forwardRef((props, ref) =>
React.createElement(IconWithRef, { ...props, ref, iconTree: ${JSON.stringify(iconData)} }));
\n`
);
case "common":
return (
`module.exports.${formattedName} = function ${formattedName} (props) {\n` +
` return GenIcon(${JSON.stringify(iconData)})(props);\n` +
`};\n`
`module.exports.${formattedName} = React.forwardRef((props, ref) =>
React.createElement(IconWithRef, { ...props, ref, iconTree: ${JSON.stringify(iconData)} }));\n`
);
case "dts":
return `export declare const ${formattedName}: IconType;\n`;
Expand Down
32 changes: 23 additions & 9 deletions packages/react-icons/src/iconBase.tsx
Expand Up @@ -20,14 +20,27 @@ function Tree2Element(tree: IconTree[]): React.ReactElement[] {
)
);
}

export function GenIcon(data: IconTree) {
// eslint-disable-next-line react/display-name
return (props: IconBaseProps) => (
<IconBase attr={{ ...data.attr }} {...props}>
{Tree2Element(data.child)}
return function GenIconBase(props: IconBaseProps) {
return (
<IconBase attr={{ ...data.attr }} {...props}>
{Tree2Element(data.child)}
</IconBase>
);
}
}

export const IconWithRef = React.forwardRef<SVGSVGElement, IconBaseProps & { iconTree: IconTree }>((
props, ref): JSX.Element => {
const { iconTree, ...rest } = props;
return (
<IconBase ref={ref} attr={{ ...iconTree.attr }} {...rest}>
{Tree2Element(iconTree.child)}
</IconBase>
);
}
})
IconWithRef.displayName = "GenIcon";

export interface IconBaseProps extends React.SVGAttributes<SVGElement> {
children?: React.ReactNode;
Expand All @@ -37,9 +50,8 @@ export interface IconBaseProps extends React.SVGAttributes<SVGElement> {
}

export type IconType = (props: IconBaseProps) => JSX.Element;
export function IconBase(
props: IconBaseProps & { attr?: Record<string, string> }
): JSX.Element {
export const IconBase = React.forwardRef<SVGSVGElement, IconBaseProps & { attr?: Record<string, string> }>((
props, ref): JSX.Element => {
const elem = (conf: IconContext) => {
const { attr, size, title, ...svgProps } = props;
const computedSize = size || conf.size || "1em";
Expand All @@ -50,6 +62,7 @@ export function IconBase(

return (
<svg
ref={ref}
stroke="currentColor"
fill="currentColor"
strokeWidth="0"
Expand Down Expand Up @@ -79,4 +92,5 @@ export function IconBase(
) : (
elem(DefaultContext)
);
}
})
IconBase.displayName = "IconBase";