Skip to content

Commit e52415e

Browse files
authored
fix(Button): memoize call to merge.all (#2252)
* fix(Button): memoize call to merge.all * refactor(button): remove sxProp from useMemo call * chore: add changeset
1 parent 8ea1f75 commit e52415e

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

.changeset/shy-flowers-help.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@primer/react': patch
3+
---
4+
5+
Update ButtonBase.tsx to memoize calls to styles to improve performance

src/Button/ButtonBase.tsx

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, {ComponentPropsWithRef, forwardRef} from 'react'
1+
import React, {ComponentPropsWithRef, forwardRef, useMemo} from 'react'
22
import {ForwardRefComponent as PolymorphicForwardRefComponent} from '../utils/polymorphic'
33
import Box from '../Box'
44
import {merge, SxProp} from '../sx'
@@ -13,14 +13,11 @@ const ButtonBase = forwardRef<HTMLElement, ButtonProps>(
1313
const iconWrapStyles = {
1414
display: 'inline-block'
1515
}
16-
const sxStyles = merge.all([
17-
getButtonStyles(theme),
18-
getSizeStyles(size, variant, false),
19-
getVariantStyles(variant, theme),
20-
sxProp as SxProp
21-
])
16+
const sxStyles = useMemo(() => {
17+
return merge.all([getButtonStyles(theme), getSizeStyles(size, variant, false), getVariantStyles(variant, theme)])
18+
}, [theme, size, variant])
2219
return (
23-
<StyledButton as={Component} sx={sxStyles} {...props} ref={forwardedRef}>
20+
<StyledButton as={Component} sx={merge(sxStyles, sxProp as SxProp)} {...props} ref={forwardedRef}>
2421
{LeadingIcon && (
2522
<Box as="span" data-component="leadingIcon" sx={iconWrapStyles}>
2623
<LeadingIcon />

0 commit comments

Comments
 (0)