|
| 1 | +<script setup lang="ts"> |
| 2 | +const { accentColors, selectedAccentColor } = useAccentColor() |
| 3 | +const { convert: _convert, download: downloadBlob } = useSvgToPng() |
| 4 | +
|
| 5 | +const customAccent = ref<string | null>(null) |
| 6 | +const customBgDark = ref(true) |
| 7 | +const customLogoRef = useTemplateRef('customLogoRef') |
| 8 | +
|
| 9 | +const activeAccentId = computed(() => customAccent.value ?? selectedAccentColor.value ?? 'sky') |
| 10 | +const activeAccentColor = computed(() => { |
| 11 | + const match = accentColors.value.find(c => c.id === activeAccentId.value) |
| 12 | + return match?.value ?? accentColors.value[0]!.value |
| 13 | +}) |
| 14 | +
|
| 15 | +function getCustomSvgString(): string { |
| 16 | + const el = customLogoRef.value?.$el as SVGElement | undefined |
| 17 | + if (!el) return '' |
| 18 | + const clone = el.cloneNode(true) as SVGElement |
| 19 | + clone.querySelectorAll('[fill="currentColor"]').forEach((path) => { |
| 20 | + ;(path as SVGElement).setAttribute('fill', customBgDark.value ? '#fafafa' : '#0a0a0a') |
| 21 | + }) |
| 22 | + clone.querySelectorAll('[fill="var(--accent)"]').forEach((path) => { |
| 23 | + const style = getComputedStyle(path as SVGElement) |
| 24 | + ;(path as SVGElement).setAttribute('fill', style.fill || activeAccentColor.value) |
| 25 | + }) |
| 26 | + clone.removeAttribute('aria-hidden') |
| 27 | + clone.removeAttribute('class') |
| 28 | + return new XMLSerializer().serializeToString(clone) |
| 29 | +} |
| 30 | +
|
| 31 | +function downloadCustomSvg() { |
| 32 | + const svg = getCustomSvgString() |
| 33 | + if (!svg) return |
| 34 | + const blob = new Blob([svg], { type: 'image/svg+xml' }) |
| 35 | + const url = URL.createObjectURL(blob) |
| 36 | + const a = document.createElement('a') |
| 37 | + a.href = url |
| 38 | + a.download = `npmx-logo-${activeAccentId.value}.svg` |
| 39 | + a.click() |
| 40 | + URL.revokeObjectURL(url) |
| 41 | +} |
| 42 | +
|
| 43 | +const pngLoading = ref(false) |
| 44 | +
|
| 45 | +async function downloadCustomPng() { |
| 46 | + const svg = getCustomSvgString() |
| 47 | + if (!svg) return |
| 48 | + pngLoading.value = true |
| 49 | + try { |
| 50 | + await document.fonts.ready |
| 51 | + const blob = new Blob([svg], { type: 'image/svg+xml' }) |
| 52 | + const url = URL.createObjectURL(blob) |
| 53 | +
|
| 54 | + const img = new Image() |
| 55 | + const loaded = new Promise<void>((resolve, reject) => { |
| 56 | + img.onload = () => resolve() |
| 57 | + img.onerror = () => reject(new Error('Failed to load custom SVG')) |
| 58 | + }) |
| 59 | + img.src = url |
| 60 | + await loaded |
| 61 | +
|
| 62 | + const scale = 2 |
| 63 | + const canvas = document.createElement('canvas') |
| 64 | + canvas.width = 602 * scale |
| 65 | + canvas.height = 170 * scale |
| 66 | + const ctx = canvas.getContext('2d')! |
| 67 | + ctx.fillStyle = customBgDark.value ? '#0a0a0a' : '#ffffff' |
| 68 | + ctx.fillRect(0, 0, canvas.width, canvas.height) |
| 69 | + ctx.scale(scale, scale) |
| 70 | + ctx.drawImage(img, 0, 0, 602, 170) |
| 71 | +
|
| 72 | + canvas.toBlob((pngBlob) => { |
| 73 | + if (pngBlob) downloadBlob(pngBlob, `npmx-logo-${activeAccentId.value}.png`) |
| 74 | + URL.revokeObjectURL(url) |
| 75 | + }, 'image/png') |
| 76 | + } |
| 77 | + finally { |
| 78 | + pngLoading.value = false |
| 79 | + } |
| 80 | +} |
| 81 | +</script> |
| 82 | + |
| 83 | +<template> |
| 84 | + <section aria-labelledby="brand-customize-heading"> |
| 85 | + <h2 id="brand-customize-heading" class="text-lg text-fg uppercase tracking-wider mb-4"> |
| 86 | + {{ $t('brand.customize.title') }} |
| 87 | + </h2> |
| 88 | + <p class="text-fg-muted leading-relaxed mb-8"> |
| 89 | + {{ $t('brand.customize.description') }} |
| 90 | + </p> |
| 91 | + |
| 92 | + <div class="border border-border rounded-lg overflow-hidden"> |
| 93 | + <!-- Live preview --> |
| 94 | + <div |
| 95 | + class="flex items-center justify-center p-10 sm:p-16 transition-colors duration-300 motion-reduce:transition-none" |
| 96 | + :class="customBgDark ? 'bg-[#0a0a0a]' : 'bg-white'" |
| 97 | + > |
| 98 | + <AppLogo |
| 99 | + ref="customLogoRef" |
| 100 | + class="h-10 sm:h-14 w-auto max-w-full transition-colors duration-300 motion-reduce:transition-none" |
| 101 | + :class="customBgDark ? 'text-[#fafafa]' : 'text-[#0a0a0a]'" |
| 102 | + :style="{ '--accent': activeAccentColor }" |
| 103 | + /> |
| 104 | + </div> |
| 105 | + |
| 106 | + <!-- Controls --> |
| 107 | + <div class="border-t border-border p-4 sm:p-6 flex flex-col sm:flex-row sm:items-center gap-4"> |
| 108 | + <!-- Accent color picker --> |
| 109 | + <fieldset class="flex items-center gap-3 flex-1 border-none p-0 m-0"> |
| 110 | + <legend class="sr-only">{{ $t('brand.customize.accent_label') }}</legend> |
| 111 | + <span class="text-xs font-mono text-fg-muted shrink-0">{{ $t('brand.customize.accent_label') }}</span> |
| 112 | + <div class="flex items-center gap-1.5" role="radiogroup"> |
| 113 | + <button |
| 114 | + v-for="color in accentColors" |
| 115 | + :key="color.id" |
| 116 | + type="button" |
| 117 | + role="radio" |
| 118 | + :aria-checked="activeAccentId === color.id" |
| 119 | + :aria-label="color.label" |
| 120 | + class="w-6 h-6 rounded-full border-2 cursor-pointer transition-all duration-150 focus-visible:ring-2 focus-visible:ring-accent focus-visible:ring-offset-2 focus-visible:ring-offset-bg motion-reduce:transition-none" |
| 121 | + :class="activeAccentId === color.id ? 'border-fg scale-110' : 'border-transparent hover:border-border-hover'" |
| 122 | + :style="{ backgroundColor: color.value }" |
| 123 | + @click="customAccent = color.id" |
| 124 | + /> |
| 125 | + </div> |
| 126 | + </fieldset> |
| 127 | + |
| 128 | + <!-- Background toggle --> |
| 129 | + <div class="flex items-center gap-3"> |
| 130 | + <span class="text-xs font-mono text-fg-muted">{{ $t('brand.customize.bg_label') }}</span> |
| 131 | + <div class="flex items-center border border-border rounded-md overflow-hidden" role="radiogroup"> |
| 132 | + <button |
| 133 | + type="button" |
| 134 | + role="radio" |
| 135 | + :aria-checked="customBgDark" |
| 136 | + :aria-label="$t('brand.logos.on_dark')" |
| 137 | + class="px-2.5 py-1 text-xs font-mono cursor-pointer border-none transition-colors duration-150 focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-accent motion-reduce:transition-none" |
| 138 | + :class="customBgDark ? 'bg-bg-muted text-fg' : 'bg-transparent text-fg-muted hover:text-fg'" |
| 139 | + @click="customBgDark = true" |
| 140 | + > |
| 141 | + {{ $t('brand.logos.on_dark') }} |
| 142 | + </button> |
| 143 | + <button |
| 144 | + type="button" |
| 145 | + role="radio" |
| 146 | + :aria-checked="!customBgDark" |
| 147 | + :aria-label="$t('brand.logos.on_light')" |
| 148 | + class="px-2.5 py-1 text-xs font-mono cursor-pointer border-none border-is border-is-border transition-colors duration-150 focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-accent motion-reduce:transition-none" |
| 149 | + :class="!customBgDark ? 'bg-bg-muted text-fg' : 'bg-transparent text-fg-muted hover:text-fg'" |
| 150 | + @click="customBgDark = false" |
| 151 | + > |
| 152 | + {{ $t('brand.logos.on_light') }} |
| 153 | + </button> |
| 154 | + </div> |
| 155 | + </div> |
| 156 | + |
| 157 | + <!-- Download buttons --> |
| 158 | + <div class="flex items-center gap-2"> |
| 159 | + <ButtonBase |
| 160 | + size="sm" |
| 161 | + classicon="i-lucide:download" |
| 162 | + :aria-label="$t('brand.customize.download_svg_aria')" |
| 163 | + @click="downloadCustomSvg" |
| 164 | + > |
| 165 | + {{ $t('brand.logos.download_svg') }} |
| 166 | + </ButtonBase> |
| 167 | + <ButtonBase |
| 168 | + size="sm" |
| 169 | + classicon="i-lucide:download" |
| 170 | + :aria-label="$t('brand.customize.download_png_aria')" |
| 171 | + :disabled="pngLoading" |
| 172 | + @click="downloadCustomPng" |
| 173 | + > |
| 174 | + {{ $t('brand.logos.download_png') }} |
| 175 | + </ButtonBase> |
| 176 | + </div> |
| 177 | + </div> |
| 178 | + </div> |
| 179 | + </section> |
| 180 | +</template> |
0 commit comments