Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
react-animate-height
Advanced tools
Lightweight React component for animating height using CSS transitions.
The react-animate-height package is a React component that allows you to animate the height of an element. This can be useful for creating smooth transitions when showing or hiding content, such as expanding or collapsing sections.
Basic Height Animation
This example demonstrates a basic height animation where the height of a div toggles between 0 and 'auto' when a button is clicked. The animation duration is set to 500 milliseconds.
import React, { useState } from 'react';
import AnimateHeight from 'react-animate-height';
const Example = () => {
const [height, setHeight] = useState(0);
return (
<div>
<button onClick={() => setHeight(height === 0 ? 'auto' : 0)}>
Toggle Height
</button>
<AnimateHeight
duration={500}
height={height} // see props documentation below
>
<div>
This content will expand and collapse with animation.
</div>
</AnimateHeight>
</div>
);
};
export default Example;
Animating to Specific Height
This example shows how to animate the height of a div to specific values (100px and 200px) when a button is clicked. The animation duration is set to 500 milliseconds.
import React, { useState } from 'react';
import AnimateHeight from 'react-animate-height';
const Example = () => {
const [height, setHeight] = useState(100);
return (
<div>
<button onClick={() => setHeight(height === 100 ? 200 : 100)}>
Toggle Height
</button>
<AnimateHeight
duration={500}
height={height} // see props documentation below
>
<div>
This content will expand and collapse to specific heights with animation.
</div>
</AnimateHeight>
</div>
);
};
export default Example;
Animating with Easing
This example demonstrates how to use easing functions with the height animation. The easing function 'ease-in-out' is applied to the animation, making the transition smoother.
import React, { useState } from 'react';
import AnimateHeight from 'react-animate-height';
const Example = () => {
const [height, setHeight] = useState(0);
return (
<div>
<button onClick={() => setHeight(height === 0 ? 'auto' : 0)}>
Toggle Height
</button>
<AnimateHeight
duration={500}
height={height} // see props documentation below
easing="ease-in-out"
>
<div>
This content will expand and collapse with easing animation.
</div>
</AnimateHeight>
</div>
);
};
export default Example;
react-collapse is a React component for animating the height of an element. It provides similar functionality to react-animate-height but focuses more on collapsing and expanding content. It offers a simple API and is easy to integrate into existing projects.
react-spring is a spring-physics-based animation library for React applications. It provides more advanced and flexible animation capabilities compared to react-animate-height, including support for animating various properties, not just height. It is suitable for more complex animation needs.
framer-motion is a powerful animation library for React that allows for complex animations and gestures. It offers more features and flexibility compared to react-animate-height, including support for keyframes, layout animations, and drag gestures. It is ideal for creating highly interactive and animated user interfaces.
No dependencies React component for animating height using CSS transitions.
Slide an element up and down or animate it to any specific height.
Content's opacity can be optionally animated as well (check animateOpacity
prop bellow).
CSS classes are applied in specific animation states, check animationStateClasses
prop.
This is version 3.x branch, rewritten to hooks, which means you'll need React version 16.8 or newer.
For version 2.x, check v2 branch
onAnimationStart
-> onHeightAnimationStart
onAnimationEnd
-> onHeightAnimationEnd
Live demo: muffinman.io/react-animate-height
Because multiple people asked how to animate list items, I created this simple example for that as well.
To build the examples locally, run:
npm install
npm start
Then open http://127.0.0.1:8000/
in your browser of choice browser.
Or play with this sandbox.
Get it from npm
$ npm install --save react-animate-height
Import and use it in your React app.
import React, { useState } from 'react';
import AnimateHeight from 'react-animate-height';
const Example = () => {
const [height, setHeight] = useState(0);
return (
<div>
<button
aria-expanded={height !== 0}
aria-controls="example-panel"
onClick={() => setHeight(height === 0 ? 'auto' : 0)}
>
{height === 0 ? 'Open' : 'Close'}
</button>
<AnimateHeight
id="example-panel"
duration={500}
height={height} // see props documentation below
>
<h1>Your content goes here</h1>
<p>Put as many React or HTML components here.</p>
</AnimateHeight>
</div>
);
};
height: numeric or percentage value (ie. '50%'
) or 'auto'
, required
When changed, element height will be animated to that height.
To slide up use 0
, for slide down use 'auto'
duration: integer, default: 500
Duration of the animation in milliseconds
delay: integer, default: 0
Animation delay in milliseconds
easing: string, default: 'ease'
CSS easing function to be applied to the animation
id: string
HTML id
attribute.
className: string
CSS class to be applied to the element
Please note that you shouldn't apply properties that are messing with the layout (like display
, height
...), as these might break height calculations
ref: React.MutableRefObject<HTMLDivElement | null>
Reference to the main div element.
const wrapperDiv = useRef<HTMLDivElement | null>(null);
<AnimateHeight ref={wrapperDiv}>
contentRef: React.MutableRefObject<HTMLDivElement | null>
Reference to the content div element.
const contentDiv = useRef<HTMLDivElement | null>(null);
<AnimateHeight contentRef={contentDiv}>
style: object
CSS style object, it will be merged with inline styles of the component
Please note that you shouldn't apply properties that are messing with the layout (display
, height
are omitted from the type already), as these might break height calculations
contentClassName: string
CSS class to be applied to content wrapper element
Please note that you shouldn't apply properties that are messing with the layout (like display
, height
...), as these might break height calculations
animationStateClasses: object
Object containing CSS class names for animation states, default:
{
animating: 'rah-animating',
animatingUp: 'rah-animating--up',
animatingDown: 'rah-animating--down',
static: 'rah-static',
animatingToHeightZero: 'rah-animating--to-height-zero',
animatingToHeightAuto: 'rah-animating--to-height-auto',
animatingToHeightSpecific: 'rah-animating--to-height-specific',
staticHeightZero: 'rah-static--height-zero',
staticHeightAuto: 'rah-static--height-auto',
staticHeightSpecific: 'rah-static--height-specific',
}
Please note that this one will be merged with the default object and cached when component is created, so changing it afterwards will have no effect.
onHeightAnimationStart: function
Callback which will be called when animation starts.
This first argument passed to this callback is an object containing newHeight
, the pixel value of the height at which the animation will end.
onHeightAnimationEnd: function
Callback which will be called when animation ends.
This first argument passed to this callback is an object containing newHeight
, the pixel value of the height at which the animation ended.
applyInlineTransitions: boolean, default: true
If this flag is set to false
only CSS classes will be applied to the element and inline
transition styles will not be present.
animateOpacity: boolean, default: false
If set to true
content will fade-in (and fade-out) while height is animated.
aria-hidden: boolean
By default, library will set aria-hidden
to true
when height is zero. If you wish to override it, you can pass the prop yourself.
disableDisplayNone: boolean, default: false
By default, library will set display: none
when height is zero. This prop allows you to disable that behavior and handle it yourself. It is useful when using auto height, check this issue for more info. Please be careful not to break accessibility when using this prop.
Additional props will be passed to the wrapper div, to make adding attrs like aria-*
easier.
Library will hide the content using display: hidden
when height props is 0. It will also apply aria-hidden="true"
in the same case, but you can override it by passing aria-hidden
prop yourself.
When using a button to toggle height, make sure you add aria-expanded
and aria-controls
to make everything accessible. Here's an example:
<button
aria-expanded={ height !== 0 }
aria-controls="example-panel" // it has to match the id passed to AnimateHeight
onClick={ toggleHeight } // your click handler that toggles height
// ... all other props
>
Toggle
</button>
<AnimateHeight id="example-panel">
Content
</AnimateHeight>
Component checks for prefers-reduced-motion
on start and disables animations if it is set to true. Please note that component doesn't listen for potential changes of prefers-reduced-motion
option.
It is not built in, but you can use AnimateHeight
and ResizeObserver to automatically animate height on content change. I created a small example for you here:
While it is animating, component has overflow: hidden
. When the animation is finished and height is set to "auto", overflow: hidden
is removed. At that moment, any margins you have on the content inside AnimateHeight
will collapse, causing content to "jump". To avoid this, just use padding inside the AnimateHeight
instead of margins.
If AnimateHeight
is a flex child and it's parent has a fixed height, animation won't work.
To fix this, you just need to add the following CSS rule to the AnimateHeight
instance.
flex-shrink: 0;
You can do it by passing a className
or directly in the style
prop
<AnimateHeight style={{flexShrink: 0}}>
Check the issue #89 for the example and more details.
FAQs
Lightweight React component for animating height using CSS transitions.
The npm package react-animate-height receives a total of 220,609 weekly downloads. As such, react-animate-height popularity was classified as popular.
We found that react-animate-height demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.