Skip to content

Commit

Permalink
perf: cached compiled templates
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Dec 24, 2018
1 parent 64fc841 commit e76b0c6
Showing 1 changed file with 27 additions and 13 deletions.
40 changes: 27 additions & 13 deletions packages/react/src/components/Card/CardWrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,32 @@ const baseStyle = css`
}
`

const CardWrap = styled('a').attrs(props => ({
rel: props.as === 'a' ? 'noopener noreferrer' : undefined,
target: props.as === 'a' ? '_blank' : undefined
}))(
baseStyle,
({ loading, contrast }) => !loading && !contrast && hoverStyle,
({ cardSize }) => isLarge(cardSize) && largeStyle,
({ reverse }) => reverse && reverseStyle,
({ backgroundColor, color, contrast }) =>
contrast && color && backgroundColor && contrastStyle,
({ backgroundColor, color, contrast }) =>
contrast && (!color || !backgroundColor) && hoverStyle
)
const createEl = ({ as }) =>
styled(as)(
baseStyle,
({ loading, contrast }) => !loading && !contrast && hoverStyle,
({ cardSize }) => isLarge(cardSize) && largeStyle,
({ reverse }) => reverse && reverseStyle,
({ backgroundColor, color, contrast }) =>
contrast && color && backgroundColor && contrastStyle,
({ backgroundColor, color, contrast }) =>
contrast && (!color || !backgroundColor) && hoverStyle
)

const CACHE = {}

const CardWrap = ({ rel, href, target, ...props }) => {
const key = JSON.stringify(props)
return createElement(
CACHE[key] || (CACHE[key] = createEl(props)),
props.as === 'a' ? { ...props, href, rel, target } : props
)
}

CardWrap.defaultProps = {
as: 'a',
rel: 'noopener noreferrer',
target: '_blank'
}

export default CardWrap

0 comments on commit e76b0c6

Please sign in to comment.