копирование

This commit is contained in:
2026-10-04 12:17:54 +10:00
parent 5823a42e40
commit ecf39ee199
2 changed files with 55 additions and 22 deletions
+33 -3
View File
@@ -3,8 +3,8 @@ import type { NavItem } from "~/types";
import { CONTACTS } from "~/data/site"; import { CONTACTS } from "~/data/site";
/** /**
* Подвал с контактными данными. Клики по телефонам открывают звонок * Подвал с контактными данными. На мобильных клик по номеру начинает
* через tel:-ссылку. * звонок (tel:-ссылка), на ПК — копирует номер и показывает toast.
*/ */
interface Props { interface Props {
links: NavItem[]; links: NavItem[];
@@ -12,6 +12,8 @@ interface Props {
} }
const props = defineProps<Props>(); const props = defineProps<Props>();
const { toastVisible, toastMessage, onPhoneClick } = usePhoneCopy();
</script> </script>
<template> <template>
@@ -43,7 +45,11 @@ const props = defineProps<Props>();
class="fs-4" class="fs-4"
id="phoneNumber" id="phoneNumber"
> >
<a :href="phone.href" class="text-ycheba"> <a
:href="phone.href"
class="text-ycheba"
@click="onPhoneClick($event, phone.display)"
>
<img <img
src="/assets/ico/call.png" src="/assets/ico/call.png"
alt="Иконка трубки" alt="Иконка трубки"
@@ -75,5 +81,29 @@ const props = defineProps<Props>();
</div> </div>
</div> </div>
</div> </div>
<!-- Уведомление о копировании номера (ПК) -->
<div
v-show="toastVisible"
class="copy-toast"
role="status"
aria-live="polite"
>
{{ toastMessage }}
</div>
</footer> </footer>
</template> </template>
<style scoped>
.copy-toast {
position: fixed;
bottom: 20px;
right: 20px;
padding: 10px 20px;
background-color: rgba(0, 0, 0, 0.8);
color: #fff;
border-radius: 5px;
font-size: 14px;
z-index: 9999;
}
</style>
+22 -19
View File
@@ -1,8 +1,8 @@
/** /**
* Копирование номера телефона в буфер обмена. * Обработка клика по номеру телефона в подвале.
* Повторяет поведение assets/java/java.js: на мобильных устройствах * На мобильных устройствах работает стандартное поведение tel:-ссылки —
* инициируется звонок, на десктопе номер копируется и показывается * инициируется звонок. На ПК переход по tel: отменяется, номер копируется
* всплывающее уведомление, исчезающее через секунду. * в буфер обмена и показывается всплывающее уведомление на 2 секунды.
*/ */
export function usePhoneCopy() { export function usePhoneCopy() {
const toastVisible = ref(false); const toastVisible = ref(false);
@@ -16,22 +16,13 @@ export function usePhoneCopy() {
clearTimeout(hideTimer); clearTimeout(hideTimer);
hideTimer = setTimeout(() => { hideTimer = setTimeout(() => {
toastVisible.value = false; toastVisible.value = false;
}, 1000); }, 2000);
}; };
const copyPhone = async (number: string, phoneHref: string) => { const copyToClipboard = async (number: string) => {
const isMobile = /Mobi|Android|iPhone|iPad|iPod/i.test(
navigator.userAgent,
);
if (isMobile) {
window.location.href = phoneHref;
return;
}
try { try {
await navigator.clipboard.writeText(number); await navigator.clipboard.writeText(number);
showToast("Номер скопирован!"); showToast("Номер телефона скопирован");
} catch { } catch {
// Fallback для браузеров без Clipboard API // Fallback для браузеров без Clipboard API
const fallbackInput = document.createElement("input"); const fallbackInput = document.createElement("input");
@@ -40,15 +31,27 @@ export function usePhoneCopy() {
fallbackInput.select(); fallbackInput.select();
try { try {
document.execCommand("copy"); document.execCommand("copy");
showToast("Номер скопирован!"); showToast("Номер телефона скопирован");
} catch { } catch {
showToast("Не удалось скопировать."); showToast("Не удалось скопировать номер.");
} }
document.body.removeChild(fallbackInput); document.body.removeChild(fallbackInput);
} }
}; };
/**
* Клик по номеру: на телефоне — звонок (дефолт tel:-ссылки),
* на ПК — отмена перехода и копирование номера в буфер обмена.
*/
const onPhoneClick = (event: MouseEvent, number: string) => {
const isMobile = /Mobi|Android|iPhone|iPad|iPod/i.test(navigator.userAgent);
if (isMobile) return;
event.preventDefault();
void copyToClipboard(number);
};
onBeforeUnmount(() => clearTimeout(hideTimer)); onBeforeUnmount(() => clearTimeout(hideTimer));
return { toastVisible, toastMessage, copyPhone }; return { toastVisible, toastMessage, onPhoneClick };
} }