Skip to content

Commit 2393acf

Browse files
committed
fix: BottomNav and BottomNavItem activeUrl
1 parent cbfe384 commit 2393acf

File tree

6 files changed

+44
-13
lines changed

6 files changed

+44
-13
lines changed

src/lib/bottom-navigation/BottomNav.svelte

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
11
<script lang="ts">
22
import { setContext } from "svelte";
33
import { twMerge } from "tailwind-merge";
4+
import { writable } from 'svelte/store';
45
import { type BottomNavProps as Props, type BottomNavContextType, bottomNav } from "./index";
56
6-
let { children, header, position = "fixed", navType = "default", outerClass, innerClass, activeClass, ...restProps }: Props = $props();
7+
let { children, header, position = "fixed", navType = "default", outerClass, innerClass, activeClass, activeUrl = '', ...restProps }: Props = $props();
78
89
const activeCls = twMerge("text-primary-700 dark:text-primary-700 hover:text-primary-900 dark:hover:text-primary-900", activeClass);
910
11+
const activeUrlStore = writable('');
12+
setContext('activeUrl', activeUrlStore);
1013
setContext("navType", navType);
1114
setContext<BottomNavContextType>("bottomNavType", { activeClass: activeCls });
1215
1316
// $inspect('position: ', position);
1417
const { outer, inner } = bottomNav({ position, navType });
1518
const outerCls = $derived(outer({ class: outerClass }));
1619
const innerCls = $derived(inner({ class: innerClass }));
20+
21+
$effect(() => {
22+
activeUrlStore.set(activeUrl);
23+
})
1724
</script>
1825

1926
<div {...restProps} class={outerCls}>

src/lib/bottom-navigation/BottomNavItem.svelte

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,20 @@
77
88
const navType: BottomNavVariantType = getContext("navType");
99
const context = getContext<BottomNavContextType>("bottomNavType") ?? {};
10-
// console.log("context", context);
11-
let currentUrl = $state("");
10+
1211
let active: boolean = $state(false);
12+
13+
const activeUrlStore = getContext('activeUrl') as { subscribe: (callback: (value: string) => void) => void };
14+
15+
let navUrl = $state('');
16+
activeUrlStore.subscribe((value) => {
17+
navUrl = value;
18+
});
1319
// let btnCls: string = $state('');
1420
// let spanCls: string = $state('');
1521
const { base, span } = bottomNavItem({ navType, appBtnPosition });
1622
$effect(() => {
17-
currentUrl = window.location.pathname;
18-
active = href === currentUrl;
23+
active = navUrl ? href === navUrl : navUrl ? navUrl.startsWith(href) : false;
1924
});
2025
2126
let btnCls = $derived(twMerge(base({ class: btnClass }), active && (activeClass ?? context.activeClass)));

src/routes/components/bottom-navigation/examples/ApplicationBar.svelte

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
<script>
22
import { BottomNav, BottomNavItem, Skeleton, ImagePlaceholder } from "$lib";
33
import { HomeSolid, WalletSolid, AdjustmentsVerticalOutline, UserCircleSolid, PlusOutline } from "flowbite-svelte-icons";
4+
import { page } from '$app/stores';
5+
6+
let activeUrl = $state($page.url.pathname);
7+
$effect(() => {
8+
activeUrl = $page.url.pathname;
9+
})
410
</script>
511

612
<div class="relative flex flex-col p-6">
713
<Skeleton class="py-4" />
814
<ImagePlaceholder class="pb-20" />
915

10-
<BottomNav position="absolute" navType="application" innerClass="grid-cols-5">
16+
<BottomNav {activeUrl} position="absolute" navType="application" innerClass="grid-cols-5">
1117
<BottomNavItem btnName="Home" appBtnPosition="left" btnClass="static">
1218
<HomeSolid id="home" class="mb-1 h-6 w-6 text-gray-500 group-hover:text-primary-600 dark:text-gray-400 dark:group-hover:text-primary-500" />
1319
</BottomNavItem>

src/routes/components/bottom-navigation/examples/IconColor.svelte

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,19 @@
33
import { AdjustmentsVerticalSolid, HomeSolid, WalletSolid, UserCircleSolid } from "flowbite-svelte-icons";
44
let svgClass = "mb-1 text-pink-500 dark:text-pink-400 group-hover:text-pink-600 dark:group-hover:text-pink-500";
55
let svgActiveClass = "mb-1 text-green-500 dark:text-green-500 group-hover:text-green-700 dark:group-hover:text-green-700";
6-
let activeUrl = $state("");
6+
import { page } from '$app/stores';
77
8-
$effect(() => {
9-
activeUrl = window.location.pathname;
10-
});
8+
let activeUrl = $state($page.url.pathname);
9+
$effect(() => {
10+
activeUrl = $page.url.pathname;
11+
})
1112
</script>
1213

1314
<div class="relative flex flex-col p-6">
1415
<Skeleton class="py-4" />
1516
<ImagePlaceholder class="pb-20" />
1617

17-
<BottomNav position="absolute" innerClass="grid-cols-4">
18+
<BottomNav {activeUrl} position="absolute" innerClass="grid-cols-4">
1819
<BottomNavItem btnName="Home" href="/">
1920
<HomeSolid class={activeUrl === "/" ? svgActiveClass : svgClass} />
2021
</BottomNavItem>

src/routes/components/bottom-navigation/examples/LinkAndActiveClass.svelte

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
<script lang="ts">
22
import { Skeleton, ImagePlaceholder, BottomNav, BottomNavItem } from "$lib";
33
import { AdjustmentsVerticalSolid, HomeSolid, WalletSolid, UserCircleSolid } from "flowbite-svelte-icons";
4+
import { page } from '$app/stores';
5+
6+
let activeUrl = $state($page.url.pathname);
7+
$effect(() => {
8+
activeUrl = $page.url.pathname;
9+
})
410
</script>
511

612
<div class="relative flex flex-col p-6">
713
<Skeleton class="py-4" />
814
<ImagePlaceholder class="pb-20" />
915

10-
<BottomNav position="absolute" innerClass="grid-cols-4">
16+
<BottomNav {activeUrl} position="absolute" innerClass="grid-cols-4">
1117
<BottomNavItem btnName="Home" href="/">
1218
<HomeSolid />
1319
</BottomNavItem>

src/routes/components/bottom-navigation/examples/OverwritingActiveClass.svelte

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
<script lang="ts">
22
import { Skeleton, ImagePlaceholder, BottomNav, BottomNavItem } from "$lib";
33
import { AdjustmentsVerticalSolid, HomeSolid, WalletSolid, UserCircleSolid } from "flowbite-svelte-icons";
4+
import { page } from '$app/stores';
5+
6+
let activeUrl = $state($page.url.pathname);
7+
$effect(() => {
8+
activeUrl = $page.url.pathname;
9+
})
410
</script>
511

612
<div class="relative flex flex-col p-6">
713
<Skeleton class="py-4" />
814
<ImagePlaceholder class="pb-20" />
915

10-
<BottomNav position="absolute" innerClass="grid-cols-4" activeClass="font-bold text-green-500 hover:text-green-900 dark:hover:text-green-700 dark:text-green-300">
16+
<BottomNav {activeUrl} position="absolute" innerClass="grid-cols-4" activeClass="font-bold text-green-500 hover:text-green-900 dark:hover:text-green-700 dark:text-green-300">
1117
<BottomNavItem btnName="Home" href="/">
1218
<HomeSolid />
1319
</BottomNavItem>

0 commit comments

Comments
 (0)