|
| 1 | +<template> |
| 2 | + <div |
| 3 | + class="input-copy-core" |
| 4 | + data-testid="input-copy-core" |
| 5 | + :class="[elementClasses, { copied: isCopied }]" |
| 6 | + > |
| 7 | + <input |
| 8 | + :id |
| 9 | + :value |
| 10 | + type="text" |
| 11 | + readonly |
| 12 | + aria-readonly="true" |
| 13 | + class="input-copy-field" |
| 14 | + /> |
| 15 | + <InputButtonCore |
| 16 | + type="button" |
| 17 | + variant="inline" |
| 18 | + class="input-copy-button" |
| 19 | + :button-text="isCopied ? copiedLabel : copyLabel" |
| 20 | + :aria-label="isCopied ? copiedLabel : copyLabel" |
| 21 | + @click="handleCopy" |
| 22 | + > |
| 23 | + <template v-if="slots.icon" #left> |
| 24 | + <slot name="icon"></slot> |
| 25 | + </template> |
| 26 | + </InputButtonCore> |
| 27 | + </div> |
| 28 | +</template> |
| 29 | + |
| 30 | +<script setup lang="ts"> |
| 31 | +interface Props { |
| 32 | + id: string; |
| 33 | + value: string; |
| 34 | + copyLabel?: string; |
| 35 | + copiedLabel?: string; |
| 36 | + feedbackDuration?: number; |
| 37 | + styleClassPassthrough?: string | string[]; |
| 38 | +} |
| 39 | +
|
| 40 | +const props = withDefaults(defineProps<Props>(), { |
| 41 | + copyLabel: "Copy", |
| 42 | + copiedLabel: "Copied!", |
| 43 | + feedbackDuration: 2000, |
| 44 | + styleClassPassthrough: () => [], |
| 45 | +}); |
| 46 | +
|
| 47 | +const emit = defineEmits<{ |
| 48 | + copy: [value: string]; |
| 49 | +}>(); |
| 50 | +
|
| 51 | +const slots = useSlots(); |
| 52 | +const isCopied = ref(false); |
| 53 | +
|
| 54 | +const handleCopy = async () => { |
| 55 | + try { |
| 56 | + await navigator.clipboard.writeText(props.value); |
| 57 | + isCopied.value = true; |
| 58 | + emit("copy", props.value); |
| 59 | + await useSleep(props.feedbackDuration); |
| 60 | + isCopied.value = false; |
| 61 | + } catch { |
| 62 | + // Clipboard API unavailable — fail silently |
| 63 | + } |
| 64 | +}; |
| 65 | +
|
| 66 | +const { elementClasses, resetElementClasses } = useStyleClassPassthrough(props.styleClassPassthrough); |
| 67 | +
|
| 68 | +watch( |
| 69 | + () => props.styleClassPassthrough, |
| 70 | + () => { |
| 71 | + resetElementClasses(props.styleClassPassthrough); |
| 72 | + } |
| 73 | +); |
| 74 | +</script> |
| 75 | + |
| 76 | +<style lang="css"> |
| 77 | +@layer components { |
| 78 | + .input-copy-core { |
| 79 | + display: flex; |
| 80 | + align-items: stretch; |
| 81 | + overflow: hidden; |
| 82 | + border: var(--form-element-border-width) solid var(--theme-input-border); |
| 83 | + border-radius: var(--form-input-border-radius); |
| 84 | + background-color: var(--theme-input-surface); |
| 85 | + transition: all var(--theme-form-transition-duration) ease-in-out; |
| 86 | +
|
| 87 | + .input-copy-field { |
| 88 | + all: unset; |
| 89 | + flex-grow: 1; |
| 90 | + padding-block: var(--input-padding-block); |
| 91 | + padding-inline: var(--input-padding-inline); |
| 92 | + font-family: var(--font-family); |
| 93 | + font-size: var(--input-font-size); |
| 94 | + color: var(--theme-input-text-color-normal); |
| 95 | + cursor: default; |
| 96 | + overflow: hidden; |
| 97 | + text-overflow: ellipsis; |
| 98 | + white-space: nowrap; |
| 99 | + min-width: 0; |
| 100 | + } |
| 101 | +
|
| 102 | + .input-copy-button.input-button-core { |
| 103 | + border-radius: 0; |
| 104 | + border-inline-start: var(--form-element-border-width) solid var(--theme-input-border); |
| 105 | + padding-inline: var(--input-padding-inline); |
| 106 | + min-width: var(--input-min-height); |
| 107 | + background-color: var(--theme-button-secondary-surface); |
| 108 | + color: var(--theme-button-secondary-text); |
| 109 | +
|
| 110 | + &:hover { |
| 111 | + background-color: var(--theme-button-primary-surface); |
| 112 | + color: var(--theme-button-primary-text); |
| 113 | + } |
| 114 | +
|
| 115 | + &:focus-visible { |
| 116 | + outline: var(--form-element-outline-width-focus) solid var(--theme-input-outline-focus); |
| 117 | + outline-offset: -4px; |
| 118 | + } |
| 119 | + } |
| 120 | +
|
| 121 | + &.copied { |
| 122 | + --_copy-success-surface: light-dark(var(--green-01), var(--green-09)); |
| 123 | + --_copy-success-text: light-dark(var(--green-08), var(--green-01)); |
| 124 | +
|
| 125 | + .input-copy-button.input-button-core { |
| 126 | + background-color: var(--_copy-success-surface); |
| 127 | + color: var(--_copy-success-text); |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | +} |
| 132 | +</style> |
0 commit comments