-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(react-server): router transition state (#224)
- Loading branch information
Showing
23 changed files
with
367 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
packages/react-server/examples/basic/src/components/global-progress.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
"use client"; | ||
|
||
import { useServerTransitionState } from "@hiogawa/react-server/client"; | ||
import { cls } from "./utils"; | ||
|
||
export function GlobalProgress() { | ||
const ctx = useServerTransitionState(); | ||
|
||
return ( | ||
<> | ||
<div | ||
className={cls( | ||
"antd-spin w-5 h-5 text-colorInfo transition duration-500", | ||
ctx.isPending ? "opacity-100" : "opacity-0", | ||
)} | ||
></div> | ||
<div | ||
className={cls( | ||
"antd-spin w-5 h-5 text-colorWarning transition duration-500", | ||
ctx.isActionPending ? "opacity-100" : "opacity-0", | ||
)} | ||
></div> | ||
</> | ||
); | ||
} |
5 changes: 4 additions & 1 deletion
5
packages/react-server/examples/basic/src/components/header.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,15 @@ | ||
import { GlobalProgress } from "./global-progress"; | ||
|
||
export function Header() { | ||
return ( | ||
<div className="flex items-center gap-2"> | ||
<div className="flex items-center gap-3"> | ||
<h1 className="text-lg font-bold">RSC Experiment</h1> | ||
<a | ||
className="antd-link i-ri-github-line w-6 h-6" | ||
href="https://github.com/hi-ogawa/vite-plugins/tree/main/packages/react-server" | ||
target="_blank" | ||
/> | ||
<GlobalProgress /> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const cls = (...args: unknown[]) => args.filter(Boolean).join(" "); |
6 changes: 4 additions & 2 deletions
6
packages/react-server/examples/basic/src/routes/demo/waku_02/_client.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
packages/react-server/examples/basic/src/routes/test/transition/_action.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
"use server"; | ||
|
||
import { sleep } from "@hiogawa/utils"; | ||
|
||
let counter = 0; | ||
|
||
export function getCounter() { | ||
return counter; | ||
} | ||
|
||
export async function changeCounter(formData: FormData) { | ||
const delta = Number(formData.get("delta")); | ||
if (delta === -1) { | ||
await sleep(2000); | ||
} else { | ||
await sleep(200); | ||
} | ||
counter += delta; | ||
} |
66 changes: 66 additions & 0 deletions
66
packages/react-server/examples/basic/src/routes/test/transition/_client.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
"use client"; | ||
|
||
import { | ||
Link, | ||
useRouter, | ||
useServerTransitionState, | ||
} from "@hiogawa/react-server/client"; | ||
import { cls } from "../../../components/utils"; | ||
import { changeCounter } from "./_action"; | ||
|
||
const TABS = [ | ||
["/test/transition", "About"], | ||
["/test/transition?sleep=2000", "Posts (2.0 sec)"], | ||
["/test/transition?sleep=200", "Contact (0.2 sec)"], | ||
] as const; | ||
|
||
export function Tablist() { | ||
const { isPending } = useServerTransitionState(); | ||
const router = useRouter(); | ||
|
||
return ( | ||
<ul className="antd-tablist flex gap-5 px-2"> | ||
{TABS.map(([href, name]) => ( | ||
<Link | ||
key={href} | ||
href={href} | ||
className={cls( | ||
"antd-tab py-1.5", | ||
href === router.history.location.href && isPending && "opacity-50", | ||
)} | ||
aria-selected={href === router.history.location.href} | ||
> | ||
<li key={href}>{name}</li> | ||
</Link> | ||
))} | ||
</ul> | ||
); | ||
} | ||
|
||
export function Counter(props: { value: number }) { | ||
const { isActionPending } = useServerTransitionState(); | ||
|
||
return ( | ||
<form action={changeCounter} className="flex flex-col items-start gap-2"> | ||
<div className="flex items-center gap-2"> | ||
<button | ||
className="antd-btn antd-btn-default px-2" | ||
name="delta" | ||
value={-1} | ||
> | ||
-1 (2.0 sec) | ||
</button> | ||
<button | ||
className="antd-btn antd-btn-default px-2" | ||
name="delta" | ||
value={+1} | ||
> | ||
+1 (0.2 sec) | ||
</button> | ||
<div className={cls(isActionPending && "opacity-50")}> | ||
Count: {props.value} | ||
</div> | ||
</div> | ||
</form> | ||
); | ||
} |
22 changes: 22 additions & 0 deletions
22
packages/react-server/examples/basic/src/routes/test/transition/layout.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import type { LayoutProps } from "@hiogawa/react-server/server"; | ||
import { getCounter } from "./_action"; | ||
import { Counter, Tablist } from "./_client"; | ||
|
||
// similar demo | ||
// https://react.dev/reference/react/useTransition#marking-a-state-update-as-a-non-blocking-transition | ||
|
||
export default async function Layout(props: LayoutProps) { | ||
return ( | ||
<div className="w-lg flex flex-col gap-4 p-4"> | ||
<div className="border p-3 flex flex-col gap-2"> | ||
<h4 className="font-bold">Navigation State</h4> | ||
<Tablist /> | ||
<div className="p-2">{props.children}</div> | ||
</div> | ||
<div className="border p-3 flex flex-col gap-2"> | ||
<h4 className="font-bold">Action state</h4> | ||
<Counter value={getCounter()} /> | ||
</div> | ||
</div> | ||
); | ||
} |
9 changes: 9 additions & 0 deletions
9
packages/react-server/examples/basic/src/routes/test/transition/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import type { PageProps } from "@hiogawa/react-server/server"; | ||
import { sleep } from "@hiogawa/utils"; | ||
|
||
export default async function Page(props: PageProps) { | ||
const url = new URL(props.request.url); | ||
const ms = Number(url.searchParams.get("sleep")); | ||
await sleep(ms); | ||
return <p>Took {ms / 1000} sec to load.</p>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
"use client"; | ||
|
||
export { Link } from "./lib/components/link"; | ||
export { useServerTransitionState, useRouter } from "./lib/client/router"; |
Oops, something went wrong.