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