11import { useState , useEffect , useRef } from "preact/hooks" ;
2+ import { useLiveRegion } from '../hooks/useLiveRegion' ;
23import CodeMirror , { EditorView } from "@uiw/react-codemirror" ;
34import { javascript } from "@codemirror/lang-javascript" ;
45import { cdnLibraryUrl , cdnSoundUrl } from "@/src/globals/globals" ;
@@ -25,6 +26,7 @@ import { Icon } from "../Icon";
2526 * }
2627 */
2728export const CodeEmbed = ( props ) => {
29+ const { ref : liveRegionRef , announce } = useLiveRegion ( ) ;
2830 const [ rendered , setRendered ] = useState ( false ) ;
2931 const initialCode = props . initialValue ?? "" ;
3032 // Source code from Google Docs sometimes uses a unicode non-breaking space
@@ -59,6 +61,7 @@ export const CodeEmbed = (props) => {
5961 } else {
6062 setPreviewCodeString ( codeString ) ;
6163 }
64+ announce ( "Sketch is running" ) ;
6265 } ;
6366
6467 const [ previewCodeString , setPreviewCodeString ] = useState ( codeString ) ;
@@ -108,6 +111,7 @@ export const CodeEmbed = (props) => {
108111 className = "bg-bg-gray-40"
109112 onClick = { ( ) => {
110113 setPreviewCodeString ( "" ) ;
114+ announce ( "Sketch stopped" ) ;
111115 } }
112116 ariaLabel = "Stop sketch"
113117 >
@@ -148,6 +152,7 @@ export const CodeEmbed = (props) => {
148152 onClick = { ( ) => {
149153 setCodeString ( initialCode ) ;
150154 setPreviewCodeString ( initialCode ) ;
155+ announce ( "Code reset to initial value." ) ;
151156 } }
152157 ariaLabel = "Reset code to initial value"
153158 className = "bg-white text-black"
@@ -156,6 +161,7 @@ export const CodeEmbed = (props) => {
156161 </ CircleButton >
157162 </ div >
158163 </ div >
164+ < span ref = { liveRegionRef } aria-live = "polite" class = "sr-only" />
159165 </ div >
160166 ) ;
161167} ;
0 commit comments