}) {
const [state, setState] = useState<'idle' | 'loading' | 'success' | 'error'>('idle');
const handleClick = async () => {
setState('loading');
try {
await onSubmit();
setState('success');
setTimeout(() => setState('idle'), 2000);
} catch {
setState('error');
setTimeout(() => setState('idle'), 2000);
}
};
const icons = {
idle: null,
loading: ,
success: ,
error: ,
};
const colors = {
idle: 'bg-blue-600 hover:bg-blue-700',
loading: 'bg-blue-600',
success: 'bg-green-600',
error: 'bg-red-600',
};
return (
{icons[state] && (
{icons[state]}
)}
{state === 'idle' && 'Submit'}
{state === 'loading' && 'Submitting...'}
{state === 'success' && 'Done!'}
{state === 'error' && 'Failed'}
);
}
```
## Form Interactions
### Floating Label Input
```tsx
import { useState, useId } from 'react';
function FloatingInput({ label, type = 'text' }: { label: string; type?: string }) {
const [value, setValue] = useState('');
const [isFocused, setIsFocused] = useState(false);
const id = useId();
const isFloating = isFocused || value.length > 0;
return (
setValue(e.target.value)}
onFocus={() => setIsFocused(true)}
onBlur={() => setIsFocused(false)}
className="peer w-full px-4 py-3 border rounded-lg outline-none transition-colors
focus:border-blue-600 focus:ring-2 focus:ring-blue-100"
/>
);
}
```
### Shake on Error
```tsx
import { motion, useAnimation } from 'framer-motion';
function ShakeInput({ error, ...props }: InputProps & { error?: string }) {
const controls = useAnimation();
useEffect(() => {
if (error) {
controls.start({
x: [0, -10, 10, -10, 10, 0],
transition: { duration: 0.4 },
});
}
}, [error, controls]);
return (
{error && (
{error}
)}
);
}
```
### Character Count
```tsx
function TextareaWithCount({ maxLength = 280 }: { maxLength?: number }) {
const [value, setValue] = useState('');
const remaining = maxLength - value.length;
const isNearLimit = remaining <= 20;
const isOverLimit = remaining < 0;
return (
);
}
```
## Feedback Patterns
### Toast Notifications
```tsx
import { motion, AnimatePresence } from 'framer-motion';
import { createContext, useContext, useState, useCallback } from 'react';
interface Toast {
id: string;
message: string;
type: 'success' | 'error' | 'info';
}
const ToastContext = createContext<{
addToast: (message: string, type: Toast['type']) => void;
} | null>(null);
export function ToastProvider({ children }: { children: React.ReactNode }) {
const [toasts, setToasts] = useState([]);
const addToast = useCallback((message: string, type: Toast['type']) => {
const id = Date.now().toString();
setToasts((prev) => [...prev, { id, message, type }]);
setTimeout(() => {
setToasts((prev) => prev.filter((t) => t.id !== id));
}, 3000);
}, []);
return (
{children}
{toasts.map((toast) => (
{toast.message}
))}
);
}
export function useToast() {
const context = useContext(ToastContext);
if (!context) throw new Error('useToast must be within ToastProvider');
return context;
}
```
### Confirmation Dialog
```tsx
function ConfirmButton({
onConfirm,
confirmText = 'Click again to confirm',
children,
}: {
onConfirm: () => void;
confirmText?: string;
children: React.ReactNode;
}) {
const [isPending, setIsPending] = useState(false);
useEffect(() => {
if (isPending) {
const timer = setTimeout(() => setIsPending(false), 3000);
return () => clearTimeout(timer);
}
}, [isPending]);
const handleClick = () => {
if (isPending) {
onConfirm();
setIsPending(false);
} else {
setIsPending(true);
}
};
return (
{isPending ? confirmText : children}
);
}
```
## Navigation Patterns
### Active Link Indicator
```tsx
import { motion } from 'framer-motion';
import { usePathname } from 'next/navigation';
function Navigation({ items }: { items: { href: string; label: string }[] }) {
const pathname = usePathname();
return (
);
}
```
### Hamburger Menu Icon
```tsx
function MenuIcon({ isOpen }: { isOpen: boolean }) {
return (
);
}
```
## Data Interactions
### Optimistic Updates
```tsx
function LikeButton({ postId, initialLiked, initialCount }) {
const [liked, setLiked] = useState(initialLiked);
const [count, setCount] = useState(initialCount);
const handleLike = async () => {
// Optimistic update
const newLiked = !liked;
setLiked(newLiked);
setCount((c) => c + (newLiked ? 1 : -1));
try {
await api.toggleLike(postId);
} catch {
// Rollback on error
setLiked(!newLiked);
setCount((c) => c + (newLiked ? -1 : 1));
}
};
return (
{liked ? : }
{count}
);
}
```
### Pull to Refresh
```tsx
import { motion, useMotionValue, useTransform } from 'framer-motion';
function PullToRefresh({ onRefresh, children }) {
const y = useMotionValue(0);
const [isRefreshing, setIsRefreshing] = useState(false);
const opacity = useTransform(y, [0, 60], [0, 1]);
const rotate = useTransform(y, [0, 60], [0, 180]);
const handleDragEnd = async (_, info) => {
if (info.offset.y > 60 && !isRefreshing) {
setIsRefreshing(true);
await onRefresh();
setIsRefreshing(false);
}
};
return (
{isRefreshing ? : }
{children}
);
}
```