Skip to content

Commit 9614380

Browse files
committed
feat: clear animation timer if element disconnects
1 parent 87e2c02 commit 9614380

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

packages/uui-text-copy/lib/uui-button-copy-text.element.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,24 @@ export class UUIButtonCopyTextElement extends UUIButtonElement {
4545
@property({ type: Number, attribute: 'animation-state-delay' })
4646
animationStateDelay: number = 250;
4747

48+
#animationTimer?: any;
49+
4850
constructor() {
4951
super();
5052
demandCustomElement(this, 'uui-icon');
5153

5254
this.addEventListener('click', this.#onClick);
5355
}
5456

55-
readonly #onClick = async (e: Event) => {
56-
const button = e.target as UUIButtonElement;
57-
button.state = 'waiting';
57+
disconnectedCallback(): void {
58+
super.disconnectedCallback();
59+
if (this.#animationTimer) {
60+
clearTimeout(this.#animationTimer);
61+
}
62+
}
63+
64+
readonly #onClick = async () => {
65+
this.state = 'waiting';
5866

5967
// By default use the value property
6068
let valueToCopy = this.text;
@@ -73,7 +81,7 @@ export class UUIButtonCopyTextElement extends UUIButtonElement {
7381
}
7482
} else {
7583
console.error(`Element ID ${this.copyFrom} not found to copy from`);
76-
button.state = 'failed';
84+
this.state = 'failed';
7785
return;
7886
}
7987
}
@@ -91,9 +99,11 @@ export class UUIButtonCopyTextElement extends UUIButtonElement {
9199
const copiedEv = new UUICopyTextEvent(UUICopyTextEvent.COPIED);
92100
copiedEv.text = valueToCopy;
93101
this.dispatchEvent(copiedEv);
102+
this.#animationTimer = setTimeout(() => {
103+
this.state = 'success';
94104
}, this.animationStateDelay);
95105
} catch (err) {
96-
button.state = 'failed';
106+
this.state = 'failed';
97107
console.error('Error copying to clipboard', err);
98108
}
99109
};

0 commit comments

Comments
 (0)