mirror of
https://github.com/jeffvli/feishin.git
synced 2026-05-15 13:00:25 +02:00
add loading state to fastaveragecolor hook
This commit is contained in:
@@ -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,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user