Skip to content

Commit b79e7f8

Browse files
committed
Make animation restart/cancel work properly
1 parent d2a7229 commit b79e7f8

1 file changed

Lines changed: 18 additions & 28 deletions

File tree

app/app.vue

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ if (import.meta.server) {
3333
setJsonLd(createWebSiteSchema())
3434
}
3535
36+
let kbdAnimations: Animation[] = []
37+
3638
// Global keyboard shortcut: "/" focuses search or navigates to search page
3739
function handleGlobalKeydown(e: KeyboardEvent) {
3840
const target = e.target as HTMLElement
@@ -62,16 +64,25 @@ function handleGlobalKeydown(e: KeyboardEvent) {
6264
6365
if (e.key === '?') {
6466
e.preventDefault()
67+
68+
for (const anim of kbdAnimations) {
69+
anim.cancel()
70+
}
71+
kbdAnimations = []
72+
6573
const kbdElements = document.querySelectorAll('kbd')
6674
for (const kbd of kbdElements) {
67-
kbd.classList.remove('kbd-highlight')
68-
kbd.classList.add('kbd-highlight')
75+
const anim = kbd.animate(
76+
[
77+
{ boxShadow: 'none' },
78+
{ boxShadow: '0 0 4px 2px var(--accent)', offset: 0.15 },
79+
{ boxShadow: '0 0 4px 2px var(--accent)', offset: 0.6 },
80+
{ boxShadow: 'none' },
81+
],
82+
{ duration: 700, easing: 'ease-out' },
83+
)
84+
kbdAnimations.push(anim)
6985
}
70-
setTimeout(() => {
71-
for (const kbd of kbdElements) {
72-
kbd.classList.remove('kbd-highlight')
73-
}
74-
}, 700)
7586
}
7687
}
7788
@@ -95,24 +106,3 @@ if (import.meta.client) {
95106
<ScrollToTop />
96107
</div>
97108
</template>
98-
99-
<style>
100-
@keyframes kbd-highlight {
101-
0% {
102-
box-shadow: none;
103-
}
104-
15% {
105-
box-shadow: 0 0 4px 2px var(--accent);
106-
}
107-
60% {
108-
box-shadow: 0 0 4px 2px var(--accent);
109-
}
110-
100% {
111-
box-shadow: none;
112-
}
113-
}
114-
115-
kbd.kbd-highlight {
116-
animation: kbd-highlight 0.7s ease-out;
117-
}
118-
</style>

0 commit comments

Comments
 (0)