diff --git a/src/routes/[country]/[location]/[photo].svelte b/src/routes/[country]/[location]/[photo].svelte index 6181a56..e8bf55b 100644 --- a/src/routes/[country]/[location]/[photo].svelte +++ b/src/routes/[country]/[location]/[photo].svelte @@ -62,6 +62,9 @@ window.history.replaceState(null, '', $page.path.replace($page.params.photo, currentPhoto.slug)) } + // Define previous URL + $: previousUrl = $previousPage ? $previousPage : `/${location.country.slug}/${location.slug}` + /** * Photo navigation @@ -71,17 +74,23 @@ canGoPrev && currentIndex++ }, 200) - // Fo to previous photo + // Go to previous photo const goToPrevious = throttle(() => { canGoNext && (currentIndex = Math.max(currentIndex - 1, 0)) }, 200) + // Close viewer and go to previous page + const closeViewer = () => { + goto(previousUrl, { replaceState: false, noscroll: true, keepfocus: true }) + } + // Enable navigation with keyboard const handleKeydown = ({ key, defaultPrevented }: KeyboardEvent) => { if (defaultPrevented) return switch (key) { case 'ArrowLeft': goToPrevious(); break; case 'ArrowRight': goToNext(); break; + case 'Escape': closeViewer(); break; default: return; } } @@ -221,7 +230,7 @@