1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
import { InterpolationValue } from 'styled-components';
import { media, OptionallyScreenSpecific, stylesForMedia } from '../../style/media';
import { css, keyframes, styled } from '../../style/theme';
export interface TransitionInfo {
from: string;
to: string;
}
const generateTransitionInfoCss = (
key: keyof TransitionInfo,
top?: TransitionInfo,
bottom?: TransitionInfo,
left?: TransitionInfo,
right?: TransitionInfo,
): string => {
const topStringIfExists = top ? `top: ${top[key]};` : '';
const bottomStringIfExists = bottom ? `bottom: ${bottom[key]};` : '';
const leftStringIfExists = left ? `left: ${left[key]};` : '';
const rightStringIfExists = right ? `right: ${right[key]};` : '';
return `
${topStringIfExists}
${bottomStringIfExists}
${leftStringIfExists}
${rightStringIfExists}
`;
};
const slideKeyframeGenerator = (
position: string,
top?: TransitionInfo,
bottom?: TransitionInfo,
left?: TransitionInfo,
right?: TransitionInfo,
) => keyframes`
from {
position: ${position};
${generateTransitionInfoCss('from', top, bottom, left, right)}
}
to {
position: ${position};
${generateTransitionInfoCss('to', top, bottom, left, right)}
}
`;
export interface PositionAnimationSettings {
top?: TransitionInfo;
bottom?: TransitionInfo;
left?: TransitionInfo;
right?: TransitionInfo;
timingFunction: string;
duration?: string;
position?: string;
}
const generatePositionAnimationCss = (positionSettings: PositionAnimationSettings) => {
return css`
animation-name: ${slideKeyframeGenerator(
positionSettings.position || 'relative',
positionSettings.top,
positionSettings.bottom,
positionSettings.left,
positionSettings.right,
)};
animation-duration: ${positionSettings.duration || '0.3s'};
animation-timing-function: ${positionSettings.timingFunction};
animation-delay: 0s;
animation-iteration-count: 1;
animation-fill-mode: forwards;
position: ${positionSettings.position || 'relative'};
width: 100%;
`;
};
export interface PositionAnimationProps {
positionSettings: OptionallyScreenSpecific<PositionAnimationSettings>;
zIndex?: OptionallyScreenSpecific<number>;
height?: string;
}
const defaultAnimation = (positionSettings: OptionallyScreenSpecific<PositionAnimationSettings>) => {
const bestDefault = 'default' in positionSettings ? positionSettings.default : positionSettings;
return generatePositionAnimationCss(bestDefault);
};
const animationForSize = (
positionSettings: OptionallyScreenSpecific<PositionAnimationSettings>,
sizeKey: 'sm' | 'md' | 'lg',
mediaFn: (...args: any[]) => InterpolationValue[],
) => {
// checking default makes sure we have a PositionAnimationSettings object
// and then we check to see if we have a setting for the specific `sizeKey`
const animationSettingsForSize = 'default' in positionSettings && positionSettings[sizeKey];
return animationSettingsForSize && mediaFn`${generatePositionAnimationCss(animationSettingsForSize)}`;
};
export const PositionAnimation =
styled.div <
PositionAnimationProps >
`
&& {
${props => props.zIndex && stylesForMedia<number>('z-index', props.zIndex)}
${props => defaultAnimation(props.positionSettings)}
${props => animationForSize(props.positionSettings, 'sm', media.small)}
${props => animationForSize(props.positionSettings, 'md', media.medium)}
${props => animationForSize(props.positionSettings, 'lg', media.large)}
${props => (props.height ? `height: ${props.height};` : '')}
}
`;
|