Spaces:
Running
Running
svelte-5-leftover-mobile-nav (#1696)
Browse files* refactor: mobile nav svelte 5 (almost)
* refactor: default title smol change
---------
Co-authored-by: Nathan Sarrazin <[email protected]>
src/lib/components/MobileNav.svelte
CHANGED
@@ -1,16 +1,12 @@
|
|
1 |
-
<!-- @migration task: review uses of `navigating` -->
|
2 |
<script lang="ts">
|
3 |
-
import { run } from "svelte/legacy";
|
4 |
-
|
5 |
-
import { navigating } from "$app/state";
|
6 |
-
import { createEventDispatcher } from "svelte";
|
7 |
import { browser } from "$app/environment";
|
|
|
8 |
import { base } from "$app/paths";
|
9 |
-
import { page } from "$app/
|
10 |
-
|
|
|
11 |
import CarbonClose from "~icons/carbon/close";
|
12 |
import CarbonTextAlignJustify from "~icons/carbon/text-align-justify";
|
13 |
-
import IconNew from "$lib/components/icons/IconNew.svelte";
|
14 |
|
15 |
interface Props {
|
16 |
isOpen?: boolean;
|
@@ -20,25 +16,24 @@
|
|
20 |
|
21 |
let { isOpen = false, title = $bindable(), children }: Props = $props();
|
22 |
|
23 |
-
run(() => {
|
24 |
-
title = title ?? "New Chat";
|
25 |
-
});
|
26 |
-
|
27 |
let closeEl: HTMLButtonElement | undefined = $state();
|
28 |
let openEl: HTMLButtonElement | undefined = $state();
|
29 |
|
30 |
-
|
|
|
|
|
31 |
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
}
|
36 |
});
|
37 |
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
|
|
|
|
42 |
openEl?.focus();
|
43 |
}
|
44 |
});
|
@@ -54,13 +49,11 @@
|
|
54 |
aria-label="Open menu"
|
55 |
bind:this={openEl}><CarbonTextAlignJustify /></button
|
56 |
>
|
57 |
-
|
58 |
-
<
|
59 |
-
|
60 |
-
<span class="truncate px-4">{title ?? ""}</span>
|
61 |
-
{/await}
|
62 |
<a
|
63 |
-
class:invisible={
|
64 |
href="{base}/"
|
65 |
class="-mr-3 flex size-12 shrink-0 items-center justify-center text-lg"><IconNew /></a
|
66 |
>
|
|
|
|
|
1 |
<script lang="ts">
|
|
|
|
|
|
|
|
|
2 |
import { browser } from "$app/environment";
|
3 |
+
import { beforeNavigate } from "$app/navigation";
|
4 |
import { base } from "$app/paths";
|
5 |
+
import { page } from "$app/state";
|
6 |
+
import IconNew from "$lib/components/icons/IconNew.svelte";
|
7 |
+
import { createEventDispatcher } from "svelte";
|
8 |
import CarbonClose from "~icons/carbon/close";
|
9 |
import CarbonTextAlignJustify from "~icons/carbon/text-align-justify";
|
|
|
10 |
|
11 |
interface Props {
|
12 |
isOpen?: boolean;
|
|
|
16 |
|
17 |
let { isOpen = false, title = $bindable(), children }: Props = $props();
|
18 |
|
|
|
|
|
|
|
|
|
19 |
let closeEl: HTMLButtonElement | undefined = $state();
|
20 |
let openEl: HTMLButtonElement | undefined = $state();
|
21 |
|
22 |
+
$effect(() => {
|
23 |
+
title ??= "New Chat";
|
24 |
+
});
|
25 |
|
26 |
+
const dispatch = createEventDispatcher();
|
27 |
+
beforeNavigate(() => {
|
28 |
+
dispatch("toggle", false);
|
|
|
29 |
});
|
30 |
|
31 |
+
let shouldFocusClose = $derived(isOpen && closeEl);
|
32 |
+
let shouldRefocusOpen = $derived(!isOpen && browser && document.activeElement === closeEl);
|
33 |
+
$effect(() => {
|
34 |
+
if (shouldFocusClose) {
|
35 |
+
closeEl?.focus();
|
36 |
+
} else if (shouldRefocusOpen) {
|
37 |
openEl?.focus();
|
38 |
}
|
39 |
});
|
|
|
49 |
aria-label="Open menu"
|
50 |
bind:this={openEl}><CarbonTextAlignJustify /></button
|
51 |
>
|
52 |
+
<div class="flex h-full items-center justify-center">
|
53 |
+
<span class="truncate px-4" data-testid="chat-title">{title}</span>
|
54 |
+
</div>
|
|
|
|
|
55 |
<a
|
56 |
+
class:invisible={!page.params?.id}
|
57 |
href="{base}/"
|
58 |
class="-mr-3 flex size-12 shrink-0 items-center justify-center text-lg"><IconNew /></a
|
59 |
>
|