Skip to content
This repository has been archived by the owner on Dec 11, 2023. It is now read-only.

Commit

Permalink
Merge pull request #470 from gluestack/fix/web-perf
Browse files Browse the repository at this point in the history
Fix/web perf
  • Loading branch information
surajahmed authored Oct 11, 2023
2 parents fdbf3b7 + e715502 commit 9f80d54
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,12 @@ const Text1 = styled(
}
);

const MyLink = styled(Link, {});
const Box = styled(View, {
bg: '$blue500',
p: '$10',
});
export function ContextBasedStyles() {
console.log('>>>>> component');
return (
<Wrapper colorMode="dark">
{/* <StyledIcon as={CameraIcon} /> */}
Expand All @@ -129,14 +133,12 @@ export function ContextBasedStyles() {
>
vdkbkdfbv
</Text1> */}
<MyLink
href={'/'}
<Box
// href={'/'}
sx={{
color: '$red500',
bg: '$red500',
}}
>
next link
</MyLink>
></Box>
</Wrapper>
);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/react/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@gluestack-style/react",
"description": "A universal & performant styling library for React Native, Next.js & React",
"version": "1.0.2-alpha.0",
"version": "1.0.2-alpha.8",
"keywords": [
"React Native",
"Next.js",
Expand Down
39 changes: 39 additions & 0 deletions packages/react/src/StyledProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { COLORMODES } from './types';
import { platformSpecificSpaceUnits } from './utils';
import { createGlobalStylesWeb } from './createGlobalStylesWeb';
import { createGlobalStyles } from './createGlobalStyles';

type Config = any;
let colorModeSet = false;

Expand Down Expand Up @@ -50,6 +51,11 @@ export const StyledProvider: React.FC<{
globalStyles,
_experimentalNestedProvider,
}) => {
const inlineStyleMap: any = React.useRef({
initialStyleInjected: false,
});
inlineStyleMap.current.initialStyleInjected = false;

const currentConfig: any = React.useMemo(() => {
//TODO: Add this later
return platformSpecificSpaceUnits(config, Platform.OS);
Expand Down Expand Up @@ -107,6 +113,38 @@ export const StyledProvider: React.FC<{
setCurrentColorMode(currentColorMode);
}, [currentColorMode]);

React.useLayoutEffect(() => {
if (Platform.OS === 'web') {
const toBeInjectedStyles: any = {};

if (inlineStyleMap.current.initialStyleInjected) {
return;
}

Object.keys(inlineStyleMap.current).forEach((key: any) => {
if (key !== 'initialStyleInjected') {
const styles = inlineStyleMap.current[key];
if (!toBeInjectedStyles[key]) {
toBeInjectedStyles[key] = document.createDocumentFragment();
}

styles.forEach((style: any) => {
if (!document.getElementById(style.id)) {
toBeInjectedStyles[key].appendChild(style);
}
});
}
});
Object.keys(toBeInjectedStyles).forEach((key) => {
let wrapperElement = document.querySelector('#' + key);
if (wrapperElement) {
wrapperElement.appendChild(toBeInjectedStyles[key]);
}
// delete inlineStyleMap.current[key];
});
inlineStyleMap.current.initialStyleInjected = true;
}
});
// // Set colormode for the first time
if (!colorModeSet) {
setCurrentColorMode(currentColorMode);
Expand All @@ -122,6 +160,7 @@ export const StyledProvider: React.FC<{
globalStyle: globalStyleMap,
animationDriverData,
setAnimationDriverData,
inlineStyleMap: inlineStyleMap.current,
};
if (_experimentalNestedProvider) {
//@ts-ignore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ export function injectComponentAndDescendantStyles(
styleTagId?: string,
type: 'boot' | 'inline' = 'boot',
_GluestackStyleSheet: StyleInjector = GluestackStyleSheet,
platform: string = ''
platform: string = '',
inlineStyleMap?: any
) {
const [
componentOrderResolvedBaseStyle,
Expand Down Expand Up @@ -84,7 +85,7 @@ export function injectComponentAndDescendantStyles(
);

if (platform === 'web') {
GluestackStyleSheet.inject(toBeInjected);
GluestackStyleSheet.inject(toBeInjected, inlineStyleMap);
}

return styleCSSIdsArr;
Expand Down
23 changes: 19 additions & 4 deletions packages/react/src/style-sheet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,15 @@ export class StyleInjector {
return toBeInjected;
}

inject(toBeInjected: any = {}) {
inject(toBeInjected: any = {}, inlineStyleMap: any) {
Object.keys(toBeInjected).forEach((type) => {
Object.keys(toBeInjected[type]).forEach((styleTag) => {
this.injectStyles(toBeInjected[type][styleTag], type, styleTag);
this.injectStyles(
toBeInjected[type][styleTag],
type,
styleTag,
inlineStyleMap
);
});
});
}
Expand Down Expand Up @@ -162,9 +167,19 @@ export class StyleInjector {
return this.#globalStyleMap;
}

injectStyles(cssRuleset: any, _wrapperType: any, _styleTagId: any) {
injectStyles(
cssRuleset: any,
_wrapperType: any,
_styleTagId: any,
inlineStyleMap: any
) {
if (cssRuleset) {
inject(`@media screen {${cssRuleset}}`, _wrapperType as any, _styleTagId);
inject(
`@media screen {${cssRuleset}}`,
_wrapperType as any,
_styleTagId,
inlineStyleMap
);
}
}
}
Expand Down
41 changes: 31 additions & 10 deletions packages/react/src/styled.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -906,7 +906,7 @@ export function verboseStyled<P, Variants, ComCon>(
styleIds = BUILD_TIME_PARAMS?.verbosedStyleIds;
}

function injectSx(sx: any, type: any = 'inline') {
function injectSx(sx: any, type: any = 'inline', inlineStyleMap?: any) {
const inlineSxTheme = {
baseStyle: sx,
};
Expand Down Expand Up @@ -965,7 +965,8 @@ export function verboseStyled<P, Variants, ComCon>(
sxHash,
type,
GluestackStyleSheet,
Platform.OS
Platform.OS,
inlineStyleMap
);

return orderedSXResolved;
Expand Down Expand Up @@ -1059,7 +1060,6 @@ export function verboseStyled<P, Variants, ComCon>(
// const resolvedComponentExtendedTheme = EXTENDED_THEME;

theme = deepMerge(theme, resolvedComponentExtendedTheme.theme);

// @ts-ignore
Object.assign(themeDefaultProps, theme?.baseStyle?.props);
if (Object.keys(EXTENDED_THEME?.BUILD_TIME_PARAMS ?? {}).length > 0) {
Expand All @@ -1070,7 +1070,8 @@ export function verboseStyled<P, Variants, ComCon>(
EXTENDED_THEME_BUILD_TIME_PARAMS?.verbosedStyleIds
);
GluestackStyleSheet.inject(
EXTENDED_THEME_BUILD_TIME_PARAMS?.toBeInjected
EXTENDED_THEME_BUILD_TIME_PARAMS?.toBeInjected,
styledContext.inlineStyleMap
);
} else {
// Merge of Extended Config Style ID's with Component Style ID's
Expand All @@ -1084,7 +1085,10 @@ export function verboseStyled<P, Variants, ComCon>(
CONFIG,
componentExtendedConfig
);
GluestackStyleSheet.inject(extendedStylesToBeInjected);
GluestackStyleSheet.inject(
extendedStylesToBeInjected,
styledContext.inlineStyleMap
);
}
}

Expand Down Expand Up @@ -1133,12 +1137,18 @@ export function verboseStyled<P, Variants, ComCon>(
componentExtendedConfig
);
if (Platform.OS === 'web') {
GluestackStyleSheet.inject(toBeInjected);
GluestackStyleSheet.inject(
toBeInjected,
styledContext.inlineStyleMap
);
}
} else {
if (Platform.OS === 'web') {
//@ts-ignore
GluestackStyleSheet.inject(BUILD_TIME_PARAMS.toBeInjected);
GluestackStyleSheet.inject(
BUILD_TIME_PARAMS.toBeInjected,
styledContext.inlineStyleMap
);
}
}

Expand Down Expand Up @@ -1229,7 +1239,10 @@ export function verboseStyled<P, Variants, ComCon>(
);

if (Platform.OS === 'web') {
GluestackStyleSheet.inject(toBeInjected);
GluestackStyleSheet.inject(
toBeInjected,
styledContext.inlineStyleMap
);
}
isInjected = true;
}
Expand Down Expand Up @@ -1432,11 +1445,19 @@ export function verboseStyled<P, Variants, ComCon>(

function injectAndUpdateSXProps(filteredPassingSx: any) {
if (Object.keys(filteredComponentSx).length > 0) {
orderedComponentSXResolved = injectSx(filteredComponentSx, 'inline');
orderedComponentSXResolved = injectSx(
filteredComponentSx,
'inline',
styledContext.inlineStyleMap
);
}

if (Object.keys(filteredPassingSx).length > 0) {
orderedPassingSXResolved = injectSx(filteredPassingSx, 'passing');
orderedPassingSXResolved = injectSx(
filteredPassingSx,
'passing',
styledContext.inlineStyleMap
);
}

const orderedSXResolved = [
Expand Down
5 changes: 4 additions & 1 deletion packages/react/src/utils/css-injector/utils/inject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ type IWrapperType =
| 'boot-descendant'
| 'inline-descendant';

export const WRAPPER_BLOCK_PREFIX = 'gs-injected';

export const hasCss = (_id: any, _text: any) => {};

export const addCss = (_id: any, _text: any) => {};
export const injectCss = (
_css: any,
_wrapperType: IWrapperType,
_styleTagId: string
_styleTagId: string,
_inlineStyleMap?: any
) => {};
export const injectGlobalCss = (
_css: any,
Expand Down
22 changes: 19 additions & 3 deletions packages/react/src/utils/css-injector/utils/inject.web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ const createStyle = (styleTagId: any, css: any) => {
export const injectCss = (
css: any,
wrapperType: IWrapperType,
styleTagId: string
styleTagId: string,
inlineStyleMap?: any
) => {
// let modifiedStylesheet = {} as any;
if (!toBeFlushedStyles[wrapperType]) {
Expand All @@ -138,8 +139,23 @@ export const injectCss = (

if (!style) {
style = createStyle(styleTagId, css);
// wrapperElement.insertBefore(style, wrapperElement.firstChild);
wrapperElement.appendChild(style);
// console.log(inlineStyleMap, 'append child here >>>>>');
if (inlineStyleMap) {
if (!inlineStyleMap?.initialStyleInjected) {
const styleMapId = `${WRAPPER_BLOCK_PREFIX}-${wrapperType}`;
const inlineMapStyles = inlineStyleMap[styleMapId];

if (inlineMapStyles) {
inlineMapStyles.push(style);
} else {
inlineStyleMap[styleMapId] = [style];
}
// console.log('hello here >>>> there');
} else {
// console.log('hello here >>>>');
wrapperElement.appendChild(style);
}
}
}
}
}
Expand Down

0 comments on commit 9f80d54

Please sign in to comment.