run4w4y / nextjs-router-events

A router events alternative for Next 13+ with app directory
MIT License
36 stars 3 forks source link

Issues not applicable to backspace event #1

Open jiwoniverse opened 1 year ago

jiwoniverse commented 1 year ago

Hi! I am currently studying front-end development. I used your library during the project, and I opened the issue because I had a question. (The project is using Next.js 13, and App router.)

After install your nextjs-router-events library, and I want to prevent by pop-up a modal window when a user leaves the write page.

The modal pops up well when I try to leave via <Link> in navigation bar, but the problem is that it doesn't pop up normally when leaves using browser-native navigation methods(refresh or backspace).

My current code is as follows.

// app/layout.tsx (Root-layout)
import QueryProvider from "./QueryProvider";
import Header from "@/components/Header";
import "@/styles/globals.css";
import type { Metadata } from "next";
import Recoil from "./Recoil";
import { ToastContainer } from "react-toastify";
import "react-toastify/ReactToastify.css";
import { RouteChangesProvider } from "nextjs-router-events";

export default function RootLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  return (
    <html lang="en">
      <body>
        <Recoil>
          <QueryProvider>
            {/* wrapped in provider */}
            <RouteChangesProvider>
              <Header />
              {children}
              <ToastContainer />
            </RouteChangesProvider>
          </QueryProvider>
        </Recoil>
      </body>
      />
    </html>
  );
}
// app/community/write/page.tsx
"use client";
import { NextPage } from "next";
import AddPost from "@/components/community/write/AddPost";
import { useEffect, useState } from "react";
import supabase from "@/libs/supabase";
import { useRouter } from "next/navigation";
import useLeaveConfirm from "@/hooks/useLeaveConfirm";

const Write: NextPage = () => {
  const router = useRouter();
  const [sessionState, setSessionState] = useState<any>(null);

  useEffect(() => {
    const getSessionState = async () => {
      const { data: session, error } = await supabase.auth.getSession();
      if (!session.session) router.push("/");
      setSessionState(session.session);
    }
    getSessionState();
  }, [router]);

  const preventClose = (e: BeforeUnloadEvent) => {
    e.preventDefault();
    e.returnValue = '';
  };

  useEffect(() => {
    window.addEventListener('beforeunload', preventClose);
    return () => {
      window.removeEventListener('beforeunload', preventClose);
    };
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, []);

  const confirmModal = useLeaveConfirm(true);

  return (
    <>
      <AddPost />
      {confirmModal}
    </>
  )
}

export default Write;
// app/hooks/useBeforeUnload.tsx
import { useEffect } from "react"

// NOTE: although there is a message argument, you really should not be relying on it, as most, if not all, modern browsers completely ignore it anyways
const useBeforeUnload = (shouldPreventUnload: boolean, message?: string) => {
  useEffect(() => {
    const abortController = new AbortController()

    if (shouldPreventUnload)
      window.addEventListener('beforeunload', (ev) => {
        ev.preventDefault()

        return (ev.returnValue = message ?? '')
      }, { capture: true, signal: abortController.signal })

    return () => abortController.abort()
  }, [shouldPreventUnload, message])
}

export default useBeforeUnload;
// app/hooks/useLeaveConfirm.tsx
import { useCallback, useState } from "react"
import { useRouteChangeEvents } from "nextjs-router-events"
import useBeforeUnload from './useBeforeUnload' // read further for an explanation
import FreezeModal from "@/components/community/ui/FreezeModal";
import { useRouter } from "next/navigation";

const useLeaveConfirm = (shouldPreventRouteChange: boolean) => {
  const router = useRouter();
  const [openModal, setOpenModal] = useState(false);
  const onBeforeRouteChange = useCallback(() => {
    if (shouldPreventRouteChange) {
      setOpenModal(true)
      return false
    }
    return true
  }, [shouldPreventRouteChange])

  const { allowRouteChange } = useRouteChangeEvents({ onBeforeRouteChange })
  useBeforeUnload(shouldPreventRouteChange)

  console.log("custom hook openModal >>> ", openModal);

  return (
    <FreezeModal
      open={openModal}
      onOpenChange={setOpenModal}
      onClick={() => {
        allowRouteChange();
      }}
      onClose={() => setOpenModal(false)}
    />
  )
}

export default useLeaveConfirm;
// app/components/community/ui/FreezeModal.tsx
"use client";

import { Dispatch, SetStateAction } from "react";

interface FreezeModalProps {
  open?: boolean;
  onOpenChange?: Dispatch<SetStateAction<boolean>>;
  onClose: () => void;
  onClick?: () => void;
}

const FreezeModal = ({ open, onOpenChange, onClose, onClick }: FreezeModalProps) => {
  console.log("Modal Component open >>> ", open);
  if (open)
  return (
      <div
        id="modal-container"
        className="fixed top-0 left-0 w-screen h-screen bg-black/30 flex justify-center items-center z-10"
      >
        <div
          id="modal-content"
          className="bg-white w-96 h-64 px-4 py-2 flex flex-col space-y-8 justify-center items-center rounded-xl relative"
        >
          <div className="flex flex-col items-center justify-center space-y-2">
            <h2 className="text-md font-bold">Warning</h2>
            <div className="flex flex-col items-center justify-center">
              <h3 className="text-lg font-extrabold">
                Are you sure you want to stop writing?
              </h3>
              <p className="text-sm text-gray-600">
                Changes are not saved.
              </p>
            </div>
          </div>
          <div className="flex space-x-4 w-full justify-center">
            <div>
              <button
                onClick={onClick}
                className="bg-gray-100 px-10 py-3 rounded-md"
              >
                quit
              </button>
            </div>
            <div>
              <button
                onClick={onClose}
                className="bg-mainGreen text-white px-10 py-3 rounded-md"
              >
                keep writing
              </button>
            </div>
          </div>
        </div>
      </div>
  );
};

export default FreezeModal;

I'm sorry I couldn't summarize the problem and brought you soooo long codes. This is my first Next.js project, so there are many things I don't know yet, and this summary was my best shot...😂

You can also see the whole code below. https://github.com/Savers-Save-Earth/Savers/tree/163-fix-community-page

Thank you very much for the wonderful library. ✨ Have a nice day!

run4w4y commented 1 year ago

Hello, thank you for your feedback! If you're wondering why the modal shown when trying to refresh the page is different from the one you see when clicking a Link, that is because it is not possible to show a custom modal for the beforeunload events, due to browser security reasons. If you're wondering why no browser-native modal is shown at all, then, to be honest, I'm not sure why the refresh prevention would not work as expected. Especially if you're using the exact same hook I provided in the README, which seems to be the case. Maybe the event listener you add in a useEffect in file app/community/write/page.tsx somehow gets in the way? Even if it doesn't, you don't really have to add the beforeunload event listener yourself, as the useBeforeUnload hook does that for you and it's already used appropriately inside the useLeaveConfirm hook, so I suggest you just remove it. If that doesn't help, please get back to me and specify which browser do you encounter this issue with, I'll try to look into it. As for the browser back button: that's a bit of an oversight on my end and I have already confirmed this issue😅. I'm currently too busy with other stuff, but I will try to fix this as soon as I have enough free time!

jiwoniverse commented 1 year ago

Hello, thank you for your feedback! If you're wondering why the modal shown when trying to refresh the page is different from the one you see when clicking a Link, that is because it is not possible to show a custom modal for the beforeunload events, due to browser security reasons. If you're wondering why no browser-native modal is shown at all, then, to be honest, I'm not sure why the refresh prevention would not work as expected. Especially if you're using the exact same hook I provided in the README, which seems to be the case. Maybe the event listener you add in a useEffect in file app/community/write/page.tsx somehow gets in the way? Even if it doesn't, you don't really have to add the beforeunload event listener yourself, as the useBeforeUnload hook does that for you and it's already used appropriately inside the useLeaveConfirm hook, so I suggest you just remove it. If that doesn't help, please get back to me and specify which browser do you encounter this issue with, I'll try to look into it. As for the browser back button: that's a bit of an oversight on my end and I have already confirmed this issue😅. I'm currently too busy with other stuff, but I will try to fix this as soon as I have enough free time!

Thank you so much for your comment!

First of all, I'm using the Chrome browser. And when I checked again, the browser default refresh prevention works, so I'm sorry that I talked about this part.

The event listener in the file app/community/write/page.tsx works the same, with or without. I think I put it in while fixing codes.

The problem is the case of browser back button, and I understand the issue that you checked!

Lastly, I want to say thank you again for leaving a comment. I hope you're always full of luck! ☘️