-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patha.mjs
More file actions
67 lines (58 loc) · 1.55 KB
/
a.mjs
File metadata and controls
67 lines (58 loc) · 1.55 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
// router/a.mjs
import van from "vanjs-core";
import { matchRoute } from "./matchRoute.mjs";
import {
getValue,
isCurrentLocation,
isCurrentPage,
navigate,
} from "./helpers.mjs";
/** @typedef {typeof import("./types").A} A */
/**
* @type {A}
*/
export const A = (
/* istanbul ignore next */
{ href, children, ...rest } = {},
...otherChildren
) => {
/* istanbul ignore next - try again later */
const props = Object.fromEntries(
Object.entries(rest || {}).filter(([_, val]) => val !== undefined),
);
const preloaded = van.state(false);
const newProps = {
href,
...props,
onclick: async (e) => {
const HREF = getValue(href);
e.preventDefault();
/* istanbul ignore next */
if (isCurrentPage(HREF)) return;
// istanbul ignore else
if (typeof props.onclick === "function") {
await props.onclick(e);
}
navigate(HREF);
},
onmouseenter: async () => {
const HREF = getValue(href);
const route = matchRoute(HREF);
// istanbul ignore else
if (typeof props.onmouseenter === "function") {
await props.onmouseenter(e);
}
/* istanbul ignore else */
if (route?.component && !preloaded.val) {
route.component();
preloaded.val = true;
}
},
};
newProps["aria-current"] = van.derive(() => {
const isPage = isCurrentPage(href);
const isLocation = isCurrentLocation(href);
return isPage ? "page" : isLocation ? "location" : null;
});
return van.tags.a(newProps, children || otherChildren);
};