grafana/public/app/core/components/Animations/SlideDown.tsx

53 lines
1.4 KiB
TypeScript
Raw Normal View History

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