Component
Close Button
A dismiss control for overlays. Icon-only (X), requires an accessible name (defaults to "Close"). Use inside dialogs, drawers, toasts, and banners; pair with Escape handling on the owning surface.
- Component
- React
- TSX
- Tailwind CSS
- MIT
Preview
Live component
9:41 100%
devsnips.dev/library/react/components/buttons/close-button/ fluid · no fixed width 100%
Install
Add to your project
npx devsnips add React/Components/Buttons/close-button React/Components/Buttons/close-button import type { ButtonHTMLAttributes } from "react";
/* DevSnips React — CloseButton
* Dismiss control for overlays. Icon-only (X); requires an accessible name
* (defaults to "Close"). 36px default; 32px in compact headers.
*/
export type ButtonSize = "xs" | "sm" | "md" | "lg" | "xl";
export type CloseVariant = "ghost" | "outline";
export interface CloseButtonProps
extends ButtonHTMLAttributes<HTMLButtonElement> {
label?: string;
variant?: CloseVariant;
size?: ButtonSize;
}
function cx(...parts: Array<string | false | null | undefined>): string {
return parts.filter(Boolean).join(" ");
}
const VARIANTS: Record<CloseVariant, string> = {
ghost: "border-transparent bg-transparent text-[var(--ds-color-foreground)] hover:bg-[var(--ds-color-surface-hover)] active:bg-[var(--ds-color-surface-active)]",
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)]",
};
const ICON_ONLY: Record<ButtonSize, string> = {
xs: "h-7 w-7 px-0 [&_svg]:size-[14px]",
sm: "h-8 w-8 px-0 [&_svg]:size-[14px]",
md: "h-9 w-9 px-0 [&_svg]:size-4",
lg: "h-10 w-10 px-0 [&_svg]:size-[18px]",
xl: "h-11 w-11 px-0 [&_svg]:size-5",
};
export function CloseButton({
label = "Close",
variant = "ghost",
size = "md",
disabled,
className,
type = "button",
...rest
}: CloseButtonProps) {
return (
<button
type={type}
aria-label={label}
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],
ICON_ONLY[size],
className,
)}
disabled={disabled}
{...rest}
>
<svg className="h-[1em] w-[1em] shrink-0" viewBox="0 0 24 24" fill="none" aria-hidden="true">
<path d="M18 6 6 18" stroke="currentColor" strokeWidth="1.75" strokeLinecap="round" strokeLinejoin="round" />
<path d="m6 6 12 12" stroke="currentColor" strokeWidth="1.75" strokeLinecap="round" strokeLinejoin="round" />
</svg>
</button>
);
}
export default CloseButton; /* 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 VARIANTS = {
ghost: "border-transparent bg-transparent text-[var(--ds-color-foreground)] hover:bg-[var(--ds-color-surface-hover)] active:bg-[var(--ds-color-surface-active)]",
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)]"
};
const ICON_ONLY = {
xs: "h-7 w-7 px-0 [&_svg]:size-[14px]",
sm: "h-8 w-8 px-0 [&_svg]:size-[14px]",
md: "h-9 w-9 px-0 [&_svg]:size-4",
lg: "h-10 w-10 px-0 [&_svg]:size-[18px]",
xl: "h-11 w-11 px-0 [&_svg]:size-5"
};
export function CloseButton({
label = "Close",
variant = "ghost",
size = "md",
disabled,
className,
type = "button",
...rest
}) {
return <button
type={type}
aria-label={label}
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],
ICON_ONLY[size],
className
)}
disabled={disabled}
{...rest}
>
<svg className="h-[1em] w-[1em] shrink-0" viewBox="0 0 24 24" fill="none" aria-hidden="true">
<path d="M18 6 6 18" stroke="currentColor" strokeWidth="1.75" strokeLinecap="round" strokeLinejoin="round" />
<path d="m6 6 12 12" stroke="currentColor" strokeWidth="1.75" strokeLinecap="round" strokeLinejoin="round" />
</svg>
</button>;
}
export default CloseButton; # Close Button
A dismiss control for overlays. Icon-only (X), requires an accessible name (defaults to "Close"). Use inside dialogs, drawers, toasts, and banners; pair with Escape handling on the owning surface.
## 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
<CloseButton onClick={closeDialog} />
```
## 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 |
|---|---|---|
| `label` | `string` | `"Close"` (rendered as `aria-label`) |
| `variant` | `ghost \| outline` | `ghost` |
| `size` | `ButtonSize` | `md` |
| `disabled` | `boolean` | `false` |
Plus all native `ButtonHTMLAttributes<HTMLButtonElement>`.
## Variants
ghost (default) · outline.
## Sizes
xs (28px) · sm (32px) · **md (36px, default)** · lg (40px) · xl (44px). Horizontal padding scales 8 → 20px; icons scale 14 → 20px. Icon-only: square (`w` == `h`).
## States
default · hover · focus-visible · disabled (reduced opacity).
## Accessibility
Icon-only, so an accessible name is **required** — `label` (default "Close") is rendered as `aria-label`. Focus-visible ring uses `color.focus-ring`. Pair with Escape handling on the owning dialog/drawer/banner.
## 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
Use inside dialogs, drawers, toasts, banners. The owning surface should close on Escape and move focus appropriately. 65 lines UTF-8 · LF · Spaces: 2
Continue browsing