Skip to main content

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:

pnpm -w run migrate item-icon-prop <path>

In a project that installs @ngrok/mantle:

npx --yes --package=@ast-grep/cli@0.51.3 ast-grep scan --rule node_modules/@ngrok/mantle/dist/migrations/item-icon-prop.yml --update-all <path>

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 icon prop, because the two icons need a choice.
  • A part with a spread, because the spread can carry icon or children.
  • 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 Icon with any prop other than svg, such as className, 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, or role, or with a spread such as <GearIcon {...props} />, which can carry one of these. The part sets its own data-slot on the icon, so the codemod leaves these alone to stay conservative. Icon keeps a named svg's accessible name, so moving a named icon by hand is safe. An icon with only aria-describedby has no name, so Icon hides it: keep it as a child.
  • An icon after something visible, such as text, a character reference like &times;, 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 whole children. When it is null or undefined, the part has no children, and a part such as Combobox.Item shows its value instead. Before the move, the icon kept children non-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 Icon import 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 or noUnusedLocals names it.
  • A Phosphor icon in a file that uses IconContext from @phosphor-icons/react. A provider can give every icon below it a role or an aria-label, and the rules cannot see which icons it reaches. The part wraps its icon in mantle's Icon, 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 same Icon.
  • An icon component that is not from @phosphor-icons/react, such as a local StatusIcon, an icon from ./icons, or an icon from another package. The part wraps its icon in mantle's Icon, 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 as icon={<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 … of variable, or a catch parameter, which shadows the import. A var anywhere in a function, such as for (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:

  1. Find every Sidebar.ItemButton, Sidebar.DisclosureTrigger, DropdownMenu.Item, DropdownMenu.CheckboxItem, DropdownMenu.RadioItem, DropdownMenu.SubTrigger, Command.Item, Combobox.Item, MultiSelect.Item, and Tabs.Trigger. Search for each name, and for a renamed import of its namespace (import { DropdownMenu as Menu }). Leave a part from another library alone.
  2. 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" /> becomes icon={<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 never null or empty first: with the icon gone, an empty children lets a part such as Combobox.Item show its value instead.
  3. Where the icon is mantle's <Icon svg={<GearIcon />} /> with no other prop, pass the svg alone: icon={<GearIcon />}. Where the Icon has only a style prop, such as className, 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 the Icon import if nothing else uses it; your linter or noUnusedLocals names it.
  4. Where the part has asChild, look at the first child of the slotted element (<Link>, <a>). Move that icon to the part's icon prop. The part renders it inside the slotted element. When the slotted element is one of these parts too, such as a Sidebar.ItemButton under DropdownMenu.Item asChild, give the icon to the inner part only. Two icon props render two icons.
  5. 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.
  6. 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 own data-slot keeps that slot only as a child, so leave it too.
  7. 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 the icon before 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

// BEFORE
<DropdownMenu.Item onSelect={openSettings}>
	<GearIcon /> Settings
</DropdownMenu.Item>
// AFTER
<DropdownMenu.Item icon={<GearIcon />} onSelect={openSettings}>
	Settings
</DropdownMenu.Item>

A bare mantle Icon

// BEFORE
<Combobox.Item value="settings">
	<Icon svg={<GearIcon />} />
	Settings
</Combobox.Item>
// AFTER: the part wraps the svg in its own Icon
<Combobox.Item icon={<GearIcon />} value="settings">
	Settings
</Combobox.Item>

An asChild part

// BEFORE: the icon is the first child of the router link
<Sidebar.ItemButton asChild>
	<Link to="/settings">
		<GearIcon />
		Settings
	</Link>
</Sidebar.ItemButton>
// AFTER: the part renders the icon inside the link
<Sidebar.ItemButton asChild icon={<GearIcon />}>
	<Link to="/settings">Settings</Link>
</Sidebar.ItemButton>

An aliased import

// BEFORE
import { DropdownMenu as Menu } from "@ngrok/mantle/dropdown-menu";
 
<Menu.SubTrigger>
	<FolderIcon />
	Move to
</Menu.SubTrigger>;
// AFTER
import { DropdownMenu as Menu } from "@ngrok/mantle/dropdown-menu";
 
<Menu.SubTrigger icon={<FolderIcon />}>Move to</Menu.SubTrigger>;

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, so icon={<GearIcon className="size-4" />} renders at size-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 IconContext provider. The codemod skips Phosphor icons in a file that uses IconContext. Check what the provider sets before you move an icon: a role or an aria-label from the provider does not reach mantle's Icon, 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 *Icon before you move it: a component that sets its own aria-label inside is hidden by the part's Icon unless the call site names it.
  • A named or described icon. The codemod skips an icon with aria-label, aria-labelledby, aria-describedby, alt, title, or role, to stay conservative. Icon keeps a named svg's accessible name, so you can move a named icon to icon by hand. Keep an icon with only aria-describedby as a child: it has no name, so Icon hides it.
  • An unused Icon import. The codemod does not remove the import from @ngrok/mantle/icon after it moves the last bare Icon. Your linter or noUnusedLocals names it.
  • An asChild part around another part. Only the inner part takes the icon. An icon on both renders two svgs in one row.
  • A label that is only an expression. The codemod skips <GearIcon />{displayName}. An empty displayName leaves the part with no children after the move, and a part that falls back to its value then 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

  1. Type check. icon is a ReactNode, so a moved element always type checks.
  2. 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.
  3. Search for Icon /> directly after the opening tag of one of the parts. The remaining hits are manual shapes from the ## Codemod list.
  4. Run the project's formatter, linter, type checker, and tests.