Skip to content

Commit

Permalink
Merge pull request #1606 from gluestack/fix/inline-theme-performance
Browse files Browse the repository at this point in the history
Fix/inline theme performance
  • Loading branch information
ankit-tailor authored Jan 11, 2024
2 parents ebde6ab + 607260c commit c0e5a85
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 25 deletions.
6 changes: 6 additions & 0 deletions packages/styled/react/src/StyledProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { propertyTokenMap } from './propertyTokenMap';
import type { COLORMODES } from './types';
import {
convertTokensToCssVariables,
generateMergedThemeTokens,
platformSpecificSpaceUnits,
} from './utils';
import { createGlobalStylesWeb } from './createGlobalStylesWeb';
Expand Down Expand Up @@ -45,6 +46,7 @@ const setCurrentColorMode = (inputColorMode: string | undefined) => {
// colorModeSet = true;
// }
};

export const StyledProvider: React.FC<{
config: Config;
colorMode?: COLORMODES;
Expand Down Expand Up @@ -98,6 +100,10 @@ export const StyledProvider: React.FC<{
);
}

configWithPlatformSpecificUnits = generateMergedThemeTokens(
configWithPlatformSpecificUnits
);

return configWithPlatformSpecificUnits;
}, [config]);

Expand Down
52 changes: 27 additions & 25 deletions packages/styled/react/src/resolver/StyledValueToCSSObject.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { CSSObject, StyledValue } from '../types';
import { deepMerge, resolvedTokenization } from '../utils';
import { deepClone } from '../utils/cssify/utils/common';
import { resolvedTokenization } from '../utils';

export function StyledValueToCSSObject(
input: StyledValue | undefined,
Expand All @@ -11,6 +10,7 @@ export function StyledValueToCSSObject(
if (!input) {
return {};
}

return resolvedTokenization(input, CONFIG, ignoreKeys, deleteIfTokenNotExist);
}
export function themeStyledValueToCSSObject(
Expand All @@ -20,34 +20,36 @@ export function themeStyledValueToCSSObject(
) {
let themeResolved1: any = {};
if (CONFIG?.themes) {
const tokens = deepClone(CONFIG.tokens);
Object.keys(CONFIG?.themes).forEach((key: any) => {
const themeTokens = CONFIG?.themes[key];
Object.keys(themeTokens).forEach((tokenKey1: any) => {
Object.keys(themeTokens[tokenKey1]).forEach((tokenKey: any) => {
delete tokens[tokenKey1][tokenKey];
});
});
});
// const tokens = deepClone(CONFIG.tokens);
// Object.keys(CONFIG?.themes).forEach((key: any) => {
// const themeTokens = CONFIG?.themes[key];
// Object.keys(themeTokens).forEach((tokenKey1: any) => {
// Object.keys(themeTokens[tokenKey1]).forEach((tokenKey: any) => {
// delete tokens[tokenKey1][tokenKey];
// });
// });
// });

// debugger;

Object.keys(CONFIG?.themes).forEach((key: any) => {
const themeResolved = StyledValueToCSSObject(
input,
{
...CONFIG,
tokens: deepMerge(deepClone(tokens), CONFIG.themes[key]),
},
ignoreKeys,
true
);
Object.keys(CONFIG?.themes).forEach((themeName: any) => {
if (themeName !== 'tokens') {
const themeResolved = StyledValueToCSSObject(
input,
{
...CONFIG,
tokens: CONFIG?.themes?.tokens[themeName],
},
ignoreKeys,
true
);

Object.keys(themeResolved).forEach((key) =>
themeResolved[key] === undefined ? delete themeResolved[key] : {}
);
Object.keys(themeResolved).forEach((key: any) =>
themeResolved[key] === undefined ? delete themeResolved[key] : {}
);

themeResolved1[key] = themeResolved;
themeResolved1[themeName] = themeResolved;
}
});
}

Expand Down
23 changes: 23 additions & 0 deletions packages/styled/react/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
import type { Config } from './types';
import { deepClone } from './utils/cssify/utils/common';

const propsNotToConvertToCSSVariables = ['shadowColor', 'textShadowColor'];

export function generateMergedThemeTokens(CONFIG: any) {
const mergedTokens: any = CONFIG;
const tokens = deepClone(CONFIG.tokens);
const themeTokens: any = {};

if (CONFIG?.themes) {
Object.keys(CONFIG.themes).forEach((key) => {
// tokens is a reserved key to merge theme tokens
if (key !== 'tokens') {
themeTokens[key] = deepMerge(tokens, CONFIG.themes[key]);
}
});

if (themeTokens) {
mergedTokens.themes.tokens = {};
Object.assign(mergedTokens.themes.tokens, themeTokens);
}
}

return mergedTokens;
}

export function convertToUnicodeString(inputString: any) {
let result = '';
if (!inputString) {
Expand Down

0 comments on commit c0e5a85

Please sign in to comment.