-
Notifications
You must be signed in to change notification settings - Fork 177
Expand file tree
/
Copy pathcomponents.tsx
More file actions
79 lines (74 loc) · 2.32 KB
/
components.tsx
File metadata and controls
79 lines (74 loc) · 2.32 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
/*@refresh skip*/
import type { JSX } from "solid-js";
import { createMemo, mergeProps, splitProps } from "solid-js";
import { useHref, useLocation, useNavigate, useResolvedPath } from "./routing.js";
import type { Location, Navigator } from "./types.js";
import { normalizePath } from "./utils.js";
declare module "solid-js" {
namespace JSX {
interface AnchorHTMLAttributes<T> {
state?: string;
noScroll?: boolean;
replace?: boolean;
preload?: boolean;
link?: boolean;
}
}
}
export interface AnchorProps extends Omit<JSX.AnchorHTMLAttributes<HTMLAnchorElement>, "state"> {
href: string;
replace?: boolean | undefined;
noScroll?: boolean | undefined;
state?: unknown | undefined;
inactiveClass?: string | undefined;
activeClass?: string | undefined;
end?: boolean | undefined;
}
export function A(props: AnchorProps) {
props = mergeProps({ inactiveClass: "inactive", activeClass: "active" }, props);
const [, rest] = splitProps(props, [
"href",
"state",
"class",
"activeClass",
"inactiveClass",
"end"
]);
const to = useResolvedPath(() => props.href);
const href = useHref(to);
const location = useLocation();
const isActive = createMemo(() => {
const to_ = to();
if (to_ === undefined) return [false, false];
const path = normalizePath(to_.split(/[?#]/, 1)[0]).toLowerCase();
const loc = normalizePath(location.pathname).toLowerCase();
return [props.end ? path === loc : loc.startsWith(path + "/") || loc === path, path === loc];
});
return (
<a
{...rest}
href={href() || props.href}
state={JSON.stringify(props.state)}
classList={{
...(props.class && { [props.class]: true }),
[props.inactiveClass!]: !isActive()[0],
[props.activeClass!]: isActive()[0],
...rest.classList
}}
link
aria-current={isActive()[1] ? "page" : undefined}
/>
);
}
export interface NavigateProps {
href: ((args: { navigate: Navigator; location: Location }) => string) | string;
state?: unknown;
}
export function Navigate(props: NavigateProps) {
const navigate = useNavigate();
const location = useLocation();
const { href, state } = props;
const path = typeof href === "function" ? href({ navigate, location }) : href;
navigate(path, { replace: true, state });
return null;
}