1
1
import { getTheme , setTheme } from '@ui5/webcomponents-base/dist/config/Theme.js' ;
2
+ import React , { useEffect , useReducer , useState } from 'react' ;
3
+ import type { CSSProperties } from 'react' ;
4
+ import { MAPPED_THEMES } from '../../.storybook/utils' ;
2
5
import { FlexBox , FlexBoxDirection , Label , Option , Panel , Select , Text , ThemeProvider } from '@ui5/webcomponents-react' ;
3
6
import { ThemingParameters } from '@ui5/webcomponents-react-base' ;
4
- import React , { CSSProperties , useReducer , useState } from 'react' ;
5
- import { MAPPED_THEMES } from '../../.storybook/utils' ;
6
7
7
8
const containerStyles = {
8
9
display : 'grid' ,
@@ -54,29 +55,40 @@ const getStyleColors = (val) => {
54
55
interface Props {
55
56
value : string ;
56
57
varKey : string ;
58
+ theme : string ;
57
59
}
58
60
59
61
interface PropsWithStyle extends Props {
60
62
style ?: CSSProperties ;
61
63
}
62
64
63
- const VariableValue = ( { value, varKey, style = { } } : PropsWithStyle ) => {
65
+ const VariableValue = ( { value, varKey, theme , style = { } } : PropsWithStyle ) => {
64
66
const [ showCSSVars , toggleCSSVars ] = useReducer ( ( prev ) => ! prev , false ) ;
67
+ const [ updatedLabel , setUpdatedLabel ] = useState (
68
+ getComputedStyle ( document . documentElement ) . getPropertyValue ( `--${ varKey } ` )
69
+ ) ;
70
+
71
+ useEffect ( ( ) => {
72
+ setTimeout ( ( ) => {
73
+ setUpdatedLabel ( getComputedStyle ( document . documentElement ) . getPropertyValue ( `--${ varKey } ` ) ) ;
74
+ } , 200 ) ;
75
+ } , [ theme ] ) ;
76
+
65
77
return (
66
78
< FlexBox key = { varKey } direction = { FlexBoxDirection . Column } style = { { width : '85%' } } >
67
79
< Text title = "Click to show CSS Variable" style = { { cursor : 'pointer' , ...style } } onClick = { toggleCSSVars } >
68
80
{ showCSSVars ? value : varKey }
69
81
</ Text >
70
- < Label > { getComputedStyle ( document . documentElement ) . getPropertyValue ( `-- ${ varKey } ` ) } </ Label >
82
+ < Label > { updatedLabel } </ Label >
71
83
</ FlexBox >
72
84
) ;
73
85
} ;
74
86
75
87
const ColorVariables = ( props : Props ) => {
76
- const { varKey, value } = props ;
88
+ const { varKey, value, theme } = props ;
77
89
return (
78
90
< div style = { { display : 'inline-flex' , alignItems : 'center' , justifyContent : 'space-between' } } >
79
- < VariableValue value = { value } varKey = { varKey } />
91
+ < VariableValue value = { value } varKey = { varKey } theme = { theme } />
80
92
< div
81
93
style = { {
82
94
...getStyleColors ( value ) ,
@@ -92,16 +104,16 @@ const ColorVariables = (props: Props) => {
92
104
} ;
93
105
94
106
const Variables = ( props : PropsWithStyle ) => {
95
- const { varKey, value, style = { } } = props ;
107
+ const { varKey, value, theme , style = { } } = props ;
96
108
return (
97
109
< div style = { { display : 'inline-flex' , alignItems : 'center' , justifyContent : 'space-between' } } >
98
- < VariableValue value = { value } varKey = { varKey } style = { style } />
110
+ < VariableValue value = { value } varKey = { varKey } style = { style } them = { theme } />
99
111
</ div >
100
112
) ;
101
113
} ;
102
114
103
115
export const ThemeableCSSVars = ( ) => {
104
- const [ currentTheme , setCurrentTheme ] = useState ( getTheme ( ) ) ;
116
+ const [ currentTheme , setCurrentTheme ] = useState < string > ( getTheme ( ) ) ;
105
117
return (
106
118
< ThemeProvider >
107
119
< div
@@ -130,21 +142,29 @@ export const ThemeableCSSVars = () => {
130
142
< Panel headerText = "Colors & Shadows" collapsed >
131
143
< div style = { containerStyles } >
132
144
{ COLORS . map ( ( [ key , value ] ) => (
133
- < ColorVariables key = { key } varKey = { key } value = { value } />
145
+ < ColorVariables key = { key } varKey = { key } value = { value } theme = { currentTheme } />
134
146
) ) }
135
147
</ div >
136
148
</ Panel >
137
149
< Panel headerText = "Fonts" collapsed >
138
150
< div style = { { ...containerStyles , gridTemplateColumns : 'repeat(1, minmax(0, 1fr))' } } >
139
151
{ FONTS . map ( ( [ key , value ] ) => {
140
- return < Variables key = { key } varKey = { key } value = { value } style = { { ...getStyleFonts ( value ) } } /> ;
152
+ return (
153
+ < Variables
154
+ key = { key }
155
+ varKey = { key }
156
+ value = { value }
157
+ style = { { ...getStyleFonts ( value ) } }
158
+ theme = { currentTheme }
159
+ />
160
+ ) ;
141
161
} ) }
142
162
</ div >
143
163
</ Panel >
144
164
< Panel headerText = "Others" collapsed >
145
165
< div style = { containerStyles } >
146
166
{ OTHERS . map ( ( [ key , value ] ) => {
147
- return < Variables key = { key } varKey = { key } value = { value } /> ;
167
+ return < Variables key = { key } varKey = { key } value = { value } theme = { currentTheme } /> ;
148
168
} ) }
149
169
</ div >
150
170
</ Panel >
0 commit comments