@@ -134,13 +134,41 @@ function el(tag, className, text) {
134134 return node ;
135135}
136136
137- function createAudioPanel ( label , src ) {
137+ function createAudioPanel ( label , src , options = { } ) {
138+ const { collapsible = false } = options ;
138139 const wrap = el ( "div" , "audio-panel" ) ;
139- const lbl = el ( "p" , "audio-label" , label ) ;
140140 const audio = document . createElement ( "audio" ) ;
141141 audio . controls = true ;
142142 audio . preload = "none" ;
143143 audio . src = src ;
144+
145+ if ( collapsible ) {
146+ wrap . classList . add ( "audio-panel-collapsible" ) ;
147+ audio . hidden = true ;
148+
149+ const toggle = document . createElement ( "button" ) ;
150+ toggle . type = "button" ;
151+ toggle . className = "audio-label audio-label-button" ;
152+ toggle . textContent = label ;
153+ toggle . setAttribute ( "aria-expanded" , "false" ) ;
154+ toggle . setAttribute ( "aria-label" , `Show ${ label . toLowerCase ( ) } audio` ) ;
155+ toggle . addEventListener ( "click" , ( ) => {
156+ const isExpanded = toggle . getAttribute ( "aria-expanded" ) === "true" ;
157+ const nextExpanded = ! isExpanded ;
158+ audio . hidden = ! nextExpanded ;
159+ toggle . setAttribute ( "aria-expanded" , String ( nextExpanded ) ) ;
160+ toggle . setAttribute (
161+ "aria-label" ,
162+ `${ nextExpanded ? "Hide" : "Show" } ${ label . toLowerCase ( ) } audio` ,
163+ ) ;
164+ if ( ! nextExpanded ) audio . pause ( ) ;
165+ } ) ;
166+
167+ wrap . append ( toggle , audio ) ;
168+ return wrap ;
169+ }
170+
171+ const lbl = el ( "p" , "audio-label" , label ) ;
144172 wrap . append ( lbl , audio ) ;
145173 return wrap ;
146174}
@@ -172,7 +200,7 @@ function createSampleCard(sample, index) {
172200 // Body
173201 card . append (
174202 header ,
175- createAudioPanel ( "Prompt speech" , sample . promptSpeech ) ,
203+ createAudioPanel ( "Prompt speech" , sample . promptSpeech , { collapsible : true } ) ,
176204 createTextPanel ( sample . text ) ,
177205 createAudioPanel ( "Generated speech" , sample . generatedSpeech ) ,
178206 ) ;
0 commit comments