feat: Esc for navigating back out of detached pages

This commit is contained in:
Aleksi Lassila
2024-05-07 16:45:43 +03:00
parent 88875c36b7
commit 7bae4273d7
4 changed files with 18 additions and 7 deletions

View File

@@ -3,7 +3,8 @@
import { type KeyEvent, type NavigateEvent, useRegistrar } from '../../selectable.js';
import { get } from 'svelte/store';
const selectable = useRegistrar();
// Top element, that when focused and back is pressed, will exit the modal
const topSelectable = useRegistrar();
function handleGoBack({ detail }: CustomEvent<KeyEvent> | CustomEvent<NavigateEvent>) {
if ('willLeaveContainer' in detail) {
@@ -11,7 +12,12 @@
detail.preventNavigation();
}
history.back();
const selectable = get(topSelectable);
if (selectable && get(selectable.focusIndex) === 0) {
history.back();
} else {
selectable?.focusChild(0) || selectable?.focus();
}
}
function handleGoToTop({ detail }: CustomEvent<KeyEvent> | CustomEvent<NavigateEvent>) {
@@ -19,11 +25,12 @@
// Navigate event
if (detail.direction === 'left' && detail.willLeaveContainer) {
detail.preventNavigation();
get(selectable)?.focus();
get(topSelectable)?.focus();
}
} else {
// Back event
get(selectable)?.focus();
const selectable = get(topSelectable);
selectable?.focusChild(0) || selectable?.focus();
}
}
</script>
@@ -35,6 +42,6 @@
>
<Container />
<Container on:navigate={handleGoToTop} on:back={handleGoToTop} focusOnMount>
<slot {handleGoBack} registrar={selectable.registrar} />
<slot {handleGoBack} registrar={topSelectable.registrar} />
</Container>
</Container>