1
1
import { useState } from 'preact/hooks' ;
2
+ import { useLiveRegion } from '../hooks/useLiveRegion' ;
2
3
import CircleButton from "../CircleButton" ;
3
4
4
5
interface CopyCodeButtonProps {
@@ -12,23 +13,7 @@ export const CopyCodeButton = ({
12
13
} : CopyCodeButtonProps ) => {
13
14
const [ isCopied , setIsCopied ] = useState ( false ) ;
14
15
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 > ( ) ;
32
17
33
18
const copyTextToClipboard = async ( ) => {
34
19
console . log ( 'Copy button clicked' ) ;
@@ -103,7 +88,7 @@ export const CopyCodeButton = ({
103
88
) }
104
89
</ CircleButton >
105
90
{ /* 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" />
107
92
</ >
108
93
) ;
109
94
} ;
0 commit comments