Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

next-navigation-guard

Package Overview
Dependencies
Maintainers
0
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

next-navigation-guard

Navigation Guard for Next.js App Router and Pages Router.

  • 0.1.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.5K
decreased by-19.98%
Maintainers
0
Weekly downloads
 
Created
Source

next-navigation-guard

You use Next.js, and you want to show "You have unsaved changes that will be lost." dialog when user leaves page? This library is just for you!

Demo

https://layerxcom.github.io/next-navigation-guard/

How does it work?

Installation

npm install next-navigation-guard
# or
yarn install next-navigation-guard
# or
pnpm install next-navigation-guard
  • App Router: app/layout.tsx

    <html lang="en">
      <body className={`${geistSans.variable} ${geistMono.variable}`}>
        <NavigationGuardProvider>{children}</NavigationGuardProvider>
      </body>
    </html>
    
  • Page Router: page/_app.tsx

    export default function MyApp({ Component, pageProps }: AppProps) {
      return (
        <NavigationGuardProvider>
          <Component {...pageProps} />
        </NavigationGuardProvider>
      );
    }
    

Usage

  • window.confirm()

    useNavigationGuard({ enabled: form.changed, confirm: () => window.confirm("You have unsaved changes that will be lost.") })
    
  • Custom dialog component

    const navGuard = useNavigationGuard({ enabled: form.changed })
    
    return (
      <>
        <YourContent />
    
        <Dialog open={navGuard.active}>
          <DialogText>You have unsaved changes that will be lost.</DialogText>
    
          <DialogActions>
            <DialogButton onClick={navGuard.reject}>Cancel</DialogButton>
            <DialogButton onClick={navGuard.accept}>Discard</DialogButton>
          </DialogActions>
        </Dialog>
      </>
    )
    

See working example in example/ directory and its NavigationGuardToggle component.

FAQs

Package last updated on 07 Oct 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc