Skip to content

Commit

Permalink
Potential improvement, drop classNames dependency (#135)
Browse files Browse the repository at this point in the history
- add classes to an array (some will be false if conditions aren't met)
- filter for truthy values
- join array elements with a space
  • Loading branch information
EarthlingDavey committed Nov 29, 2022
1 parent 15f7f2f commit 8acddde
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions src/index.tsx
@@ -1,5 +1,4 @@
import React, { CSSProperties, useEffect, useRef, useState } from 'react';
import classNames from 'classnames';

// ------------------ Types

Expand Down Expand Up @@ -68,12 +67,14 @@ function getStaticStateClasses(
animationStateClasses: AnimationStateClasses,
height: Height
) {
return classNames({
[animationStateClasses.static]: true,
[animationStateClasses.staticHeightZero]: height === 0,
[animationStateClasses.staticHeightSpecific]: height > 0,
[animationStateClasses.staticHeightAuto]: height === 'auto',
});
return [
animationStateClasses.static,
height === 0 && animationStateClasses.staticHeightZero,
height > 0 && animationStateClasses.staticHeightSpecific,
height === 'auto' && animationStateClasses.staticHeightAuto,
]
.filter((v) => v)
.join(' ');
}

// ------------------ Component
Expand Down Expand Up @@ -208,16 +209,18 @@ const AnimateHeight: React.FC<AnimateHeightProps> = ({
}

// Animation classes
const newAnimationStateClassNames = classNames({
[stateClasses.current.animating]: true,
[stateClasses.current.animatingUp]:
prevHeight.current === 'auto' || height < prevHeight.current,
[stateClasses.current.animatingDown]:
height === 'auto' || height > prevHeight.current,
[stateClasses.current.animatingToHeightZero]: timeoutHeight === 0,
[stateClasses.current.animatingToHeightAuto]: timeoutHeight === 'auto',
[stateClasses.current.animatingToHeightSpecific]: timeoutHeight > 0,
});
const newAnimationStateClassNames = [
stateClasses.current.animating,
(prevHeight.current === 'auto' || height < prevHeight.current) &&
stateClasses.current.animatingUp,
(height === 'auto' || height > prevHeight.current) &&
stateClasses.current.animatingDown,
timeoutHeight === 0 && stateClasses.current.animatingToHeightZero,
timeoutHeight === 'auto' && stateClasses.current.animatingToHeightAuto,
timeoutHeight > 0 && stateClasses.current.animatingToHeightSpecific,
]
.filter((v) => v)
.join(' ');

// Animation classes to be put after animation is complete
const timeoutAnimationStateClasses = getStaticStateClasses(
Expand Down

0 comments on commit 8acddde

Please sign in to comment.