An open API service indexing awesome lists of open source software.

https://github.com/wallpants/use-confirm-hook

Replace your browser's "window.confirm" with a custom React Component.
https://github.com/wallpants/use-confirm-hook

browser confirm react shadcn-ui

Last synced: about 2 months ago
JSON representation

Replace your browser's "window.confirm" with a custom React Component.

Awesome Lists containing this project

README

        

# use-confirm-hook

Replace your browser's `window.confirm` with a custom _React Component_.

This module is released as a TypeScript module, make sure your bundler supports them.

Tested with [vite](https://vitejs.dev/) and [bun](https://bun.sh/).

## Install

```bash
bun add use-confirm-hook
```

## Usage

Create your custom confirm component:

```tsx
// confirm-dialog.tsx
import { useConfirm } from "use-confirm-hook";
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
} from "./ui/alert-dialog";

export const ConfirmDialog = () => {
const { isAsking, message, deny, confirm } = useConfirm();

return (



Confirm
{message}


Cancel
Confirm



);
};
```

At the very root of your React App add the _Provider_ and your _React Component_.

```tsx
import { UseConfirmProvider } from "use-confirm-hook";
import { ConfirmDialog } from "./confirm-dialog";
import { App } from "./app";

export default function Root() {
return (




);
}
```

Ask the user to confirm an action:

```tsx
import { useConfirm } from "use-confirm-hook";

export default function Component() {
const { ask } = useConfirm();

function handleDelete() {
const res = await ask("Are you sure?");
if (res) {
console.log("continue with deletion");
} else {
console.log("don't delete");
}
}

return (


Delete

);
}
```

## Credits

This project is a simplified version of [https://github.com/tsivinsky/use-confirm](https://github.com/tsivinsky/use-confirm).