2019-03-19 12:24:09 -05:00
|
|
|
import React, { CSSProperties, FC } from 'react';
|
2018-01-31 07:19:07 -06:00
|
|
|
import Transition from 'react-transition-group/Transition';
|
|
|
|
|
2018-10-11 04:56:32 -05:00
|
|
|
interface Style {
|
|
|
|
transition?: string;
|
|
|
|
overflow?: string;
|
|
|
|
}
|
|
|
|
|
2018-10-31 06:50:44 -05:00
|
|
|
// When animating using max-height we need to use a static value.
|
2018-01-31 07:19:07 -06:00
|
|
|
// If this is not enough, pass in <SlideDown maxHeight="....
|
2018-10-31 06:50:44 -05:00
|
|
|
const defaultMaxHeight = '200px';
|
2018-01-31 07:19:07 -06:00
|
|
|
const defaultDuration = 200;
|
2018-10-31 06:50:44 -05:00
|
|
|
|
2018-10-11 04:56:32 -05:00
|
|
|
export const defaultStyle: Style = {
|
2018-01-31 07:19:07 -06:00
|
|
|
transition: `max-height ${defaultDuration}ms ease-in-out`,
|
|
|
|
overflow: 'hidden',
|
|
|
|
};
|
|
|
|
|
2019-03-19 12:24:09 -05:00
|
|
|
export interface Props {
|
|
|
|
children: React.ReactNode;
|
|
|
|
in: boolean;
|
|
|
|
maxHeight?: number;
|
|
|
|
style?: CSSProperties;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const SlideDown: FC<Props> = ({ children, in: inProp, maxHeight = defaultMaxHeight, style = defaultStyle }) => {
|
2018-01-31 07:19:07 -06:00
|
|
|
// There are 4 main states a Transition can be in:
|
|
|
|
// ENTERING, ENTERED, EXITING, EXITED
|
2019-03-19 12:24:09 -05:00
|
|
|
// https://reactcommunity.or[g/react-transition-group/
|
|
|
|
const transitionStyles: { [str: string]: CSSProperties } = {
|
2018-01-31 07:19:07 -06:00
|
|
|
exited: { maxHeight: 0 },
|
|
|
|
entering: { maxHeight: maxHeight },
|
2018-11-06 11:14:29 -06:00
|
|
|
entered: { maxHeight: 'unset', overflow: 'visible' },
|
2018-01-31 07:19:07 -06:00
|
|
|
exiting: { maxHeight: 0 },
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Transition in={inProp} timeout={defaultDuration}>
|
|
|
|
{state => (
|
|
|
|
<div
|
|
|
|
style={{
|
2018-10-11 04:56:32 -05:00
|
|
|
...style,
|
2018-01-31 07:19:07 -06:00
|
|
|
...transitionStyles[state],
|
2019-03-19 12:24:09 -05:00
|
|
|
inProp,
|
2018-01-31 07:19:07 -06:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</Transition>
|
|
|
|
);
|
|
|
|
};
|