add loading state to fastaveragecolor hook

This commit is contained in:
jeffvli
2025-09-28 19:17:34 -07:00
parent 7f13260b75
commit d1aeb97dae
@@ -29,6 +29,8 @@ export const useFastAverageColor = (args: {
const { algorithm, default: defaultColor, id, src, srcLoaded } = args; const { algorithm, default: defaultColor, id, src, srcLoaded } = args;
const idRef = useRef<string | undefined>(id); const idRef = useRef<string | undefined>(id);
const [isLoading, setIsLoading] = useState(false);
const [background, setBackground] = useState<{ const [background, setBackground] = useState<{
background: string | undefined; background: string | undefined;
isDark: boolean; isDark: boolean;
@@ -43,6 +45,7 @@ export const useFastAverageColor = (args: {
const fac = new FastAverageColor(); const fac = new FastAverageColor();
if (src && srcLoaded) { if (src && srcLoaded) {
setIsLoading(true);
fac.getColorAsync(src, { fac.getColorAsync(src, {
algorithm: algorithm || 'dominant', algorithm: algorithm || 'dominant',
ignoredColor: [ ignoredColor: [
@@ -68,6 +71,9 @@ export const useFastAverageColor = (args: {
isDark: true, isDark: true,
isLight: false, isLight: false,
}); });
})
.finally(() => {
setIsLoading(false);
}); });
} else if (srcLoaded) { } else if (srcLoaded) {
idRef.current = id; idRef.current = id;
@@ -88,5 +94,6 @@ export const useFastAverageColor = (args: {
colorId: idRef.current, colorId: idRef.current,
isDark: background.isDark, isDark: background.isDark,
isLight: background.isLight, isLight: background.isLight,
isLoading,
}; };
}; };