-
Notifications
You must be signed in to change notification settings - Fork 328
Expand file tree
/
Copy pathBottomNavItem.svelte
More file actions
94 lines (79 loc) · 2.75 KB
/
Copy pathBottomNavItem.svelte
File metadata and controls
94 lines (79 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<script lang="ts">
import type { BottomNavItemProps } from "$lib";
import { getTheme, warnThemeDeprecation } from "$lib/theme/themeUtils";
import clsx from "clsx";
import { getBottomNavContext } from "$lib/context";
import type { HTMLAnchorAttributes, HTMLButtonAttributes } from "svelte/elements";
import { bottomNavItem } from "./theme";
import { untrack } from "svelte";
let { children, btnName, appBtnPosition = "middle", activeClass, class: className, classes, btnClass, spanClass, active: manualActive, ...restProps }: BottomNavItemProps = $props();
warnThemeDeprecation(
"BottomNavItem",
untrack(() => ({ spanClass, btnClass })),
{ spanClass: "span", btnClass: "class" }
);
const styling = $derived(classes ?? { span: spanClass });
// Theme context
const theme = $derived(getTheme("bottomNavItem"));
const context = getBottomNavContext();
let navUrl = $derived(context?.activeUrl || "");
const { base, span } = $derived(bottomNavItem({ navType: context?.navType, appBtnPosition }));
// Determine active state based on manual prop or URL matching
let isActive = $derived.by(() => {
const href = restProps.href ?? "";
return manualActive !== undefined
? !!manualActive
: navUrl
? href === "/"
? navUrl === "/"
: href && (navUrl === href || navUrl.startsWith(href + "/") || (href !== "/" && navUrl.replace(/^https?:\/\/[^/]+/, "").startsWith(href)))
: false;
});
function getCommonClass() {
return base({ class: clsx(isActive && (activeClass ?? context?.activeClass), theme?.base, className ?? btnClass) });
}
function getSpanClass() {
return span({ class: clsx(isActive && (activeClass ?? context?.activeClass), theme?.span, styling.span) });
}
/* eslint-disable @typescript-eslint/no-explicit-any */
const commonProps: Record<string, any> = $derived({
"aria-label": btnName,
class: getCommonClass(),
...restProps
});
const anchorProps: HTMLAnchorAttributes = $derived({
...commonProps
});
const buttonProps: HTMLButtonAttributes = $derived({
...commonProps,
type: "button" as const
});
</script>
{#if restProps.href === undefined}
<button {...buttonProps}>
{@render children()}
<span class={getSpanClass()}>{btnName}</span>
</button>
{:else}
<a {...anchorProps}>
{@render children()}
<span class={getSpanClass()}>{btnName}</span>
</a>
{/if}
<!--
@component
[Go to docs](https://flowbite-svelte.com/)
## Type
[BottomNavItemProps](https://github.com/themesberg/flowbite-svelte/blob/main/src/lib/types.ts#L280)
## Props
@prop children
@prop btnName
@prop appBtnPosition = "middle"
@prop activeClass
@prop class: className
@prop classes
@prop btnClass
@prop spanClass
@prop active: manualActive
@prop ...restProps
-->