feat: improve SocialLinks component accessibility and layout

This commit is contained in:
Cat Tom 2025-03-26 01:49:35 +08:00
parent f1e3d4b698
commit 7c19281020

View File

@ -1,93 +1,106 @@
<script setup>
import { computed } from "vue";
import { useMouseInElement } from "@vueuse/core";
import { useTheme } from "vuetify";
import { ref, computed } from "vue"
import { useMouseInElement } from "@vueuse/core"
import { useTheme } from "vuetify"
//
const theme = useTheme();
const theme = useTheme()
const cardRef = ref(null)
const { elementX, elementY } = useMouseInElement(cardRef)
//
const cardRef = ref(null);
const { elementX, elementY } = useMouseInElement(cardRef);
//
const socialPlatforms = [
{
name: "GitHub",
icon: "ri-github-fill",
url: "https://github.com/cattom",
color: "#181717",
hoverColor: "#6e5494",
ariaLabel: "访问我的GitHub主页",
},
{
name: "Twitter",
icon: "ri-twitter-x-fill",
url: "https://twitter.com/cattom",
color: "#000000",
hoverColor: "#1DA1F2",
ariaLabel: "在Twitter上关注我",
},
{
name: "LinkedIn",
icon: "ri-linkedin-fill",
url: "https://linkedin.com/in/cattom",
color: "#0A66C2",
hoverColor: "#0077B5",
ariaLabel: "查看我的LinkedIn资料",
},
{
name: "Email",
icon: "ri-mail-fill",
url: "mailto:hi@cattom.me",
color: "#D44638",
hoverColor: "#EA4335",
ariaLabel: "发送电子邮件给我",
},
{
name: "WeChat",
icon: "ri-wechat-fill",
color: "#07C160",
hoverColor: "#2DC100",
ariaLabel: "扫描我的微信二维码",
qrCode: "/assets/qr-wechat.png",
showQr: ref(false),
},
];
//
const cardTransform = computed(() => {
const MAX_TILT = 8;
const x = (elementX.value - (cardRef.value?.offsetWidth / 2 || 0)) / 20;
const y = ((cardRef.value?.offsetHeight / 2 || 0) - elementY.value) / 20;
return {
transform: `
perspective(1000px)
rotateX(${Math.min(Math.max(-y, -MAX_TILT), MAX_TILT)}deg)
rotateY(${Math.min(Math.max(x, -MAX_TILT), MAX_TILT)}deg)
`,
transition: "transform 0.5s cubic-bezier(0.03, 0.98, 0.52, 0.99)",
};
});
//
// props
const props = defineProps({
socialPlatforms: {
type: Array,
required: true
required: false,
default: () => [
{
name: "GitHub",
icon: "ri-github-fill",
url: "https://github.com/cattom",
color: "#181717",
hoverColor: "#6e5494",
ariaLabel: "访问我的GitHub主页"
},
{
name: "Twitter",
icon: "ri-twitter-x-fill",
url: "https://twitter.com/cattom",
color: "#000000",
hoverColor: "#1DA1F2",
ariaLabel: "在Twitter上关注我"
},
{
name: "LinkedIn",
icon: "ri-linkedin-fill",
url: "https://linkedin.com/in/cattom",
color: "#0A66C2",
hoverColor: "#0077B5",
ariaLabel: "查看我的LinkedIn资料"
},
{
name: "Email",
icon: "ri-mail-fill",
url: "mailto:hi@cattom.me",
color: "#D44638",
hoverColor: "#EA4335",
ariaLabel: "发送电子邮件给我"
},
{
name: "WeChat",
icon: "ri-wechat-fill",
color: "#07C160",
hoverColor: "#2DC100",
ariaLabel: "扫描我的微信二维码",
qrCode: "/assets/qr-wechat.png",
showQr: false
}
],
validator: platforms =>
Array.isArray(platforms) &&
platforms.every(p =>
typeof p === 'object' &&
p !== null &&
p.name &&
typeof p.name === 'string'
)
}
})
//
const qrStates = ref({})
const toggleQrCode = (platform) => {
if (platform.qrCode) {
qrStates.value[platform.name] = !qrStates.value[platform.name]
emit('showWechat', qrStates.value[platform.name])
}
}
const emit = defineEmits(['showWechat'])
//
const platformsWithQr = computed(() =>
props.socialPlatforms.filter(p => p.qrCode)
)
const cardTransform = computed(() => {
const MAX_TILT = 8
const x = (elementX.value - (cardRef.value?.offsetWidth / 2 || 0)) / 20
const y = ((cardRef.value?.offsetHeight / 2 || 0) - elementY.value) / 20
return {
transform: `
perspective(1000px)
rotateX(${Math.min(Math.max(-y, -MAX_TILT), MAX_TILT)}deg)
rotateY(${Math.min(Math.max(x, -MAX_TILT), MAX_TILT)}deg)
`,
transition: "transform 0.5s cubic-bezier(0.03, 0.98, 0.52, 0.99)"
}
})
//
const openLink = (url) => {
if (!url) return;
window.open(url, "_blank", "noopener,noreferrer");
};
if (!url) return
window.open(url, "_blank", "noopener,noreferrer")
}
</script>
<template>
@ -96,35 +109,46 @@ const openLink = (url) => {
<TransitionGroup name="fade" tag="div" class="qr-container">
<div
v-for="platform in platformsWithQr"
v-show="platform.showQr?.value"
v-show="qrStates[platform.name]"
:key="`qr-${platform.name}`"
class="qr-code"
>
<img :src="platform.qrCode" :alt="`${platform.name}二维码`" />
<img
:src="platform.qrCode"
:alt="`${platform.name}二维码`"
@click.stop
/>
<button
class="qr-close"
@click="toggleQrCode(platform)"
aria-label="关闭二维码"
>
&times;
</button>
</div>
</TransitionGroup>
<!-- 社交链接主体 -->
<div class="social-grid">
<div
v-for="(platform, index) in socialPlatforms"
v-for="(platform, index) in props.socialPlatforms"
:key="index"
class="social-item"
:style="{ '--hover-color': platform.hoverColor }"
@click="
platform.qrCode ? toggleQrCode(platform) : openLink(platform.url)
"
:style="{ '--hover-color': platform.hoverColor || platform.color }"
@click="platform.qrCode ? toggleQrCode(platform) : openLink(platform.url)"
role="button"
:aria-label="platform.ariaLabel || `${platform.name}链接`"
tabindex="0"
@keydown.enter="platform.qrCode ? toggleQrCode(platform) : openLink(platform.url)"
>
<div class="social-icon-wrapper">
<i
:class="platform.icon"
class="social-icon"
:aria-label="platform.ariaLabel"
:style="{ color: platform.color }"
/>
</div>
<span class="social-name">{{ platform.name }}</span>
<!-- 悬停光效 -->
<div class="hover-light" />
</div>
</div>
@ -138,17 +162,22 @@ const openLink = (url) => {
background: rgba(var(--v-theme-surface), 0.8);
backdrop-filter: blur(12px);
border-radius: 1.5rem;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1),
box-shadow:
0 10px 30px rgba(0, 0, 0, 0.1),
inset 0 0 0 1px rgba(255, 255, 255, 0.1);
will-change: transform;
overflow: hidden;
transition: background 0.3s ease, box-shadow 0.3s ease;
transition:
background 0.3s ease,
box-shadow 0.3s ease;
border: 1px solid rgba(255, 255, 255, 0.05);
}
/* 暗黑模式适配 */
[data-theme="dark"] .social-links-card {
:global([data-theme="dark"]) .social-links-card {
background: rgba(var(--v-theme-surface), 0.6);
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3),
box-shadow:
0 10px 30px rgba(0, 0, 0, 0.3),
inset 0 0 0 1px rgba(255, 255, 255, 0.05);
}
@ -169,6 +198,11 @@ const openLink = (url) => {
padding: 0.8rem 0;
border-radius: 0.8rem;
transition: all 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
outline: none;
}
.social-item:focus-visible {
box-shadow: 0 0 0 2px var(--hover-color);
}
.social-item:hover {
@ -191,7 +225,6 @@ const openLink = (url) => {
.social-icon {
font-size: 1.6rem;
color: var(--text-primary);
transition: all 0.3s ease;
}
@ -234,49 +267,69 @@ const openLink = (url) => {
}
/* 二维码样式 */
.qr-overlay {
.qr-container {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.7);
display: flex;
align-items: center;
justify-content: center;
z-index: 100;
backdrop-filter: blur(5px);
pointer-events: none;
}
.qr-container {
.qr-code {
background: white;
padding: 1.5rem;
border-radius: 1rem;
text-align: center;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
position: relative;
pointer-events: auto;
}
.qr-image {
.qr-code img {
width: 180px;
height: 180px;
margin-bottom: 0.5rem;
display: block;
margin: 0 auto;
}
.qr-hint {
color: var(--text-primary);
font-weight: 500;
margin-top: 0.5rem;
.qr-close {
position: absolute;
top: 0.5rem;
right: 0.5rem;
background: transparent;
border: none;
font-size: 1.5rem;
cursor: pointer;
color: #666;
width: 2rem;
height: 2rem;
display: flex;
align-items: center;
justify-content: center;
border-radius: 50%;
transition: all 0.2s ease;
}
.qr-close:hover {
background: #f0f0f0;
color: #333;
}
/* 过渡动画 */
.fade-enter-active,
.fade-leave-active {
transition: opacity 0.3s ease;
transition: all 0.3s ease;
}
.fade-enter-from,
.fade-leave-to {
opacity: 0;
transform: scale(0.95);
}
/* 响应式调整 */
@ -294,5 +347,14 @@ const openLink = (url) => {
.social-icon {
font-size: 1.4rem;
}
.qr-code {
padding: 1rem;
}
.qr-code img {
width: 150px;
height: 150px;
}
}
</style>
</style>