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

@prezly/react-promise-modal

v0.3.1

Published

The proper (and easy) way of doing modals in React. With Promises.

Downloads

4,159

Readme

React Promise Modal

The easies way of using modals in React. With Promises.

Usage

reactModal() function accepts a render callback that renders a modal from given three arguments:

  • show — boolean to tell if the window is visible or not. Used for in/out transitions. Primarily intended to be used as react-bootstrap Modal show property.

  • onDismiss — should be invoked when a modal is dismissed. Always resolves the promise to undefined.

  • onSubmit — should be invoked when a modal is submitted/confirmed. Resolves to true if no arguments given. But you can pass any value as argument to defined resolve value. For obvious reason this value cannot be undefined.

The function returns a Promise that is resolved with an undefined if the modal was dismissed or true (or any value you provide) when it's submitted.

import reactModal from '@prezly/react-promise-modal';

const result = await reactModal(({ show, onSubmit, onDismiss }) => (
    // Use any modal implementation
    <Modal show={show} onHide={onDismiss}>
       OK?
       <button onClick={onDismiss}>Cancel</Button>
       <button onClick={() => onSubmit('OK Clicked')}>OK</Button>
    </Modal>
));

if (result === undefined) {    
    console.log('The modal was dismissed or cancelled');
} else {
    console.log(result); // outputs "OK Clicked"
}

Confirmation

You can easily implement a confirmation modal using reactModal():

import reactModal from '@prezly/react-promise-modal';
import { Modal } from 'react-bootstrap'; 

const isConfirmed = await reactModal(({ show, onSubmit, onDismiss }) => (
    // Use any modal window implementation you need.
    // We're using React Bootstrap Modal for demo purposes here
     
    <Modal.Dialog show={show} onHide={onDismiss}>
      <Modal.Body>
        Confirm you really want to star this repository.
      </Modal.Body>
    
      <Modal.Footer>
        <Button variant="secondary" onClick={onDismiss}>Cancel</Button>
        <Button variant="primary" onClick={onSubmit}>Confirm</Button>
      </Modal.Footer>
    </Modal.Dialog>
));

Alert

import reactModal from '@prezly/react-promise-modal';
import { Modal } from 'react-bootstrap'; 

await reactModal(({ show, onDismiss }) => (
    // Use any modal window implementation you need.
    // We're using React Bootstrap Modal for demo purposes here
     
    <Modal.Dialog show={show} onHide={onDismiss}>
        <Modal.Header>
            Error
        </Modal.Header>
      
        <Modal.Body>
            An error occured. Can&lsquo;t fix it.
        </Modal.Body>
    
        <Modal.Footer>
           <Button variant="primary" onClick={onDismiss}>OK</Button>
        </Modal.Footer>
    </Modal.Dialog>
));

Prompt user input

import reactModal from '@prezly/react-promise-modal';
import { Modal } from 'react-bootstrap'; 

class FilenamePromptModal extends Component {
    static propTypes = {
        show: PropTypes.bool.isRequired,
        onSubmit: PropTypes.func.isRequired,
        onDismiss: PropTypes.func.isRequired,
    };
    
    state = {
        filename: 'Untitled.txt',
    };
    
    handleChange = (event) => {
        this.setState({ filename: event.target.value });
    };
    
    handleSubmit = () => {
        this.props.onSubmit(this.state.filename);    
    };
    
    render() {
        const { show, onDismiss } = this.props;
        
        return (
            <Modal.Dialog show={show} onHide={onDismiss}>
                <form onSubmit={this.handleSubmit}>
                    <Modal.Body>
                        <p>Please enter filename:</p>
                        <input autoFocus value={this.state.filename} onChange={this.handleChange} />
                    </Modal.Body>
                  
                    <Modal.Footer>
                        <Button variant="secondary" onClick={onDismiss}>Cancel</Button>
                        <Button variant="primary" type="submit">Confirm</Button>
                    </Modal.Footer>
                </form>
            </Modal.Dialog>        
        );
    }
}

const filename = await reactModal(({ show, onSubmit, onDismiss }) => (
    // Use any modal window implementation you need.
    // We're using React Bootstrap Modal for demo purposes here
     
    <FilenamePromptModal show={show} onSubmit={onSubmit} onDismiss={onDismiss} />
));

Brought to you with :metal: by Prezly.