Blur Action Swap

Labels and icons that blur out and back in, like Copy turning to Copied.

Preview

"use client";

import { Check, Copy, Moon, Sun } from "lucide-react";
import { useState } from "react";
import {
  ActionSwapBlurButton,
  type ActionSwapItem,
} from "@/components/motion/action-swap-blur";

const TEXT_ITEMS: ActionSwapItem[] = [
  { id: "copy", label: "Copy" },
  { id: "copied", label: "Copied" },
];

const ICON_ITEMS: ActionSwapItem[] = [
  {
    id: "light",
    label: "Light",
    icon: <Sun className="h-4 w-4" />,
    ariaLabel: "Use light theme",
  },
  {
    id: "dark",
    label: "Dark",
    icon: <Moon className="h-4 w-4" />,
    ariaLabel: "Use dark theme",
  },
];

const CTA_ITEMS: ActionSwapItem[] = [
  {
    id: "copy",
    label: "Copy link",
    icon: <Copy className="h-4 w-4" />,
    ariaLabel: "Copy link",
  },
  {
    id: "copied",
    label: "Copied",
    icon: <Check className="h-4 w-4" />,
    ariaLabel: "Copied",
  },
];

export function ActionSwapBlurPreview() {
  const [textValue, setTextValue] = useState(TEXT_ITEMS[0]?.id);
  const [iconValue, setIconValue] = useState(ICON_ITEMS[0]?.id);
  const [ctaValue, setCtaValue] = useState(CTA_ITEMS[0]?.id);

  return (
    <div className="flex flex-wrap items-center justify-center gap-3">
      <ActionSwapBlurButton
        items={TEXT_ITEMS}
        value={textValue}
        onValueChange={setTextValue}
        variant="secondary"
      />
      <ActionSwapBlurButton
        items={ICON_ITEMS}
        value={iconValue}
        onValueChange={setIconValue}
        variant="outline"
        size="icon"
        iconOnly
      />
      <ActionSwapBlurButton
        items={CTA_ITEMS}
        value={ctaValue}
        onValueChange={setCtaValue}
        variant="primary"
      />
    </div>
  );
}

Install

Add it with the shadcn CLI, or copy the source manually.

$ bunx --bun shadcn add @beui/action-swap-blur

API Reference

ActionSwapBlurButton

defaultValue?
string

The initial item id when uncontrolled. Default: the first item.

Default—
className?
string

Classes merged onto the root element.

Default—
size?
"sm" | "md" | "lg" | "icon"

Button height and padding. Default: "md"

Default—
value?
string

The id of the shown item, for controlled use.

Default—
items
ActionSwapItem[]

States the button steps through, each with a label and optional icon.

Default—
onValueChange?
((value: string, item: ActionSwapItem) => void)

Called with the next item's id and the item when a press advances it.

Default—
variant?
"outline" | "ghost" | "primary" | "secondary"

Visual style of the button. Default: "secondary"

Default—
iconOnly?
boolean

Show only the icon and use the label as the accessible name. Default: true for size "icon"

Default—
cycle?
boolean

Advance to the next item on each press, wrapping at the end. Default: true

Default—

ActionSwapBlurText

className?
string

Classes merged onto the root element.

Default—
value
string

Key for the current text; a new value plays the swap.

Default—

ActionSwapBlurIcon

className?
string

Classes merged onto the root element.

Default—
value
string

Key for the current icon; a new value plays the swap.

Default—

More in Action Swap

Updated