This commit is contained in:
ryan
2026-07-13 15:08:40 +08:00
parent 08e8eea932
commit 1eff7878a1
441 changed files with 26491 additions and 18405 deletions
+22 -20
View File
@@ -1,44 +1,46 @@
"use client"
'use client';
import {createContext, useCallback, useContext, useRef, useState} from "react"
import {createContext, useCallback, useContext, useRef, useState,} from 'react';
interface BellRingContextType {
isRinging: boolean
triggerRing: () => void
isRinging: boolean;
triggerRing: () => void;
}
const BellRingContext = createContext<BellRingContextType | undefined>(undefined)
const BellRingContext = createContext<BellRingContextType | undefined>(
undefined,
);
/**
* 铃铛响铃动画上下文提供者
* 用于在 toast 通知时触发顶栏铃铛的响铃动画
*/
export function BellRingProvider({ children }: { children: React.ReactNode }) {
const [isRinging, setIsRinging] = useState(false)
const timeoutRef = useRef<NodeJS.Timeout | null>(null)
const [isRinging, setIsRinging] = useState(false);
const timeoutRef = useRef<NodeJS.Timeout | null>(null);
const triggerRing = useCallback(() => {
// 如果正在响铃,立即重新开始动画
if (timeoutRef.current) {
clearTimeout(timeoutRef.current)
clearTimeout(timeoutRef.current);
}
setIsRinging(false)
setIsRinging(false);
requestAnimationFrame(() => {
requestAnimationFrame(() => {
setIsRinging(true)
setIsRinging(true);
timeoutRef.current = setTimeout(() => {
setIsRinging(false)
timeoutRef.current = null
}, 600)
})
})
}, [])
setIsRinging(false);
timeoutRef.current = null;
}, 600);
});
});
}, []);
return (
<BellRingContext.Provider value={{ isRinging, triggerRing }}>
{children}
</BellRingContext.Provider>
)
);
}
/**
@@ -46,9 +48,9 @@ export function BellRingProvider({ children }: { children: React.ReactNode }) {
* @returns {{ isRinging: boolean, triggerRing: () => void }}
*/
export function useBellRing() {
const context = useContext(BellRingContext)
const context = useContext(BellRingContext);
if (context === undefined) {
throw new Error("useBellRing must be used within a BellRingProvider")
throw new Error('useBellRing must be used within a BellRingProvider');
}
return context
return context;
}