Skip to content

Commit 22b0e1c

Browse files
authored
Allow custom backNavigation (#607)
On parent component, add `on:backNavigation={handler}` to `NavBar` component. In handler, call `event.preventDefault()` to prevent the default behavior of going back to the main page.
1 parent c838ac7 commit 22b0e1c

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/lib/components/Navbar.svelte

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,18 @@ The navbar component.
1515
} from '$lib/data/stores';
1616
import { page } from '$app/stores';
1717
import { base } from '$app/paths';
18+
import { createEventDispatcher } from 'svelte';
19+
import { goto } from '$app/navigation';
1820
1921
$: actionBarColor = $s['ui.bar.action']['background-color'];
22+
23+
const dispatch = createEventDispatcher();
24+
function handleBackNavigation() {
25+
const shouldContinue = dispatch('backNavigation', { detail: $page.route.id}, {cancelable: true});
26+
if (shouldContinue) {
27+
goto(`${base}/`);
28+
}
29+
}
2030
</script>
2131

2232
<!--
@@ -33,13 +43,15 @@ The navbar component.
3343
<HamburgerIcon color="white" />
3444
</label>
3545
{:else}
36-
<a href="{base}/" class="dy-btn dy-btn-ghost dy-btn-circle">
46+
<!-- svelte-ignore a11y-click-events-have-key-events -->
47+
<!-- svelte-ignore a11y-no-static-element-interactions -->
48+
<div on:click={handleBackNavigation} class="dy-btn dy-btn-ghost dy-btn-circle">
3749
{#if $direction === 'ltr'}
3850
<ArrowBackIcon color="white" />
3951
{:else}
4052
<ArrowForwardIcon color="white" />
4153
{/if}
42-
</a>
54+
</div>
4355
{/if}
4456
<slot name="left-buttons" />
4557
</div>

0 commit comments

Comments
 (0)