feat: Implement back button and selectable registrars

This commit is contained in:
Aleksi Lassila
2024-04-16 01:50:13 +03:00
parent f519fb7447
commit 32bde1ff9e
16 changed files with 197 additions and 89 deletions

View File

@@ -1,21 +1,36 @@
<script>
<script lang="ts">
import Container from '../../../Container.svelte';
</script>
import { type KeyEvent, type NavigateEvent, useRegistrar } from '../../selectable.js';
import { get } from 'svelte/store';
<Container
on:navigate={({ detail }) => {
if (
detail.direction === 'left' &&
detail.willLeaveContainer &&
detail.selectable === detail.options.target
) {
history.back();
const selectable = useRegistrar();
function handleGoBack({ detail }: CustomEvent<KeyEvent> | CustomEvent<NavigateEvent>) {
if ('willLeaveContainer' in detail) {
if (detail.direction !== 'left' || !detail.willLeaveContainer) return;
detail.preventNavigation();
}
}}
focusOnMount
trapFocus
class="fixed inset-0 z-20 bg-stone-950 overflow-y-auto"
>
<slot />
history.back();
}
function handleGoToTop({ detail }: CustomEvent<KeyEvent> | CustomEvent<NavigateEvent>) {
if ('willLeaveContainer' in detail) {
// Navigate event
if (detail.direction === 'left' && detail.willLeaveContainer) {
detail.preventNavigation();
get(selectable)?.focus();
}
} else {
// Back event
get(selectable)?.focus();
}
}
</script>
<Container class="fixed inset-0 z-20 bg-stone-950 overflow-y-auto" trapFocus direction="horizontal">
<Container />
<Container on:navigate={handleGoToTop} on:back={handleGoToTop} focusOnMount>
<slot {handleGoBack} registrar={selectable.registrar} />
</Container>
</Container>