11import { useState } from 'preact/hooks' ;
2+ import { useLiveRegion } from '../hooks/useLiveRegion' ;
23import CircleButton from "../CircleButton" ;
34
45interface CopyCodeButtonProps {
@@ -12,23 +13,7 @@ export const CopyCodeButton = ({
1213} : CopyCodeButtonProps ) => {
1314 const [ isCopied , setIsCopied ] = useState ( false ) ;
1415
15- // const liveId = useId();
16- const [ liveId ] = useState (
17- ( ) => 'copy-live-' + Math . random ( ) . toString ( 36 ) . slice ( 2 )
18- ) ;
19-
20- /** Write a message into the live region so screen readers will announce it */
21- const announce = ( message : string ) => {
22- const region = document . getElementById ( liveId ) as HTMLSpanElement | null ;
23- if ( ! region ) return ;
24-
25- region . textContent = message ;
26-
27- // Clear the text after 1 s so a future announcement will fire again
28- setTimeout ( ( ) => {
29- region . textContent = '' ;
30- } , 1000 ) ;
31- } ;
16+ const { ref : liveRegionRef , announce } = useLiveRegion < HTMLSpanElement > ( ) ;
3217
3318 const copyTextToClipboard = async ( ) => {
3419 console . log ( 'Copy button clicked' ) ;
@@ -103,7 +88,7 @@ export const CopyCodeButton = ({
10388 ) }
10489 </ CircleButton >
10590 { /* Visually hidden live region for accessibility announcements */ }
106- < span id = { liveId } aria-live = "polite" class = "sr-only" />
91+ < span ref = { liveRegionRef } aria-live = "polite" class = "sr-only" />
10792 </ >
10893 ) ;
10994} ;
0 commit comments