Skip to content

Commit 87e2c02

Browse files
committed
feat: use real class property to hold copied value
1 parent 67d6eef commit 87e2c02

File tree

3 files changed

+15
-25
lines changed

3 files changed

+15
-25
lines changed
Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
11
import { UUIEvent } from '@umbraco-ui/uui-base/lib/events';
22
import { UUIButtonCopyTextElement } from './uui-button-copy-text.element';
33

4-
interface UUICopyTextEventInit extends EventInit {
5-
detail?: { text: string };
6-
}
7-
84
export class UUICopyTextEvent extends UUIEvent<
95
{ text: string },
106
UUIButtonCopyTextElement
117
> {
128
public static readonly COPIED: string = 'copied';
139
public static readonly COPYING: string = 'copying';
1410

15-
constructor(evName: string, eventInit: UUICopyTextEventInit = {}) {
16-
super(evName, {
17-
...eventInit,
18-
});
19-
}
11+
/**
12+
* The text content that is about to be copied to the clipboard
13+
*/
14+
public text: string | null = null;
2015
}

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

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -78,24 +78,19 @@ export class UUIButtonCopyTextElement extends UUIButtonElement {
7878
}
7979
}
8080

81-
const beforeCopyEv = new UUICopyTextEvent(UUICopyTextEvent.COPYING, {
82-
detail: { text: valueToCopy },
83-
});
81+
const beforeCopyEv = new UUICopyTextEvent(UUICopyTextEvent.COPYING);
82+
beforeCopyEv.text = valueToCopy;
8483
this.dispatchEvent(beforeCopyEv);
8584

86-
if (beforeCopyEv.detail.text != null) {
87-
valueToCopy = beforeCopyEv.detail.text;
85+
if (beforeCopyEv.text != null) {
86+
valueToCopy = beforeCopyEv.text;
8887
}
8988

9089
try {
9190
await navigator.clipboard.writeText(valueToCopy);
92-
this.dispatchEvent(
93-
new UUICopyTextEvent(UUICopyTextEvent.COPIED, {
94-
detail: { text: valueToCopy },
95-
}),
96-
);
97-
setTimeout(() => {
98-
button.state = 'success';
91+
const copiedEv = new UUICopyTextEvent(UUICopyTextEvent.COPIED);
92+
copiedEv.text = valueToCopy;
93+
this.dispatchEvent(copiedEv);
9994
}, this.animationStateDelay);
10095
} catch (err) {
10196
button.state = 'failed';

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ export const CopiedEvent: Story = {
131131
<uui-button-copy-text
132132
${spread(args)}
133133
@copied=${(event: UUICopyTextEvent) => {
134-
alert(`Copied text: ${event.detail.text}`);
134+
alert(`Copied text: ${event.text}`);
135135
}}></uui-button-copy-text>
136136
`,
137137
parameters: {
@@ -141,7 +141,7 @@ export const CopiedEvent: Story = {
141141
<uui-button-copy-text text="Copy this text"></uui-button-copy-text>
142142
<script>
143143
document.querySelector('uui-button-copy-text').addEventListener('copied', (event) => {
144-
alert(\`Copied text: \${event.detail.text}\`);
144+
alert(\`Copied text: \${event.text}\`);
145145
});
146146
</script>
147147
`,
@@ -159,7 +159,7 @@ export const ModifyClipboardContent: Story = {
159159
<uui-button-copy-text
160160
${spread(args)}
161161
@copying=${(event: UUICopyTextEvent) => {
162-
event.detail.text += ' - Modified before copying';
162+
event.text += ' - Modified before copying';
163163
}}></uui-button-copy-text>
164164
`,
165165
parameters: {
@@ -169,7 +169,7 @@ export const ModifyClipboardContent: Story = {
169169
<uui-button-copy-text text="Original text"></uui-button-copy-text>
170170
<script>
171171
document.querySelector('uui-button-copy-text').addEventListener('copying', (event) => {
172-
event.detail.text += ' - Modified before copying';
172+
event.text += ' - Modified before copying';
173173
});
174174
</script>
175175
`,

0 commit comments

Comments
 (0)