Skip to content

fix(createIcon): Fix broken API in createIcon.tsx#12336

Open
tlabaj wants to merge 5 commits intopatternfly:mainfrom
tlabaj:create_icon
Open

fix(createIcon): Fix broken API in createIcon.tsx#12336
tlabaj wants to merge 5 commits intopatternfly:mainfrom
tlabaj:create_icon

Conversation

@tlabaj
Copy link
Copy Markdown
Contributor

@tlabaj tlabaj commented Apr 9, 2026

What: Closes #12328

What was changed:

  • Support both the new shape (CreateIconProps with icon / rhUiIcon) and the legacy flat createIcon({ … svgPath … }) by normalizing at runtime (detect via icon / rhUiIcon keys).
  • Accept svgPath or svgPathData on IconDefinition; resolve to svgPathData internally (svgPathData wins if both are set).
  • Export LegacyFlatIconDefinition as an alias for migration/typing.
  • Tests: Legacy + new createIcon shapes; RH UI (nested swap markup, set prop, missing-map warning).
  • Docs: expanded JSDoc on createIcon (@param / @returns).

Summary by CodeRabbit

  • New Features

    • Icons accept both legacy and modern definition formats for broader compatibility and consistent rendering.
    • Optional mapping supports a dual/SVG swap mode that can render nested or flat SVGs depending on the requested set.
  • Bug Fixes

    • Improved SVG selection and fallback logic; clearer console warnings and a specific error when a required nested icon definition is missing.
  • Tests

    • Added tests for legacy inputs, deprecated nested fields, rh‑ui mapping behaviors, warnings, and error cases.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 9, 2026

Walkthrough

Adds runtime normalization to accept both legacy flat and new nested icon shapes, splits icon type definitions, enforces rendering from svgPathData, updates createIcon signature/normalization, and expands tests for legacy/deprecated inputs, error cases, and rh-ui mapping/fallback behavior.

Changes

Cohort / File(s) Summary
Icon Creation Core
packages/react-icons/src/createIcon.tsx
Reworked icon typings (split base/variants), added IconDefinitionWithSvgPathData/LegacyFlatIconDefinition, changed createIcon to accept `CreateIconProps
Test Coverage
packages/react-icons/src/__tests__/createIcon.test.tsx
Extended tests: assert rendering from svgPathData, add legacy flat { svgPath } and deprecated nested icon.svgPath coverage, add missing-nested-icon error test, and new describe block validating rh-ui mapping, set variants, and warn+fallback behavior.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

Suggested reviewers

  • wise-king-sullyman
  • kmcfaul
  • thatblindgeye
  • mcoker
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: restoring the broken createIcon API to support legacy flat argument shapes alongside new nested CreateIconProps.
Linked Issues check ✅ Passed The PR successfully addresses #12328 by implementing runtime support for both legacy flat createIcon shape and new nested CreateIconProps, restoring compatibility for downstream consumers.
Out of Scope Changes check ✅ Passed All changes are directly scoped to resolving the createIcon API compatibility issue; tests cover legacy/new shapes, RH UI mapping, and error handling as required by #12328.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
⚔️ Resolve merge conflicts
  • Resolve merge conflict in branch create_icon

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@patternfly-build
Copy link
Copy Markdown
Collaborator

patternfly-build commented Apr 9, 2026

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@packages/react-icons/src/createIcon.tsx`:
- Around line 86-99: normalizeCreateIconArg currently allows nested
CreateIconProps to return with icon undefined which later causes invalid SVG
output; update normalizeCreateIconArg (the isNestedCreateIconProps branch) to
validate that p.icon is present (not null/undefined) and throw a clear Error
(e.g., "createIcon: missing default 'icon' for nested props 'name'") if it's
missing before calling normalizeIconDefinition; still normalize rhUiIcon as
before using normalizeIconDefinition when non-null. Ensure the thrown message
references the name (p.name) so callers can identify the bad input.
- Around line 17-30: The public IconDefinition union causes consumers to need
type-narrowing; replace the union by exporting a single interface that extends
IconDefinitionBase and includes both svgPathData and the deprecated svgPath
(marked in JSDoc), then update the exported IconDefinition type to that single
interface; specifically, remove IconDefinitionWithSvgPath |
IconDefinitionWithSvgPathData as the union, keep IconDefinitionWithSvgPathData
and IconDefinitionWithSvgPath (or collapse them) but ensure IconDefinition
refers to the unified interface so helpers can access svgPathData directly and
existing internals like resolveSvgPathData() continue to work without changes.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: d1c895fb-106c-4779-b20e-c9d2064e6bd2

📥 Commits

Reviewing files that changed from the base of the PR and between 911223a and 764d78b.

📒 Files selected for processing (2)
  • packages/react-icons/src/__tests__/createIcon.test.tsx
  • packages/react-icons/src/createIcon.tsx

Comment thread packages/react-icons/src/createIcon.tsx Outdated
Comment thread packages/react-icons/src/createIcon.tsx
Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@packages/react-icons/src/__tests__/createIcon.test.tsx`:
- Around line 1-2: Remove the default React import and the accompanying ESLint
suppression in createIcon.test.tsx: delete the line importing React (the default
identifier "React") and the comment "// eslint-disable-next-line
no-restricted-imports -- test file excluded from package tsconfig; default
import satisfies TS/JSX", since the project uses the automatic JSX runtime and
default React imports are disallowed by the no-restricted-imports rule.
- Around line 198-216: The test 'set="rh-ui" with no rhUiIcon mapping falls back
to default and warns' currently restores the jest.spyOn mock
(warnSpy.mockRestore()) only at the end, which can leak a mocked console.warn if
an assertion throws; wrap the test's execution (render, expects) in a
try-finally and call warnSpy.mockRestore() in the finally block so the spy
created for console.warn is always restored; locate the warnSpy declaration in
this test (const warnSpy = jest.spyOn(console, 'warn').mockImplementation(...))
and move the restore into a finally guarding the render/expect code for
IconNoRhMapping created via createIcon.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: cd163c56-8281-441d-960a-7bf1888bbda2

📥 Commits

Reviewing files that changed from the base of the PR and between 764d78b and 94d8ad5.

📒 Files selected for processing (2)
  • packages/react-icons/src/__tests__/createIcon.test.tsx
  • packages/react-icons/src/createIcon.tsx
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/react-icons/src/createIcon.tsx

Comment thread packages/react-icons/src/__tests__/createIcon.test.tsx Outdated
Comment thread packages/react-icons/src/__tests__/createIcon.test.tsx
@tlabaj tlabaj requested review from a team, kmcfaul and wise-king-sullyman and removed request for a team April 13, 2026 15:28
Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
packages/react-icons/src/createIcon.tsx (1)

199-203: Simplify branch condition and tighten NormalizedCreateIconProps.icon to non-optional.

Two small, related cleanups in this block:

  1. Line 199: (set === undefined && rhUiIcon === null) || set !== undefined reduces to set !== undefined || rhUiIcon === null.
  2. Line 200-201: set !== undefined && set === 'rh-ui' — the first conjunct is redundant; set === 'rh-ui' already implies set !== undefined.
  3. NormalizedCreateIconProps.icon (line 86) is typed optional, but normalizeCreateIconArg always assigns it (nested path throws when null at line 97-101; legacy path always calls normalizeIconDefinition). Making it non-optional lets you drop the iconData ?? ({} as Partial<…>) fallback on line 203 and the | undefined on line 200, keeping the runtime invariant reflected in the types.
♻️ Proposed refactor

On line 86:

-  icon?: IconDefinitionWithSvgPathData;
+  icon: IconDefinitionWithSvgPathData;

On lines 199-203:

-      if ((set === undefined && rhUiIcon === null) || set !== undefined) {
-        const iconData: IconDefinitionWithSvgPathData | undefined =
-          set !== undefined && set === 'rh-ui' && rhUiIcon !== null ? rhUiIcon : icon;
-        const { xOffset, yOffset, width, height, svgPathData, svgClassName } =
-          iconData ?? ({} as Partial<IconDefinitionWithSvgPathData>);
+      if (set !== undefined || rhUiIcon === null) {
+        const iconData: IconDefinitionWithSvgPathData =
+          set === 'rh-ui' && rhUiIcon !== null ? rhUiIcon : icon;
+        const { xOffset, yOffset, width, height, svgPathData, svgClassName } = iconData;

You can then drop the icon && … guard on line 250 as well (still safe, just redundant).

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/react-icons/src/createIcon.tsx` around lines 199 - 203, Simplify the
conditional and tighten the icon type: change the branch `(set === undefined &&
rhUiIcon === null) || set !== undefined` to `set !== undefined || rhUiIcon ===
null`, replace `set !== undefined && set === 'rh-ui'` with `set === 'rh-ui'`,
and make NormalizedCreateIconProps.icon non-optional in normalizeCreateIconArg
so callers always get an IconDefinition; then remove the `| undefined` from the
iconData declaration and drop the fallback `iconData ?? ({} as Partial<...>)` as
well as the redundant `icon && …` guard elsewhere (e.g., the check around svg
rendering).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@packages/react-icons/src/createIcon.tsx`:
- Around line 199-203: Simplify the conditional and tighten the icon type:
change the branch `(set === undefined && rhUiIcon === null) || set !==
undefined` to `set !== undefined || rhUiIcon === null`, replace `set !==
undefined && set === 'rh-ui'` with `set === 'rh-ui'`, and make
NormalizedCreateIconProps.icon non-optional in normalizeCreateIconArg so callers
always get an IconDefinition; then remove the `| undefined` from the iconData
declaration and drop the fallback `iconData ?? ({} as Partial<...>)` as well as
the redundant `icon && …` guard elsewhere (e.g., the check around svg
rendering).

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: c738d752-fb52-452d-97ef-f346e6a210a6

📥 Commits

Reviewing files that changed from the base of the PR and between 94d8ad5 and aea36ea.

📒 Files selected for processing (2)
  • packages/react-icons/src/__tests__/createIcon.test.tsx
  • packages/react-icons/src/createIcon.tsx

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
packages/react-icons/src/createIcon.tsx (1)

198-207: ⚠️ Potential issue | 🟡 Minor

console.warn fires on every render; also simplify redundant conditions.

  1. The warning at Lines 200-202 is inside render(), so every re-render of a parent using <MyIcon set="rh-ui" /> against an icon with no rhUiIcon re-logs the same message — easy to flood the console. Consider gating it with a static dev-only flag on the class (or moving the check to componentDidMount) so it logs at most once per component.
  2. The guard at Line 205 — (set === undefined && rhUiIcon === null) || set !== undefined — reduces to set !== undefined || rhUiIcon === null.
  3. At Line 207, set !== undefined && set === 'rh-ui' is equivalent to just set === 'rh-ui'.
Proposed readability tweaks (warn dedup left to your discretion)
-      if ((set === undefined && rhUiIcon === null) || set !== undefined) {
+      if (set !== undefined || rhUiIcon === null) {
         const iconData: IconDefinitionWithSvgPathData | undefined =
-          set !== undefined && set === 'rh-ui' && rhUiIcon !== null ? rhUiIcon : icon;
+          set === 'rh-ui' && rhUiIcon !== null ? rhUiIcon : icon;
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/react-icons/src/createIcon.tsx` around lines 198 - 207, The
console.warn inside render() for when set === 'rh-ui' and rhUiIcon === null
causes repeated logs on every render; move that warning to componentDidMount (or
gate it with a static dev-only seen flag on the component) so it runs at most
once per mounted component, and remove the eslint-disable comment. Also simplify
the render guard expression by replacing "(set === undefined && rhUiIcon ===
null) || set !== undefined" with "set !== undefined || rhUiIcon === null", and
replace the ternary condition "set !== undefined && set === 'rh-ui'" with simply
"set === 'rh-ui'" when computing iconData (refer to render(), rhUiIcon, set,
icon, and the IconDefinitionWithSvgPathData assignment).
🧹 Nitpick comments (2)
packages/react-icons/src/createIcon.tsx (2)

122-152: Consolidate duplicated path/viewBox rendering with the single-SVG branch.

Lines 124-150 and the inline single-SVG branch at Lines 208-225 compute the same viewBox, svgClassName merge, and svgPaths array/string rendering. Extracting a shared helper (e.g. buildSvgInner(icon) returning { viewBox, svgClassName, svgPaths }) would remove the duplication and keep future fixes (e.g., accessibility or keying tweaks) in one place.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/react-icons/src/createIcon.tsx` around lines 122 - 152, The
createSvg implementation duplicates viewBox, className merging, and svgPaths
construction that are also used in the single-SVG rendering branch; extract that
shared logic into a new helper (e.g., buildSvgInner(icon)) which returns {
viewBox, svgClassName, svgPaths } and have both createSvg and the single-SVG
render call buildSvgInner instead of duplicating the code; ensure the helper
uses the same symbol names (xOffset, yOffset, width, height, svgPathData,
svgClassName) and preserves path keying/className behavior so both createSvg and
the single-SVG branch reuse identical viewBox, class merging, and path
rendering.

89-120: Tighten NormalizedCreateIconProps.icon to required.

Both branches of normalizeCreateIconArg guarantee icon is set (legacy always normalizes arg; nested throws if missing). Making icon required on the internal type removes the need for the iconData ?? ({} as Partial<…>) fallback at Line 209 and the icon ?? {} at Line 124, and eliminates the | undefined on iconData at Line 206.

Proposed type tightening
 interface NormalizedCreateIconProps {
   name?: string;
-  icon?: IconDefinitionWithSvgPathData;
+  icon: IconDefinitionWithSvgPathData;
   rhUiIcon: IconDefinitionWithSvgPathData | null;
 }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/react-icons/src/createIcon.tsx` around lines 89 - 120, The
NormalizedCreateIconProps.interface currently marks `icon` optional but both
branches of `normalizeCreateIconArg` always provide a normalized icon; update
the interface so `icon` is required (change `icon?:
IconDefinitionWithSvgPathData` to `icon: IconDefinitionWithSvgPathData`) and set
`rhUiIcon: IconDefinitionWithSvgPathData | null` remains; then update any
callers that previously used `icon ?? {}` or `iconData ?? ({} as Partial<...>)`
(references: `normalizeCreateIconArg`, usages around the former `icon ?? {}` and
`iconData` handling) to rely on the now-required `icon` without fallback,
removing the `| undefined` from `iconData` types and the fallback expressions.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@packages/react-icons/src/createIcon.tsx`:
- Around line 198-207: The console.warn inside render() for when set === 'rh-ui'
and rhUiIcon === null causes repeated logs on every render; move that warning to
componentDidMount (or gate it with a static dev-only seen flag on the component)
so it runs at most once per mounted component, and remove the eslint-disable
comment. Also simplify the render guard expression by replacing "(set ===
undefined && rhUiIcon === null) || set !== undefined" with "set !== undefined ||
rhUiIcon === null", and replace the ternary condition "set !== undefined && set
=== 'rh-ui'" with simply "set === 'rh-ui'" when computing iconData (refer to
render(), rhUiIcon, set, icon, and the IconDefinitionWithSvgPathData
assignment).

---

Nitpick comments:
In `@packages/react-icons/src/createIcon.tsx`:
- Around line 122-152: The createSvg implementation duplicates viewBox,
className merging, and svgPaths construction that are also used in the
single-SVG rendering branch; extract that shared logic into a new helper (e.g.,
buildSvgInner(icon)) which returns { viewBox, svgClassName, svgPaths } and have
both createSvg and the single-SVG render call buildSvgInner instead of
duplicating the code; ensure the helper uses the same symbol names (xOffset,
yOffset, width, height, svgPathData, svgClassName) and preserves path
keying/className behavior so both createSvg and the single-SVG branch reuse
identical viewBox, class merging, and path rendering.
- Around line 89-120: The NormalizedCreateIconProps.interface currently marks
`icon` optional but both branches of `normalizeCreateIconArg` always provide a
normalized icon; update the interface so `icon` is required (change `icon?:
IconDefinitionWithSvgPathData` to `icon: IconDefinitionWithSvgPathData`) and set
`rhUiIcon: IconDefinitionWithSvgPathData | null` remains; then update any
callers that previously used `icon ?? {}` or `iconData ?? ({} as Partial<...>)`
(references: `normalizeCreateIconArg`, usages around the former `icon ?? {}` and
`iconData` handling) to rely on the now-required `icon` without fallback,
removing the `| undefined` from `iconData` types and the fallback expressions.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 991b1b0d-13d1-4420-84cb-4800498423f9

📥 Commits

Reviewing files that changed from the base of the PR and between aea36ea and a68833b.

📒 Files selected for processing (1)
  • packages/react-icons/src/createIcon.tsx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Icons - revert createIcon API changes

2 participants