@@ -21,6 +21,54 @@ type CopyButtonOptions = {
2121
2222function setButtonLabel ( button : HTMLButtonElement , label : string ) {
2323 button . setAttribute ( "aria-label" , label ) ;
24+ // Preserve Lit's marker nodes so a later locale change can rerender this label.
25+ const visibleLabel = button . querySelector ( "[data-copy-label]" ) ?. lastChild ;
26+ if ( visibleLabel ?. nodeType === Node . TEXT_NODE ) {
27+ visibleLabel . nodeValue = label ;
28+ }
29+ }
30+
31+ export async function handleCopyButton ( event : Event , text : string , idleLabel : string ) {
32+ const button = event . currentTarget as HTMLButtonElement | null ;
33+ if ( ! button || button . dataset . copying === "1" ) {
34+ return ;
35+ }
36+
37+ // Older reset timers must not replace feedback from a newer copy attempt.
38+ const attempt = String ( Number ( button . dataset . copyAttempt ?? "0" ) + 1 ) ;
39+ button . dataset . copyAttempt = attempt ;
40+ button . dataset . copying = "1" ;
41+ button . setAttribute ( "aria-busy" , "true" ) ;
42+ button . disabled = true ;
43+
44+ const copied = await copyToClipboard ( text ) ;
45+ delete button . dataset . copying ;
46+ button . removeAttribute ( "aria-busy" ) ;
47+ button . disabled = false ;
48+ if ( ! button . isConnected || button . dataset . copyAttempt !== attempt ) {
49+ return ;
50+ }
51+
52+ const feedback = copied ? "copied" : "error" ;
53+ delete button . dataset [ copied ? "error" : "copied" ] ;
54+ button . dataset [ feedback ] = "1" ;
55+ const feedbackLabel = t ( copied ? "common.copied" : "common.copyFailed" ) ;
56+ setButtonLabel ( button , feedbackLabel ) ;
57+
58+ const duration = copied ? COPIED_FOR_MS : ERROR_FOR_MS ;
59+ window . setTimeout ( ( ) => {
60+ if ( ! button . isConnected || button . dataset . copyAttempt !== attempt ) {
61+ return ;
62+ }
63+ delete button . dataset [ feedback ] ;
64+ // A locale rerender can replace the idle label while feedback is still active.
65+ const renderedLabel =
66+ button . querySelector ( "[data-copy-label]" ) ?. textContent ?? button . getAttribute ( "aria-label" ) ;
67+ setButtonLabel (
68+ button ,
69+ renderedLabel && renderedLabel !== feedbackLabel ? renderedLabel : idleLabel ,
70+ ) ;
71+ } , duration ) ;
2472}
2573
2674function createCopyButton ( options : CopyButtonOptions ) : TemplateResult {
@@ -31,51 +79,7 @@ function createCopyButton(options: CopyButtonOptions): TemplateResult {
3179 class =${ options . bare ? "chat-copy-btn" : "btn btn--xs chat-copy-btn" }
3280 type ="button"
3381 aria-label=${ idleLabel }
34- @click=${ async ( e : Event ) => {
35- const btn = e . currentTarget as HTMLButtonElement | null ;
36-
37- if ( ! btn || btn . dataset . copying === "1" ) {
38- return ;
39- }
40-
41- btn . dataset . copying = "1" ;
42- btn . setAttribute ( "aria-busy" , "true" ) ;
43- btn . disabled = true ;
44-
45- const copied = await copyToClipboard ( options . text ( ) ) ;
46- if ( ! btn . isConnected ) {
47- return ;
48- }
49-
50- delete btn . dataset . copying ;
51- btn . removeAttribute ( "aria-busy" ) ;
52- btn . disabled = false ;
53-
54- if ( ! copied ) {
55- btn . dataset . error = "1" ;
56- setButtonLabel ( btn , t ( "common.copyFailed" ) ) ;
57-
58- window . setTimeout ( ( ) => {
59- if ( ! btn . isConnected ) {
60- return ;
61- }
62- delete btn . dataset . error ;
63- setButtonLabel ( btn , idleLabel ) ;
64- } , ERROR_FOR_MS ) ;
65- return ;
66- }
67-
68- btn . dataset . copied = "1" ;
69- setButtonLabel ( btn , t ( "common.copied" ) ) ;
70-
71- window . setTimeout ( ( ) => {
72- if ( ! btn . isConnected ) {
73- return ;
74- }
75- delete btn . dataset . copied ;
76- setButtonLabel ( btn , idleLabel ) ;
77- } , COPIED_FOR_MS ) ;
78- } }
82+ @click=${ ( event : Event ) => void handleCopyButton ( event , options . text ( ) , idleLabel ) }
7983 >
8084 < span class ="chat-copy-btn__icon " aria-hidden ="true ">
8185 < span class ="chat-copy-btn__icon-copy "> ${ icons . copy } </ span >
0 commit comments