This repository has been archived on 2025-10-10. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
hello.kei/src/hooks/useSfx.ts
Guz 2011662392 improved sound system 🔊
Created a volume control for all sfx on the application, and a custom hook to "connect" everthing.
2022-08-08 19:24:55 -03:00

40 lines
899 B
TypeScript

import { useLocalStorage } from '@mantine/hooks';
import { useSound } from 'use-sound';
import { SpriteMap } from 'use-sound/dist/types';
/**
* Custom hook to play sound effects.
*
* @param file The file name of the sound effect.
*/
export default function useSfx(
file: string,
config?: { format?: string[]; sprite?: SpriteMap }
) {
const [sfxVolume, setVolume] = useLocalStorage<number>({
key: 'sfx-volume',
defaultValue: 0.3,
});
/**
* Regex to extract the extension from file name.
*
* Thanks to Tomalak, on stackoverflow for the regex.
* https://stackoverflow.com/a/680982
* CC BY-SA 4.0
*/
const regExt = /(?:\.([^.]+))?$/;
const [play] = useSound(`/sounds/sfx/${file}`, {
format: config?.format ?? [regExt.exec(file)?.[1] ?? file.split('.').pop()],
volume: sfxVolume,
sprite: config?.sprite,
});
return {
play,
volume: sfxVolume,
setVolume,
};
}