OpenSourceUIOpenSourceUIOpensource UI
GitHubTwitter/X
  1. Home
  2. Components
  3. Buttons
  4. Download Button
Back to ButtonsComponents / Buttons

Download Button

Free Buttons React component — DownloadButton. MIT licensed, copy-paste ready for Next.js and Tailwind CSS.

Raised 3D download key that sinks while downloading — arrow morphs to spinner to check — then pops back up as a solid emerald done state.

Preview

Setup

How to use

Free for personal and commercial use. No UI attribution required. Include the MIT copyright notice when copying component source into your project. Read the MIT license.

  1. 1

    Run in your terminal:

    $npm install clsx tailwind-merge lucide-react
  2. 2

    Copy lib/cn.ts below. Skip if you already have cn().

  3. 3

    Copy the code below and create components/buttons/download-button.tsx in your project.

  4. 4

    Import and render:

    Example

    import { DownloadButton } from "@/components/buttons/download-button";

    <DownloadButton label="Download" onDownload={fetchFile} />

lib/cn.ts

1
2
3
4
5
6
import { type ClassValue, clsx } from "clsx";
import { twMerge } from "tailwind-merge";
 
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}

components/buttons/download-button.tsx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
"use client";
 
import {
forwardRef,
useEffect,
useRef,
useState,
type ComponentPropsWithoutRef,
} from "react";
 
import { cn } from "@/lib/cn";
import { ArrowDownToLine, Check, Loader2 } from "lucide-react";
 
type Phase = "idle" | "downloading" | "done";
 
export type DownloadButtonProps = Readonly<
{
label?: string;
downloadingLabel?: string;
doneLabel?: string;
downloadMs?: number;
resetMs?: number;
onDownload?: () => void;
} & Omit<ComponentPropsWithoutRef<"button">, "onDownload">
>;
 
// Icons stack in one fixed slot and crossfade, so nothing shifts.
const ICON_LAYER =
"absolute inset-0 grid place-items-center transition-opacity duration-300 ease-out motion-reduce:transition-none";
 
// Labels stack in one grid cell; outgoing fades out, incoming fades in after.
const LABEL_LAYER =
"col-start-1 row-start-1 text-left transition-opacity ease-out motion-reduce:transition-none";
 
// Download — raised key sinks while downloading, then settles into a green
// done state; icon morphs arrow → spinner → check in place.
export const DownloadButton = forwardRef<HTMLButtonElement, DownloadButtonProps>(
(
{
className,
label = "Download File",
downloadingLabel = "Downloading...",
doneLabel = "Downloaded",
downloadMs = 1400,
resetMs = 1800,
onDownload,
onClick,
...props
},
ref,
) => {
const [phase, setPhase] = useState<Phase>("idle");
const timerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
 
useEffect(() => {
return () => {
if (timerRef.current !== null) globalThis.clearTimeout(timerRef.current);
};
}, []);
 
const schedule = (fn: () => void, ms: number) => {
if (timerRef.current !== null) globalThis.clearTimeout(timerRef.current);
timerRef.current = globalThis.setTimeout(fn, ms);
};
 
const handleClick = (event: React.MouseEvent<HTMLButtonElement>) => {
onClick?.(event);
if (phase !== "idle") return;
 
setPhase("downloading");
onDownload?.();
 
schedule(() => {
setPhase("done");
schedule(() => setPhase("idle"), resetMs);
}, downloadMs);
};
 
const idle = phase === "idle";
const downloading = phase === "downloading";
const done = phase === "done";
 
return (
<button
ref={ref}
type="button"
data-slot="download-button"
data-phase={phase}
aria-busy={downloading || undefined}
onClick={handleClick}
className={cn(
"inline-flex h-10 items-center gap-2 rounded-lg px-4 font-sans text-sm font-medium outline-none select-none",
"transition-[background-color,box-shadow,color] duration-300 ease-[cubic-bezier(0.32,0.72,0,1)] motion-reduce:transition-none",
"focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-neutral-900",
// Soft 3D key; sinks in while pressed or downloading.
"shadow-[0_0_0_1px_rgba(0,0,0,0.06),0_1px_1px_rgba(0,0,0,0.1),0_2px_4px_rgba(0,0,0,0.08),inset_0_1px_2px_rgba(255,255,255,0.4),inset_0_-2px_4px_rgba(0,0,0,0.08)]",
downloading &&
"shadow-[0_0_0_1px_rgba(0,0,0,0.06),0_1px_1px_rgba(0,0,0,0.06),inset_0_1px_2px_rgba(0,0,0,0.08),inset_0_2px_4px_rgba(0,0,0,0.04),inset_0_-1px_2px_rgba(0,0,0,0.05)]",
idle &&
"cursor-pointer bg-neutral-50 text-neutral-700 hover:text-neutral-900 active:bg-neutral-100 active:shadow-[0_0_0_1px_rgba(0,0,0,0.06),0_1px_1px_rgba(0,0,0,0.06),inset_0_1px_2px_rgba(0,0,0,0.08),inset_0_2px_4px_rgba(0,0,0,0.04),inset_0_-1px_2px_rgba(0,0,0,0.05)]",
downloading && "cursor-default bg-neutral-100 text-neutral-500",
// Done: emerald 3D key — soft sheen + deep lift (no hard white rim).
done &&
"cursor-default bg-emerald-600 text-white shadow-[0_1px_1px_rgba(0,0,0,0.25),0_3px_6px_rgba(0,0,0,0.2),0_6px_12px_rgba(0,0,0,0.15),inset_0_1px_2px_rgba(255,255,255,0.22),inset_0_-3px_6px_rgba(0,0,0,0.35)]",
className,
)}
{...props}
>
<span className="sr-only" aria-live="polite">
{downloading ? downloadingLabel : done ? doneLabel : ""}
</span>
 
{/* Fixed icon slot keeps the icon and label aligned in every phase. */}
<span className="relative size-4 shrink-0">
<span
aria-hidden
className={cn(ICON_LAYER, idle ? "opacity-100" : "opacity-0")}
>
<ArrowDownToLine size={15} strokeWidth={2} />
</span>
<span
aria-hidden
className={cn(ICON_LAYER, downloading ? "opacity-100" : "opacity-0")}
>
<Loader2
size={15}
strokeWidth={2}
className="animate-spin motion-reduce:animate-none"
/>
</span>
<span
aria-hidden
className={cn(ICON_LAYER, done ? "opacity-100" : "opacity-0")}
>
<Check size={15} strokeWidth={2.5} />
</span>
</span>
 
<span className="grid">
<span
aria-hidden={!idle}
className={cn(
LABEL_LAYER,
idle ? "opacity-100 duration-300 delay-200" : "opacity-0 duration-200",
)}
>
{label}
</span>
<span
aria-hidden={!downloading}
className={cn(
LABEL_LAYER,
downloading
? "opacity-100 duration-300 delay-200"
: "opacity-0 duration-200",
)}
>
{downloadingLabel}
</span>
<span
aria-hidden={!done}
className={cn(
LABEL_LAYER,
done ? "opacity-100 duration-300 delay-200" : "opacity-0 duration-200",
)}
>
{doneLabel}
</span>
</span>
</button>
);
},
);
 
DownloadButton.displayName = "DownloadButton";
PreviousNext

On this

page

Jump to a section on this page.

  • lib/cn.ts
  • components/buttons/download-button.tsx

Sponsor spot · demo preview

Logo

Your brand name

Your tagline or category

A short pitch about your product or service goes here. This is a preview of how sponsor cards will look in this sidebar.

Your call to action

This slot is available. Sponsor Opensource UI and reach developers browsing components every day.