Component
Refresh Button
Re-fetch with in-flight feedback. `onRefresh` may return a Promise; while pending, the refresh icon spins (respecting reduced motion), the button is disabled, and `aria-busy` is set. Icon-only mode for compact toolbars.
- Component
- React
- TSX
- Tailwind CSS
- MIT
Preview
Live component
9:41 100%
devsnips.dev/library/react/components/buttons/refresh-button/ fluid · no fixed width 100%
Install
Add to your project
npx devsnips add React/Components/Buttons/refresh-button React/Components/Buttons/refresh-button import { useState } from "react";
import type { ButtonHTMLAttributes } from "react";
/* DevSnips React — RefreshButton
* Re-fetch with in-flight feedback. onRefresh may return a Promise; while
* pending the icon spins (reduced-motion safe), the button is disabled,
* and aria-busy is set.
*/
export type ButtonSize = "xs" | "sm" | "md" | "lg" | "xl";
export type RefreshVariant = "ghost" | "outline" | "secondary";
export interface RefreshButtonProps
extends Omit<ButtonHTMLAttributes<HTMLButtonElement>, "onClick"> {
onRefresh?: () => void | Promise<void>;
label?: string;
showLabel?: boolean;
variant?: RefreshVariant;
size?: ButtonSize;
}
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<RefreshVariant, 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)]",
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)]",
};
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 RefreshButton({
onRefresh,
label = "Refresh",
showLabel = false,
variant = "ghost",
size = "sm",
className,
type = "button",
...rest
}: RefreshButtonProps) {
const [loading, setLoading] = useState(false);
async function run() {
if (loading) return;
setLoading(true);
try { await Promise.resolve(onRefresh?.()); }
finally { setLoading(false); }
}
return (
<button
type={type}
aria-label={showLabel ? undefined : 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],
showLabel ? SIZES[size] : ICON_ONLY[size],
className,
)}
onClick={run}
disabled={loading}
aria-busy={loading || undefined}
{...rest}
>
<svg className={cx("h-[1em] w-[1em] shrink-0", loading && "animate-spin motion-reduce:animate-none")} viewBox="0 0 24 24" fill="none" aria-hidden="true">
<path d="M3 12a9 9 0 0 1 15-6.7L21 8" stroke="currentColor" strokeWidth="1.75" strokeLinecap="round" strokeLinejoin="round" />
<path d="M21 3v5h-5" stroke="currentColor" strokeWidth="1.75" strokeLinecap="round" strokeLinejoin="round" />
<path d="M21 12a9 9 0 0 1-15 6.7L3 16" stroke="currentColor" strokeWidth="1.75" strokeLinecap="round" strokeLinejoin="round" />
<path d="M3 21v-5h5" stroke="currentColor" strokeWidth="1.75" strokeLinecap="round" strokeLinejoin="round" />
</svg>
{showLabel && <span>{loading ? "Refreshing…" : label}</span>}
</button>
);
}
export default RefreshButton; /* 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.
*/
import { useState } from "react";
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 = {
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)]",
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)]"
};
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 RefreshButton({
onRefresh,
label = "Refresh",
showLabel = false,
variant = "ghost",
size = "sm",
className,
type = "button",
...rest
}) {
const [loading, setLoading] = useState(false);
async function run() {
if (loading) return;
setLoading(true);
try {
await Promise.resolve(onRefresh?.());
} finally {
setLoading(false);
}
}
return <button
type={type}
aria-label={showLabel ? undefined : 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],
showLabel ? SIZES[size] : ICON_ONLY[size],
className
)}
onClick={run}
disabled={loading}
aria-busy={loading || undefined}
{...rest}
>
<svg className={cx("h-[1em] w-[1em] shrink-0", loading && "animate-spin motion-reduce:animate-none")} viewBox="0 0 24 24" fill="none" aria-hidden="true">
<path d="M3 12a9 9 0 0 1 15-6.7L21 8" stroke="currentColor" strokeWidth="1.75" strokeLinecap="round" strokeLinejoin="round" />
<path d="M21 3v5h-5" stroke="currentColor" strokeWidth="1.75" strokeLinecap="round" strokeLinejoin="round" />
<path d="M21 12a9 9 0 0 1-15 6.7L3 16" stroke="currentColor" strokeWidth="1.75" strokeLinecap="round" strokeLinejoin="round" />
<path d="M3 21v-5h5" stroke="currentColor" strokeWidth="1.75" strokeLinecap="round" strokeLinejoin="round" />
</svg>
{showLabel && <span>{loading ? "Refreshing\u2026" : label}</span>}
</button>;
}
export default RefreshButton; # Refresh Button
Re-fetch with in-flight feedback. `onRefresh` may return a Promise; while pending, the refresh icon spins (respecting reduced motion), the button is disabled, and `aria-busy` is set. Icon-only mode for compact toolbars.
## 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
<RefreshButton onRefresh={refetch} />
```
## 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 |
|---|---|---|
| `onRefresh` | `() => void \| Promise<void>` | — |
| `label` | `string` | `"Refresh"` |
| `showLabel` | `boolean` | `false` (icon-only when false; `label` becomes `aria-label`) |
| `variant` | `ghost \| outline \| secondary` | `ghost` |
| `size` | `ButtonSize` | `sm` |
Plus all native `ButtonHTMLAttributes<HTMLButtonElement>`.
## Variants
ghost (default) · outline · secondary.
## Sizes
xs (28px) · sm (32px) · **md (36px, default)** · lg (40px) · xl (44px). Horizontal padding scales 8 → 20px; icons scale 14 → 20px. Default is `sm`; icon-only by default for compact toolbars.
## States
default · hover · focus-visible · refreshing (spinner-spin icon + `aria-busy`, disabled) · 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. `aria-busy` reflects the refreshing state; `aria-label` is `label`. Reduced motion disables the spin animation.
## 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
Icon-only by default — place in a toolbar next to a data region. Pass `showLabel` for a labeled refresh action. 91 lines UTF-8 · LF · Spaces: 2
Continue browsing