1
1
import { useState , useEffect , useRef } from "preact/hooks" ;
2
+ import { useLiveRegion } from '../hooks/useLiveRegion' ;
2
3
import CodeMirror , { EditorView } from "@uiw/react-codemirror" ;
3
4
import { javascript } from "@codemirror/lang-javascript" ;
4
5
import { cdnLibraryUrl , cdnSoundUrl } from "@/src/globals/globals" ;
@@ -25,6 +26,7 @@ import { Icon } from "../Icon";
25
26
* }
26
27
*/
27
28
export const CodeEmbed = ( props ) => {
29
+ const { ref : liveRegionRef , announce } = useLiveRegion ( ) ;
28
30
const [ rendered , setRendered ] = useState ( false ) ;
29
31
const initialCode = props . initialValue ?? "" ;
30
32
// Source code from Google Docs sometimes uses a unicode non-breaking space
@@ -59,6 +61,7 @@ export const CodeEmbed = (props) => {
59
61
} else {
60
62
setPreviewCodeString ( codeString ) ;
61
63
}
64
+ announce ( "Sketch is running" ) ;
62
65
} ;
63
66
64
67
const [ previewCodeString , setPreviewCodeString ] = useState ( codeString ) ;
@@ -108,6 +111,7 @@ export const CodeEmbed = (props) => {
108
111
className = "bg-bg-gray-40"
109
112
onClick = { ( ) => {
110
113
setPreviewCodeString ( "" ) ;
114
+ announce ( "Sketch stopped" ) ;
111
115
} }
112
116
ariaLabel = "Stop sketch"
113
117
>
@@ -148,6 +152,7 @@ export const CodeEmbed = (props) => {
148
152
onClick = { ( ) => {
149
153
setCodeString ( initialCode ) ;
150
154
setPreviewCodeString ( initialCode ) ;
155
+ announce ( "Code reset to initial value." ) ;
151
156
} }
152
157
ariaLabel = "Reset code to initial value"
153
158
className = "bg-white text-black"
@@ -156,6 +161,7 @@ export const CodeEmbed = (props) => {
156
161
</ CircleButton >
157
162
</ div >
158
163
</ div >
164
+ < span ref = { liveRegionRef } aria-live = "polite" class = "sr-only" />
159
165
</ div >
160
166
) ;
161
167
} ;
0 commit comments