|
| 1 | +<!-- Icon button that copies `text` and confirms with a drawn check. Motion follows |
| 2 | + Arkloop's CopyIconButton (c9ca6979e): the glyph shrinks out (60ms), the next |
| 3 | + one stays hidden for 75ms and then grows in (75ms scale / 50ms fade); the |
| 4 | + return to the copy glyph is the same swap. The check is held while the |
| 5 | + pointer rests on the button and returns only after it leaves; clicks are |
| 6 | + ignored until the copy glyph is back. |
| 7 | + `busy` (v-model) covers that whole span, so a caller that wraps the button |
| 8 | + in a tooltip can keep it closed while the check is the confirmation. The |
| 9 | + root is the Button itself, so it can sit under a TooltipTrigger as-child. --> |
| 10 | +<template> |
| 11 | + <Button |
| 12 | + type="button" |
| 13 | + variant="ghost" |
| 14 | + size="icon-sm" |
| 15 | + :aria-label="copied ? t('common.copied') : t('common.copy')" |
| 16 | + @click="handleCopy" |
| 17 | + @pointerenter="hovered = true" |
| 18 | + @pointerleave="handlePointerLeave" |
| 19 | + > |
| 20 | + <!-- Two layers: a caller may scale this outer span (e.g. a press), while |
| 21 | + the inner keyed span owns the swap transition, so neither overrides |
| 22 | + the other's `transition`. --> |
| 23 | + <span class="flex items-center justify-center"> |
| 24 | + <Transition |
| 25 | + name="copy-glyph" |
| 26 | + mode="out-in" |
| 27 | + @after-enter="handleGlyphEntered" |
| 28 | + > |
| 29 | + <span |
| 30 | + :key="copied ? 'check' : 'copy'" |
| 31 | + class="flex items-center justify-center" |
| 32 | + > |
| 33 | + <CopyAnimatedCheckIcon |
| 34 | + v-if="copied" |
| 35 | + :class="iconClass" |
| 36 | + :stroke-width="1.75" |
| 37 | + /> |
| 38 | + <!-- Mirrored so the stacked squares read top-left over bottom-right. --> |
| 39 | + <CopyConnectedIcon |
| 40 | + v-else |
| 41 | + :class="[iconClass, '-scale-x-100']" |
| 42 | + :stroke-width="1.75" |
| 43 | + /> |
| 44 | + </span> |
| 45 | + </Transition> |
| 46 | + </span> |
| 47 | + </Button> |
| 48 | +</template> |
| 49 | + |
| 50 | +<script setup lang="ts"> |
| 51 | +import { onBeforeUnmount, ref } from 'vue' |
| 52 | +import { useI18n } from 'vue-i18n' |
| 53 | +import { Button, toast, useClipboard } from '@felinic/ui' |
| 54 | +import CopyConnectedIcon from './copy-connected-icon.vue' |
| 55 | +import CopyAnimatedCheckIcon from './copy-animated-check-icon.vue' |
| 56 | +
|
| 57 | +const props = withDefaults(defineProps<{ |
| 58 | + text: string |
| 59 | + iconClass?: string |
| 60 | +}>(), { iconClass: 'size-[18px]' }) |
| 61 | +
|
| 62 | +// From the click until the copy glyph has fully re-entered. |
| 63 | +const busy = defineModel<boolean>('busy', { default: false }) |
| 64 | +
|
| 65 | +const { t } = useI18n() |
| 66 | +const { copyText } = useClipboard() |
| 67 | +
|
| 68 | +const copied = ref(false) |
| 69 | +const hovered = ref(false) |
| 70 | +// The hold ran out while the pointer was still on the button. |
| 71 | +let returnPending = false |
| 72 | +let holdTimer: ReturnType<typeof setTimeout> | null = null |
| 73 | +
|
| 74 | +async function handleCopy() { |
| 75 | + if (busy.value) return |
| 76 | + busy.value = true |
| 77 | + if (!await copyText(props.text)) { |
| 78 | + busy.value = false |
| 79 | + toast.error(t('common.copyFailed')) |
| 80 | + return |
| 81 | + } |
| 82 | + copied.value = true |
| 83 | +} |
| 84 | +
|
| 85 | +function handleGlyphEntered() { |
| 86 | + if (!copied.value) { |
| 87 | + busy.value = false |
| 88 | + return |
| 89 | + } |
| 90 | + // The 1.5s hold counts from the moment the check is fully in. |
| 91 | + holdTimer = setTimeout(() => { |
| 92 | + holdTimer = null |
| 93 | + if (hovered.value) returnPending = true |
| 94 | + else copied.value = false |
| 95 | + }, 1500) |
| 96 | +} |
| 97 | +
|
| 98 | +function handlePointerLeave() { |
| 99 | + hovered.value = false |
| 100 | + if (!returnPending) return |
| 101 | + returnPending = false |
| 102 | + copied.value = false |
| 103 | +} |
| 104 | +
|
| 105 | +onBeforeUnmount(() => { |
| 106 | + if (holdTimer) clearTimeout(holdTimer) |
| 107 | +}) |
| 108 | +</script> |
| 109 | + |
| 110 | +<style scoped> |
| 111 | +.copy-glyph-leave-active { |
| 112 | + transition: |
| 113 | + scale 60ms ease-in, |
| 114 | + opacity 60ms ease-in; |
| 115 | +} |
| 116 | +
|
| 117 | +/* The 75ms delay is the hidden gap before the incoming glyph grows in; the |
| 118 | + check's stroke is already drawing during it. */ |
| 119 | +.copy-glyph-enter-active { |
| 120 | + transition: |
| 121 | + scale 75ms ease-out 75ms, |
| 122 | + opacity 50ms ease-out 75ms; |
| 123 | +} |
| 124 | +
|
| 125 | +.copy-glyph-enter-from, |
| 126 | +.copy-glyph-leave-to { |
| 127 | + scale: 0.5; |
| 128 | + opacity: 0; |
| 129 | +} |
| 130 | +
|
| 131 | +@media (prefers-reduced-motion: reduce) { |
| 132 | + .copy-glyph-enter-active, |
| 133 | + .copy-glyph-leave-active { |
| 134 | + transition: none; |
| 135 | + } |
| 136 | +} |
| 137 | +</style> |
0 commit comments