1
+ import styled , { css , CSSProperties } from 'styled-components'
1
2
import { AppTheme , MediaQuery } from './theme'
2
- import styled , { CSSProperties , css } from 'styled-components'
3
3
4
4
export const omitKeys = < T extends { [ k : string ] : unknown } > (
5
5
obj : T ,
@@ -20,14 +20,24 @@ export const dynamicStyleKeys = [
20
20
'@media-mobile' ,
21
21
]
22
22
23
- const nonPixelKeys = [ 'flexGrow' , 'flexShrink' , 'lineHeight' , 'fontWeight ']
23
+ const animationKeys = [ '@animation ' ]
24
24
25
- export const mapStylesToCSS = style =>
25
+ const nonPixelKeys = [
26
+ 'flexGrow' ,
27
+ 'flexShrink' ,
28
+ 'fontWeight' ,
29
+ 'lineHeight' ,
30
+ 'opacity' ,
31
+ ]
32
+
33
+ export const mapStylesToCSS = ( style ) =>
26
34
Object . entries ( style )
27
35
. map (
28
36
( [ cssKey , cssValue ] ) =>
29
- `${ cssKey . replace ( / [ A - Z ] / g, match => `-${ match . toLowerCase ( ) } ` ) } : ${
30
- isNaN ( cssValue as any ) || nonPixelKeys . includes ( cssKey ) ? cssValue : `${ cssValue } px`
37
+ `${ cssKey . replace ( / [ A - Z ] / g, ( match ) => `-${ match . toLowerCase ( ) } ` ) } : ${
38
+ isNaN ( cssValue as any ) || nonPixelKeys . includes ( cssKey )
39
+ ? cssValue
40
+ : `${ cssValue } px`
31
41
} ;`,
32
42
)
33
43
. join ( ' ' )
@@ -36,15 +46,6 @@ export const mapStylesToCSS = style =>
36
46
* Creates a styled component with support for pseudo elements and media query styles provided from a style object
37
47
*/
38
48
39
- export type StyledCSSProperties = {
40
- css : CSSProperties
41
- ':hover' ?: CSSProperties
42
- ':focus' ?: CSSProperties
43
- ':invalid' ?: CSSProperties
44
- ':readonly' ?: CSSProperties
45
- ':disabled' ?: CSSProperties
46
- '@media-mobile' ?: CSSProperties
47
- }
48
49
export const createStyled = ( type : any ) =>
49
50
typeof type === 'string'
50
51
? styled [ type ] `
@@ -93,10 +94,14 @@ export const getUseCSSStyles = <Theme>() => <C extends keyof Theme>(
93
94
elementKeys : Array < K > | K ,
94
95
internalOverride ?: CSSProperties ,
95
96
) : string => {
97
+ const keys = Array . isArray ( elementKeys )
98
+ ? elementKeys
99
+ : ( [ elementKeys ] as Array < K > )
100
+
96
101
const styles = {
97
102
css : omitKeys (
98
103
{
99
- ...( ( Array . isArray ( elementKeys ) ? elementKeys : [ elementKeys ] ) . reduce (
104
+ ...( keys . reduce (
100
105
( obj , elementKey ) => ( {
101
106
...obj ,
102
107
...theme [ componentKey ] [ elementKey ] ,
@@ -106,12 +111,12 @@ export const getUseCSSStyles = <Theme>() => <C extends keyof Theme>(
106
111
) as any ) ,
107
112
...( internalOverride || { } ) ,
108
113
} ,
109
- dynamicStyleKeys as any ,
114
+ dynamicStyleKeys . concat ( animationKeys ) as any ,
110
115
) ,
111
116
...dynamicStyleKeys . reduce (
112
117
( obj , k ) => ( {
113
118
...obj ,
114
- [ k ] : ( Array . isArray ( elementKeys ) ? elementKeys : [ elementKeys ] ) . reduce (
119
+ [ k ] : keys . reduce (
115
120
( obj , elementKey ) => ( {
116
121
...obj ,
117
122
...( theme [ componentKey ] [ elementKey ] [ k ] || { } ) ,
@@ -126,6 +131,14 @@ export const getUseCSSStyles = <Theme>() => <C extends keyof Theme>(
126
131
) ,
127
132
}
128
133
134
+ const animationKey = keys . find (
135
+ ( elementKey ) => ! ! theme [ componentKey ] [ elementKey ] [ '@animation' ] ,
136
+ )
137
+ let animation
138
+ if ( animationKey ) {
139
+ animation = theme [ componentKey ] [ animationKey ] [ '@animation' ]
140
+ }
141
+
129
142
const cssStyles = `
130
143
${ mapStylesToCSS ( styles . css || { } ) }
131
144
@@ -152,6 +165,8 @@ export const getUseCSSStyles = <Theme>() => <C extends keyof Theme>(
152
165
@media ${ MediaQuery . Mobile } {
153
166
${ mapStylesToCSS ( styles [ '@media-mobile' ] || { } ) }
154
167
}
168
+
169
+ ${ animation ? `&.animate { animation: ${ animation } ; }` : '' }
155
170
`
156
171
157
172
return cssStyles
0 commit comments