Skip to content

Commit fe62c7f

Browse files
authored
theme designer: fix tab states (#23729)
* add custom theme to export * add typing, update format * remove reducer * preserve custom state
1 parent 60ac3d4 commit fe62c7f

3 files changed

Lines changed: 35 additions & 17 deletions

File tree

packages/react-components/theme-designer/src/components/Sidebar/EditTab.tsx

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,32 +41,25 @@ const useStyles = makeStyles({
4141
export interface EditTabProps {
4242
sidebarId: string;
4343
dispatchState: React.Dispatch<DispatchTheme>;
44+
formState: CustomAttributes;
45+
setFormState: React.Dispatch<CustomAttributes>;
4446
}
4547

4648
export const EditTab: React.FC<EditTabProps> = props => {
4749
const styles = useStyles();
4850

49-
const { sidebarId, dispatchState } = props;
51+
const { sidebarId, dispatchState, formState, setFormState } = props;
5052

51-
const initialForm = {
52-
keyColor: '#006bc7',
53-
hueTorsion: 0,
54-
darkCp: 2 / 3,
55-
lightCp: 1 / 3,
56-
isDark: false,
57-
};
53+
const initialForm: CustomAttributes = formState;
5854

5955
const formReducer = (state: CustomAttributes, action: { attributes: CustomAttributes; type: string }) => {
56+
setFormState(action.attributes);
6057
dispatchState({ ...form, type: 'Custom', customAttributes: action.attributes, overrides: {} });
6158
return action.attributes;
6259
};
6360

6461
const [form, dispatchForm] = React.useReducer(formReducer, initialForm);
6562

66-
React.useEffect(() => {
67-
dispatchState({ type: 'Custom', customAttributes: form, overrides: {} });
68-
}, [dispatchState, form]);
69-
7063
const toggleTheme = () => {
7164
dispatchForm({ attributes: { ...form, isDark: !form.isDark }, type: 'isDark' });
7265
};

packages/react-components/theme-designer/src/components/Sidebar/Sidebar.tsx

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
import * as React from 'react';
33
import { makeStyles, mergeClasses, shorthands } from '@griffel/react';
44
import { TabValue, TabList, Tab, SelectTabEvent, SelectTabData, useId, tokens } from '@fluentui/react-components';
5+
import type { CustomAttributes, DispatchTheme } from '../../useThemeDesignerReducer';
56

6-
import type { DispatchTheme } from '../../useThemeDesignerReducer';
77
import { UseTab } from './UseTab';
88
import { EditTab } from './EditTab';
99

@@ -78,10 +78,25 @@ export const Sidebar: React.FC<SidebarProps> = props => {
7878
const sidebarId = useId();
7979

8080
const [tab, setTab] = React.useState<TabValue>('use');
81-
const handleTabChange = (event: SelectTabEvent, data: SelectTabData) => setTab(data.value);
81+
const handleTabChange = (event: SelectTabEvent, data: SelectTabData) => {
82+
if (data.value === 'edit') {
83+
props.dispatchState({ type: 'Custom', customAttributes: formState, overrides: {} });
84+
} else if (data.value === 'use') {
85+
setTheme('Custom');
86+
}
87+
setTab(data.value);
88+
};
8289

8390
const [theme, setTheme] = React.useState<string>('Teams Light');
8491

92+
const [formState, setFormState] = React.useState<CustomAttributes>({
93+
keyColor: '#006bc7',
94+
hueTorsion: 0,
95+
darkCp: 2 / 3,
96+
lightCp: 1 / 3,
97+
isDark: false,
98+
});
99+
85100
return (
86101
<div className={mergeClasses(styles.root, props.className)}>
87102
<TabList className={styles.tabs} size="medium" selectedValue={tab} onTabSelect={handleTabChange}>
@@ -99,9 +114,17 @@ export const Sidebar: React.FC<SidebarProps> = props => {
99114
setTheme={setTheme}
100115
dispatchState={props.dispatchState}
101116
setTab={setTab}
117+
formState={formState}
118+
/>
119+
)}
120+
{tab === 'edit' && (
121+
<EditTab
122+
sidebarId={sidebarId}
123+
dispatchState={props.dispatchState}
124+
formState={formState}
125+
setFormState={setFormState}
102126
/>
103127
)}
104-
{tab === 'edit' && <EditTab sidebarId={sidebarId} dispatchState={props.dispatchState} />}
105128
</div>
106129
);
107130
};

packages/react-components/theme-designer/src/components/Sidebar/UseTab.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
TabValue,
1616
} from '@fluentui/react-components';
1717

18-
import type { DispatchTheme } from '../../useThemeDesignerReducer';
18+
import type { CustomAttributes, DispatchTheme } from '../../useThemeDesignerReducer';
1919

2020
const useStyles = makeStyles({
2121
root: {
@@ -37,17 +37,19 @@ export interface UseTabProps {
3737
dispatchState: React.Dispatch<DispatchTheme>;
3838
sidebarId: string;
3939
setTab: React.Dispatch<TabValue>;
40+
formState: CustomAttributes;
4041
}
4142

4243
export const UseTab: React.FC<UseTabProps> = props => {
4344
const styles = useStyles();
4445

45-
const { theme, setTheme, dispatchState, sidebarId } = props;
46+
const { theme, setTheme, dispatchState, sidebarId, formState } = props;
4647

4748
const handleThemeChange: MenuProps['onCheckedValueChange'] = (e, data) => {
4849
const newTheme = data.checkedItems[0] as string;
4950
if (newTheme === 'Custom') {
5051
props.setTab('edit');
52+
dispatchState({ type: newTheme, customAttributes: formState, overrides: {} });
5153
} else {
5254
dispatchState({ type: newTheme, overrides: {} });
5355
}

0 commit comments

Comments
 (0)