Files
front/composables/usePhoneCopy.ts
T
2026-09-26 19:30:59 +10:00

55 lines
1.7 KiB
TypeScript

/**
* Копирование номера телефона в буфер обмена.
* Повторяет поведение assets/java/java.js: на мобильных устройствах
* инициируется звонок, на десктопе номер копируется и показывается
* всплывающее уведомление, исчезающее через секунду.
*/
export function usePhoneCopy() {
const toastVisible = ref(false);
const toastMessage = ref("");
let hideTimer: ReturnType<typeof setTimeout> | undefined;
const showToast = (text: string) => {
toastMessage.value = text;
toastVisible.value = true;
clearTimeout(hideTimer);
hideTimer = setTimeout(() => {
toastVisible.value = false;
}, 1000);
};
const copyPhone = async (number: string, phoneHref: string) => {
const isMobile = /Mobi|Android|iPhone|iPad|iPod/i.test(
navigator.userAgent,
);
if (isMobile) {
window.location.href = phoneHref;
return;
}
try {
await navigator.clipboard.writeText(number);
showToast("Номер скопирован!");
} catch {
// Fallback для браузеров без Clipboard API
const fallbackInput = document.createElement("input");
fallbackInput.value = number;
document.body.appendChild(fallbackInput);
fallbackInput.select();
try {
document.execCommand("copy");
showToast("Номер скопирован!");
} catch {
showToast("Не удалось скопировать.");
}
document.body.removeChild(fallbackInput);
}
};
onBeforeUnmount(() => clearTimeout(hideTimer));
return { toastVisible, toastMessage, copyPhone };
}