加载到成功
请求进行时文字滑走、换成加载圈;成功后对勾会自己画出来。
用 React 制作一个有三种状态的提交按钮:idle、loading、done,保存在 data-state 中。按钮宽度保持固定,避免跳动。点击后切换到 loading:文字向上滑出并淡出(translateY(-100%),200ms),同时淡入一个 18px 的加载圈(3/4 边框的圆,每 700ms 线性旋转 360 度)。模拟 1.4 秒的请求后切换到 done:加载圈淡出,一个 SVG 对勾通过把 stroke-dashoffset 从路径长度动画到 0 自己画出来,时长 400ms,缓动为 cubic-bezier(0.16, 1, 0.3, 1),文字为“订单已提交”。2 秒后回到 idle。loading 时禁用按钮并设置 aria-busy,并用 aria-live="polite" 播报状态文字。在 prefers-reduced-motion 下用 150ms 的淡入淡出代替滑动和描绘,加载圈保持静止。默认文字为“提交订单”。
源代码
'use client'
import { useEffect, useRef, useState } from 'react'
import s from './demo.module.css'
type State = 'idle' | 'loading' | 'done'
export default function LoadingButton({ labels }: { labels: Record<string, string> }) {
const [state, setState] = useState<State>('idle')
const timers = useRef<ReturnType<typeof setTimeout>[]>([])
useEffect(() => () => timers.current.forEach(clearTimeout), [])
const submit = () => {
setState('loading')
timers.current.push(
setTimeout(() => setState('done'), 1400),
setTimeout(() => setState('idle'), 3400),
)
}
return (
<button
type="button"
className={s.button}
data-state={state}
disabled={state === 'loading'}
aria-busy={state === 'loading'}
onClick={() => state === 'idle' && submit()}
>
<span className={s.label}>{state === 'done' ? labels.done : labels.idle}</span>
<span className={s.spinner} aria-hidden="true" />
<svg className={s.check} viewBox="0 0 24 24" aria-hidden="true">
<path d="M5 12.5l4.5 4.5L19 7.5" />
</svg>
<span className={s.sr} aria-live="polite">
{state === 'loading' ? labels.loading : state === 'done' ? labels.done : ''}
</span>
</button>
)
}
.button {
position: relative;
display: inline-flex;
align-items: center;
justify-content: center;
gap: 8px;
width: 200px;
height: 52px;
overflow: hidden;
border: 0;
border-radius: 12px;
background: var(--stage-ink);
color: var(--stage-bg);
font: 600 var(--text-base) / 1 var(--font-body);
white-space: nowrap;
cursor: pointer;
transition: background-color 200ms var(--ease-out);
}
.button:focus-visible { outline: 2px solid var(--color-focus); outline-offset: 3px; }
.button:disabled { cursor: progress; }
.button[data-state='done'] { background: var(--stage-accent); color: var(--stage-accent-ink); }
.label { transition: transform 200ms var(--ease-out), opacity 200ms var(--ease-out); }
.button[data-state='loading'] .label { transform: translateY(-100%); opacity: 0; }
.spinner {
position: absolute;
width: 18px;
height: 18px;
border: 2px solid currentColor;
border-right-color: transparent;
border-radius: 50%;
opacity: 0;
transition: opacity 150ms linear;
animation: spin 700ms linear infinite;
}
.button[data-state='loading'] .spinner { opacity: 1; }
.check { width: 0; height: 20px; overflow: visible; }
.button[data-state='done'] .check { width: 20px; }
.check path {
fill: none;
stroke: currentColor;
stroke-width: 2.5;
stroke-linecap: round;
stroke-linejoin: round;
stroke-dasharray: 22;
stroke-dashoffset: 22;
}
.button[data-state='done'] .check path { animation: draw 400ms 100ms var(--ease-out) forwards; }
@keyframes spin { to { transform: rotate(360deg); } }
@keyframes draw { to { stroke-dashoffset: 0; } }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
@media (prefers-reduced-motion: reduce) {
.spinner { animation: none; }
.label { transition: opacity 150ms linear; }
.button[data-state='loading'] .label { transform: none; }
.button[data-state='done'] .check path { animation: none; stroke-dashoffset: 0; }
}