Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 5 additions & 60 deletions packages/icon/scripts/prebuild/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import path from 'path';

import { getChecksum } from './checksum';
import { indexTemplate } from './indexTemplate';
import { applyLeafyGreenTemplate } from './leafyGreenTemplate';
import { FileObject, PrebuildOptions } from './prebuild.types';

const SRC_PATH = path.resolve(__dirname, '..', '..', 'src');
Expand Down Expand Up @@ -141,71 +142,15 @@ function makeFileProcessor(outputDir: string, options?: PrebuildOptions) {
{ componentName: file.name },
);

// Post-process to add custom LeafyGreen wrapper
const customizedSVGR = processedSVGR.replace(
/import \* as React from "react";\nimport type \{ SVGProps \} from "react";\nconst (\w+) = \(props: SVGProps<SVGSVGElement>\) => (.*?);\nexport default \1;/s,
`import * as React from 'react';
import { css, cx } from '@leafygreen-ui/emotion';
import { generateAccessibleProps, sizeMap } from '../glyphCommon';
import { LGGlyph } from '../types';

export interface $1Props extends LGGlyph.ComponentProps {}

const $1 = ({
className,
size = 16,
title,
'aria-label': ariaLabel,
'aria-labelledby': ariaLabelledby,
fill,
role = 'img',
...props
}: $1Props) => {
const fillStyle = css\`
color: \${fill};
\`;

const noFlexShrink = css\`
flex-shrink: 0;
\`;

const accessibleProps = generateAccessibleProps(role, '$1', {
title,
'aria-label': ariaLabel,
'aria-labelledby': ariaLabelledby
});

const svgElement = $2;

return React.cloneElement(svgElement, {
className: cx(
{
[fillStyle]: fill != null,
},
noFlexShrink,
className,
),
height: typeof size === 'number' ? size : sizeMap[size],
width: typeof size === 'number' ? size : sizeMap[size],
role,
...accessibleProps,
...props,
});
};

$1.displayName = '$1';
$1.isGlyph = true;

export default $1;`,
);

// Apply custom LeafyGreen wrapper using template
const customizedSVGR = applyLeafyGreenTemplate(processedSVGR, file.name);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this just moves the brittle string replacement to a separate file. We should use the transform Config.template property

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed Cursor has difficulty doing this. There's a weird bug/quirk in the tpl function where it doesn't like when you concat words after interpolated var names.

If you tell cursor to do that it works:
e.g. instead of ${variables.componentName}Props) use ${variables.componentName + "Props)"}

const scriptPath =
'packages/icon/' +
path.relative(path.resolve(__dirname, process.cwd()), __filename);

if (customizedSVGR === null) {
if (!customizedSVGR) {
throw new Error(
`Regex replacement failed for file "${file.name}.svg". The SVGR output could not be customized and would result in a broken icon component.`,
`SVGR transformation failed for file "${file.name}.svg". The component could not be generated.`,
);
}

Expand Down
69 changes: 69 additions & 0 deletions packages/icon/scripts/prebuild/leafyGreenTemplate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* LeafyGreen template transformation for SVGR-generated components
* This replaces the previous inline regex with a more maintainable template approach
*/

export function applyLeafyGreenTemplate(
svgrOutput: string,
componentName: string,
): string {
// This pattern matches the standard SVGR TypeScript output structure
const svgrPattern =
/import \* as React from "react";\nimport type \{ SVGProps \} from "react";\nconst (\w+) = \(props: SVGProps<SVGSVGElement>\) => (.*?);\nexport default \1;/s;

const leafyGreenTemplate = `import * as React from 'react';
import { css, cx } from '@leafygreen-ui/emotion';
import { generateAccessibleProps, sizeMap } from '../glyphCommon';
import { LGGlyph } from '../types';

export interface $1Props extends LGGlyph.ComponentProps {}

const $1 = ({
className,
size = 16,
title,
'aria-label': ariaLabel,
'aria-labelledby': ariaLabelledby,
fill,
role = 'img',
...props
}: $1Props) => {
const fillStyle = css\`
color: \${fill};
\`;

const noFlexShrink = css\`
flex-shrink: 0;
\`;

const accessibleProps = generateAccessibleProps(role, '${componentName}', {
title,
'aria-label': ariaLabel,
'aria-labelledby': ariaLabelledby
});

const svgElement = $2;

return React.cloneElement(svgElement, {
className: cx(
{
[fillStyle]: fill != null,
},
noFlexShrink,
className,
),
height: typeof size === 'number' ? size : sizeMap[size],
width: typeof size === 'number' ? size : sizeMap[size],
role,
...accessibleProps,
...props,
});
};

$1.displayName = '${componentName}';
$1.isGlyph = true;

export default $1;`;

return svgrOutput.replace(svgrPattern, leafyGreenTemplate);
}
2 changes: 1 addition & 1 deletion packages/icon/src/generated/AIModel.tsx

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/icon/src/generated/ActivityFeed.tsx

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/icon/src/generated/AddFile.tsx

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/icon/src/generated/AllProducts.tsx

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/icon/src/generated/AnalyticsNode.tsx

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/icon/src/generated/Apps.tsx

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/icon/src/generated/Array.tsx

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/icon/src/generated/ArrowDown.tsx

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/icon/src/generated/ArrowLeft.tsx

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/icon/src/generated/ArrowRight.tsx

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/icon/src/generated/ArrowUp.tsx

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/icon/src/generated/Award.tsx

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/icon/src/generated/Beaker.tsx

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/icon/src/generated/Bell.tsx

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/icon/src/generated/Biometric.tsx

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/icon/src/generated/Boolean.tsx

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/icon/src/generated/Building.tsx

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/icon/src/generated/Bulb.tsx

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/icon/src/generated/Calendar.tsx

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/icon/src/generated/Camera.tsx

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/icon/src/generated/Cap.tsx

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/icon/src/generated/CaretDown.tsx

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/icon/src/generated/CaretLeft.tsx

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/icon/src/generated/CaretRight.tsx

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/icon/src/generated/CaretUp.tsx

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/icon/src/generated/ChartFilled.tsx

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/icon/src/generated/Charts.tsx

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading