|
4 | 4 | import { location } from '$lib/core/Location.js'; |
5 | 5 | import { resolveHashValue } from '$lib/core/resolveHashValue.js'; |
6 | 6 | import { getLinkContext, type ILinkContext } from '$lib/LinkContext/LinkContext.svelte'; |
| 7 | + import { isRouteActive } from '$lib/public-utils.js'; |
7 | 8 | import { getRouterContext } from '$lib/Router/Router.svelte'; |
8 | 9 | import type { ActiveState, Hash, RouteStatus } from '$lib/types.js'; |
9 | | - import { assertAllowedRoutingMode } from '$lib/utils.js'; |
| 10 | + import { assertAllowedRoutingMode, joinStyles } from '$lib/utils.js'; |
10 | 11 | import { type Snippet } from 'svelte'; |
11 | 12 | import type { HTMLAnchorAttributes } from 'svelte/elements'; |
12 | 13 |
|
|
92 | 93 | const calcReplace = $derived(replace ?? linkContext?.replace ?? false); |
93 | 94 | const calcPreserveQuery = $derived(preserveQuery ?? linkContext?.preserveQuery ?? false); |
94 | 95 | const calcPrependBasePath = $derived(prependBasePath ?? linkContext?.prependBasePath ?? false); |
95 | | - const isActive = $derived(!!router?.routeStatus[activeState?.key ?? '']?.match); |
| 96 | + const isActive = $derived(isRouteActive(router, activeState?.key)); |
96 | 97 | const calcHref = $derived( |
97 | 98 | calculateHref( |
98 | 99 | { |
|
111 | 112 | const newState = calculateState(resolvedHash, typeof state === 'function' ? state() : state); |
112 | 113 | location.goTo(calcHref, { state: newState, replace: calcReplace }); |
113 | 114 | } |
114 | | -
|
115 | | - function styleString() { |
116 | | - let baseStyle = style ? style.trim() : ''; |
117 | | - if (baseStyle && !baseStyle.endsWith(';')) { |
118 | | - baseStyle += ';'; |
119 | | - } |
120 | | - if (!activeState?.style) { |
121 | | - return baseStyle || undefined; |
122 | | - } |
123 | | - if (typeof activeState.style === 'string') { |
124 | | - return baseStyle ? `${baseStyle} ${activeState.style}` : activeState.style; |
125 | | - } |
126 | | - const calculatedStyle = Object.entries(activeState.style) |
127 | | - .map(([key, value]) => `${key}: ${value}`) |
128 | | - .join('; '); |
129 | | - return baseStyle ? `${baseStyle} ${calculatedStyle}` : calculatedStyle; |
130 | | - } |
131 | 115 | </script> |
132 | 116 |
|
133 | 117 | <a |
134 | 118 | href={calcHref} |
135 | 119 | class={[cssClass, (isActive && activeState?.class) || undefined]} |
136 | | - style={isActive ? styleString() : style} |
| 120 | + style={isActive ? joinStyles(style, activeState?.style) : style} |
137 | 121 | aria-current={isActive ? (activeState?.ariaCurrent ?? 'page') : undefined} |
138 | 122 | onclick={handleClick} |
139 | 123 | {...restProps} |
|
0 commit comments