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

Add icon attribution in aria-details #815

Open
wants to merge 3 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
11 changes: 7 additions & 4 deletions packages/react-icons/scripts/templates.ts
@@ -1,20 +1,23 @@
import { IconDefinition } from "./_types";

export function iconRowTemplate(
icon,
formattedName,
icon: IconDefinition,
formattedName: string,
iconData,
type = "module"
) {
const {name, license, licenseUrl, projectUrl, ...iconDefinition} = icon;
switch (type) {
case "module":
return (
`export function ${formattedName} (props) {\n` +
` return GenIcon(${JSON.stringify(iconData)})(props);\n` +
` return GenIcon(${JSON.stringify({name, license, licenseUrl, projectUrl})}, ${JSON.stringify(iconData)})(props);\n` +
`};\n`
);
case "common":
return (
`module.exports.${formattedName} = function ${formattedName} (props) {\n` +
` return GenIcon(${JSON.stringify(iconData)})(props);\n` +
` return GenIcon(${JSON.stringify({name, license, licenseUrl, projectUrl})}, ${JSON.stringify(iconData)})(props);\n` +
`};\n`
);
case "dts":
Expand Down
12 changes: 10 additions & 2 deletions packages/react-icons/src/iconBase.tsx
Expand Up @@ -8,6 +8,13 @@ export interface IconTree {
child: IconTree[];
}

export interface IconLicense {
name: string;
license: string;
licenseUrl: string;
projectUrl: string;
}

function Tree2Element(tree: IconTree[]): React.ReactElement[] {
return (
tree &&
Expand All @@ -20,10 +27,11 @@ function Tree2Element(tree: IconTree[]): React.ReactElement[] {
)
);
}
export function GenIcon(data: IconTree) {
export function GenIcon(license: IconLicense, data: IconTree) {
const license_attr = {"aria-details": `Icon by ${license.name} (${license.projectUrl}) under the ${license.license} license. url: ${license.licenseUrl}`};
// eslint-disable-next-line react/display-name
return (props: IconBaseProps) => (
<IconBase attr={{ ...data.attr }} {...props}>
<IconBase attr={{ ...license_attr, ...data.attr }} {...props} >
{Tree2Element(data.child)}
</IconBase>
);
Expand Down