Skip to content

Commit

Permalink
WIP: Memoize inline styles
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcusNotheis committed Aug 2, 2019
1 parent 2d5d525 commit 551bd56
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions packages/main/src/components/FlexBox/index.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { StyleClassHelper } from '@ui5/webcomponents-react-base';
import React, { CSSProperties, FC, forwardRef, ReactNode, ReactNodeArray, Ref } from 'react';
import React, { CSSProperties, FC, forwardRef, ReactNode, ReactNodeArray, Ref, useMemo } from 'react';
import { createUseStyles } from 'react-jss';
import { CommonProps } from '../../interfaces/CommonProps';
import { styles } from './Flexbox.jss';
import { FlexBoxJustifyContent } from '../../lib/FlexBoxJustifyContent';
import { FlexBoxAlignItems } from '../../lib/FlexBoxAlignItems';
import { FlexBoxDirection } from '../../lib/FlexBoxDirection';
import { FlexBoxJustifyContent } from '../../lib/FlexBoxJustifyContent';
import { FlexBoxWrap } from '../../lib/FlexBoxWrap';
import { styles } from './Flexbox.jss';

const useStyles = createUseStyles(styles, { name: 'FlexBox' });

Expand Down Expand Up @@ -53,24 +53,26 @@ const FlexBox: FC<FlexBoxPropTypes> = forwardRef((props: FlexBoxPropTypes, ref:
flexBoxClasses.put(classes.flexBoxDisplayInline);
}

const inlineStyle = {} as CSSProperties;
if (height) {
inlineStyle.height = height;
}
if (width) {
inlineStyle.width = width;
}

if (className) {
flexBoxClasses.put(className);
}

if (style) {
Object.assign(inlineStyle, style);
}
const memoizedStyles = useMemo(() => {
const innerStyles: CSSProperties = {};
if (height) {
innerStyles.height = height;
}
if (width) {
innerStyles.width = width;
}
if (style) {
Object.assign(innerStyles, style);
}
return innerStyles;
}, [height, width, style]);

return (
<div ref={ref} className={flexBoxClasses.valueOf()} style={inlineStyle} title={tooltip} slot={slot}>
<div ref={ref} className={flexBoxClasses.valueOf()} style={memoizedStyles} title={tooltip} slot={slot}>
{children}
</div>
);
Expand Down

0 comments on commit 551bd56

Please sign in to comment.