Git Product home page Git Product logo

react-mounting-transition's Introduction

React Mounting Transition

react-mounting-transition is a simple lib to help you add transitions to components you dynamically mount to the DOM

โš ๏ธ this lib is still in development and needs a bit of polish

Why is this necessary?

Certain elements are better dynamically added to the DOM on-demand. A good example of this would be a modal.

  1. When the modal is opened, it's best to create a brand new containing div and add it to the DOM to avoid z-index issues.
  2. For performance reasons, it's good to add the modal elements only when the modal is open to reduce the amount of elements on the first paint.

However, when mounting components dynamically to the DOM, it's hard to add transitions because you have to add different class names at different states of the transition while keeping the component mounted until the transition is finished.

You can think of this lib as a simpler alternative to react-transition-group with one specific use case.

Installation

npm i @ricokahler/react-mounting-transition

Usage

import React, { useEffect, useRef } from 'react';
import classNames from 'classnames';
import MountingTransition from '@ricokahler/mounting-transition';

interface Props {
  open: boolean;
  onClose: () => void;
  className?: React.ReactNode;
  children: React.ReactNode;
}

function Modal({ className, children, open, onClose }: Props) {
  const handleMount = async () => {
    // do something useful like handle focus
  };

  const handleUnmount = async () => {
    // do something useful like handle focus
  };

  return (
    <MountingTransition
      mounted={open}
      portal={{ containerClass: 'modal__container' }}
      timeout={250}
      onMount={handleMount}
      onUnmount={handleUnmount}
    >
      {({ active }) => (
        <>
          <div
            className={classNames('modal__backdrop', {
              'modal__backdrop--active': active,
            })}
            onClick={onClose}
          />
          <div
            aria-modal="true"
            className={classNames(className, 'modal', {
              'modal--active': active,
            })}
          >
            {children}
          </div>
        </>
      )}
    </MountingTransition>
  );
}

export default Modal;
.modal__container {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  z-index: 100;
  display: flex;
}

.modal__backdrop {
  opacity: 0;
  transition: opacity 250ms;
  position: absolute;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
}

.modal__backdrop--active {
  opacity: 1;
}

.modal {
  width: 640px;
  max-width: 100%;
  z-index: 1;
  opacity: 0;
  transition: opacity 250ms;
  margin: auto;
  transform: translateY(100%);
}

.modal--active {
  opacity: 1;
  transform: translateY(0);
}

react-mounting-transition's People

Contributors

renovate[bot] avatar renovate-bot avatar ricokahler avatar

Stargazers

Roman avatar StormyTalent avatar Fabien Franzen avatar

Watchers

 avatar  avatar

react-mounting-transition's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/release.yml
  • actions/checkout v2
  • actions/setup-node v2
  • codecov/codecov-action v2
  • ubuntu 18.04
.github/workflows/test.yml
  • actions/checkout v2
  • actions/setup-node v2
  • codecov/codecov-action v2
  • ubuntu 18.04
npm
package.json
  • @babel/core 7.21.4
  • @babel/preset-env 7.21.4
  • @babel/preset-react 7.18.6
  • @babel/preset-typescript 7.21.4
  • @rollup/plugin-babel 5.3.1
  • @rollup/plugin-node-resolve 13.3.0
  • @sanity/eslint-config-studio 2.0.0
  • @types/jest 27.5.2
  • @types/react 17.0.55
  • @types/react-dom 17.0.19
  • @types/react-test-renderer 17.0.2
  • eslint 8.26.0
  • jest 27.5.1
  • react 17.0.2
  • react-dom 17.0.2
  • react-test-renderer 17.0.2
  • rollup 2.79.1
  • semantic-release 19.0.5
  • typescript 4.8.4
  • react ^16 || ^17 || ^18
  • react-dom ^16 || ^17 || ^18

  • Check this box to trigger a request for Renovate to run again on this repository

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.