npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

material-confirm-alert

v4.0.0

Published

A simple react material confirmation alert library

Downloads

23

Readme

material-confirm-alert

GitHub license npm version Coverage Status

A simple material-ui confirmation alert based on React Context API.

Installation

This library requires React and @material-ui/core

Install and save the package as dependencies of the project.

npm i --save material-confirm-alert

Usage

First, import the ConfirmAlertProvider from the package and use it to wrap your component wherever you want to use the confirmation alert. If you want to use it across the whole app, wrap your App component inside it.

Note: If you are using material-ui ThemeProvider, wrap the ConfirmAlertProvider inside it.

import { ConfirmAlertProvider } from 'material-confirm-alert';
...

export const App = () => {
    return (
        <ConfirmAlertProvider>
            ...
        </ConfirmAlertProvider>
    )
}

Next, inside your component where you want to use the confirmation alert, import the useConfirmAlert hook from the package. This hook returns a confirm function which will be used to call confirmation alert.

import { useConfirmAlert } from 'material-confirm-alert';
...

const MyComponent = () => {
    const confirm = useConfirmAlert();
    const handleClick = async () => {
        // confirm returns a promise and can be used with async/await
        const result = await confirm('Do you want to delete?');
        if(result) {
            // Handle operation when user confirm
            ...
        } else {
            // Handle operation when user cancel
            ...
        }
    }

    return (
        <Button onClick={handleClick}>Delete</Button>
    );
}

Demo

material-confirm-alert-demo

API

<ConfirmAlertProvider />

This is a Provider generated using the React Context API. The component which is using the confirmation alert requires to be wrapped inside this Provider.

Example -
// Import
import { ConfirmAlertProvider } from 'material-confirm-alert';
...

// App Component
export const App = () => {
    ...
    // return
    return (
        <ConfirmAlertProvider>
            // children
            ...
        </ConfirmAlertProvider>
    )
}

useConfirmAlert()

returns confirm function

Example -
// imports
import { useConfirmAlert } from 'material-confirm-alert'
...

// Inside components
const MyComponent = () => {
    const confirm = useConfirmAlert();
    ...
}

confirm(options, callback)

This function is used to call the confirmation dialog. It takes an object or a string as parameter. It returns a promise and can be used with async/await. The promise will resolve into boolean based on user choice.

| Parameters | Type | Optional | Description | | ---------- | ------------------------------- | -------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | options | object or string or (empty) | Yes | Sets the options for the confirmation dialog. Check options API for more details. If only string is passed, then it will be the title of the confirmation dialog. |

Example -
// Inside Components
const MyComponent = () => {
    const confirm = useConfirmAlert();
    const handleClick = async () => {
        const result = await confirm('Do you want to delete?');
        if(result) {
            // Handle operation when user confirm
        } else {
            // Handle operation when user cancel
        }
    }
    // return
    ...
}

options

This object can be used to set the optional parameters for the confirmation dialog.

| Parameters | Type | Default | Description | | ------------------ | -------- | --------------- | ------------------------------------------------ | | title | string | 'Are you sure?' | Sets the title for the confirmation dialog | | description | string | (empty) | Sets the description for the confirmation dialog | | okButtonText | string | 'Ok' | Sets the 'Ok' button text | | cancelButtonText | string | 'Cancel' | Sets the 'Cancel' button text |

Example -
// Inside Components
const MyComponent = () => {
    const confirm = useConfirmAlert();
    const handleClick = async () => {
        const result = await confirm({
            'title': 'Do you want to delete this item?',
            'description': 'This cannot be undone once deleted!',
            'okButtonText': 'Yes',
            'cancelButtonText': 'No'
        });
        if(result) {
            // Handle operation when user confirm
        } else {
            // Handle operation when user cancel
        }
    }
    // return
    ...
}