Arrow fill button
A pill link or button whose label sits beside a small solid disc carrying an arrow. On hover or keyboard focus the circle's colour sweeps across the whole pill, the label turns to the contrast colour and the arrow nudges forward; on press the pill gives slightly. External links point the arrow north-east, open in a new tab and say so to screen readers. Renders a link when given href and a button otherwise, with three sizes, two tones, an arrow at either end and a held-open filled state. The sweep becomes an instant colour swap when reduced motion is requested.
Source
import type { AnchorHTMLAttributes, ButtonHTMLAttributes, CSSProperties, ReactNode } from "react";
import { ArrowRight } from "lucide-react";
import { cn } from "@/lib/utils";
type Size = "sm" | "md" | "lg";
type Tone = "primary" | "foreground";
type BaseProps = {
children: ReactNode;
size?: Size;
tone?: Tone;
/** Which end holds the arrow circle; the fill sweeps from that side. */
iconPosition?: "end" | "start";
/** Opens in a new tab: the arrow points north-east and screen readers hear "(opens in new tab)". */
external?: boolean;
/** Keeps the fill on, for example for the current item in a set. */
filled?: boolean;
className?: string;
};
type AsButton = BaseProps & Omit<ButtonHTMLAttributes<HTMLButtonElement>, keyof BaseProps> & { href?: undefined };
type AsAnchor = BaseProps & Omit<AnchorHTMLAttributes<HTMLAnchorElement>, keyof BaseProps> & { href: string };
export type FillArrowButtonProps = AsButton | AsAnchor;
// Circle diameter and the gap around it, per size.
const SIZES: Record<Size, { d: number; p: number; text: string; pad: string; icon: string }> = {
sm: { d: 24, p: 4, text: "text-sm", pad: "16px", icon: "size-3.5" },
md: { d: 32, p: 5, text: "text-sm", pad: "20px", icon: "size-4" },
lg: { d: 40, p: 6, text: "text-base", pad: "26px", icon: "size-[18px]" },
};
const CSS = `.chm-fab{--fab-fill:var(--primary);--fab-on:var(--primary-foreground);height:calc(var(--fab-d) + 2 * var(--fab-p))}
.chm-fab[data-tone=foreground]{--fab-fill:var(--foreground);--fab-on:var(--background)}
.chm-fab__fill{position:absolute;inset:0;border-radius:inherit;background:var(--fab-fill);clip-path:inset(var(--fab-p) var(--fab-p) var(--fab-p) calc(100% - var(--fab-p) - var(--fab-d)) round 9999px)}
.chm-fab[data-side=start] .chm-fab__fill{clip-path:inset(var(--fab-p) calc(100% - var(--fab-p) - var(--fab-d)) var(--fab-p) var(--fab-p) round 9999px)}
.chm-fab__icon{color:var(--fab-on)}
.chm-fab__arrow{transform:translateX(0)}
.chm-fab[data-external] .chm-fab__arrow{transform:rotate(-45deg)}
.chm-fab:is(:hover,:focus-visible,[data-filled]):not(:disabled,[aria-disabled=true]) .chm-fab__fill{clip-path:inset(0 round 9999px)}
.chm-fab:is(:hover,:focus-visible,[data-filled]):not(:disabled,[aria-disabled=true]) .chm-fab__label{color:var(--fab-on)}
.chm-fab:is(:hover,:focus-visible):not(:disabled,[aria-disabled=true]) .chm-fab__arrow{transform:translateX(3px)}
.chm-fab[data-external]:is(:hover,:focus-visible):not(:disabled,[aria-disabled=true]) .chm-fab__arrow{transform:translate(2px,-2px) rotate(-45deg)}
.chm-fab:active:not(:disabled,[aria-disabled=true]){transform:scale(.97)}
@media (prefers-reduced-motion:no-preference){
.chm-fab{transition:transform .15s ease}
.chm-fab__fill{transition:clip-path .45s cubic-bezier(.65,0,.25,1)}
.chm-fab__label{transition:color .3s ease .05s}
.chm-fab__arrow{transition:transform .3s cubic-bezier(.3,1.4,.5,1)}
}`;
export function FillArrowButton(props: FillArrowButtonProps) {
const {
children, size = "md", tone = "primary", iconPosition = "end", external = false, filled = false, className, style, ...rest
} = props;
const s = SIZES[size];
const start = iconPosition === "start";
const classes = cn(
"chm-fab group relative isolate inline-flex select-none items-center gap-3 overflow-hidden rounded-full",
"bg-card font-medium text-foreground shadow-sm ring-1 ring-border ring-inset",
"outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background",
"disabled:cursor-not-allowed disabled:opacity-50 aria-disabled:cursor-not-allowed aria-disabled:opacity-50",
start && "flex-row-reverse",
s.text,
className,
);
const vars = {
"--fab-d": `${s.d}px`,
"--fab-p": `${s.p}px`,
[start ? "paddingRight" : "paddingLeft"]: s.pad,
[start ? "paddingLeft" : "paddingRight"]: `${s.p}px`,
...style,
} as CSSProperties;
const data = {
"data-tone": tone,
"data-side": iconPosition,
"data-external": external ? "" : undefined,
"data-filled": filled ? "" : undefined,
};
const inner = (
<>
<style href="chm-fill-arrow-button" precedence="default">{CSS}</style>
<span aria-hidden="true" className="chm-fab__fill" />
<span className="chm-fab__label relative whitespace-nowrap">
{children}
{external && <span className="sr-only"> (opens in new tab)</span>}
</span>
<span
aria-hidden="true"
className="chm-fab__icon relative grid shrink-0 place-items-center rounded-full"
style={{ width: s.d, height: s.d }}
>
<ArrowRight className={cn("chm-fab__arrow", s.icon)} strokeWidth={2.25} />
</span>
</>
);
if (rest.href !== undefined) {
const anchor = rest as AnchorHTMLAttributes<HTMLAnchorElement>;
return (
<a
{...anchor}
{...data}
className={classes}
style={vars}
target={external ? "_blank" : anchor.target}
rel={external ? cn("noopener noreferrer", anchor.rel) : anchor.rel}
>
{inner}
</a>
);
}
const button = rest as ButtonHTMLAttributes<HTMLButtonElement>;
return (
<button type="button" {...button} {...data} className={classes} style={vars}>
{inner}
</button>
);
}
