|
3 | 3 |
|
4 | 4 | import { addWindowEventListener } from "./libs/util"; |
5 | 5 |
|
6 | | - export let link: LinkOrHref = null; |
7 | | - export let href: string = linkHref(link); |
8 | | - export let target: string = href.startsWith("?") ? undefined : "_blank"; |
| 6 | + export let href: LinkOrHref = null; |
| 7 | + export let target: string = null; |
9 | 8 | export let download: boolean | undefined = undefined; |
10 | 9 |
|
11 | | - $: label = linkLabel(link) ?? href; |
| 10 | + $: actualHref = linkHref(href); |
| 11 | + $: actualLabel = linkLabel(href) ?? actualHref; |
| 12 | + $: actualTarget = |
| 13 | + target ?? |
| 14 | + (!actualHref || actualHref.startsWith("?") ? undefined : "_blank"); |
12 | 15 |
|
13 | 16 | let search = window.location.search; |
14 | | - $: isCurrent = href.startsWith("?") && search === href; |
| 17 | + $: isCurrent = |
| 18 | + actualHref && actualHref.startsWith("?") && search === actualHref; |
15 | 19 |
|
16 | 20 | function onclick(e: MouseEvent) { |
17 | | - if (!e.ctrlKey && href.startsWith("?") && window.history?.pushState) { |
| 21 | + if (!e.ctrlKey && actualHref.startsWith("?") && window.history?.pushState) { |
18 | 22 | e.preventDefault(); |
19 | | - window.history.pushState(null, href, href); |
| 23 | + window.history.pushState(null, actualHref, actualHref); |
20 | 24 | window.dispatchEvent(new PopStateEvent("onpushstate", { state: null })); |
21 | 25 | } |
22 | 26 | } |
|
31 | 35 | <svelte:window on:popstate={onstatechanged} /> |
32 | 36 |
|
33 | 37 | {#if isCurrent} |
34 | | - <span class="selected"><slot>{label}</slot></span> |
| 38 | + <span class="selected"><slot>{actualLabel}</slot></span> |
35 | 39 | {:else} |
36 | | - <a {href} {target} {download} on:click={onclick} referrerpolicy="no-referrer" |
37 | | - ><slot>{label}</slot></a |
| 40 | + <a |
| 41 | + href={actualHref} |
| 42 | + target={actualTarget} |
| 43 | + {download} |
| 44 | + referrerpolicy="no-referrer" |
| 45 | + on:click={onclick}><slot>{actualLabel}</slot></a |
38 | 46 | > |
39 | 47 | {/if} |
0 commit comments