Skip to content

Commit 8a5b109

Browse files
committed
more fixes from review comments
1 parent 68b1e2b commit 8a5b109

1 file changed

Lines changed: 27 additions & 28 deletions

File tree

packages/react-icons/src/createIcon.tsx

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ export interface IconDefinitionBase {
1515
}
1616

1717
/**
18-
* SVG path content for one icon variant (default or rh-ui). At runtime at least one of
19-
* `svgPathData` or `svgPath` must be set; if both are present, `svgPathData` is used.
18+
* On-disk / nested icon data: `svgPathData` (preferred) or deprecated `svgPath` (at least one is required at
19+
* runtime for rendering; if both are set, `svgPathData` takes precedence in {@link resolveSvgPathData}).
2020
*/
2121
export interface IconDefinition extends IconDefinitionBase {
2222
svgPathData?: string | SVGPathObject[];
@@ -26,18 +26,15 @@ export interface IconDefinition extends IconDefinitionBase {
2626
svgPath?: string | SVGPathObject[];
2727
}
2828

29-
/** Narrows {@link IconDefinition} to the preferred shape with required `svgPathData`. */
30-
export type IconDefinitionWithSvgPathData = Required<Pick<IconDefinition, 'svgPathData'>> & IconDefinition;
31-
3229
/**
33-
* @deprecated Use {@link IconDefinition} with `svgPathData` instead.
34-
* Narrows {@link IconDefinition} to the legacy shape with required `svgPath`.
30+
* {@link createIconBase} and rendering use this after {@link resolveSvgPathData} — not part of the public
31+
* type surface.
3532
*/
36-
export type IconDefinitionWithSvgPath = Required<Pick<IconDefinition, 'svgPath'>> & IconDefinition;
33+
type NormalizedIconDefinition = Required<Pick<IconDefinition, 'svgPathData'>> & IconDefinition;
3734

3835
/**
39-
* Props for {@link createIconBase} — nested icon definition(s). Used by generated icons and callers
40-
* that already structure data as `{ icon, rhUiIcon? }`.
36+
* Nested (current) public API: `{ icon, rhUiIcon?, name? }` as produced by the icon generator and
37+
* `createIconBase` consumers.
4138
*/
4239
export interface CreateIconBaseProps {
4340
name?: string;
@@ -46,15 +43,8 @@ export interface CreateIconBaseProps {
4643
}
4744

4845
/**
49-
* @deprecated Use {@link CreateIconBaseProps} instead.
50-
*/
51-
export type CreateIconProps = CreateIconBaseProps;
52-
53-
/**
54-
* The **flat (legacy) public API** for {@link createIcon}: the same path/view fields as
55-
* {@link IconDefinition} at the top level, plus optional `rhUiIcon` for the dual-`set` layout.
56-
* `createIcon` is the only entry that accepts this shape; it maps to {@link CreateIconBaseProps} and calls
57-
* {@link createIconBase} (see {@link createIcon}).
46+
* **Flat (legacy) public API** for {@link createIcon} only — not an alias of {@link IconDefinition} so
47+
* the legacy shape is obvious at the call site. `createIcon` maps this to {@link CreateIconBaseProps}.
5848
*/
5949
export interface CreateIconLegacyProps {
6050
name?: string;
@@ -96,8 +86,8 @@ function resolveSvgPathData(icon: IconDefinition): string | SVGPathObject[] {
9686
throw new Error('@patternfly/react-icons: IconDefinition must define svgPathData or svgPath');
9787
}
9888

99-
/** Produces a single {@link IconDefinitionWithSvgPathData} for internal rendering. */
100-
function normalizeIconDefinition(icon: IconDefinition): IconDefinitionWithSvgPathData {
89+
/** Produces a single {@link NormalizedIconDefinition} for internal rendering. */
90+
function normalizeIconDefinition(icon: IconDefinition): NormalizedIconDefinition {
10191
return {
10292
name: icon.name,
10393
width: icon.width,
@@ -121,7 +111,7 @@ function pathElementsFromResolvedData(svgPathData: string | SVGPathObject[]): Re
121111
}
122112

123113
/** Renders an inner `<svg>` with viewBox and path(s) for the dual-SVG (CSS swap) layout. */
124-
const createSvg = (icon: IconDefinitionWithSvgPathData, iconClassName: string) => {
114+
const createSvg = (icon: NormalizedIconDefinition, iconClassName: string) => {
125115
const { xOffset, yOffset, width, height, svgPathData, svgClassName } = icon;
126116
const _xOffset = xOffset ?? 0;
127117
const _yOffset = yOffset ?? 0;
@@ -206,7 +196,7 @@ export function createIconBase({
206196
}
207197

208198
if (set !== undefined || normalizedRhUiIcon === null) {
209-
const iconData: IconDefinitionWithSvgPathData =
199+
const iconData: NormalizedIconDefinition =
210200
set === 'rh-ui' && normalizedRhUiIcon !== null ? normalizedRhUiIcon : normalizedIcon;
211201
const { xOffset, yOffset, width, height, svgPathData, svgClassName } = iconData;
212202
const _xOffset = xOffset ?? 0;
@@ -257,12 +247,21 @@ export function createIconBase({
257247
}
258248

259249
/**
260-
* Legacy **flat** factory: maps {@link CreateIconLegacyProps} to {@link CreateIconBaseProps} and calls
261-
* {@link createIconBase} (all remapping lives here; `createIconBase` only implements rendering). For the
262-
* nested `icon` / `rhUiIcon` shape, use {@link createIconBase} directly.
250+
* Flat **legacy** entry point: turn {@link CreateIconLegacyProps} into a nested
251+
* `icon: IconDefinition` with `svgPathData` resolved, then call {@link createIconBase} (all legacy mapping
252+
* lives in this function). Prefer {@link createIconBase} for the nested `icon` / `rhUiIcon` shape.
263253
*/
264254
export function createIcon(legacy: CreateIconLegacyProps): React.ComponentClass<SVGIconProps> {
265-
const { rhUiIcon = null, ...iconFields } = legacy;
266-
const icon: IconDefinition = iconFields;
255+
const { rhUiIcon = null, ...flat } = legacy;
256+
const icon: IconDefinition = {
257+
name: flat.name,
258+
width: flat.width,
259+
height: flat.height,
260+
xOffset: flat.xOffset,
261+
yOffset: flat.yOffset,
262+
svgClassName: flat.svgClassName,
263+
// Fold deprecated svgPath (and de-dupe vs svgPathData) in one place, then omit svgPath in the object.
264+
svgPathData: resolveSvgPathData(flat as IconDefinition)
265+
};
267266
return createIconBase({ name: icon.name, icon, rhUiIcon });
268267
}

0 commit comments

Comments
 (0)