Code Monkey home page Code Monkey logo

react-show's Introduction

React Show Logo
Join the community on Spectrum

React Show

A css powered (graphics accelerated) animation component for React.

Why?

  • You need to animate, reveal, collapse your react components. Everyone does!
  • You want it smoothly animated, even on mobile. Like butter on a glide-cam!
  • You don't want to bloat your app with custom physics-based animation frameworks, request-animation-frame-happy animation loops or even jQuery...heaven forbid.

Features

  • 3.7 kb gzipped
  • Powered by native CSS animations & transitions on the GPU when possible
  • Animates height: auto; and width: auto;

Demo

Chat with us on Spectrum!

Need Help? Click here to sign up for the React-Tools Spectrum community. We are constantly discussing implementation details and answering questions. :)

Table of Contents

Installation

$ yarn add react-show
# or
$ npm install --save react-show

Usage

Simple Usage

You can create a simple expander component with the Animate component!

import { Animate } from "react-show";

const SimpleExample = () => (
  <Animate
    show={true || false} // Toggle true or false to show or hide the content!
    duration={500}
    style={{
      height: "auto"
    }}
    start={{
      height: 0 // The starting style for the component.
      // If the 'leave' prop isn't defined, 'start' is reused!
    }}
  >
    Hello world!
  </Animate>
);

Advanced Usage

You can create a simple expander component with the Animate component!

import { Animate } from "react-show";

const SimpleExample = () => (
  <Animate
    show={true || false} // Toggle true or false to show or hide the content!
    transitionOnMount // Will trigger the transition when the component is mounted and show === true
    preMount // Mounts the component's children on first render even if show === false
    stayMounted // Forces the component's children to remain mounted when show === false
    component="span" // Use a <span> (or custom) component as the wrapper instead of a <div>
    style={{
      // The permanent styles for the component
      height: `${Math.random() * 100}px`,
      background: "white"
    }}
    start={{
      // The starting styles for the hidden component.
      opacity: 0,
      height: 0
    }}
    enter={{
      // These styles will be applied when the component enters
      opacity: 1,
      height: 'auto'
      background: "green"
    }}
    leave={{
      // these styles will be applied when the component leaves
      opacity: 0,
      height: 0,
      background: "red"
    }}
  >
    Hello world!
  </Animate>
);

Duration, Easing, Transition Properties

You can configure duration, easing, and transition by:

  • Setting a component-wide default with the duration, easing, and transitionProperty props.
  • Optionally using a lifecycle specific style property like transitionDuration, transitionTimingFunction, transitionProperty or even transitionDelay.

ReactShow comes with a wide variety of easings built-in! See Easing Options for a full list.

import { Animate } from "react-show";

const DurationAndEasingExample = () => (
  <Animate
    show={true || false} // Toggle true or false to show or hide the content!
    duration={500} // // The duration of the transition in milliseconds
    easing={Animate.easings.easeOutQuad} // Comes with all the easings you could want!
    enter={{
      // Only use this duration/delay during the `enter` stage
      transitionDuration: ".3s",
      transitionDelay: "1s"
    }}
  >
    Hello world!
  </Animate>
);

Lifecycle

React-Show uses the following lifecycle to determine which styles to show:

When show === true

  • If component is not mounted
    • If transitionOnMount === true
      • The start styles are set as the initial style
    • If transitionOnMount === false
      • The enter styles are set as the initial style
    • The component is mounted
  • If component is already mounted
    • The enter styles are set as the initial style
  • If a width or height style is/was set to auto
    • The display: block and overflow: hidden styles are temporarily applied
    • The component scrollWidth/scrollHeight is measured
    • The display: block and overflow: hidden styles are removed
  • The enter styles are applied and component waits until all transitions complete
  • All lifecycle styles are removed and component waits until all transitions complete
  • The onFinish prop function is fired

When show === false

  • If a width or height style is/was set to auto
    • The display: block and overflow: hidden styles are temporarily applied
    • The component scrollWidth/scrollHeight is measured
    • The display: block and overflow: hidden styles are removed
  • The leave or start styles are applied and component waits until all transitions complete
  • If stayMounted === false
    • The component is unmounted

Easing Options

React-Show comes packaged with some awesome easings that are accessible via Animate.easings. They are extremely simple to use too:

import { Animate } from "react-show";

const SimpleExample = () => (
  <Animate show={true} easing={Animate.easings.easeOutQuart}>
    Hello world!
  </Animate>
);

Below is a full list of the available easings exported at Animate.easings

import { easings } from "react-show";

easings ===
  {
    // Cubic
    easeInCubic: "cubic-bezier(0.550, 0.055, 0.675, 0.190)",
    easeOutCubic: "cubic-bezier(0.215, 0.610, 0.355, 1.000)",
    easeInOutCubic: "cubic-bezier(0.645, 0.045, 0.355, 1.000)",

    // Circ
    easeInCirc: "cubic-bezier(0.600, 0.040, 0.980, 0.335)",
    easeOutCirc: "cubic-bezier(0.075, 0.820, 0.165, 1.000)",
    easeInOutCirc: "cubic-bezier(0.785, 0.135, 0.150, 0.860)",

    // Expo
    easeInExpo: "cubic-bezier(0.950, 0.050, 0.795, 0.035)",
    easeOutExpo: "cubic-bezier(0.190, 1.000, 0.220, 1.000)",
    easeInOutExpo: "cubic-bezier(1.000, 0.000, 0.000, 1.000)",

    // Quad
    easeInQuad: "cubic-bezier(0.550, 0.085, 0.680, 0.530)",
    easeOutQuad: "cubic-bezier(0.250, 0.460, 0.450, 0.940)",
    easeInOutQuad: "cubic-bezier(0.455, 0.030, 0.515, 0.955)",

    // Quart
    easeInQuart: "cubic-bezier(0.895, 0.030, 0.685, 0.220)",
    easeOutQuart: "cubic-bezier(0.165, 0.840, 0.440, 1.000)",
    easeInOutQuart: "cubic-bezier(0.770, 0.000, 0.175, 1.000)",

    // Quint
    easeInQuint: "cubic-bezier(0.755, 0.050, 0.855, 0.060)",
    easeOutQuint: "cubic-bezier(0.230, 1.000, 0.320, 1.000)",
    easeInOutQuint: "cubic-bezier(0.860, 0.000, 0.070, 1.000)",

    // Sine
    easeInSine: "cubic-bezier(0.470, 0.000, 0.745, 0.715)",
    easeOutSine: "cubic-bezier(0.390, 0.575, 0.565, 1.000)",
    easeInOutSine: "cubic-bezier(0.445, 0.050, 0.550, 0.950)",

    // Back
    easeInBack: "cubic-bezier(0.600, -0.280, 0.735, 0.045)",
    easeOutBack: "cubic-bezier(0.175,  0.885, 0.320, 1.275)",
    easeInOutBack: "cubic-bezier(0.680, -0.550, 0.265, 1.550)"
  };

API

<Animate>

Props
Prop Required Default Value Description
show true false Determines whether to "show" the content or not.
duration 300 The transition-duration of the transition used to show the content
easing easeOutQuint The transition-timing-function used to show the content
transitionProperty all The transition-property used to show the content
preMount false If true, element will mount on first render if show === false
stayMounted false If true, element will stay mounted when show === false
transitionOnMount false If true, element will animate from the start style on mount
style undefined React style object (See lifecycle for more details)
start undefined React style object (See lifecycle for more details)
enter undefined React style object (See lifecycle for more details)
leave undefined React style object (See lifecycle for more details)
component div Use a (or custom) component as the wrapper instead of a
onFinish noop Function called when the component finishes transitioning.
onMount noop Function called when the children passed to Animate are mounted.
onWillUnmount noop Function called right before the children passed to Animate are unmounted.

Contributing

We are always looking for people to help us grow react-show's capabilities and examples. If you have an issue, feature request, or pull request, let us know!

License

React Show uses the MIT license. For more information on this license, click here.

react-show's People

Contributors

brodanoel avatar brumm avatar josefernand avatar paulkarcher avatar tannerlinsley avatar wgil avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

react-show's Issues

Vue show

React Show is a very nice react-component. Inspired by this pkg, I try my best to write a Vue version - Vue Show.

Children seem to be unmounted on first render, appear after re-render

Hey,

Here's a demo: https://codesandbox.io/s/0vr08vv8wl

I'm trying to create some kind of horizontal accordion component which renders an array of items, of which only one can be active. An inactive item should collapse the label to only show a 'header' component. The entire thing should be responsive.
The items' container is a ReactShow node, which is set to flexGrow: 1 if active, else flexGrow: 0. The active index is cycled from 0 through 2 onClick on the container component.

This is what I get initially:
0vr08vv8wl codesandbox io_ 1

This is how I want it, which happens after three clicks on the container):
0vr08vv8wl codesandbox io_

It seems like the 'hidden' items are not rendered initially, despite setting unmountOnHide={false} (which I thought was the issue at first). However, after rendering once, they remain rendered. Setting unmountOnHide={true} does not seem to have any effect.

Maybe I'm just using this in an unsupported way?

Not possible to know when children are mounted for a11y focus-management

When expanding or collapsing elements which contain inputs, it's a common accessibility pattern to focus the first focusable element. However, when I set the state to show, even when using the setState callback, the elements still haven't been mounted. This makes it not possible to reliably focus the first element. Can I propose an onMount event prop of some kind, that signals the parent component when the children have actually been mounted? If that sounds acceptable I'd be happy to look at creating a pull request myself. Thanks for all the hard work on this package!

No styles resolved for the enter prop

Version

v3.0.2

Error

Error: ReactShow: No styles were resolved for the "enter" prop!

Usage

<Show
    show={this.state.open}
    duration={200}
    styleHide={{ opacity: 0, height: 0 }}
    styleShow={{ opacity: 1, height: 'auto' }}
  >
    {children}
  </Show>

Docs to migrate to 3.0?

Hey,

I was wondering about the new 3.0 version. I've seen no docs on upgrading, am I missing something?

Thanks!

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.