<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sammy Mathias Wanyama</title>
    <description>The latest articles on DEV Community by Sammy Mathias Wanyama (@wanyama413).</description>
    <link>https://dev.to/wanyama413</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1425087%2Fcf4db127-890e-4289-bccb-688ca5293ab7.png</url>
      <title>DEV Community: Sammy Mathias Wanyama</title>
      <link>https://dev.to/wanyama413</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/wanyama413"/>
    <language>en</language>
    <item>
      <title>Implementing Photo Modal in Next JS using Parallel Routes &amp; Intercepting Routes</title>
      <dc:creator>Sammy Mathias Wanyama</dc:creator>
      <pubDate>Tue, 16 Jul 2024 11:08:39 +0000</pubDate>
      <link>https://dev.to/wanyama413/implementing-photo-modal-in-next-js-using-parallel-routes-intercepting-routes-2dpn</link>
      <guid>https://dev.to/wanyama413/implementing-photo-modal-in-next-js-using-parallel-routes-intercepting-routes-2dpn</guid>
      <description>&lt;p&gt;Implementing Modals using parallel routes &amp;amp; intercepting routes provided in Next JS app router offers the following benefits:&lt;/p&gt;

&lt;p&gt;1.Ability to share the modal content using a url&lt;/p&gt;

&lt;p&gt;2.Preserving context when the page is refreshed, instead of closing the modal.&lt;/p&gt;

&lt;p&gt;3.Closing the Modal on backward navigation &amp;amp; re-opening the modal on forward navigation&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F79akf2wknyirh9t78rj6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F79akf2wknyirh9t78rj6.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Why use with Parallel Routes?&lt;/p&gt;

&lt;p&gt;Parallel Routes in Next JS are created by defining ‘slots’ which is a folder created by prepending an ‘@’ before the folder name e.g @modal or @photogrid&lt;/p&gt;

&lt;p&gt;Paralle Routes work same way as Componentization where you can have different UI blocks in a single page with their own routes and logic. Using parallel routes however have the following benefits:-&lt;/p&gt;

&lt;p&gt;Parallel Routes can be streamed independently, allowing you to define independent error and loading states for each route.&lt;br&gt;
Next.js will perform a partial render, changing the subpage within the slot, while maintaining the other slot’s active subpages, even if they don’t match the current URL.&lt;br&gt;
These 2 Routing techniques would therefore help us to implement a modal in Next JS and make sure that:&lt;/p&gt;

&lt;p&gt;We can share the Modal through a url (Intercepting Routes)&lt;br&gt;
On page refresh or shareable link navigation, modal context is preserved. (Intercepting Routes)&lt;br&gt;
Keeping the context of the photo grid when navigating a modal (Parallel Routes)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx2fniim5fxp3dqhvkl4a.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx2fniim5fxp3dqhvkl4a.jpg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In your app directory, create a blogs folder with the following logic to create a photo grid in its page.jsx file&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import Link from "next/link";
import blogs from "../../blogs";
import Image from "next/image";

const Blogs = () =&amp;gt; {
  return (
    &amp;lt;div className="grid grid-cols-2 place-items-center place-content-center gap-5 sm:grid-cols-4"&amp;gt;
      {blogs.map((blog) =&amp;gt; {
        return (
          &amp;lt;article key={blog.id}&amp;gt;
            &amp;lt;Link href={`/blogs/${blog.title}`}&amp;gt;
              &amp;lt;Image alt={blog.title} src={blog.url} width={250} height={250} /&amp;gt;
            &amp;lt;/Link&amp;gt;
          &amp;lt;/article&amp;gt;
        );
      })}
    &amp;lt;/div&amp;gt;
  );
};

export default Blogs;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Then create a dynamic route for each photo from the grid,&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react";
import blogs from "../../../blogs";
const Blog = ({ params }) =&amp;gt; {
  const find = blogs.find((blog) =&amp;gt; blog.title == params.title);
  return &amp;lt;div&amp;gt;{find.title}&amp;lt;/div&amp;gt;;
};

export default Blog;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Then create a parallel route by creating a folder in the app directory starting with an @ as in the screenshot below @modal.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F67to0zft96ia7c20v6oj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F67to0zft96ia7c20v6oj.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Every parallel route must have a default.js file for when the page gets refreshed and only one of the routes is matched by Next JS, the content in the default.jsx file gets displayed for the other slots. On our case we return a null as we only want to show a modal when the user clicks on a photo.&lt;/p&gt;

&lt;p&gt;Inside the @modal slot create an intercepting route which intercepts the blog route we create earlier. If the slot is in the same segment as the intercepted route, you use only a single dot i.e (.) followed by the folder name of the intercepted route folder name.&lt;/p&gt;

&lt;p&gt;(.) to match segments on the same level&lt;br&gt;
(..) to match segments one level above&lt;br&gt;
(..)(..) to match segments two levels above&lt;br&gt;
(...) to match segments from the root app directory&lt;br&gt;
You might say since the (.)blogs is inside the @modal folder which is inturn inside the app folder then it is 1 segment above but parallel routes/slots do not affect routing, you can’t navigate to /slots.&lt;/p&gt;

&lt;p&gt;Inside the intercepted route dynamic route i.e page.jsx file, paste the following code&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react";
import blogs from "../../../../blogs";
import Modal from "@/components/Modal";
import Image from "next/image";
const InterceptedRoute = ({ params }) =&amp;gt; {
  const finder = blogs.find((blog) =&amp;gt; blog.title == params.title);
  return (
    &amp;lt;Modal&amp;gt;
      &amp;lt;article&amp;gt;
        &amp;lt;Image src={finder.url} alt={finder.title} width={400} height={400} /&amp;gt;
      &amp;lt;/article&amp;gt;
    &amp;lt;/Modal&amp;gt;
  );
};

export default InterceptedRoute;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The code to the modal component is&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"use client";
import { useCallback, useRef, useEffect, MouseEventHandler } from "react";
import { useRouter } from "next/navigation";

export default function Modal({ children }: { children: React.ReactNode }) {
  const overlay = useRef(null);
  const wrapper = useRef(null);
  const router = useRouter();

  const onDismiss = useCallback(() =&amp;gt; {
    router.back();
  }, [router]);

  const onClick: MouseEventHandler = useCallback(
    (e) =&amp;gt; {
      if (e.target === overlay.current || e.target === wrapper.current) {
        if (onDismiss) onDismiss();
      }
    },
    [onDismiss, overlay, wrapper]
  );

  const onKeyDown = useCallback(
    (e: KeyboardEvent) =&amp;gt; {
      if (e.key === "Escape") onDismiss();
    },
    [onDismiss]
  );

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

  return (
    &amp;lt;div
      ref={overlay}
      className="fixed z-10 left-0 right-0 top-0 bottom-0 mx-auto bg-black/60 "
      onClick={onClick}
    &amp;gt;
      &amp;lt;div
        ref={wrapper}
        className="w-[100%] absolute top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2 sm:w-10/12 md:w-8/12 lg:w-2/5 p-6"
      &amp;gt;
        {children}
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The slot can now be accessed as a prop in the root layout:-&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react";
import "./globals.css";

const RootLayout = ({ children, modal }) =&amp;gt; {
  return (
    &amp;lt;html lang="en"&amp;gt;
      &amp;lt;body className="p-5 sm:p-[80px] h-[100vh] "&amp;gt;
        {children}
        {modal}
      &amp;lt;/body&amp;gt;
    &amp;lt;/html&amp;gt;
  );
};

export default RootLayout;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;You may need to restart your development server and then THAT'S IT!&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>frontend</category>
    </item>
  </channel>
</rss>
