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

modal.react.js

v2.1.1-alpha1

Published

## What is it?

Downloads

103

Readme

modal.react.js Library

What is it?

modal.react.js is a library that allows you to create smart, customizable, and reusable modal windows with React. The library is written in TypeScript and provides a simple API to create and manage modal windows in your application with ease.

Features

  • Create and manage modals using an easy to use API.
  • Customize the look of your modals by passing your own components.
  • Handle the state of your modals with built-in hooks.
  • Implement endless modals with multiple areas.

Usage

Creating a ModalsArea:

Before you can create a modal window, you must create at least one ModalsArea component. This component will be responsible for rendering all the modals that you create in your application.

import { createArea, ModalsArea } from 'modal.react.js';

export const area = createArea();

function App() {
  return (
    <div>
      <ModalsArea area={area} />
      {/* Insert your application code here */}
    </div>
  );
}

Creating a Modal window:

To create a modal window, you need to use the createModal function. This function takes two parameters: the area where the modal should be rendered and an optional object with configuration options.

import { createModal } from 'modal.react.js';

const m1 = createModal(area);
const res = m1.show(<YourModalComponent />);
await m1.close();

Customizing the Modal:

You can customize the look of the modal by passing your own components as children to the Modal component. You can also use the onComplete prop to handle actions when the modal is closed.

import { Modal } from 'modal.react.js'

function MyCustomModal(props) {
  return (
    <div className="my-custom-modal">
      {props.children}
    </div>
  )
}

function App() {
  return (
    <Modal area={area} onComplete={() => console.log('Modal closed.')}>
      <MyCustomModal>
        <p>Hello World</p>
      </MyCustomModal>
    </Modal>
  )
}

Using Hooks:

The library has built-in hooks to help you handle the state and behavior of your modal windows.

useFlowValue

The useFlowValue hook allows you to specify the initial, live, and unmount values of an element. You can use this hook to create animations or transitions for your modals.

import {useFlowValue } from 'modal.react.js'

function MyCustomModal(props) {
  const time = 500;
  const flow = useFlowValue(
    time, /* animation time in ms */
    0, /* initial */
    1, /* after render */
    0 /* before unmount */
  );

  return (
    <div style={{ opacity: flow, transition: `${time}ms` }}>
      {props.children}
    </div>
  )
}

useCompleteModal

The useCompleteModal hook allows you to handle the completion of your modals. It returns a complete object with two functions: success and fail. You can call one of these functions to handle successful or unsuccessful completion of your modal window.

import { useState } from 'react';
import { useCompleteModal } from 'modal.react.js';

function MyCustomModal(props) {
  const [state, setState] = useState('');
  const complete = useCompleteModal<string>();

  function handleClick() {
    complete.success(state);
  }

  return (
    <div>
      <input value={state} onChange={(e) => setState(e.target.value)} />
      <button onClick={() => handleClick()}>Done</button>
    </div>
  );
}

showModal function

The showModal function allows you to create a modal window from pure code.

import { showModal } from 'modal.react.js';

async function handleClick(event) {
  const result = await showModal(area, <YourCustomModal />);
  console.log(result);
}

Installation

You can install the library using Yarn or NPM:

  • yarn add modal.react.js
  • npm i --save modal.react.js

Conclusion

modal.react.js is an easy-to-use and powerful library that helps you to create and manage modals in your React application. Whether you're building a large-scale application or a small one-off project, you can use this library to create beautiful, UX-optimized modals with minimal effort.