feat(#2526): add select sound with playing
parent
e8d7452d2e
commit
2ffa73944a
@ -0,0 +1,23 @@ |
|||||||
|
import { useEffect, useState } from 'react' |
||||||
|
|
||||||
|
export const useAudioPlayer = (url: string) => { |
||||||
|
const [audio] = useState(new Audio(url)) |
||||||
|
const [playing, setPlaying] = useState(false) |
||||||
|
|
||||||
|
const toggle = () => setPlaying(!playing) |
||||||
|
|
||||||
|
useEffect(() => { |
||||||
|
playing ? audio.play() : audio.pause() |
||||||
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
|
}, [playing]) |
||||||
|
|
||||||
|
useEffect(() => { |
||||||
|
audio.addEventListener('ended', () => setPlaying(false)) |
||||||
|
return () => { |
||||||
|
audio.removeEventListener('ended', () => setPlaying(false)) |
||||||
|
} |
||||||
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
|
}, []) |
||||||
|
|
||||||
|
return { playing, toggle } |
||||||
|
} |
||||||
@ -0,0 +1,19 @@ |
|||||||
|
import { Icon } from 'features/Icon' |
||||||
|
|
||||||
|
import { ScAudioContainer } from './styled' |
||||||
|
|
||||||
|
import { useAudioPlayer } from './hooks' |
||||||
|
|
||||||
|
type AudioPropsType = { |
||||||
|
url: string, |
||||||
|
} |
||||||
|
|
||||||
|
export const AudioPlayer = ({ url }: AudioPropsType) => { |
||||||
|
const { toggle } = useAudioPlayer(url) |
||||||
|
|
||||||
|
return ( |
||||||
|
<ScAudioContainer onClick={toggle}> |
||||||
|
<Icon refIcon='Sound' /> |
||||||
|
</ScAudioContainer> |
||||||
|
) |
||||||
|
} |
||||||
@ -0,0 +1,4 @@ |
|||||||
|
import styled from 'styled-components/macro' |
||||||
|
|
||||||
|
export const ScAudioContainer = styled.div` |
||||||
|
` |
||||||
Loading…
Reference in new issue