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

hicones-sweet-modal

v0.0.1-beta

Published

A flexible modal library for React

Downloads

9

Readme

React Sweet Modal

npm version License: MIT

Sweet Modal React is a lightweight, customizable modal library for React applications. It provides an easy-to-use API for showing modals while allowing full customization of the modal's appearance and behavior.

This library is inspired by the simplicity and flexibility of shadcn's Sonner toast library and the powerful features of SweetAlert2. We've combined these inspirations to create a modal system that is both easy to use and highly customizable.

Features

  • Simple API for showing and closing modals
  • Customizable modal components
  • TypeScript support
  • Lightweight with minimal dependencies
  • Easy integration with existing React projects

Installation

Install the package using npm:

npm install sweet-modal-react

Or using yarn:

yarn add sweet-modal-react

Usage

Basic Usage

  1. Wrap your app with the ModalContainer component:
import { ModalContainer } from 'sweet-modal-react';
import CustomModal from './CustomModal';

function App() {
  return (
    <div>
      {/* Your app content */}
      <ModalContainer modalComponent={CustomModal} />
    </div>
  );
}
  1. Create a custom modal component:
import React from 'react';

const CustomModal = ({ modal, onClose }) => (
  <div className="modal">
    <h2>{modal.title}</h2>
    <p>{modal.content}</p>
    <button onClick={onClose}>Close</button>
  </div>
);

export default CustomModal;
  1. Use the showModal function to display modals:
import { showModal } from 'sweet-modal-react';

function MyComponent() {
  const handleClick = () => {
    showModal({
      title: 'Hello',
      content: 'This is a modal!',
      // Any other props you want to pass to your custom modal
    });
  };

  return <button onClick={handleClick}>Show Modal</button>;
}

Advanced Usage

You can create multiple types of modals by using different custom components:

import { ModalContainer, showModal } from 'sweet-modal-react';
import ConfirmationModal from './ConfirmationModal';
import InfoModal from './InfoModal';

function App() {
  const showConfirmation = () => {
    showModal({
      type: 'confirmation',
      title: 'Confirm Action',
      content: 'Are you sure you want to proceed?',
      onConfirm: () => console.log('Confirmed'),
      onCancel: () => console.log('Cancelled'),
    });
  };

  const showInfo = () => {
    showModal({
      type: 'info',
      title: 'Information',
      content: 'This is some important information.',
    });
  };

  const modalComponent = (props) => {
    switch (props.modal.type) {
      case 'confirmation':
        return <ConfirmationModal {...props} />;
      case 'info':
        return <InfoModal {...props} />;
      default:
        return null;
    }
  };

  return (
    <div>
      <button onClick={showConfirmation}>Show Confirmation</button>
      <button onClick={showInfo}>Show Info</button>
      <ModalContainer modalComponent={modalComponent} />
    </div>
  );
}

API

showModal(options: ModalOptions): string

Shows a modal with the given options. Returns the modal's unique ID.

closeModal(id: string): void

Closes the modal with the given ID.

<ModalContainer modalComponent={YourCustomModal} />

A component that manages the rendering of modals. It should be placed at the root of your app.

TypeScript Support

This library includes TypeScript definitions. You can import types like this:

import { ModalOptions, ModalItem, ModalComponent } from 'sweet-modal-react';

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Author

Henrique Almeida