Skip to main content
Morphing Status Island

Morphing Status Island

Fluid spring-physics status island that morphs smoothly between synced, unsaved, saving, and saved states.

Morphing
10/16
Unsaved changes
Unsaved changes

1. Install Dependencies

$npm install framer-motion lucide-react

2. Copy Component Code

components/ui/MorphingStatusIsland.tsx
1"use client";
2
3import React, { useEffect, useRef, useState } from "react";
4import { AnimatePresence, motion, useReducedMotion } from "framer-motion";
5import { Check, CloudCheck, Loader2, PencilLine } from "lucide-react";
6
7const APPLE_EASE = [0.22, 1, 0.36, 1] as const;
8
9export type MorphingIslandStatus = "idle" | "unsaved" | "saving" | "saved";
10
11export interface MorphingStatusIslandProps {
12 /** Controlled status state. */
13 status?: MorphingIslandStatus;
14 /** Initial status when uncontrolled. */
15 defaultStatus?: MorphingIslandStatus;
16 /** Fired when internal actions or timers transition the status. */
17 onStatusChange?: (status: MorphingIslandStatus) => void;
18 /** Async or sync callback triggered when Save is clicked. */
19 onSave?: () => Promise<void> | void;
20 /** Callback triggered when Reset is clicked. */
21 onReset?: () => void;
22 /** Optional wrapper classes. */
23 className?: string;
24}
25
26/** Spring-physics status island that morphs smoothly across sync, edit, save, and completion states. */
27export function MorphingStatusIsland({
28 status,
29 defaultStatus = "idle",
30 onStatusChange,
31 onSave,
32 onReset,
33 className = "",
34}: MorphingStatusIslandProps) {
35 const prefersReducedMotion = useReducedMotion();
36 const [internalStatus, setInternalStatus] =
37 useState<MorphingIslandStatus>(defaultStatus);
38 const [progress, setProgress] = useState(0);
39
40 const activeStatus = status !== undefined ? status : internalStatus;
41 const isControlled = status !== undefined;
42 const onStatusChangeRef = useRef(onStatusChange);
43 const onSaveRef = useRef(onSave);
44
45 useEffect(() => {
46 onStatusChangeRef.current = onStatusChange;
47 }, [onStatusChange]);
48
49 useEffect(() => {
50 onSaveRef.current = onSave;
51 }, [onSave]);
52
53 function setNextStatus(next: MorphingIslandStatus) {
54 if (!isControlled) {
55 setInternalStatus(next);
56 }
57 onStatusChangeRef.current?.(next);
58 }
59
60 // Drive live 0% -> 100% progress during "saving" state over 1500ms, then transition to "saved"
61 useEffect(() => {
62 if (activeStatus !== "saving") {
63 return;
64 }
65
66 let cancelled = false;
67 let rafId = 0;
68 const duration = 1500;
69 const startTime = performance.now();
70 let savePromiseSettled = false;
71
72 const finishSaving = () => {
73 if (cancelled) return;
74 setNextStatus("saved");
75 };
76
77 const runCustomSave = async () => {
78 if (!onSaveRef.current) {
79 savePromiseSettled = true;
80 return;
81 }
82 try {
83 await onSaveRef.current();
84 } finally {
85 savePromiseSettled = true;
86 }
87 };
88
89 void runCustomSave();
90
91 const tick = (now: number) => {
92 if (cancelled) return;
93 const elapsed = now - startTime;
94 const ratio = Math.min(1, elapsed / duration);
95 const nextProgress = Math.round(ratio * 100);
96 setProgress(nextProgress);
97
98 if (ratio < 1) {
99 rafId = window.requestAnimationFrame(tick);
100 } else if (savePromiseSettled) {
101 finishSaving();
102 } else {
103 const waitInterval = window.setInterval(() => {
104 if (savePromiseSettled || cancelled) {
105 window.clearInterval(waitInterval);
106 if (!cancelled) finishSaving();
107 }
108 }, 40);
109 }
110 };
111
112 rafId = window.requestAnimationFrame(tick);
113
114 return () => {
115 cancelled = true;
116 if (rafId) {
117 window.cancelAnimationFrame(rafId);
118 }
119 };
120 // eslint-disable-next-line react-hooks/exhaustive-deps
121 }, [activeStatus, isControlled]);
122
123 const displayProgress = activeStatus === "saving" ? progress : 0;
124
125 // Auto-settle from "saved" back to "idle" after 2200ms
126 useEffect(() => {
127 if (activeStatus !== "saved") return;
128
129 const timerId = window.setTimeout(() => {
130 setNextStatus("idle");
131 }, 2200);
132
133 return () => window.clearTimeout(timerId);
134 // eslint-disable-next-line react-hooks/exhaustive-deps
135 }, [activeStatus, isControlled]);
136
137 const layoutTransition = prefersReducedMotion
138 ? { duration: 0 }
139 : { type: "spring" as const, stiffness: 500, damping: 36, mass: 0.8 };
140
141 const contentTransition = prefersReducedMotion
142 ? { duration: 0 }
143 : { duration: 0.18, ease: APPLE_EASE };
144
145 const isExpanded = activeStatus === "unsaved";
146
147 const statusAnnouncement =
148 activeStatus === "idle"
149 ? "Synced"
150 : activeStatus === "unsaved"
151 ? "Unsaved changes"
152 : activeStatus === "saving"
153 ? "Saving changes"
154 : "All changes saved";
155
156 return (
157 <motion.div
158 layout={!prefersReducedMotion}
159 transition={{ layout: layoutTransition }}
160 className={`relative inline-flex max-w-full min-w-0 items-center overflow-hidden rounded-[20px] border-0 bg-white shadow-[0_4px_20px_rgba(0,0,0,0.06)] dark:border-0 dark:bg-[#111113] dark:shadow-[inset_0_1px_0_rgba(255,255,255,0.10),0_12px_32px_rgba(0,0,0,0.6)] ${
161 isExpanded ? "p-2" : "min-h-10 px-4 py-1"
162 } ${className}`}
163 >
164 <span className="visually-hidden" aria-live="polite">
165 {statusAnnouncement}
166 </span>
167 <AnimatePresence mode="popLayout" initial={false}>
168 {activeStatus === "idle" && (
169 <motion.div
170 key="idle"
171 initial={
172 prefersReducedMotion
173 ? false
174 : { opacity: 0, y: 4, filter: "blur(2px)" }
175 }
176 animate={{ opacity: 1, y: 0, filter: "blur(0px)" }}
177 exit={
178 prefersReducedMotion
179 ? undefined
180 : { opacity: 0, y: -4, filter: "blur(2px)" }
181 }
182 transition={contentTransition}
183 className="inline-flex items-center gap-2"
184 >
185 <CloudCheck
186 className="h-3.5 w-3.5 shrink-0 text-black/45 dark:text-white/45"
187 strokeWidth={1.75}
188 aria-hidden="true"
189 />
190 <span className="text-xs leading-4 font-medium tracking-[-0.01em] whitespace-nowrap text-[#0A0A0A] dark:text-white">
191 Synced
192 </span>
193 </motion.div>
194 )}
195
196 {activeStatus === "unsaved" && (
197 <motion.div
198 key="unsaved"
199 initial={
200 prefersReducedMotion
201 ? false
202 : { opacity: 0, y: 4, filter: "blur(2px)" }
203 }
204 animate={{ opacity: 1, y: 0, filter: "blur(0px)" }}
205 exit={
206 prefersReducedMotion
207 ? undefined
208 : { opacity: 0, y: -4, filter: "blur(2px)" }
209 }
210 transition={contentTransition}
211 className="inline-flex max-w-full min-w-0 flex-wrap items-center gap-y-1.5"
212 >
213 <div className="inline-flex min-w-0 items-center gap-2 pl-2 pr-3">
214 <PencilLine
215 className="h-3.5 w-3.5 shrink-0 text-black/55 dark:text-white/55"
216 strokeWidth={1.75}
217 aria-hidden="true"
218 />
219 <span className="truncate text-xs leading-4 font-medium tracking-[-0.01em] text-[#0A0A0A] dark:text-white">
220 Unsaved changes
221 </span>
222 </div>
223
224 <div className="inline-flex shrink-0 items-center gap-2">
225 <button
226 type="button"
227 onClick={() => {
228 onReset?.();
229 setNextStatus("idle");
230 }}
231 className="inline-flex min-h-8 items-center justify-center rounded-[12px] bg-[#F3F3F5] px-3 text-xs leading-4 font-medium tracking-[-0.01em] text-black/75 transition-all duration-150 hover:bg-[#EAEAEF] hover:text-black active:scale-[0.97] dark:bg-[#1A1A1E] dark:text-white/75 dark:hover:bg-[#222227] dark:hover:text-white"
232 >
233 Reset
234 </button>
235 <button
236 type="button"
237 onClick={() => setNextStatus("saving")}
238 className="inline-flex min-h-8 items-center justify-center rounded-[12px] bg-black px-3 text-xs leading-4 font-medium tracking-[-0.01em] text-white transition-all duration-150 hover:bg-black/90 active:scale-[0.97] dark:bg-white dark:text-black dark:hover:bg-white/90"
239 >
240 Save
241 </button>
242 </div>
243 </motion.div>
244 )}
245
246 {activeStatus === "saving" && (
247 <motion.div
248 key="saving"
249 initial={
250 prefersReducedMotion
251 ? false
252 : { opacity: 0, y: 4, filter: "blur(2px)" }
253 }
254 animate={{ opacity: 1, y: 0, filter: "blur(0px)" }}
255 exit={
256 prefersReducedMotion
257 ? undefined
258 : { opacity: 0, y: -4, filter: "blur(2px)" }
259 }
260 transition={contentTransition}
261 className="inline-flex items-center gap-2"
262 >
263 <Loader2
264 className={`h-3.5 w-3.5 shrink-0 text-black/55 dark:text-white/55 ${
265 prefersReducedMotion ? "" : "animate-spin"
266 }`}
267 strokeWidth={1.75}
268 aria-hidden="true"
269 />
270 <span className="text-xs leading-4 font-medium tracking-[-0.01em] whitespace-nowrap text-[#0A0A0A] dark:text-white">
271 Saving changes
272 </span>
273 <span className="font-mono text-xs leading-4 tabular-nums text-black/45 dark:text-white/45">
274 {displayProgress}%
275 </span>
276 </motion.div>
277 )}
278
279 {activeStatus === "saved" && (
280 <motion.div
281 key="saved"
282 initial={
283 prefersReducedMotion
284 ? false
285 : { opacity: 0, y: 4, filter: "blur(2px)" }
286 }
287 animate={{ opacity: 1, y: 0, filter: "blur(0px)" }}
288 exit={
289 prefersReducedMotion
290 ? undefined
291 : { opacity: 0, y: -4, filter: "blur(2px)" }
292 }
293 transition={contentTransition}
294 className="inline-flex items-center gap-2"
295 >
296 <Check
297 className="h-3.5 w-3.5 shrink-0 text-emerald-500"
298 strokeWidth={1.75}
299 aria-hidden="true"
300 />
301 <span className="text-xs leading-4 font-medium tracking-[-0.01em] whitespace-nowrap text-[#0A0A0A] dark:text-white">
302 All changes saved
303 </span>
304 </motion.div>
305 )}
306 </AnimatePresence>
307
308 {/* Razor-thin bottom progress hairline tucked inside the clipped 20px island */}
309 {activeStatus === "saving" && (
310 <div
311 aria-hidden="true"
312 className="pointer-events-none absolute inset-x-0 bottom-0 h-[2px] bg-black/[0.04] dark:bg-white/[0.06]"
313 >
314 <div
315 style={{ width: `${displayProgress}%` }}
316 className="h-full bg-emerald-500"
317 />
318 </div>
319 )}
320 </motion.div>
321 );
322}

Usage Example

import { MorphingStatusIsland } from "@/components/ui/MorphingStatusIsland";
export default function DocumentSaveBar() {
return <MorphingStatusIsland defaultStatus="unsaved" />;
}

3. Props Reference

Morphing Status Island props API reference
PropTypeDefaultDescription
status"idle" | "unsaved" | "saving" | "saved"undefinedControlled status mode for the island.
defaultStatus"idle" | "unsaved" | "saving" | "saved""idle"Initial status when running in uncontrolled mode.
onStatusChange(status: MorphingIslandStatus) => voidundefinedCallback fired whenever status changes internally.
onSave() => Promise<void> | voidundefinedOptional async/sync handler awaited before settling into the saved state.
onReset() => voidundefinedOptional handler called when clicking Reset.