Code Monkey home page Code Monkey logo

modal.react.js's Introduction

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.

modal.react.js's People

Contributors

jwo1f avatar dependabot[bot] avatar

Stargazers

 avatar  avatar Alexander avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.