Skip to content

Commit 8684ea4

Browse files
committed
refactor: links types are in sync and support router_mode
1 parent e8313e3 commit 8684ea4

File tree

4 files changed

+51
-20
lines changed

4 files changed

+51
-20
lines changed

packages/pranx/FEATURES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@
1919
-[ ] Custom Error Pages
2020
-[ ] Websockets
2121
-[ ] Graphql
22+
-[ ] Env out of the box
Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,70 @@
11
import { type ComponentProps, forwardRef } from "preact/compat";
2+
import type { LinkProps } from "types/client";
23
import { _useAppContext } from "../app-context";
34

4-
export type LinkProps = Omit<ComponentProps<"a">, "href"> & {
5-
to: string;
6-
};
7-
85
export const Link = forwardRef<HTMLAnchorElement, LinkProps>((props, forwardedRef) => {
96
if (!props.to) {
107
throw new Error("Link element must provide a `to` property");
118
}
129

13-
if (typeof window !== "undefined" && window.pranx.router_mode === "spa") {
14-
const { set } = _useAppContext();
10+
if (typeof window === "undefined" || window.pranx.router_mode === "mpa") {
1511
return (
16-
<a
12+
<LinkMpa
13+
ref={forwardedRef}
14+
{...props}
15+
/>
16+
);
17+
}
18+
19+
if (window.pranx.router_mode === "spa") {
20+
return (
21+
<LinkSPA
1722
ref={forwardedRef}
1823
{...props}
19-
data-to={props.to}
20-
href={props.to}
21-
onClick={() => {
22-
set("prop_status", "loading");
23-
}}
24-
>
25-
{props.children}
26-
</a>
24+
/>
2725
);
2826
}
2927

28+
return <LinkFallback />;
29+
});
30+
31+
export const LinkSPA = forwardRef<HTMLAnchorElement, LinkProps>((props, forwardedRef) => {
32+
const { set } = _useAppContext();
3033
return (
3134
<a
3235
ref={forwardedRef}
3336
{...props}
3437
data-to={props.to}
3538
href={props.to}
39+
onClick={() => {
40+
set("prop_status", "loading");
41+
}}
3642
>
3743
{props.children}
3844
</a>
3945
);
4046
});
47+
48+
export const LinkMpa = forwardRef<HTMLAnchorElement, LinkProps>((props, forwardedRef) => {
49+
return (
50+
<a
51+
ref={forwardedRef}
52+
{...props}
53+
data-to={props.to}
54+
href={props.to}
55+
>
56+
{props.children}
57+
</a>
58+
);
59+
});
60+
61+
export const LinkFallback = forwardRef<HTMLAnchorElement, ComponentProps<"a">>(
62+
(props, forwardedRef) => {
63+
return (
64+
<a
65+
ref={forwardedRef}
66+
{...props}
67+
/>
68+
);
69+
}
70+
);

packages/pranx/src/client/mount.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { hydrate, type VNode } from "preact";
1+
import { type ComponentChild, hydrate } from "preact";
22

3-
export function mount(app: VNode<any>, root: Element | DocumentFragment): void {
3+
export function mount(app: ComponentChild, root: Element | DocumentFragment): void {
44
document.addEventListener("DOMContentLoaded", () => {
55
hydrate(app, root);
66
});

packages/pranx/types/client.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { ComponentProps, FunctionalComponent, Ref, VNode } from "preact";
1+
import { ComponentChild, ComponentProps, FunctionalComponent, Ref, VNode } from "preact";
22
import { PropsWithChildren } from "preact/compat";
33

4-
export type LinkProps = ComponentProps<"a"> & {
4+
export type LinkProps = Omit<ComponentProps<"a">, "href"> & {
55
to: string;
66
};
77

@@ -16,7 +16,7 @@ export declare const useAppContext: () => {
1616
props_status: "ready" | "loading" | "error";
1717
};
1818

19-
export function mount(app: any, root: Element | DocumentFragment): void;
19+
function mount(app: ComponentChild, root: Element | DocumentFragment): void;
2020

2121
export type PranxRouterProps = PropsWithChildren & {
2222
/**

0 commit comments

Comments
 (0)