feat: animate reply loading state

This commit is contained in:
lezi-fun committed 2026-08-12 20:50:51 +08:00
1 parent 207b64b705
commit caad17d195
4 files changed
+136 -18

No files matched your search

+76
View File
@@ -1513,6 +1513,43 @@ html.home-view-transition.returning-home::view-transition-old(post-edited-detail
animation: editedDateCollapse 0.24s 0.04s cubic-bezier(0.4, 0, 1, 1) both;
}
/* 回复仍在加载时返回:从页面中独立抽离,向上淡出后再消失。 */
html.home-view-transition.returning-home #pagePost .reply-loading-state {
view-transition-name: reply-loading-return;
}
html.home-view-transition.returning-home::view-transition-group(reply-loading-return) {
animation: none;
z-index: 14;
}
html.home-view-transition.returning-home::view-transition-image-pair(reply-loading-return) {
overflow: visible;
mix-blend-mode: normal;
}
html.home-view-transition.returning-home::view-transition-old(reply-loading-return) {
animation: replyLoadingReturnOut 0.24s cubic-bezier(0.4, 0, 1, 1) both;
mix-blend-mode: normal;
-webkit-mask-image: linear-gradient(to bottom, #000 0%, #000 40%, transparent 60%, transparent 100%);
mask-image: linear-gradient(to bottom, #000 0%, #000 40%, transparent 60%, transparent 100%);
-webkit-mask-size: 100% 250%;
mask-size: 100% 250%;
-webkit-mask-repeat: no-repeat;
mask-repeat: no-repeat;
}
@keyframes replyLoadingReturnOut {
from {
-webkit-mask-position: 0 0;
mask-position: 0 0;
}
to {
-webkit-mask-position: 0 100%;
mask-position: 0 100%;
}
}
@keyframes editedDateExpand {
from {
opacity: 0;
@@ -3390,6 +3427,45 @@ a.btn-secondary {
to { transform: rotate(360deg); }
}
/* 回复慢请求:帖子展开完成后才柔和出现,快速请求不会挂载此状态。 */
.reply-loading-state {
min-height: 76px;
margin-top: 16px;
display: flex;
align-items: center;
justify-content: center;
gap: 10px;
color: var(--glass-text-light);
font-size: 13px;
animation: replyLoadingReveal 0.32s ease-out both;
}
.reply-loading-spinner {
width: 16px;
height: 16px;
border: 2px solid var(--glass-border);
border-top-color: var(--primary);
border-radius: 50%;
animation: spin 0.8s linear infinite;
}
html.post-view-transition #pagePost .reply-loading-state {
opacity: 0;
transform: translateY(5px);
animation: none;
}
@keyframes replyLoadingReveal {
from {
opacity: 0;
transform: translateY(5px);
}
to {
opacity: 0.78;
transform: translateY(0);
}
}
/* ===== 评论抽屉(平滑展开/收起) ===== */
.replies-drawer {
display: grid;
+58 -18
View File
@@ -8,6 +8,7 @@ import { Icon } from './Icons';
import { renderMarkdown } from '@/lib/markdown';
import CaptchaImage from './CaptchaImage';
import { useToast } from './Toast';
import { useTranslation } from 'react-i18next';
function avatar(username) {
return 'https://ui-avatars.com/api/?name=' + encodeURIComponent(username || '匿名用户') +
@@ -17,7 +18,10 @@ function avatar(username) {
export default function ReplyList({ postId, onRefresh }) {
const { currentUser, openUser } = useApp();
const { toast, confirmAction } = useToast();
const [replies, setReplies] = useState([]);
const { t } = useTranslation();
const [replies, setReplies] = useState(null);
const [loading, setLoading] = useState(true);
const [showLoading, setShowLoading] = useState(false);
const [content, setContent] = useState('');
const [captcha, setCaptcha] = useState(null);
const [captchaInput, setCaptchaInput] = useState('');
@@ -25,6 +29,7 @@ export default function ReplyList({ postId, onRefresh }) {
const [replyTo, setReplyTo] = useState(null);
const replyAreaRef = useRef(null);
const replyInputRef = useRef(null);
const loadRequestRef = useRef(0);
// 点击"回复":记录目标回复 ID,滚动到回复框并聚焦
const startReply = (replyId) => {
@@ -35,14 +40,37 @@ export default function ReplyList({ postId, onRefresh }) {
}, 120);
};
const load = useCallback(async () => {
const load = useCallback(async ({ reset = false } = {}) => {
const requestId = ++loadRequestRef.current;
if (reset) setReplies(null);
setLoading(true);
try {
const data = await API.getReplies(postId);
if (requestId !== loadRequestRef.current) return;
setReplies(data || []);
} catch { setReplies([]); }
} catch {
if (requestId !== loadRequestRef.current) return;
setReplies([]);
} finally {
if (requestId === loadRequestRef.current) setLoading(false);
}
}, [postId]);
useEffect(() => { load(); }, [load]);
useEffect(() => {
load({ reset: true });
return () => { loadRequestRef.current += 1; };
}, [load]);
// 快请求不闪加载态;慢请求的加载动画由 CSS 等帖子 View Transition 结束后再淡入。
useEffect(() => {
if (!loading) {
setShowLoading(false);
return;
}
const timer = setTimeout(() => setShowLoading(true), 180);
return () => clearTimeout(timer);
}, [loading]);
useEffect(() => { API.getCaptcha().then((c) => setCaptcha(c)).catch(() => {}); }, [postId]);
const handleSubmit = async () => {
@@ -82,33 +110,43 @@ export default function ReplyList({ postId, onRefresh }) {
};
// 计算每条回复的嵌套深度(按 reply_to_id 精确追踪回复链)
const loadedReplies = replies || [];
const replyDepths = (() => {
const map = {};
const getDepth = (r) => {
if (map[r.id] !== undefined) return map[r.id];
if (!r.reply_to_id) { map[r.id] = 0; return 0; }
const parent = replies.find((x) => x.id === r.reply_to_id);
const parent = loadedReplies.find((x) => x.id === r.reply_to_id);
const d = parent ? getDepth(parent) + 1 : 0;
map[r.id] = d;
return d;
};
replies.forEach((r) => getDepth(r));
loadedReplies.forEach((r) => getDepth(r));
return map;
})();
// 正在回复的目标回复对象(用于显示 @用户名)
const replyToObj = replyTo ? replies.find((x) => x.id === replyTo) : null;
const replyToObj = replyTo ? loadedReplies.find((x) => x.id === replyTo) : null;
return (
<>
<div className="post-reply-count" style={{ marginTop: 20, fontSize: 14, color: '#64748b' }}>
<Icon name="message" size={14} /> {replies.length} 条回复
</div>
<div style={{ marginTop: 12 }}>
{replies.length === 0 ? (
<div style={{ color: '#94a3b8', padding: '20px 0', textAlign: 'center' }}><Icon name="message" size={16} /> 还没有回复,快来发表第一条回复吧</div>
) : (
replies.map((r) => {
{loading && replies === null ? (
showLoading && (
<div className="reply-loading-state" role="status" aria-live="polite">
<span className="reply-loading-spinner" aria-hidden="true" />
<span>{t('reply.loading')}</span>
</div>
)
) : (
<>
<div className="post-reply-count" style={{ marginTop: 20, fontSize: 14, color: '#64748b' }}>
<Icon name="message" size={14} /> {loadedReplies.length} 条回复
</div>
<div style={{ marginTop: 12 }}>
{loadedReplies.length === 0 ? (
<div style={{ color: '#94a3b8', padding: '20px 0', textAlign: 'center' }}><Icon name="message" size={16} /> 还没有回复,快来发表第一条回复吧</div>
) : (
loadedReplies.map((r) => {
const rTime = r.created_at ? new Date(r.created_at).toLocaleString('zh-CN') : '';
const nestDepth = Math.min(replyDepths[r.id] || 0, 5);
return (
@@ -135,9 +173,11 @@ export default function ReplyList({ postId, onRefresh }) {
<div className="reply-content" dangerouslySetInnerHTML={{ __html: renderMarkdown(r.content) }} />
</div>
);
})
)}
</div>
})
)}
</div>
</>
)}
<div id="replyArea" ref={replyAreaRef} style={{ marginTop: 24, borderTop: '1px solid var(--border)', paddingTop: 20 }}>
<h3 style={{ fontSize: 16, marginBottom: 12 }}><Icon name="message" size={16} /> 发表回复</h3>
+1
View File
@@ -58,6 +58,7 @@
"dropHint": "Click or drag to upload image"
},
"reply": {
"loading": "Loading replies...",
"submit": "Submit reply",
"placeholder": "Write your reply...",
"empty": "No replies yet — be the first to comment",
+1
View File
@@ -58,6 +58,7 @@
"dropHint": "点击或拖拽上传图片"
},
"reply": {
"loading": "正在加载回复...",
"submit": "提交回复",
"placeholder": "写下你的回复...",
"empty": "还没有回复,快来发表第一条回复吧",