File tree Expand file tree Collapse file tree 1 file changed +45
-7
lines changed
nepalingo-web/src/components Expand file tree Collapse file tree 1 file changed +45
-7
lines changed Original file line number Diff line number Diff line change 11import { Meaning } from "@/hooks/useDictionary" ;
2- import React from "react" ;
2+ import React , { useEffect , useState } from "react" ;
3+ import { FontAwesomeIcon } from "@fortawesome/react-fontawesome" ;
4+ import { faVolumeHigh } from "@fortawesome/free-solid-svg-icons" ;
35
46const SearchResponseCard = ( { meaning } : { meaning : Meaning } ) => {
7+ const [ audio , setAudio ] = useState < HTMLAudioElement | null > ( null ) ;
8+
9+ useEffect ( ( ) => {
10+ return ( ) => {
11+ if ( audio ) {
12+ audio . pause ( ) ;
13+ audio . currentTime = 0 ;
14+ }
15+ } ;
16+ } , [ audio ] ) ;
17+
18+ const handlePronunciation = ( event : React . MouseEvent < HTMLButtonElement > ) => {
19+ event . stopPropagation ( ) ;
20+
21+ if ( ! meaning . audio ?. uri ) {
22+ return ;
23+ }
24+
25+ if ( audio ) {
26+ if ( ! audio . paused ) {
27+ audio . pause ( ) ;
28+ audio . currentTime = 0 ;
29+ } else {
30+ audio . currentTime = 0 ;
31+ void audio . play ( ) ;
32+ }
33+ } else {
34+ const newAudio = new Audio ( meaning . audio . uri ) ;
35+ setAudio ( newAudio ) ;
36+ void newAudio . play ( ) ;
37+ }
38+ } ;
39+
540 return (
641 < div
742 key = { meaning . meaningOriginal }
@@ -19,12 +54,15 @@ const SearchResponseCard = ({ meaning }: { meaning: Meaning }) => {
1954 </ p >
2055 ) }
2156 </ div >
22- { meaning . audio && (
23- < audio
24- controls
25- src = { meaning . audio . uri }
26- className = "max-w-28 "
27- > </ audio >
57+ { meaning . audio ?. uri && (
58+ < button
59+ type = "button"
60+ onClick = { handlePronunciation }
61+ className = "self-start text-white text-2xl transition-colors hover:text-primary focus-visible:outline-none"
62+ aria-label = { `Play pronunciation for ${ meaning . meaningOriginal } ` }
63+ >
64+ < FontAwesomeIcon icon = { faVolumeHigh } />
65+ </ button >
2866 ) }
2967 </ div >
3068 < div className = "flex flex-row flex-wrap gap-2 mt-2" >
You can’t perform that action at this time.
0 commit comments