Skip to content

Commit 00b00a7

Browse files
committed
fix: previous solution was overkill
1 parent f3b1aac commit 00b00a7

File tree

3 files changed

+26
-23
lines changed

3 files changed

+26
-23
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ Icon components accept all props that you can pass to a normal SVG element, incl
7575
- **title?**: `string` – Accessibility label
7676
- **titleId?**: `string` – Accessibility label ID
7777
- **testID?**: `string` – testID for tests
78-
- **duotoneColor?**: `string` – Duotone fill color. Can be any CSS color string, including `hex`, `rgb`, `rgba`, `hsl`, `hsla`, named colors. Default value to black. ⚠️ Use `duototocolor` when importing the weight icon directly, `import Star from 'phosphor-react-native/src/duotone/Star'`.;
79-
- **duotoneOpacity?**: `number` – The opacity of the duotoneColor. Default value to 0.2. ⚠️ Use `duotoneopacity` when importing the weight icon directly, `import Star from 'phosphor-react-native/src/duotone/Star'`.;
78+
- **duotoneColor?**: `string` – Duotone fill color. Can be any CSS color string, including `hex`, `rgb`, `rgba`, `hsl`, `hsla`, named colors. Default value to black.
79+
- **duotoneOpacity?**: `number` – The opacity of the duotoneColor. Default value to 0.2.
8080

8181
### Context
8282

generator/generate-svg.mjs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,20 +70,30 @@ const generateIconWithWeight = (icon, weight) => {
7070
.replace(
7171
'const',
7272
weight === 'duotone'
73-
? "import type { DuotoneProps } from '../lib'\n\nconst"
73+
? "import type { IconProps } from '../lib'\n\nconst"
7474
: "import type { IconProps } from '../lib'\n\nconst"
7575
)
76-
.replace('SvgProps', weight === 'duotone' ? 'DuotoneProps' : 'IconProps')
76+
.replace(
77+
'SvgProps',
78+
weight === 'duotone'
79+
? 'IconProps'
80+
: `Exclude<IconProps, 'duotoneColor' | 'duotoneOpacity'>`
81+
)
7782
.replace(' xmlns="http://www.w3.org/2000/svg"', '')
7883
.replace(
7984
'<Svg ',
8085
`<Svg className="${iconName}__svg-icon-phosphor" testID={props.testID ?? 'phosphor-react-native-${iconName}'} `
8186
);
8287
if (weight === 'duotone') {
83-
tsCode = tsCode.replace(
84-
'opacity={0.2}',
85-
'opacity={props.duotoneopacity ?? 0.2} fill={props.duotonecolor ?? "currentColor"}'
86-
);
88+
tsCode = tsCode
89+
.replace(
90+
'opacity={0.2}',
91+
'opacity={duotoneOpacity} fill={duotoneColor}'
92+
)
93+
.replace(
94+
'...props',
95+
`duotoneColor="currentColor",\n duotoneOpacity=0.2,\n ...props`
96+
);
8797
}
8898

8999
// fix icons with small dots (#4)
@@ -182,6 +192,11 @@ function ${componentName}({ weight, color, size, style, mirrored, duotoneColor,
182192
183193
const mirroredValue = mirrored ?? contextMirrored
184194
195+
const duotoneProps =
196+
(weight ?? contextWeight) === 'duotone'
197+
? { duotoneColor: duotoneColor ?? contextDuotoneColor, duotoneOpacity: duotoneOpacity ?? contextDuotoneOpacity }
198+
: undefined
199+
185200
return (
186201
<IconComponent
187202
color={color ?? contextColor}
@@ -195,15 +210,13 @@ function ${componentName}({ weight, color, size, style, mirrored, duotoneColor,
195210
}),
196211
},
197212
]}
198-
duotonecolor={duotoneColor ?? contextDuotoneColor}
199-
duotoneopacity={duotoneOpacity ?? contextDuotoneOpacity}
213+
{...duotoneProps}
200214
{...props}
201215
/>
202216
)
203217
}
204218
205-
export default ${componentName}
206-
`;
219+
export default ${componentName}`;
207220

208221
const filePath = path.join(__dirname, '../src/icons', `${component}.tsx`);
209222

src/lib/index.tsx

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export type IconWeight =
1111

1212
export type PaintFunction = (color: string) => React.ReactNode | null;
1313

14-
interface IconPropsBase {
14+
export interface IconProps {
1515
color?: string;
1616
size?: string | number;
1717
weight?: IconWeight;
@@ -23,16 +23,6 @@ interface IconPropsBase {
2323
title?: string; // SVGRProps
2424
titleId?: string; // SVGRProps
2525
}
26-
export interface IconProps extends IconPropsBase {
27-
duotoneColor?: string;
28-
duotoneOpacity?: number;
29-
}
30-
31-
// Warning: React does not recognize the duotoneColor prop on a DOM element.
32-
export interface DuotoneProps extends IconPropsBase {
33-
duotonecolor?: string;
34-
duotoneopacity?: number;
35-
}
3626

3727
export type Icon = React.FC<IconProps>;
3828

0 commit comments

Comments
 (0)