1
- import { theme } from '@redis-ui/styles'
1
+ import { theme , useTheme } from '@redis-ui/styles'
2
2
import { ReactNode } from 'react'
3
3
import styled , { css , keyframes } from 'styled-components'
4
4
5
- const backgroundStyles = {
6
- loaderBar : {
7
- primary : css `
8
- background-color: ${ theme . semantic . color . background . primary500 } ;
9
- ` ,
10
- } ,
5
+ export type EuiColorNames =
6
+ | 'inherit'
7
+ | 'default'
8
+ | 'primary'
9
+ | 'danger'
10
+ | 'warning'
11
+ | 'success'
12
+
13
+ interface LoaderBarProps {
14
+ color ?: string
15
+ }
16
+
17
+ export type ColorType = LoaderBarProps [ 'color' ] | EuiColorNames | ( string & { } )
18
+ type ThemeColors = typeof theme . semantic . color
19
+
20
+ export const getBarBackgroundColor = (
21
+ color : ColorType ,
22
+ themeColors : ThemeColors ,
23
+ ) => {
24
+ if ( ! color ) {
25
+ return themeColors . background . primary300
26
+ }
27
+
28
+ const barBackgroundColors : Map < ColorType , string > = new Map ( [
29
+ [ 'inherit' , 'inherit' ] ,
30
+ [ 'default' , themeColors . background . primary300 ] ,
31
+ [ 'primary' , themeColors . background . primary300 ] ,
32
+ [ 'danger' , themeColors . background . danger600 ] ,
33
+ [ 'warning' , themeColors . background . attention600 ] ,
34
+ [ 'success' , themeColors . background . success600 ] ,
35
+ ] )
36
+
37
+ return barBackgroundColors . get ( color ) ?? color
38
+ }
39
+
40
+ export interface MapProps extends LoaderBarProps {
41
+ $color ?: ColorType
42
+ }
43
+
44
+ export const useColorBackgroundStyles = ( { $color } : MapProps = { } ) => {
45
+ const theme = useTheme ( )
46
+ const colors = theme . semantic . color
47
+
48
+ const getColorValue = ( color ?: ColorType ) =>
49
+ getBarBackgroundColor ( color , colors )
50
+
51
+ return css `
52
+ background-color: ${ getColorValue ( $color ) } ;
53
+ `
11
54
}
12
55
13
56
const loading = keyframes `
@@ -33,19 +76,14 @@ export const LoaderContainer = styled.div<LoaderContainerProps>`
33
76
border-radius: 2px;
34
77
`
35
78
36
- type LoaderBarColor = keyof typeof backgroundStyles . loaderBar
37
-
38
- interface LoaderBarProps {
39
- color : LoaderBarColor
40
- }
79
+ export const LoaderBar = styled . div < MapProps > `
80
+ ${ useColorBackgroundStyles }
41
81
42
- export const LoaderBar = styled . div < LoaderBarProps > `
43
82
position: absolute;
44
83
height: 100%;
45
84
width: 100%;
46
85
border-radius: 2px;
47
86
48
- ${ ( { color = 'primary' } ) => backgroundStyles . loaderBar [ color ] } ;
49
87
animation: ${ loading } 1s ease-in-out infinite;
50
88
`
51
89
0 commit comments