feat: esc image preview

This commit is contained in:
Timothy J. Baek 2024-05-06 19:12:43 -07:00
parent 7874bea5f4
commit 04a9115254

View File

@ -1,8 +1,12 @@
<script lang="ts">
import { onMount } from 'svelte';
export let show = false;
export let src = '';
export let alt = '';
let mounted = false;
const downloadImage = (url, filename) => {
fetch(url)
.then((response) => response.blob())
@ -18,6 +22,27 @@
})
.catch((error) => console.error('Error downloading image:', error));
};
const handleKeyDown = (event: KeyboardEvent) => {
if (event.key === 'Escape') {
console.log('Escape');
show = false;
}
};
onMount(() => {
mounted = true;
});
$: if (mounted) {
if (show) {
window.addEventListener('keydown', handleKeyDown);
document.body.style.overflow = 'hidden';
} else {
window.removeEventListener('keydown', handleKeyDown);
document.body.style.overflow = 'unset';
}
}
</script>
{#if show}