-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththemeHelper.ts
More file actions
26 lines (22 loc) · 821 Bytes
/
themeHelper.ts
File metadata and controls
26 lines (22 loc) · 821 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import { Theme } from '@emotion/react';
export const flattenObject = (input: Record<string, any>) => {
const flattened: Record<string, any> = {};
Object.keys(input).forEach(key => {
if (typeof input[key] === 'object' && input[key] !== null) {
Object.assign(flattened, flattenObject(input[key]));
} else {
flattened[key] = input[key];
}
});
return flattened;
};
export const convertThemeToCSSVariables = ({ variables, ...theme }: Theme) => {
const flattenedTheme = flattenObject(theme);
const flattenedVariables = flattenObject(variables);
const returnValue: string[] = [];
// eslint-disable-next-line no-restricted-syntax
for (const [key, value] of Object.entries(flattenedTheme)) {
returnValue.push(`${flattenedVariables[key]}: ${value};`);
}
return returnValue;
};