Skip to content

Commit

Permalink
Use app router with interception (vercel#4)
Browse files Browse the repository at this point in the history
* Link codemod

* Image codemod

* Use pnpm

* Upgrade Next.js and React

* Use app router with interception
  • Loading branch information
timneutkens authored Apr 7, 2023
1 parent cfe4b1e commit f3cc917
Show file tree
Hide file tree
Showing 18 changed files with 492 additions and 1,056 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/.next
/node_modules
.vercel
14 changes: 14 additions & 0 deletions app/@modal/(..)photos/[id]/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Photo from "../../../../components/frame";
import Modal from "../../../../components/modal";
import swagPhotos from "../../../../photos";

export default function PhotoModal({ params: { id: photoId } }) {
const photos = swagPhotos;
const photo = photoId && photos.find((p) => p.id === photoId);

return (
<Modal>
<Photo photo={photo} />
</Modal>
);
}
3 changes: 3 additions & 0 deletions app/@modal/default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Default() {
return null;
}
3 changes: 3 additions & 0 deletions app/default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Default() {
return null;
}
12 changes: 12 additions & 0 deletions app/layout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import "../styles/reboot.css";

export default function Layout(props) {
return (
<html>
<body>
{props.children}
{props.modal}
</body>
</html>
);
}
33 changes: 33 additions & 0 deletions app/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import Link from "next/link";
import styles from "../styles/home.module.scss";
import swagPhotos from "../photos";
import BlurImage from "../components/BlurImage";

export default function Home() {
const photos = swagPhotos;

return (
<main className={styles.container}>
<div>
<h1>NextGram</h1>
</div>
<div className={styles.images}>
{photos.map(({ id, imageSrc }) => (
<div key={id} className={styles.imageContainer}>
<div key={id} className={styles.imageWrapper}>
<Link href={`/photos/${id}`}>
<BlurImage
alt=""
src={imageSrc}
height={500}
width={500}
objectFit="cover"
/>
</Link>
</div>
</div>
))}
</div>
</main>
);
}
16 changes: 16 additions & 0 deletions app/photos/[id]/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from "react";
import Photo from "../../../components/frame";
import swagPhotos from "../../../photos";
import { permalink, wrap } from "./styles.module.css";

export default function PhotoPage({ params: { id } }) {
const photo = swagPhotos.find((p) => p.id === id);

return (
<div className={permalink}>
<div className={wrap}>
<Photo photo={photo} />
</div>
</div>
);
}
10 changes: 10 additions & 0 deletions app/photos/[id]/styles.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.permalink {
padding: 100px;
text-align: center;
}

.wrap {
display: inline-block;
border: 1px solid #999;
margin: auto;
}
12 changes: 5 additions & 7 deletions components/BlurImage/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import Image from "next/image";
"use client";
import Image from "next/legacy/image";
import styles from "./styles.module.css";
import { useState } from "react";
import cn from "clsx";

export default function BlurImage(props) {
const [isLoading, setLoading] = useState(true);
// const [isLoading, setLoading] = useState(true);
return (
<Image
{...props}
className={cn(
styles["image-transition"],
isLoading ? styles["image-loading"] : styles["image-loaded"]
)}
onLoadingComplete={() => setLoading(false)}
className={cn(styles["image-transition"], styles["image-loaded"])}
// onLoadingComplete={() => setLoading(false)}
/>
);
}
2 changes: 1 addition & 1 deletion components/frame/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Image from "next/image";
import Image from "next/legacy/image";
import styles from "./styles.module.css";

export default function Photo({ photo }) {
Expand Down
28 changes: 23 additions & 5 deletions components/modal/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { useCallback, useRef } from 'react';
import Photo from '../frame';
import styles from './styles.module.css';
"use client";
import { useCallback, useRef, useEffect } from "react";
import styles from "./styles.module.css";
import { useRouter } from "next/navigation";

export default function Modal({ photo, onDismiss }) {
export default function Modal({ children }) {
const overlay = useRef();
const wrapper = useRef();
const router = useRouter();

const onDismiss = useCallback(() => {
router.back();
}, [router]);

const onClick = useCallback(
(e) => {
Expand All @@ -15,10 +21,22 @@ export default function Modal({ photo, onDismiss }) {
[onDismiss, overlay, wrapper]
);

const onKeyDown = useCallback(
(e) => {
if (e.key === "Escape") onDismiss();
},
[onDismiss]
);

useEffect(() => {
document.addEventListener("keydown", onKeyDown);
return () => document.removeEventListener("keydown", onKeyDown);
}, [onKeyDown]);

return (
<div ref={overlay} className={styles.overlay} onClick={onClick}>
<div ref={wrapper} className={styles.wrapper}>
<Photo photo={photo} />
{children}
</div>
</div>
);
Expand Down
9 changes: 8 additions & 1 deletion next.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
/**
* @type {import('next').NextConfig}
*/
module.exports = {
reactStrictMode: true,
images: {
domains: ['pbs.twimg.com'],
domains: ["pbs.twimg.com"],
},
experimental: {
appDir: true,
},
};
Loading

0 comments on commit f3cc917

Please sign in to comment.