Skip to content

Commit 5e7c8da

Browse files
authored
docs(ThemableThemingParameters): fix css var value update (#4718)
1 parent 3990376 commit 5e7c8da

File tree

1 file changed

+32
-12
lines changed

1 file changed

+32
-12
lines changed

docs/knowledgeBaseExamples/ThemeableThemingParameters.tsx

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
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';
25
import { FlexBox, FlexBoxDirection, Label, Option, Panel, Select, Text, ThemeProvider } from '@ui5/webcomponents-react';
36
import { ThemingParameters } from '@ui5/webcomponents-react-base';
4-
import React, { CSSProperties, useReducer, useState } from 'react';
5-
import { MAPPED_THEMES } from '../../.storybook/utils';
67

78
const containerStyles = {
89
display: 'grid',
@@ -54,29 +55,40 @@ const getStyleColors = (val) => {
5455
interface Props {
5556
value: string;
5657
varKey: string;
58+
theme: string;
5759
}
5860

5961
interface PropsWithStyle extends Props {
6062
style?: CSSProperties;
6163
}
6264

63-
const VariableValue = ({ value, varKey, style = {} }: PropsWithStyle) => {
65+
const VariableValue = ({ value, varKey, theme, style = {} }: PropsWithStyle) => {
6466
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+
6577
return (
6678
<FlexBox key={varKey} direction={FlexBoxDirection.Column} style={{ width: '85%' }}>
6779
<Text title="Click to show CSS Variable" style={{ cursor: 'pointer', ...style }} onClick={toggleCSSVars}>
6880
{showCSSVars ? value : varKey}
6981
</Text>
70-
<Label>{getComputedStyle(document.documentElement).getPropertyValue(`--${varKey}`)}</Label>
82+
<Label>{updatedLabel}</Label>
7183
</FlexBox>
7284
);
7385
};
7486

7587
const ColorVariables = (props: Props) => {
76-
const { varKey, value } = props;
88+
const { varKey, value, theme } = props;
7789
return (
7890
<div style={{ display: 'inline-flex', alignItems: 'center', justifyContent: 'space-between' }}>
79-
<VariableValue value={value} varKey={varKey} />
91+
<VariableValue value={value} varKey={varKey} theme={theme} />
8092
<div
8193
style={{
8294
...getStyleColors(value),
@@ -92,16 +104,16 @@ const ColorVariables = (props: Props) => {
92104
};
93105

94106
const Variables = (props: PropsWithStyle) => {
95-
const { varKey, value, style = {} } = props;
107+
const { varKey, value, theme, style = {} } = props;
96108
return (
97109
<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} />
99111
</div>
100112
);
101113
};
102114

103115
export const ThemeableCSSVars = () => {
104-
const [currentTheme, setCurrentTheme] = useState(getTheme());
116+
const [currentTheme, setCurrentTheme] = useState<string>(getTheme());
105117
return (
106118
<ThemeProvider>
107119
<div
@@ -130,21 +142,29 @@ export const ThemeableCSSVars = () => {
130142
<Panel headerText="Colors & Shadows" collapsed>
131143
<div style={containerStyles}>
132144
{COLORS.map(([key, value]) => (
133-
<ColorVariables key={key} varKey={key} value={value} />
145+
<ColorVariables key={key} varKey={key} value={value} theme={currentTheme} />
134146
))}
135147
</div>
136148
</Panel>
137149
<Panel headerText="Fonts" collapsed>
138150
<div style={{ ...containerStyles, gridTemplateColumns: 'repeat(1, minmax(0, 1fr))' }}>
139151
{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+
);
141161
})}
142162
</div>
143163
</Panel>
144164
<Panel headerText="Others" collapsed>
145165
<div style={containerStyles}>
146166
{OTHERS.map(([key, value]) => {
147-
return <Variables key={key} varKey={key} value={value} />;
167+
return <Variables key={key} varKey={key} value={value} theme={currentTheme} />;
148168
})}
149169
</div>
150170
</Panel>

0 commit comments

Comments
 (0)