Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a6ac1d3

Browse files
committed
Better loading state
1 parent d98f615 commit a6ac1d3

File tree

5 files changed

+56
-12
lines changed

5 files changed

+56
-12
lines changed

‎app/login/page.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Link from 'next/link';
22
import { Form } from 'app/form';
33
import { signIn } from 'app/auth';
4+
import { SubmitButton } from 'app/submit-button';
45

56
export default function Login() {
67
return (
@@ -22,12 +23,7 @@ export default function Login() {
2223
});
2324
}}
2425
>
25-
<button
26-
type="submit"
27-
className="flex h-10 w-full items-center justify-center rounded-md border text-sm transition-all focus:outline-none"
28-
>
29-
Sign In
30-
</button>
26+
<SubmitButton>Sign in</SubmitButton>
3127
<p className="text-center text-sm text-gray-600">
3228
{"Don't have an account? "}
3329
<Link href="/register" className="font-semibold text-gray-800">

‎app/register/page.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import Link from 'next/link';
22
import { Form } from 'app/form';
33
import { redirect } from 'next/navigation';
44
import { createUser, getUser } from 'app/db';
5+
import { SubmitButton } from 'app/submit-button';
56

67
export default function Login() {
78
async function register(formData: FormData) {
@@ -28,12 +29,7 @@ export default function Login() {
2829
</p>
2930
</div>
3031
<Form action={register}>
31-
<button
32-
type="submit"
33-
className="flex h-10 w-full items-center justify-center rounded-md border text-sm transition-all focus:outline-none"
34-
>
35-
Sign Up
36-
</button>
32+
<SubmitButton>Sign Up</SubmitButton>
3733
<p className="text-center text-sm text-gray-600">
3834
{'Already have an account? '}
3935
<Link href="/login" className="font-semibold text-gray-800">

‎app/submit-button.tsx

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'use client';
2+
3+
import { useFormStatus } from 'react-dom';
4+
5+
export function SubmitButton({ children }: { children: React.ReactNode }) {
6+
const { pending } = useFormStatus();
7+
8+
return (
9+
<button
10+
type={pending ? 'button' : 'submit'}
11+
aria-disabled={pending}
12+
className="flex h-10 w-full items-center justify-center rounded-md border text-sm transition-all focus:outline-none"
13+
>
14+
{children}
15+
{pending && (
16+
<svg
17+
className="animate-spin ml-2 h-4 w-4 text-black"
18+
xmlns="http://www.w3.org/2000/svg"
19+
fill="none"
20+
viewBox="0 0 24 24"
21+
>
22+
<circle
23+
className="opacity-25"
24+
cx="12"
25+
cy="12"
26+
r="10"
27+
stroke="currentColor"
28+
strokeWidth="4"
29+
/>
30+
<path
31+
className="opacity-75"
32+
fill="currentColor"
33+
d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4z"
34+
/>
35+
</svg>
36+
)}
37+
<span aria-live="polite" className="sr-only" role="status">
38+
{pending ? 'Loading' : 'Submit form'}
39+
</span>
40+
</button>
41+
);
42+
}

‎package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"dependencies": {
1010
"@types/node": "^20.10.5",
1111
"@types/react": "^18.2.45",
12+
"@types/react-dom": "^18.2.18",
1213
"bcrypt-ts": "^5.0.0",
1314
"drizzle-orm": "^0.29.2",
1415
"geist": "^1.2.0",

‎pnpm-lock.yaml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /