Mantle Item Icon Prop Migration Guide
@ngrok/mantle adds an icon prop to Sidebar.ItemButton, Sidebar.DisclosureTrigger, DropdownMenu.Item, DropdownMenu.CheckboxItem, DropdownMenu.RadioItem, DropdownMenu.SubTrigger, Command.Item, Combobox.Item, MultiSelect.Item, and Tabs.Trigger. The prop has the same contract as Button's icon: the part renders the icon before its label, with size-5, shrink-0, and a gap, and a size class on the icon element wins. An icon you pass as a child still renders, so nothing breaks. This guide moves a leading icon child into the prop.
TL;DR: Where one of these parts has an icon as its first child, followed by a label, remove the icon child and pass it as icon={<GearIcon />}. For mantle's <Icon svg={<GearIcon />} /> with no other prop, pass icon={<GearIcon />}. On an asChild part, the icon is the first child of your slotted element: move it to the part, not to the element.
Codemod
From a mantle checkout:
In a project that installs @ngrok/mantle:
The rules apply steps 2 to 4. An icon is a self-closing Phosphor icon. The rules read its value import from @phosphor-icons/react, @phosphor-icons/react/ssr, a per-icon subpath such as @phosphor-icons/react/Gear, or a dist/csr/<Name> or dist/ssr/<Name> path. The export name decides, so import { GearIcon as Settings } moves <Settings />. A namespace import counts too (<Phosphor.GearIcon />). A type-only import binds no component, so it does not count. Mantle's bare <Icon svg={…} /> counts too. The rules move it when nothing visible comes before it in the part (whitespace does not count) and text or an element follows it, or when it is the first child of the one element under an asChild or asChild={true} part. They also remove the space or the {" "} that separated the icon from the label. A bare <Icon svg={<X />} /> from @ngrok/mantle/icon, under its own name or an alias, gives up its svg. When the element under an asChild part is one of these parts too, only that inner part takes the icon. The rules read a part through its import from @ngrok/mantle/<subpath>, under its own name or an alias. These stay manual:
- A part that already has an
iconprop, because the two icons need a choice. - A part with a spread, because the spread can carry
iconorchildren. - A part with a dynamic
asChild={value}, because the element that holds the icon is known only at runtime. - A lone icon with no label. That item is icon-only: give it an accessible name, and leave the icon where it is.
- An icon that is not the first child, such as a trailing caret, or an icon inside a wrapper of your own, such as
<span className="flex gap-2">. - A mantle
Iconwith any prop other thansvg, such asclassName,aria-label, or a spread, because its props need a choice: keep them on the svg or drop them. - An icon with its own
data-slot,aria-label,aria-labelledby,aria-describedby,alt,title, orrole, or with a spread such as<GearIcon {...props} />, which can carry one of these. The part sets its owndata-sloton the icon, so the codemod leaves these alone to stay conservative.Iconkeeps a named svg's accessible name, so moving a named icon by hand is safe. An icon with onlyaria-describedbyhas no name, soIconhides it: keep it as a child. - An icon after something visible, such as text, a character reference like
×, or an expression. - An icon whose label is only an expression, such as
<GearIcon />{displayName}, or only a{" "}. After the move, the expression is the part's wholechildren. When it isnullorundefined, the part has no children, and a part such asCombobox.Itemshows itsvalueinstead. Before the move, the icon keptchildrennon-empty, so no fallback showed. Move the icon by hand when the value is never empty, or keep a text or element label beside it. - The
Iconimport from@ngrok/mantle/icon. When the rules move the last bare<Icon svg={…} />in a file, the import stays and is unused. Remove it if nothing else uses it; your linter ornoUnusedLocalsnames it. - A Phosphor icon in a file that uses
IconContextfrom@phosphor-icons/react. A provider can give every icon below it aroleor anaria-label, and the rules cannot see which icons it reaches. The part wraps itsiconin mantle'sIcon, which hides the icon from screen readers when it sees no name at the call site. Move an icon by hand when the provider gives it no name, or pass the name at the call site. A bare<Icon svg={…} />in that file still moves, because it already wraps the svg in the sameIcon. - An icon component that is not from
@phosphor-icons/react, such as a localStatusIcon, an icon from./icons, or an icon from another package. The part wraps itsiconin mantle'sIcon, which hides the icon from screen readers when it sees no name at the call site. A component can set its own name inside, such as<svg role="img" aria-label="Unsaved changes" />, and the codemod cannot see that. Move such an icon by hand when it is decorative; when it names itself, pass the name at the call site, such asicon={<StatusIcon aria-label="Unsaved changes" />}, or keep it as a child. - An icon with children, such as
<GearIcon>…</GearIcon>, and an icon behind a condition, such as{busy ? <SpinnerIcon /> : <GearIcon />}. - A part that reaches the file through a local re-export or a destructured namespace, and a function or block that binds the namespace name to a parameter, a destructured name, a local, a
for … ofvariable, or acatchparameter, which shadows the import. Avaranywhere in a function, such asfor (var Menu of menus), shadows the import in the whole function. - Step 5, because a class string is not JSX syntax.
The codemod is safe to run twice: a part that took the prop has an icon, so a second run skips it. Pass --dry-run to the mantle command, or leave out --update-all from the ast-grep command, to print the rewrites first. Run your formatter after the rewrite.
You are an agent migrating a codebase. Do this:
- Find every
Sidebar.ItemButton,Sidebar.DisclosureTrigger,DropdownMenu.Item,DropdownMenu.CheckboxItem,DropdownMenu.RadioItem,DropdownMenu.SubTrigger,Command.Item,Combobox.Item,MultiSelect.Item, andTabs.Trigger. Search for each name, and for a renamed import of its namespace (import { DropdownMenu as Menu }). Leave a part from another library alone. - Where the part's first child is an icon element and a label follows it, remove the icon child and add it as
icon={…}on the part. Keep the icon's props:<GearIcon className="size-4" />becomesicon={<GearIcon className="size-4" />}. Remove a space or a{" "}between the icon and the label. When the label is only an expression, such as{displayName}, check that it is nevernullor empty first: with the icon gone, an emptychildrenlets a part such asCombobox.Itemshow itsvalueinstead. - Where the icon is mantle's
<Icon svg={<GearIcon />} />with no other prop, pass the svg alone:icon={<GearIcon />}. Where theIconhas only a style prop, such asclassName, move the class onto the svg:icon={<GearIcon className="text-danger-600" />}. Where it carries a name (aria-label,title), keep the name on the svg:icon={<GearIcon aria-label="Settings" />}. After the move, remove theIconimport if nothing else uses it; your linter ornoUnusedLocalsnames it. - Where the part has
asChild, look at the first child of the slotted element (<Link>,<a>). Move that icon to the part'siconprop. The part renders it inside the slotted element. When the slotted element is one of these parts too, such as aSidebar.ItemButtonunderDropdownMenu.Item asChild, give the icon to the inner part only. Twoiconprops render two icons. - Delete classes that only placed a child icon: a
gap-*on the part that spaced the icon from the label, and a[&>svg]or[&_svg]size class on the part. The part now sizes and spaces the icon. - Leave these alone: a part that already has
icon, a lone icon with no label (an icon-only item), and an icon that is not the first child. An icon with its owndata-slotkeeps that slot only as a child, so leave it too. - After editing, run the project's formatter, linter, type checker, and tests.
Why a prop? A child icon is a sibling of the label, so it gets no size, no
shrink-0, and no gap from the part, and icons of different items fail to line up. A browser translation engine can also move bare label text, and an icon that comes and goes beside that text breaks React's next update. The part renders theiconbefore its own label span, so the layout is the same in each item and the DOM stays safe.
Before → after
An item with a leading icon
A bare mantle Icon
An asChild part
An aliased import
Edge cases & gotchas
- An icon-only item. A part with one icon and no label stays as it is. Give it an
aria-label, or a visually hidden label. - A trailing icon. An icon after the label, such as a caret or an external-link mark, is not the leading icon. Leave it as a child.
- A size class on the icon. It still wins over the part's
size-5, soicon={<GearIcon className="size-4" />}renders atsize-4. - A gap or an svg size class on the part. It spaced or sized a child icon. The part now does that, so delete the class (step 5).
- An
IconContextprovider. The codemod skips Phosphor icons in a file that usesIconContext. Check what the provider sets before you move an icon: aroleor anaria-labelfrom the provider does not reach mantle'sIcon, so it hides the icon. - An icon component of your own. The codemod moves only Phosphor icons and a bare mantle
Icon. Check each other*Iconbefore you move it: a component that sets its ownaria-labelinside is hidden by the part'sIconunless the call site names it. - A named or described icon. The codemod skips an icon with
aria-label,aria-labelledby,aria-describedby,alt,title, orrole, to stay conservative.Iconkeeps a named svg's accessible name, so you can move a named icon toiconby hand. Keep an icon with onlyaria-describedbyas a child: it has no name, soIconhides it. - An unused
Iconimport. The codemod does not remove the import from@ngrok/mantle/iconafter it moves the last bareIcon. Your linter ornoUnusedLocalsnames it. - An
asChildpart around another part. Only the inner part takes the icon. Aniconon both renders two svgs in one row. - A label that is only an expression. The codemod skips
<GearIcon />{displayName}. An emptydisplayNameleaves the part with no children after the move, and a part that falls back to itsvaluethen shows the value. Move the icon by hand only when the expression always renders text. - A conditional icon.
{loading ? <SpinnerIcon /> : <GearIcon />}moves by hand:icon={loading ? <SpinnerIcon /> : <GearIcon />}.
How to verify
- Type check.
iconis aReactNode, so a moved element always type checks. - Load a menu, a palette, a combobox, a sidebar, and a set of tabs that you changed. Each icon sits before its label at the same size, and the icons of all items line up.
- Search for
Icon />directly after the opening tag of one of the parts. The remaining hits are manual shapes from the## Codemodlist. - Run the project's formatter, linter, type checker, and tests.