UI.MD

Swap Button

A button whose icon and label change with a state at one fixed width: every face is stacked in one cell and centred, and the new face fades in.

Installation

$
npx shadcn@latest add https://ui.mattdowney.com/r/swap-button.json

Usage

import { SwapButton } from "@/components/ui/swap-button"

Examples

Default

Three faces in one button: press to use the idea, then to copy. The width is the widest face, and each face sits centred in it, so nothing beside the button moves.

Text only

Faces without icons work the same way. Change the variant with the state when the two faces mean different weights.

API Reference

PropTypeDefaultDescription
statesRecord<string, { icon?: IconProp; label: ReactNode }>-Every face the button can show, keyed by name. All are rendered, so the button is as wide as the widest.
statestring-The key of the face showing now.
...propsButtonProps-Every other Button prop (variant, size, onClick, disabled, className, ...) is forwarded. Icons come from the faces, not iconStart.