Compare commits

..

1 Commits

Author SHA1 Message Date
Tim
4b8229b0a1 feat: refresh base user avatar styling 2025-09-24 01:21:12 +08:00

View File

@@ -1,14 +1,18 @@
<template> <template>
<NuxtLink <component
:to="resolvedLink" :is="wrapperTag"
:to="isLink ? resolvedLink : undefined"
class="base-user-avatar" class="base-user-avatar"
:class="wrapperClass" :class="wrapperClass"
:style="wrapperStyle" :style="wrapperStyle"
v-bind="wrapperAttrs" v-bind="wrapperAttrs"
:role="isLink ? undefined : 'img'"
:aria-label="altText"
:title="altText"
> >
<BaseImage :src="currentSrc" :alt="altText" :class="imageClass" @error="onError" /> <span class="base-user-avatar-backdrop" aria-hidden="true" />
<span v-if="showInitial" class="base-user-avatar-initial">{{ userInitial }}</span> <BaseImage :src="currentSrc" :alt="altText" class="base-user-avatar-img" @error="onError" />
</NuxtLink> </component>
</template> </template>
<script setup> <script setup>
@@ -70,124 +74,52 @@ const resolvedLink = computed(() => {
const altText = computed(() => props.alt || '用户头像') const altText = computed(() => props.alt || '用户头像')
const identifier = computed(() => {
if (props.userId !== null && props.userId !== undefined && props.userId !== '') {
return String(props.userId)
}
if (props.alt && props.alt.trim()) {
return props.alt.trim()
}
return altText.value
})
const initialSource = computed(() => {
if (props.alt && props.alt.trim() && props.alt.trim() !== '用户头像') {
return props.alt.trim()
}
if (attrs.title && typeof attrs.title === 'string' && attrs.title.trim()) {
return attrs.title.trim()
}
if (props.userId !== null && props.userId !== undefined && props.userId !== '') {
return String(props.userId)
}
return ''
})
const isDefaultAvatar = computed(() => currentSrc.value === DEFAULT_AVATAR)
function parseCssSize(value) {
if (typeof value === 'number' && Number.isFinite(value)) {
return { numeric: value, unit: 'px' }
}
if (typeof value !== 'string') return null
const trimmed = value.trim()
if (!trimmed) return null
const directNumber = Number(trimmed)
if (!Number.isNaN(directNumber)) {
return { numeric: directNumber, unit: 'px' }
}
const match = trimmed.match(/^(-?\d*\.?\d+)([a-z%]+)$/i)
if (!match) return null
return { numeric: Number(match[1]), unit: match[2] }
}
const sizeStyle = computed(() => { const sizeStyle = computed(() => {
if (!props.width && props.width !== 0) return null if (!props.width && props.width !== 0) return null
const value = typeof props.width === 'number' ? `${props.width}px` : props.width const value = typeof props.width === 'number' ? `${props.width}px` : props.width
if (!value) return null if (!value) return null
const parsed = parseCssSize(value) return { width: value, height: value }
const style = { width: value, height: value, '--avatar-size': value }
if (parsed && Number.isFinite(parsed.numeric)) {
const computedFont = (parsed.numeric * 0.42).toFixed(2)
const normalized = computedFont.replace(/\.00$/, '')
style['--avatar-font-size'] = `${normalized}${parsed.unit}`
}
return style
}) })
function stringToColorSeed(value) { const accentHue = computed(() => {
if (!value) return 0 const seed = props.userId ?? props.alt
const source = seed !== undefined && seed !== null ? String(seed) : ''
if (!source) return 198
let hash = 0 let hash = 0
for (let i = 0; i < value.length; i += 1) { for (let index = 0; index < source.length; index += 1) {
hash = (hash << 5) - hash + value.charCodeAt(i) hash = (hash << 5) - hash + source.charCodeAt(index)
hash |= 0 hash |= 0
} }
return Math.abs(hash) return Math.abs(hash) % 360
} })
const accentStyle = computed(() => { const accentStyles = computed(() => {
if (!isDefaultAvatar.value) return null const hue = accentHue.value
const seed = stringToColorSeed(identifier.value)
const hue = seed % 360
const altHue = (hue + 37) % 360
const saturation = 72
const lightness = 78
const start = `hsl(${hue}, ${saturation}%, ${Math.min(lightness + 8, 95)}%)`
const end = `hsl(${altHue}, ${Math.max(saturation - 12, 45)}%, ${Math.max(lightness - 12, 48)}%)`
return { return {
'--avatar-background': `linear-gradient(135deg, ${start}, ${end})`, '--avatar-accent': `hsl(${hue}, 74%, 54%)`,
'--avatar-border-color': `hsla(${hue}, ${Math.max(saturation - 24, 32)}%, ${Math.max( '--avatar-accent-light': `hsl(${hue}, 95%, 82%)`,
lightness - 35, '--avatar-accent-soft': `hsl(${hue}, 96%, 95%)`,
28, '--avatar-accent-border': `hsla(${hue}, 70%, 48%, 0.28)`,
)}%, 0.55)`, '--avatar-accent-shadow': `hsla(${hue}, 68%, 36%, 0.2)`,
'--avatar-text-color': '#ffffff',
} }
}) })
const wrapperStyle = computed(() => { const wrapperStyle = computed(() => {
const attrStyle = attrs.style const attrStyle = attrs.style
return [ return [accentStyles.value, sizeStyle.value, attrStyle]
{ '--avatar-font-size': 'clamp(0.75rem, 0.6rem + 0.4vw, 1.75rem)' },
sizeStyle.value,
accentStyle.value,
attrStyle,
]
}) })
const isLink = computed(() => !props.disableLink && !!resolvedLink.value)
const wrapperTag = computed(() => (isLink.value ? 'NuxtLink' : 'div'))
const wrapperClass = computed(() => [ const wrapperClass = computed(() => [
attrs.class, attrs.class,
{ { 'is-rounded': props.rounded, 'is-interactive': isLink.value },
'is-rounded': props.rounded,
'has-default': isDefaultAvatar.value,
'is-interactive': !props.disableLink && Boolean(resolvedLink.value),
},
]) ])
const imageClass = computed(() => ['base-user-avatar-img', { 'is-default': isDefaultAvatar.value }])
const userInitial = computed(() => {
const source = initialSource.value || ''
const trimmed = source.trim()
if (!trimmed) return ''
const match = trimmed.match(/[\p{L}\p{N}]/u)
if (match && match[0]) return match[0].toUpperCase()
return trimmed.charAt(0).toUpperCase()
})
const showInitial = computed(() => isDefaultAvatar.value && Boolean(userInitial.value))
const wrapperAttrs = computed(() => { const wrapperAttrs = computed(() => {
const { class: _class, style: _style, ...rest } = attrs const { class: _class, style: _style, to: _to, href: _href, ...rest } = attrs
return rest return rest
}) })
@@ -200,28 +132,26 @@ function onError() {
<style scoped> <style scoped>
.base-user-avatar { .base-user-avatar {
--avatar-background: var(--avatar-placeholder-color, #f0f0f0); position: relative;
--avatar-border-color: rgba(15, 23, 42, 0.08);
--avatar-text-color: rgba(255, 255, 255, 0.86);
--avatar-shadow: 0 6px 18px rgba(15, 23, 42, 0.12);
--avatar-shadow-hover: 0 10px 24px rgba(15, 23, 42, 0.16);
--avatar-size: 3rem;
display: inline-flex; display: inline-flex;
align-items: center; align-items: center;
justify-content: center; justify-content: center;
overflow: hidden; overflow: hidden;
position: relative; border-radius: 16px;
background: var(--avatar-background); background: linear-gradient(
border: 1px solid var(--avatar-border-color); 140deg,
border-radius: var(--avatar-border-radius, 16px); var(--avatar-accent-soft, rgba(17, 182, 197, 0.12)) 0%,
box-shadow: var(--avatar-shadow); var(--avatar-accent-light, rgba(17, 182, 197, 0.22)) 100%
color: var(--avatar-text-color); );
border: 1px solid var(--avatar-accent-border, rgba(17, 182, 197, 0.2));
box-shadow:
0 1px 2px rgba(15, 52, 67, 0.08),
0 3px 8px var(--avatar-accent-shadow, rgba(17, 182, 197, 0.18));
transition: transition:
transform 0.28s ease, transform 0.3s ease,
box-shadow 0.32s ease, box-shadow 0.3s ease,
border-color 0.32s ease, border-color 0.3s ease,
filter 0.28s ease; background 0.3s ease;
isolation: isolate;
} }
.base-user-avatar.is-rounded { .base-user-avatar.is-rounded {
@@ -229,78 +159,57 @@ function onError() {
} }
.base-user-avatar:not(.is-rounded) { .base-user-avatar:not(.is-rounded) {
border-radius: var(--avatar-border-radius, 16px); border-radius: 0;
} }
.base-user-avatar.is-interactive { .base-user-avatar-backdrop {
cursor: pointer;
}
.base-user-avatar.is-interactive:hover,
.base-user-avatar.is-interactive:focus-visible {
transform: translateY(-1px) scale(1.01);
box-shadow: var(--avatar-shadow-hover);
border-color: rgba(59, 130, 246, 0.4);
}
.base-user-avatar.has-default::after {
content: '';
position: absolute; position: absolute;
inset: 1px; inset: 0;
border-radius: inherit;
background: rgba(15, 23, 42, 0.12);
mix-blend-mode: soft-light;
opacity: 0.25;
pointer-events: none; pointer-events: none;
transition: opacity 0.28s ease; background:
} radial-gradient(circle at 28% 28%, rgba(255, 255, 255, 0.72), transparent 62%),
linear-gradient(150deg, rgba(255, 255, 255, 0.08), transparent),
.base-user-avatar.has-default:hover::after, linear-gradient(
.base-user-avatar.has-default:focus-visible::after { 140deg,
opacity: 0.18; var(--avatar-accent-soft, rgba(17, 182, 197, 0.08)) 0%,
var(--avatar-accent-light, rgba(17, 182, 197, 0.18)) 100%
);
opacity: 0.75;
transition:
opacity 0.35s ease,
transform 0.35s ease;
z-index: 0;
} }
.base-user-avatar-img { .base-user-avatar-img {
position: relative;
width: 100%; width: 100%;
height: 100%; height: 100%;
object-fit: cover; object-fit: cover;
display: block; display: block;
filter: saturate(108%); z-index: 1;
transition: border-radius: inherit;
opacity 0.35s ease, transition: transform 0.35s ease;
transform 0.35s ease,
filter 0.35s ease;
} }
.base-user-avatar-img.is-default { .base-user-avatar.is-interactive:hover,
opacity: 0.32; .base-user-avatar.is-interactive:focus-visible {
filter: saturate(90%) brightness(1.05); transform: translateY(-1px) scale(1.02);
mix-blend-mode: multiply; border-color: var(--avatar-accent, var(--primary-color, #0a6e78));
box-shadow:
0 6px 16px var(--avatar-accent-shadow, rgba(17, 182, 197, 0.24)),
0 3px 6px rgba(15, 52, 67, 0.18);
outline: none;
} }
.base-user-avatar-initial { .base-user-avatar.is-interactive:hover .base-user-avatar-backdrop,
position: absolute; .base-user-avatar.is-interactive:focus-visible .base-user-avatar-backdrop {
inset: 0; opacity: 1;
display: flex; transform: scale(1.05);
align-items: center;
justify-content: center;
font-weight: 600;
font-size: var(--avatar-font-size, calc(var(--avatar-size) * 0.42));
text-transform: uppercase;
letter-spacing: 0.04em;
pointer-events: none;
user-select: none;
color: inherit;
text-shadow: 0 1px 4px rgba(15, 23, 42, 0.4);
} }
@media (prefers-reduced-motion: reduce) { .base-user-avatar.is-interactive:hover .base-user-avatar-img,
.base-user-avatar { .base-user-avatar.is-interactive:focus-visible .base-user-avatar-img {
transition: none; transform: scale(1.02);
}
.base-user-avatar-img {
transition: none;
}
} }
</style> </style>