-
Notifications
You must be signed in to change notification settings - Fork 23
/
KCopy.vue
317 lines (281 loc) · 7.98 KB
/
KCopy.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
<template>
<div class="k-copy">
<span
v-if="badge && badgeLabel"
class="copy-badge-text"
>
{{ badgeLabel }}
</span>
<div
class="copy-container"
:class="{ 'copy-element': truncate || badge, 'badge-styles': badge }"
>
<KTooltip
v-if="format !== 'hidden'"
:class="[textTooltipClasses]"
data-testid="copy-tooltip-wrapper"
placement="bottomStart"
position-fixed
:text="textTooltipLabel"
>
<div
ref="copyTextElement"
class="copy-text"
:class="{ 'monospace': monospace || !badge }"
>
{{ textFormat }}
</div>
</KTooltip>
<KTooltip
class="text-icon-wrapper"
max-width="500px"
placement="bottomStart"
position-fixed
:text="tooltipText"
>
<KClipboardProvider v-slot="{ copyToClipboard }">
<button
:id="copyButtonElementId"
:aria-label="tooltipText"
class="copy-to-clipboard-button"
data-testid="copy-to-clipboard"
type="button"
@click.stop="copyIdToClipboard(copyToClipboard)"
>
<CopyIcon
class="text-icon"
decorative
:size="KUI_ICON_SIZE_30"
/>
</button>
</KClipboardProvider>
</KTooltip>
</div>
</div>
</template>
<script setup lang="ts">
import type { PropType } from 'vue'
import { computed, ref, watch, onMounted, onUnmounted } from 'vue'
import { v4 as uuid4 } from 'uuid'
import { ResizeObserverHelper } from '@/utilities/resizeObserverHelper'
import { CopyIcon } from '@kong/icons'
import KClipboardProvider from '@/components/KClipboardProvider'
import KTooltip from '@/components/KTooltip/KTooltip.vue'
import { KUI_ICON_SIZE_30 } from '@kong/design-tokens'
const props = defineProps({
/**
* Text displayed before the copyable text when
* `badge` is true
*/
badgeLabel: {
type: String,
default: '',
},
/**
* The copyable text
*/
text: {
type: String,
required: true,
},
/**
* Tooltip text displayed on hover over the `text`
*/
textTooltip: {
type: String,
default: '',
},
/**
* Tooltip text displayed on hover over copy button
*/
copyTooltip: {
type: String,
default: '',
},
/**
* Formatting for copyable text (default, hidden, redacted, deleted)
*/
format: {
type: String as PropType<'default' | 'hidden' | 'redacted' | 'deleted'>,
required: false,
default: 'default',
validator: (val: string) => ['default', 'hidden', 'redacted', 'deleted'].includes(val),
},
/**
* Whether or not to display as a badge
*/
badge: {
type: Boolean,
default: false,
},
/**
* Whether or not to use monospace font
*/
monospace: {
type: Boolean,
default: false,
},
/**
* Whether or not the text should be truncated
*/
truncate: {
type: Boolean,
default: false,
},
/**
* Tooltip text displayed on successful copy
*/
successTooltip: {
type: String,
default: 'Copied!',
},
/**
* Number of characters to truncate at
*/
truncationLimit: {
type: [Number, String],
default: 8,
},
})
const copyButtonElementId = uuid4()
const tooltipText = ref<string>('')
const nonSuccessText = computed((): string => {
if (!props.badgeLabel || props.copyTooltip) {
return props.copyTooltip || 'Copy'
}
// strip trailing colon from label if one exists
return `Copy ${props.badgeLabel.replace(/:$/, '')}`
})
watch(nonSuccessText, (value: string): void => {
tooltipText.value = value
}, { immediate: true })
// Computed for dynamic classes
const textTooltipClasses = computed((): string => {
const tooltipWrapperClass = 'copy-tooltip-wrapper' // this selector is referenced in vars.scss - do not update without checking for usage in there first
return `${tooltipWrapperClass} ${(props.truncate && isTruncated.value) || props.badge ? 'truncate-content' : ''}`
})
const textFormat = computed(() => {
if (props.format === 'redacted') {
return '*****'
} else if (props.format === 'deleted') {
return `*${String(props.text || '').substring(0, 5)}`
}
// This regex will only remove the quotes if they are the first and last characters of the string (truncateLimitText)
return (props.truncate && props.truncationLimit && truncateLimitText.value) ? truncateLimitText.value.replace(/^"(.*)"$/, '$1') : props.text
})
const textTooltipLabel = computed((): string | undefined => {
if (props.textTooltip) {
return props.textTooltip
}
// don't show text tooltip if text is redacted or not truncated
if (props.format === 'redacted' || !isTruncated.value) {
return undefined
}
return props.text
})
const updateTooltipText = (msg?: string): void => {
tooltipText.value = msg || props.successTooltip
setTimeout(() => {
tooltipText.value = nonSuccessText.value
}, 1800)
}
const copyIdToClipboard = (executeCopy: (prop: string) => boolean) => {
if (!executeCopy(props.text)) {
updateTooltipText('Failed to copy')
return
}
updateTooltipText()
}
const copy = () => {
if (document.getElementById(copyButtonElementId)) {
document.getElementById(copyButtonElementId)?.click()
}
}
defineExpose({
copy,
})
const copyTextElement = ref<HTMLDivElement>()
const resizeObserver = ref<ResizeObserverHelper>()
const truncateLimitText = computed((): string | null => props.truncate && typeof props.truncationLimit === 'number' ? `${String(props.text || '').substring(0, props.truncationLimit) + '...'}` : null)
const isTruncated = ref<boolean>(false)
const setTruncation = (): void => {
if (!props.truncate) {
return
}
if (props.truncationLimit !== 'auto' && truncateLimitText.value) {
isTruncated.value = true
} else if (props.truncationLimit === 'auto' && copyTextElement.value) {
isTruncated.value = copyTextElement.value.offsetWidth < copyTextElement.value.scrollWidth
}
}
onMounted(() => {
resizeObserver.value = ResizeObserverHelper.create(setTruncation)
resizeObserver.value.observe(copyTextElement.value as HTMLDivElement)
})
onUnmounted(() => {
if (resizeObserver.value) {
resizeObserver.value.unobserve(copyTextElement.value as HTMLDivElement)
}
})
</script>
<style lang="scss" scoped>
/* Component styles */
.k-copy {
align-items: center;
display: flex;
max-width: 100%; /** necessary for truncation */
.copy-element {
align-items: center;
display: inline-flex;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
.copy-text {
@include truncate;
}
.truncate-content {
display: inline-block;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
}
.badge-styles {
@include badgeWrapper;
@include decorativeBadgeAppearance;
}
.copy-container {
align-items: center;
display: flex;
font-size: var(--kui-font-size-20, $kui-font-size-20);
gap: var(--kui-space-30, $kui-space-30);
line-height: var(--kui-line-height-20, $kui-line-height-20);
white-space: nowrap;
}
.monospace {
font-family: var(--kui-font-family-code, $kui-font-family-code);
font-weight: var(--kui-font-weight-regular, $kui-font-weight-regular);
}
.text-icon-wrapper {
align-items: center;
cursor: pointer;
display: flex;
.text-icon:not(.k-button .k-copy .text-icon-wrapper .text-icon):not(.badge-styles .text-icon-wrapper .text-icon) {
&:hover,
&:focus {
// only applies to non-badge as for badge the mixin takes care hover styles
color: var(--kui-color-text-neutral-strong, $kui-color-text-neutral-strong) !important;
}
}
}
.copy-badge-text {
color: var(--kui-color-text-neutral, $kui-color-text-neutral);
font-size: var(--kui-font-size-20, $kui-font-size-20);
line-height: var(--kui-line-height-20, $kui-line-height-20);
margin-right: var(--kui-space-20, $kui-space-20);
}
.copy-to-clipboard-button {
@include defaultButtonReset;
}
}
</style>