Skip to content
This repository was archived by the owner on Mar 4, 2020. It is now read-only.

feat(useStyles): avoid creating new instances of mergedStyles if there are no inline overrides #2226

Merged
merged 8 commits into from
Jan 23, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions packages/react-bindings/src/styles/getStyles.ts
Original file line number Diff line number Diff line change
@@ -69,11 +69,20 @@ const getStyles = (options: GetStylesOptions): GetStylesResult => {
: resolvedComponentVariables[displayName]

// Resolve styles using resolved variables, merge results, allow props.styles to override
const mergedStyles: ComponentSlotStylesPrepared = mergeComponentStyles(
theme.componentStyles[displayName],
props.design && withDebugId({ root: props.design }, 'props.design'),
props.styles && withDebugId({ root: props.styles } as ComponentSlotStylesInput, 'props.styles'),
)
let mergedStyles: ComponentSlotStylesPrepared = theme.componentStyles[displayName] || {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As the theme is immutable, the mergedStyles by default will always reference to the same object.

root: () => ({}),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is necessary for the components that don't have any styles in the theme. It is always a new object, but it should be ok as we don't have anything to cache anyway

}

const hasInlineOverrides = !_.isNil(props.design) || !_.isNil(props.styles)

if (hasInlineOverrides) {
mergedStyles = mergeComponentStyles(
mergedStyles,
props.design && withDebugId({ root: props.design }, 'props.design'),
props.styles &&
withDebugId({ root: props.styles } as ComponentSlotStylesInput, 'props.styles'),
)
}

const styleParam: ComponentStyleFunctionParam = {
displayName,