Skip to content

Commit e76b0c6

Browse files
committed
perf: cached compiled templates
1 parent 64fc841 commit e76b0c6

File tree

1 file changed

+27
-13
lines changed

1 file changed

+27
-13
lines changed

packages/react/src/components/Card/CardWrap.js

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,32 @@ const baseStyle = css`
6464
}
6565
`
6666

67-
const CardWrap = styled('a').attrs(props => ({
68-
rel: props.as === 'a' ? 'noopener noreferrer' : undefined,
69-
target: props.as === 'a' ? '_blank' : undefined
70-
}))(
71-
baseStyle,
72-
({ loading, contrast }) => !loading && !contrast && hoverStyle,
73-
({ cardSize }) => isLarge(cardSize) && largeStyle,
74-
({ reverse }) => reverse && reverseStyle,
75-
({ backgroundColor, color, contrast }) =>
76-
contrast && color && backgroundColor && contrastStyle,
77-
({ backgroundColor, color, contrast }) =>
78-
contrast && (!color || !backgroundColor) && hoverStyle
79-
)
67+
const createEl = ({ as }) =>
68+
styled(as)(
69+
baseStyle,
70+
({ loading, contrast }) => !loading && !contrast && hoverStyle,
71+
({ cardSize }) => isLarge(cardSize) && largeStyle,
72+
({ reverse }) => reverse && reverseStyle,
73+
({ backgroundColor, color, contrast }) =>
74+
contrast && color && backgroundColor && contrastStyle,
75+
({ backgroundColor, color, contrast }) =>
76+
contrast && (!color || !backgroundColor) && hoverStyle
77+
)
78+
79+
const CACHE = {}
80+
81+
const CardWrap = ({ rel, href, target, ...props }) => {
82+
const key = JSON.stringify(props)
83+
return createElement(
84+
CACHE[key] || (CACHE[key] = createEl(props)),
85+
props.as === 'a' ? { ...props, href, rel, target } : props
86+
)
87+
}
88+
89+
CardWrap.defaultProps = {
90+
as: 'a',
91+
rel: 'noopener noreferrer',
92+
target: '_blank'
93+
}
8094

8195
export default CardWrap

0 commit comments

Comments
 (0)