Skip to content

Commit 51c59b7

Browse files
feat: a bit of loading state in login button
1 parent 4013fc6 commit 51c59b7

File tree

3 files changed

+57
-11
lines changed

3 files changed

+57
-11
lines changed

src/app/login/github.tsx

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,50 @@
22
import { Button } from "@/components/ui/button";
33
import Image from "next/image";
44
import { signIn } from "@/lib/auth";
5+
import { useTransition } from "react";
6+
import { cn } from "@/lib/utils";
7+
import { Spinner } from "@/components/spinner";
58

69
export function Github({ callbackURL }: { callbackURL: string }) {
10+
const [isLoading, start] = useTransition();
711
return (
812
<Button
913
type="submit"
1014
variant="secondary"
1115
onClick={async () => {
16+
start(
17+
() =>
18+
new Promise((res) => {
19+
setTimeout(res, 1000);
20+
}),
21+
);
1222
await signIn.social({
1323
provider: "github",
1424
callbackURL,
1525
});
1626
}}
17-
className="grid w-full min-w-60 grid-cols-[auto_1fr] grid-rows-1 space-x-2 bg-white py-6 hover:bg-white/80"
27+
className={cn(
28+
"w-full min-w-52 py-6",
29+
isLoading
30+
? "flex cursor-not-allowed items-center justify-center bg-white/20 text-white hover:bg-white/20"
31+
: "grid grid-cols-[auto_1fr] grid-rows-1 bg-white hover:bg-white/80",
32+
)}
1833
>
19-
<Image
20-
className="justify-self-end"
21-
src={`https://authjs.dev/img/providers/github.svg`}
22-
unoptimized
23-
alt={`logo of github`}
24-
width={24}
25-
height={24}
26-
/>
27-
<span className="justify-self-start text-black">GitHub</span>
34+
{isLoading ? (
35+
<Spinner className="h-5 w-5 dark:fill-white dark:text-gray-400" />
36+
) : (
37+
<>
38+
<Image
39+
className="justify-self-end"
40+
src={`https://authjs.dev/img/providers/github.svg`}
41+
unoptimized
42+
alt={`logo of github`}
43+
width={24}
44+
height={24}
45+
/>
46+
<span className="justify-center text-black">Sign in with GitHub</span>
47+
</>
48+
)}
2849
</Button>
2950
);
3051
}

src/components/spinner.tsx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { cn } from "@/lib/utils"
2+
3+
export function Spinner({ className }: React.HtmlHTMLAttributes<SVGElement>) {
4+
return (
5+
<div role="status">
6+
<svg
7+
aria-hidden="true"
8+
className={cn("h-8 w-8 animate-spin fill-secondary text-gray-200 dark:text-gray-600", className )}
9+
viewBox="0 0 100 101"
10+
fill="none"
11+
xmlns="http://www.w3.org/2000/svg"
12+
>
13+
<path
14+
d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z"
15+
fill="currentColor"
16+
/>
17+
<path
18+
d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z"
19+
fill="currentFill"
20+
/>
21+
</svg>
22+
<span className="sr-only">Loading...</span>
23+
</div>
24+
);
25+
}

src/components/ui/button.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { cva, type VariantProps } from "class-variance-authority";
55
import { cn } from "@/lib/utils";
66

77
const buttonVariants = cva(
8-
"inline-flex items-center justify-center gap-2 whitespace-nowrap rounded-md text-sm font-medium transition-colors focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-30 [&_svg]:pointer-events-none [&_svg]:size-4 [&_svg]:shrink-0",
8+
"inline-flex items-center justify-center gap-2 whitespace-nowrap rounded-md text-sm font-medium transition-colors focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-30 [&_svg]:pointer-events-none [&_svg]:shrink-0",
99
{
1010
variants: {
1111
variant: {

0 commit comments

Comments
 (0)