1
1
import './swing.css' ;
2
- import { NamedAnimationPairs , extend , maybeScopedStyleSheet , setKeyframes , setStyles } from './animation-style' ;
2
+ import { type NamedAnimationPairs , extend , styleSheet , setKeyframes , setStyles } from './animation-style' ;
3
3
import type { TransitionAnimation , TransitionDirectionalAnimations } from 'astro' ;
4
4
5
5
type SwingKeyframeParameter = {
6
6
axis ?: { x ?: number ; y ?: number ; z ?: number ; } ;
7
- angle ?: { grow ?: string ; shrink ?: string ; } ;
8
- midOpacity ?: number ;
9
- toOpacity ?: number ;
7
+ angle ?: { leave ?: string ; enter ?: string ; } ;
8
+ opacity ?: { mid ?: number ; to ?: number ; } ;
10
9
} ;
11
10
12
11
type CustomSwingOptions = {
13
- keyframes ?: SwingKeyframeParameter ;
12
+ keyframes ?: SwingKeyframeParameter | string ;
14
13
base ?: AnimationProperties ;
15
- animations ?: NamedAnimationPairs ;
14
+ extensions ?: NamedAnimationPairs ;
16
15
} ;
17
16
18
17
19
18
export const genKeyframes = (
20
- name : string ,
19
+ keyframeNamePrefix : string ,
21
20
x : number = 0 ,
22
21
y : number = 0 ,
23
22
z : number = 0 ,
24
- growAngle = "90deg" ,
25
- shrinkAngle = "-90deg" ,
23
+ leaveAngle = "90deg" ,
24
+ enterAngle = "-90deg" ,
26
25
midOpacity : number = 1 ,
27
26
toOpacity : number = 0
28
- ) =>
29
- `
30
- @keyframes ${ name } FwdSwingOut {
27
+ ) => setKeyframes ( keyframeNamePrefix , `
28
+ @keyframes ${ keyframeNamePrefix } FwdSwingOut {
31
29
from {
32
30
transform: rotate3d(${ x } , ${ y } , ${ z } , 0);
33
31
opacity: 1;
@@ -36,13 +34,13 @@ export const genKeyframes = (
36
34
opacity: ${ midOpacity } ;
37
35
}
38
36
to {
39
- transform: rotate3d(${ x } , ${ y } , ${ z } , ${ growAngle } );
37
+ transform: rotate3d(${ x } , ${ y } , ${ z } , ${ leaveAngle } );
40
38
opacity: ${ toOpacity } ;
41
39
}
42
40
}
43
- @keyframes ${ name } FwdSwingIn {
41
+ @keyframes ${ keyframeNamePrefix } FwdSwingIn {
44
42
from {
45
- transform: rotate3d(${ x } , ${ y } , ${ z } , ${ shrinkAngle } );
43
+ transform: rotate3d(${ x } , ${ y } , ${ z } , ${ enterAngle } );
46
44
opacity: ${ toOpacity } ;
47
45
}
48
46
50% {
@@ -53,7 +51,7 @@ export const genKeyframes = (
53
51
opacity: 1;
54
52
}
55
53
}
56
- @keyframes ${ name } BwdSwingOut {
54
+ @keyframes ${ keyframeNamePrefix } BwdSwingOut {
57
55
from {
58
56
transform: rotate3d(${ x } , ${ y } , ${ z } , 0);
59
57
opacity: 1;
@@ -62,13 +60,13 @@ export const genKeyframes = (
62
60
opacity: ${ midOpacity } ;
63
61
}
64
62
to {
65
- transform: rotate3d(${ x } , ${ y } , ${ z } , ${ shrinkAngle } );
63
+ transform: rotate3d(${ x } , ${ y } , ${ z } , ${ enterAngle } );
66
64
opacity: ${ toOpacity } ;
67
65
}
68
66
}
69
- @keyframes ${ name } BwdSwingIn {
67
+ @keyframes ${ keyframeNamePrefix } BwdSwingIn {
70
68
from {
71
- transform: rotate3d(${ x } , ${ y } , ${ z } , ${ growAngle } );
69
+ transform: rotate3d(${ x } , ${ y } , ${ z } , ${ leaveAngle } );
72
70
opacity: ${ toOpacity } ;
73
71
}
74
72
50% {
@@ -78,12 +76,12 @@ export const genKeyframes = (
78
76
transform: rotate3d(${ x } , ${ y } , ${ z } , 0);
79
77
opacity: 1;
80
78
}
81
- }` ;
79
+ }` ) ;
82
80
83
81
export type AnimationProperties = Omit < TransitionAnimation , "name" > ;
84
82
85
83
export const swing = ( animation ?: AnimationProperties ) => namedSwing ( '' , animation ) ;
86
- export const namedSwing = ( name : string , animation ?: AnimationProperties ) => {
84
+ export const namedSwing = ( keyframeNamePrefix : string , animation ?: AnimationProperties ) => {
87
85
const common = {
88
86
easing : 'ease-in-out' ,
89
87
fillMode : 'both' ,
@@ -92,34 +90,38 @@ export const namedSwing = (name: string, animation?: AnimationProperties) => {
92
90
} ;
93
91
94
92
const forwards = {
95
- old : { ...common , name : `${ name } FwdSwingOut` } ,
96
- new : { delay : common . duration , ...common , name : `${ name } FwdSwingIn` } ,
93
+ old : { ...common , name : `${ keyframeNamePrefix } FwdSwingOut` } ,
94
+ new : { delay : common . duration , ...common , name : `${ keyframeNamePrefix } FwdSwingIn` } ,
97
95
} ;
98
96
99
97
const backwards = {
100
- old : { ...common , name : `${ name } BwdSwingOut` } ,
101
- new : { delay : common . duration , ...common , name : `${ name } BwdSwingIn` } ,
98
+ old : { ...common , name : `${ keyframeNamePrefix } BwdSwingOut` } ,
99
+ new : { delay : common . duration , ...common , name : `${ keyframeNamePrefix } BwdSwingIn` } ,
102
100
} ;
103
101
return { forwards, backwards } as TransitionDirectionalAnimations ;
104
102
} ;
105
103
106
104
107
105
export const customSwing = (
108
- name : string ,
106
+ transitionName : string ,
109
107
options : CustomSwingOptions ,
110
108
scope ?: string
111
109
) => {
112
- const { keyframes, base, animations : extensions } = options ;
113
- const animations = extend ( namedSwing ( name , base ) , extensions ?? { } ) ;
114
- const axis = keyframes ?. axis ?? { y : 1 } ;
110
+ const { keyframes, base, extensions } = options ;
115
111
116
- setKeyframes (
117
- name ,
118
- genKeyframes ( name , axis ?. x , axis ?. y , axis ?. z , keyframes ?. angle ?. grow , keyframes ?. angle ?. shrink , keyframes ?. midOpacity , keyframes ?. toOpacity )
119
- ) ;
112
+ let keyframeNamePrefix : string ;
120
113
121
- let { scope : finalScope , styles } = maybeScopedStyleSheet ( name , scope , animations ) ;
122
- setStyles ( name , styles ) ;
114
+ if ( typeof keyframes === 'string' ) {
115
+ keyframeNamePrefix = keyframes ;
116
+ } else {
117
+ keyframeNamePrefix = transitionName ;
118
+ const axis = keyframes ?. axis ?? { y : 1 } ;
119
+ genKeyframes ( keyframeNamePrefix , axis ?. x , axis ?. y , axis ?. z , keyframes ?. angle ?. leave , keyframes ?. angle ?. enter , keyframes ?. opacity ?. mid , keyframes ?. opacity ?. to ) ;
120
+ }
121
+
122
+ const animations = extend ( namedSwing ( keyframeNamePrefix , base ) , extensions ?? { } ) ;
123
+ const { scope : finalScope , styles } = styleSheet ( { transitionName, scope, animations } ) ;
124
+ setStyles ( transitionName , styles ) ;
123
125
return finalScope ;
124
126
} ;
125
127
0 commit comments