Component
Button With Icon
A labeled button with a leading or trailing icon. Icons use the shared size token for the chosen button size, with the standard control gap keeping icon and label optically aligned.
- Component
- React
- TSX
- Tailwind CSS
- MIT
Preview
Live component
9:41 100%
devsnips.dev/library/react/components/buttons/button-with-icon/ fluid · no fixed width 100%
Install
Add to your project
npx devsnips add React/Components/Buttons/button-with-icon React/Components/Buttons/button-with-icon import type { ButtonHTMLAttributes, ReactNode } from "react";
/* DevSnips React — ButtonWithIcon
* Labeled button with a leading/trailing icon. Icons use the shared size
* token for the chosen button size; the 8px control gap keeps icon and
* label optically aligned.
*/
export type ButtonSize = "xs" | "sm" | "md" | "lg" | "xl";
export type WithIconVariant = "solid" | "outline" | "secondary" | "ghost";
export type IconPosition = "leading" | "trailing";
export interface ButtonWithIconProps
extends ButtonHTMLAttributes<HTMLButtonElement> {
/** Icon name (rendered by the inline Icon helper). */
icon?: string;
iconPosition?: IconPosition;
variant?: WithIconVariant;
size?: ButtonSize;
/** Override the leading slot with a custom icon node. */
iconLeft?: ReactNode;
/** Override the trailing slot with a custom icon node. */
iconRight?: ReactNode;
}
function cx(...parts: Array<string | false | null | undefined>): string {
return parts.filter(Boolean).join(" ");
}
const SIZES: Record<ButtonSize, string> = {
xs: "h-7 gap-1 px-2 text-xs [&_svg]:size-[14px]",
sm: "h-8 gap-1.5 px-3 text-xs [&_svg]:size-[14px]",
md: "h-9 gap-2 px-3.5 text-[13px] [&_svg]:size-4",
lg: "h-10 gap-2 px-4 text-[13px] [&_svg]:size-[18px]",
xl: "h-11 gap-2 px-5 text-sm [&_svg]:size-5",
};
const VARIANTS: Record<WithIconVariant, string> = {
solid: "border-transparent bg-[var(--ds-color-primary)] text-[var(--ds-color-primary-foreground)] hover:bg-[color-mix(in_srgb,var(--ds-color-primary)_88%,#000)] active:bg-[color-mix(in_srgb,var(--ds-color-primary)_80%,#000)]",
outline: "border-[var(--ds-color-border-strong)] bg-transparent text-[var(--ds-color-foreground)] hover:bg-[var(--ds-color-surface-hover)] active:bg-[var(--ds-color-surface-active)]",
secondary: "border-[var(--ds-color-border)] bg-[var(--ds-color-secondary)] text-[var(--ds-color-secondary-foreground)] hover:bg-[var(--ds-color-surface-active)] active:bg-[var(--ds-color-surface-active)]",
ghost: "border-transparent bg-transparent text-[var(--ds-color-foreground)] hover:bg-[var(--ds-color-surface-hover)] active:bg-[var(--ds-color-surface-active)]",
};
function Icon({ name, className }: { name?: string; className?: string }) {
if (!name) return null;
// Minimal stroke icon set used by the preview. In a real project, import
// your icon library here; the component only needs an SVG node.
const common = { width: "1em", height: "1em", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: 1.75, strokeLinecap: "round", strokeLinejoin: "round", className, "aria-hidden": "true", focusable: "false" } as const;
const paths: Record<string, ReactNode> = {
"download": <path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4" />,
"arrow-right": <><path d="M5 12h14" /><path d="m13 5 7 7-7 7" /></>,
"plus": <><path d="M12 5v14" /><path d="M5 12h14" /></>,
"save": <><path d="M19 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h11l5 5v11a2 2 0 0 1-2 2z" /><path d="M17 21v-8H7v8" /><path d="M7 3v5h8" /></>,
};
return <svg {...common}>{paths[name]}</svg>;
}
export function ButtonWithIcon({
children,
icon,
iconPosition = "leading",
variant = "solid",
size = "md",
disabled,
iconLeft,
iconRight,
className,
type = "button",
...rest
}: ButtonWithIconProps) {
const leading = iconPosition === "leading" ? (iconLeft ?? (icon ? <Icon name={icon} className="shrink-0" /> : null)) : iconLeft;
const trailing = iconPosition === "trailing" ? (iconRight ?? (icon ? <Icon name={icon} className="shrink-0" /> : null)) : iconRight;
return (
<button
type={type}
className={cx(
"inline-flex select-none items-center justify-center whitespace-nowrap rounded-[var(--ds-radius-sm)] border font-medium leading-none transition-colors duration-150 ease-out motion-reduce:transition-none focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-[var(--ds-color-focus-ring)] disabled:pointer-events-none disabled:opacity-50",
VARIANTS[variant],
SIZES[size],
className,
)}
disabled={disabled}
{...rest}
>
{leading}
<span>{children}</span>
{trailing}
</button>
);
}
export default ButtonWithIcon; /* DevSnips React — JavaScript parity build.
* Same API, behavior, and classes as code.tsx; TypeScript types removed.
* Regenerated from code.tsx — edit code.tsx and re-run the generator.
*/
function cx(...parts) {
return parts.filter(Boolean).join(" ");
}
const SIZES = {
xs: "h-7 gap-1 px-2 text-xs [&_svg]:size-[14px]",
sm: "h-8 gap-1.5 px-3 text-xs [&_svg]:size-[14px]",
md: "h-9 gap-2 px-3.5 text-[13px] [&_svg]:size-4",
lg: "h-10 gap-2 px-4 text-[13px] [&_svg]:size-[18px]",
xl: "h-11 gap-2 px-5 text-sm [&_svg]:size-5"
};
const VARIANTS = {
solid: "border-transparent bg-[var(--ds-color-primary)] text-[var(--ds-color-primary-foreground)] hover:bg-[color-mix(in_srgb,var(--ds-color-primary)_88%,#000)] active:bg-[color-mix(in_srgb,var(--ds-color-primary)_80%,#000)]",
outline: "border-[var(--ds-color-border-strong)] bg-transparent text-[var(--ds-color-foreground)] hover:bg-[var(--ds-color-surface-hover)] active:bg-[var(--ds-color-surface-active)]",
secondary: "border-[var(--ds-color-border)] bg-[var(--ds-color-secondary)] text-[var(--ds-color-secondary-foreground)] hover:bg-[var(--ds-color-surface-active)] active:bg-[var(--ds-color-surface-active)]",
ghost: "border-transparent bg-transparent text-[var(--ds-color-foreground)] hover:bg-[var(--ds-color-surface-hover)] active:bg-[var(--ds-color-surface-active)]"
};
function Icon({ name, className }) {
if (!name) return null;
const common = { width: "1em", height: "1em", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: 1.75, strokeLinecap: "round", strokeLinejoin: "round", className, "aria-hidden": "true", focusable: "false" };
const paths = {
"download": <path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4" />,
"arrow-right": <><path d="M5 12h14" /><path d="m13 5 7 7-7 7" /></>,
"plus": <><path d="M12 5v14" /><path d="M5 12h14" /></>,
"save": <><path d="M19 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h11l5 5v11a2 2 0 0 1-2 2z" /><path d="M17 21v-8H7v8" /><path d="M7 3v5h8" /></>
};
return <svg {...common}>{paths[name]}</svg>;
}
export function ButtonWithIcon({
children,
icon,
iconPosition = "leading",
variant = "solid",
size = "md",
disabled,
iconLeft,
iconRight,
className,
type = "button",
...rest
}) {
const leading = iconPosition === "leading" ? iconLeft ?? (icon ? <Icon name={icon} className="shrink-0" /> : null) : iconLeft;
const trailing = iconPosition === "trailing" ? iconRight ?? (icon ? <Icon name={icon} className="shrink-0" /> : null) : iconRight;
return <button
type={type}
className={cx(
"inline-flex select-none items-center justify-center whitespace-nowrap rounded-[var(--ds-radius-sm)] border font-medium leading-none transition-colors duration-150 ease-out motion-reduce:transition-none focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-[var(--ds-color-focus-ring)] disabled:pointer-events-none disabled:opacity-50",
VARIANTS[variant],
SIZES[size],
className
)}
disabled={disabled}
{...rest}
>
{leading}
<span>{children}</span>
{trailing}
</button>;
}
export default ButtonWithIcon; # Button With Icon
A labeled button with a leading or trailing icon. Icons use the shared size token for the chosen button size, with the standard control gap keeping icon and label optically aligned.
## Installation
This component requires **React** and **Tailwind CSS**. Drop `code.tsx` (or `code.jsx` for JavaScript projects) into your project. Tailwind utility classes are included directly in the component, so no separate CSS file is required.
The component consumes the DevSnips semantic design tokens through Tailwind arbitrary values (for example `bg-[var(--ds-color-primary)]`). Define the `--ds-*` tokens once in your theme — see [React/DESIGN_TOKENS.md](../../../DESIGN_TOKENS.md) for the full token spec.
## Usage
```tsx
<ButtonWithIcon icon="download" iconPosition="trailing">Export</ButtonWithIcon>
```
## JavaScript
A `code.jsx` build is provided for projects that ship plain JSX. It exposes the same API and behavior as `code.tsx` — only the TypeScript types are removed.
## Props
| Prop | Type | Default |
|---|---|---|
| `children` | `ReactNode` | — |
| `icon` | `string` (icon name) | — |
| `iconPosition` | `leading \| trailing` | `leading` |
| `variant` | `solid \| outline \| secondary \| ghost` | `solid` |
| `size` | `ButtonSize` | `md` |
| `disabled` | `boolean` | `false` |
Plus all native `ButtonHTMLAttributes<HTMLButtonElement>`. Provide your own icon set; this component renders an `<Icon name>` helper slot — see Notes.
## Variants
solid (default) · outline · secondary · ghost.
## Sizes
xs (28px) · sm (32px) · **md (36px, default)** · lg (40px) · xl (44px). Horizontal padding scales 8 → 20px; icons scale 14 → 20px.
## States
default · hover · active · focus-visible · disabled (reduced opacity).
## Accessibility
Renders a native `<button>`. Focus-visible ring uses `color.focus-ring`. Loading sets `aria-busy` and disables to prevent double-submit. Disabled never removes the affordance (reduced opacity, not hidden). Meets the 44px touch target at lg/xl. Decorative icons are marked `aria-hidden`; the label provides the accessible name.
## Styling
Tailwind classes are included directly in the component and consume the DevSnips semantic design tokens (`--ds-*`) via arbitrary values. The button themes with the surface automatically in light and dark mode. No component-specific CSS file is needed.
## Design Tokens
See [React/DESIGN_TOKENS.md](../../../DESIGN_TOKENS.md) for the authoritative token specification. This button uses the semantic color, radius, and motion tokens; define them once in your project theme and every button in the family stays in sync.
## Notes
The shipped `icon` prop accepts an icon name string rendered by a small inline `Icon` helper (drop in your own). To use a custom icon node, pass `iconLeft`/`iconRight` to SolidButton/OutlineButton/etc. instead. 93 lines UTF-8 · LF · Spaces: 2
Continue browsing