mirror of
https://github.com/remvze/moodist.git
synced 2025-09-29 15:30:49 -04:00
feat: add loading state for sounds
This commit is contained in:
parent
85e42f3606
commit
aaccbee3d7
@ -77,6 +77,14 @@
|
|||||||
|
|
||||||
& .icon {
|
& .icon {
|
||||||
color: #34d399;
|
color: #34d399;
|
||||||
|
|
||||||
|
& .spinner {
|
||||||
|
animation-duration: 1s;
|
||||||
|
animation-iteration-count: infinite;
|
||||||
|
animation-name: spinner;
|
||||||
|
animation-timing-function: linear;
|
||||||
|
line-height: 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -89,3 +97,13 @@
|
|||||||
line-height: 1.6;
|
line-height: 1.6;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@keyframes spinner {
|
||||||
|
0% {
|
||||||
|
transform: rotate(0deg);
|
||||||
|
}
|
||||||
|
|
||||||
|
100% {
|
||||||
|
transform: rotate(360deg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import { useCallback, useEffect } from 'react';
|
import { useCallback, useEffect } from 'react';
|
||||||
|
import { ImSpinner9 } from 'react-icons/im/index';
|
||||||
|
|
||||||
import { Range } from './range';
|
import { Range } from './range';
|
||||||
import { Favorite } from './favorite';
|
import { Favorite } from './favorite';
|
||||||
@ -78,7 +79,15 @@ export function Sound({
|
|||||||
onKeyDown={toggle}
|
onKeyDown={toggle}
|
||||||
>
|
>
|
||||||
<Favorite id={id} />
|
<Favorite id={id} />
|
||||||
<div className={styles.icon}>{icon}</div>
|
<div className={styles.icon}>
|
||||||
|
{sound.isLoading ? (
|
||||||
|
<span className={styles.spinner}>
|
||||||
|
<ImSpinner9 />
|
||||||
|
</span>
|
||||||
|
) : (
|
||||||
|
icon
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
<h3 id={id}>{label}</h3>
|
<h3 id={id}>{label}</h3>
|
||||||
<Range id={id} />
|
<Range id={id} />
|
||||||
</div>
|
</div>
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { useMemo, useEffect, useCallback } from 'react';
|
import { useMemo, useEffect, useCallback, useState } from 'react';
|
||||||
import { Howl } from 'howler';
|
import { Howl } from 'howler';
|
||||||
|
|
||||||
import { useSSR } from './use-ssr';
|
import { useSSR } from './use-ssr';
|
||||||
@ -7,12 +7,21 @@ export function useSound(
|
|||||||
src: string,
|
src: string,
|
||||||
options: { loop?: boolean; volume?: number } = {},
|
options: { loop?: boolean; volume?: number } = {},
|
||||||
) {
|
) {
|
||||||
|
const [hasLoaded, setHasLoaded] = useState(false);
|
||||||
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
const { isBrowser } = useSSR();
|
const { isBrowser } = useSSR();
|
||||||
const sound = useMemo<Howl | null>(() => {
|
const sound = useMemo<Howl | null>(() => {
|
||||||
let sound: Howl | null = null;
|
let sound: Howl | null = null;
|
||||||
|
|
||||||
if (isBrowser) {
|
if (isBrowser) {
|
||||||
sound = new Howl({ preload: false, src: src });
|
sound = new Howl({
|
||||||
|
onload: () => {
|
||||||
|
setIsLoading(false);
|
||||||
|
setHasLoaded(true);
|
||||||
|
},
|
||||||
|
preload: false,
|
||||||
|
src: src,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return sound;
|
return sound;
|
||||||
@ -31,10 +40,14 @@ export function useSound(
|
|||||||
|
|
||||||
const play = useCallback(() => {
|
const play = useCallback(() => {
|
||||||
if (sound) {
|
if (sound) {
|
||||||
sound.load();
|
if (!hasLoaded) {
|
||||||
|
setIsLoading(true);
|
||||||
|
sound.load();
|
||||||
|
}
|
||||||
|
|
||||||
sound.play();
|
sound.play();
|
||||||
}
|
}
|
||||||
}, [sound]);
|
}, [sound, hasLoaded]);
|
||||||
|
|
||||||
const stop = useCallback(() => {
|
const stop = useCallback(() => {
|
||||||
if (sound) sound.stop();
|
if (sound) sound.stop();
|
||||||
@ -44,7 +57,10 @@ export function useSound(
|
|||||||
if (sound) sound.pause();
|
if (sound) sound.pause();
|
||||||
}, [sound]);
|
}, [sound]);
|
||||||
|
|
||||||
const control = useMemo(() => ({ pause, play, stop }), [play, stop, pause]);
|
const control = useMemo(
|
||||||
|
() => ({ isLoading, pause, play, stop }),
|
||||||
|
[play, stop, pause, isLoading],
|
||||||
|
);
|
||||||
|
|
||||||
return control;
|
return control;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user