# react-router-navigation-confirm

> A collection of components to display a custom confirmation dialog on navigation. More flexible solution to prevent than default react-router 'Prompt'

Latest version **1.1.11** (published 2021-05-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-router-navigation-confirm
pnpm add react-router-navigation-confirm
yarn add react-router-navigation-confirm
bun add react-router-navigation-confirm
```

## Health

**Score 40/100 (D)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 1.1.11 |
| Published | 2021-05-17 |
| First published | 2019-02-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 69.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 8 |
| Author | BezzubovEgor |
| Maintainers | bezzubov_egor |
| Keywords | dialog, modal, confirm, navigation, prompt, react, router, user, getUserConfirmation |

## Links

- npm: https://www.npmjs.com/package/react-router-navigation-confirm
- Repository: https://github.com/BezzubovEgor/react-router-navigation-confirm
- Homepage: https://github.com/BezzubovEgor/react-router-navigation-confirm/issues#readme
- Issues: https://github.com/BezzubovEgor/react-router-navigation-confirm/issues
- npm.io page: https://npm.io/package/react-router-navigation-confirm

## Alternatives

- [express-promise-router](https://npm.io/package/express-promise-router.md) — 736.1K weekly downloads
- [next-usequerystate](https://npm.io/package/next-usequerystate.md) — 29.8K weekly downloads
- [@bitkyc08/opencodex](https://npm.io/package/@bitkyc08/opencodex.md) — 4.6K weekly downloads
- [lynkr](https://npm.io/package/lynkr.md) — 575 weekly downloads
- [baremetal.js](https://npm.io/package/baremetal.js.md) — 42 weekly downloads

## Recent versions

- 1.1.11 (latest) — 2021-05-17
- 1.1.7 — 2020-03-18
- 1.1.4 — 2020-03-18
- 1.1.3 — 2020-03-18
- 1.0.2 — 2020-03-10
- 1.0.0 — 2019-04-02
- 0.2.6 — 2019-04-01
- 0.2.5 — 2019-02-28
- 0.2.4 — 2019-02-05
- 0.2.2 — 2019-02-05
- 0.2.1 — 2019-02-05

## README

#

<p align="center">
    <a href="https://bezzubovegor.github.io/react-router-navigation-confirm">
        <img src="https://bezzubovegor.github.io/react-router-navigation-confirm/logo.png" width="120" title="RRNC" alt="RRNC"/>
    </a>
</p>

<h2 align="center">
  React Router Navigation Confirm
</h2>

<p align="center">
    <a href="https://travis-ci.com/BezzubovEgor/react-router-navigation-confirm">
        <img src="https://travis-ci.com/BezzubovEgor/react-router-navigation-confirm.svg?branch=master" alt="Build Status"/>
    </a>
    <a href="https://opensource.org/licenses/MIT">
        <img src="https://img.shields.io/badge/License-MIT-yellow.svg" alt="License: MIT"/>
    </a>
    <a href="https://codecov.io/gh/BezzubovEgor/react-router-navigation-confirm">
        <img src="https://codecov.io/gh/BezzubovEgor/react-router-navigation-confirm/branch/master/graph/badge.svg?token=XAhNiBN2e9" alt="codecov"/>
    </a>
</p>

## Table of Contents

- [Table of Contents](#table-of-contents)
- [Installation](#installation)
- [Demo](#demo)
- [General Usage](#general-usage)
  - [Basic example](#basic-example)
  - [Custom dialog](#custom-dialog)
- [API](#api)
  - [**1. `<HistoryListener/>`**](#1-historylistener)
  - [**2. `<NavigationConfirm/>`**](#2-navigationconfirm)
  - [**3. `<NavigationConfirmModal/>`**](#3-navigationconfirmmodal)
- [License](#license)

## Installation

To install this component you can use [npm](https://www.npmjs.com/) or [yarn](https://yarnpkg.com/):

    $ npm install --save react-router-navigation-confirm
    $ yarn add react-router-navigation-confirm

## Demo

You can see demo and docs on this [site](https://bezzubovegor.github.io/react-router-navigation-confirm).

## General Usage

### Basic example

To show confirmation dialog on every navigation you only need to include `<HistoryListener/>` provider-component to the root router of your app and `<NavigationConfirmModal/>` to any Route (inside `<HistoryListener>`) in which you want to display confirmation dialog:

```jsx
import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter } from 'react-router-dom';
import { NavigationConfirmModal, HistoryListener } from 'react-router-navigation-confirm';

import App from './App';


ReactDOM.render(
    <BrowserRouter>
        <div>
            <HistoryListener>
                <NavigationConfirmModal/>
            <HistoryListener/>
            <App />
        </div>
    </BrowserRouter>
    , document.getElementById('root')
);
```

1. `HistoryListener` - the component-provider which start to listen all changes of history and help to understand when you go forward and when you go back in the navigation(`NavigationConfirmModal` and `NavigationConfirm` will not work without HistoryListener).

2. `NavigationConfirmModal` - the component that displays confirmation dialog with to buttons (*Confirm* and *Cancel*) when you try to navigate to other page or Route.

### Custom dialog

If you want to create custom dialog you can use `NavigationConfirm` confirm component:

```js
import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter } from 'react-router-dom';
import { NavigationConfirm, HistoryListener } from 'react-router-navigation-confirm';

import App from './App';

const children = ({ onConfirm, onCancel }) => (
    <div>
        <button onClick={ onConfirm }>Confirm</button>
        <button onClick={ onCancel }>Cancel</button>
    </div>
)


ReactDOM.render(
    <BrowserRouter>
        <div>
            <HistoryListener>
                <NavigationConfirm>
                    { children }
                </NavigationConfirm>
            </HistoryListener>
            <App />
        </div>
    </BrowserRouter>
    , document.getElementById('root')
);

```

`NavigationConfirm` is the component that displays custom children, you should provide function that takes object with `onConfirm` and `onCancel` and return some React element. You can use `onConfirm` to approve navigation ot `onCancel` function to reject it and stay on the same page / Route.

## API

### **1. `<HistoryListener/>`**

| Property | Description | Default | Type | Required |
|:---|:---|:---|:---|:---|
| `children` | React element / node to render into the `HistoryListener` component | `undefined` | `React.ReactNode` | *false* |

### **2. `<NavigationConfirm/>`**

| Property | Description | Default | Type | Required |
|:---|:---|:---|:---|:---|
|`children` | function that takes object with `onConfirm` and `onCancel` fields-functions and returns React node to render when user navigate to other page / Route| *no default* | `({ onConfirm: function, onCancel: function }) => React.ReactNode;` | *true* |
|`unloadMsg` | message to show in the confirmation dialog on page unload event (in new versions in browser message alwase takes default messges according browser) | `'msg'` | `string` | *false* |
|`when` | condition to render confirmation, you can hide or show dialog by pass `true` or `false`, or create custom behaviour using function | `true` | `boolean | ((location: Location, routeProps: RouteComponentProps) => boolean)` | *false* |

### **3. `<NavigationConfirmModal/>`**

| Property | Description | Default | Type | Required |
|:---|:---|:---|:---|:---|
|`children` | React element / node to render into the `NavigationConfirmModal` component | `'Are you sure you want to leave this page?'` | `React.ReactNode` | *false* |
|`when` | condition to render confirmation, you can hide or show dialog by pass `true` or `false`, or create custom behaviour using function | `true` | `boolean | ((location: Location, routeProps: RouteComponentProps) => boolean)` | *false* |
|`onCancel` | function to call when user click on cancel button | `undefined` | `() => void` | *false* |
|`onConfirm` | function to call when user click on confirm button | `undefined` | `() => void` | *false* |
|`confirmText` | Text for confirm button | `'Confirm'` | `string` | *false* |
|`cancelText` | Text for cancel button | `'Cancel'` | `string` | *false* |
|`modalClassName` | modal class name | `'nc-modal'` | `string` | *false* |
|`backdropClassName` | class name for modal backdrop | `'nc-modal__backdrop'` | `string` | *false* |
|`contentClassName` | content class name | `'nc-modal__content'` | `string` | *false* |
|`bodyClassName` | class name for body container | `'nc-modal__body'` | `string` | *false* |
|`footerClassName` | class name for modal footer | `'nc-modal__footer'` | `string` | *false* |
|`buttonClassName` | class name for all modal buttons (applyed for confirm and cancel) | `'nc-modal__button'` | `string` | *false* |
|`buttonConfirmClassName` | confirm button aditional style | `'confirm'` | `string` | *false* |

## License

MIT

---
_Source: https://npm.io/package/react-router-navigation-confirm · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
