Branchwell

Become a Pro / website · becomeapro.ai / src / components / mdx / say.tsx

Raw 1.3 KB on f63bc385c0fa362af985bfb576a8b5aa2a3df60f
1"use client";
2
3import { useRef, useState } from "react";
4import { audioFileFor } from "@/lib/audio";
5
6// A word or short sentence with a plain play button for its pronunciation clip (public/audio, made by scripts/make-audio.mjs)
7export function Say({ word, hint }: { word: string; hint?: string }) {
8 const audio = useRef<HTMLAudioElement>(null);
9 const [playing, setPlaying] = useState(false);
10
11 const play = () => {
12 const el = audio.current;
13 if (!el) return;
14 el.currentTime = 0;
15 void el.play();
16 };
17
18 return (
19 <span className="not-prose my-2 flex flex-wrap items-baseline gap-x-3 gap-y-1">
20 <button
21 type="button"
22 onClick={play}
23 className="inline-flex items-center gap-1.5 rounded border border-foreground/50 px-2 py-0.5 text-[15px] hover:border-foreground"
24 aria-label={`Play: ${word}`}
25 >
26 <span aria-hidden="true">{playing ? "■" : "▶"}</span> Play
27 </button>
28 <strong className="text-[18px]">{word}</strong>
29 {hint && <span className="text-[16px] text-[var(--ash)]">{hint}</span>}
30 <audio
31 ref={audio}
32 src={audioFileFor(word)}
33 preload="none"
34 onPlay={() => setPlaying(true)}
35 onEnded={() => setPlaying(false)}
36 onPause={() => setPlaying(false)}
37 />
38 </span>
39 );
40}