Skip to content

Commit 693ce68

Browse files
authored
move defaultProps to function defaults (#2759)
I'm updating all non-deprecated components that use `defaultProps` to stop using it and using function defaults instead. As described in the issue below, `defaultProps` is going to be deprecated so it'd be nice to be clear of it before the new React version is released. Closes #2758
1 parent 9e8d0e9 commit 693ce68

39 files changed

+148
-271
lines changed

.changeset/witty-camels-relate.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 defaultProps to be JS function defaults

src/AnchoredOverlay/AnchoredOverlay.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ export const AnchoredOverlay: React.FC<React.PropsWithChildren<AnchoredOverlayPr
9999
overlayProps,
100100
focusTrapSettings,
101101
focusZoneSettings,
102-
side,
103-
align,
102+
side = 'outside-bottom',
103+
align = 'start',
104104
}) => {
105105
const anchorRef = useProvidedRefOrCreate(externalAnchorRef)
106106
const [overlayRef, updateOverlayRef] = useRenderForcingRef<HTMLDivElement>()
@@ -194,8 +194,3 @@ export const AnchoredOverlay: React.FC<React.PropsWithChildren<AnchoredOverlayPr
194194
}
195195

196196
AnchoredOverlay.displayName = 'AnchoredOverlay'
197-
198-
AnchoredOverlay.defaultProps = {
199-
side: 'outside-bottom',
200-
align: 'start',
201-
}

src/Autocomplete/AutocompleteMenu.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,10 @@ function AutocompleteMenu<T extends AutocompleteItemProps>(props: AutocompleteMe
133133
items,
134134
selectedItemIds,
135135
sortOnCloseFn,
136-
emptyStateText,
136+
emptyStateText = 'No selectable options',
137137
addNewItem,
138138
loading,
139-
selectionVariant,
139+
selectionVariant = 'single',
140140
filterFn,
141141
'aria-labelledby': ariaLabelledBy,
142142
onOpenChange,
@@ -326,11 +326,6 @@ function AutocompleteMenu<T extends AutocompleteItemProps>(props: AutocompleteMe
326326
)
327327
}
328328

329-
AutocompleteMenu.defaultProps = {
330-
emptyStateText: 'No selectable options',
331-
selectionVariant: 'single',
332-
}
333-
334329
AutocompleteMenu.displayName = 'AutocompleteMenu'
335330

336331
export type AutocompleteMenuProps = ComponentProps<typeof AutocompleteMenu>

src/Avatar.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import React from 'react'
12
import styled from 'styled-components'
23
import {get} from './constants'
34
import sx, {SxProp} from './sx'
@@ -22,7 +23,7 @@ function getBorderRadius({size, square}: StyledAvatarProps) {
2223
}
2324
}
2425

25-
const Avatar = styled.img.attrs<StyledAvatarProps>(props => ({
26+
const StyledAvatar = styled.img.attrs<StyledAvatarProps>(props => ({
2627
height: props.size,
2728
width: props.size,
2829
}))<StyledAvatarProps>`
@@ -34,11 +35,8 @@ const Avatar = styled.img.attrs<StyledAvatarProps>(props => ({
3435
box-shadow: 0 0 0 1px ${get('colors.avatar.border')};
3536
${sx}
3637
`
37-
Avatar.defaultProps = {
38-
size: 20,
39-
alt: '',
40-
square: false,
41-
}
38+
39+
const Avatar = ({size = 20, alt = '', ...rest}: StyledAvatarProps) => <StyledAvatar alt={alt} size={size} {...rest} />
4240

4341
export type AvatarProps = ComponentProps<typeof Avatar>
4442
export default Avatar

src/BaseStyles.tsx

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,16 @@ const Base = styled.div<SystemTypographyProps & SystemCommonProps>`
3636
export type BaseStylesProps = ComponentProps<typeof Base>
3737

3838
function BaseStyles(props: BaseStylesProps) {
39-
const {children, ...rest} = props
39+
const {children, color = 'fg.default', fontFamily = 'normal', lineHeight = 'default', ...rest} = props
40+
4041
const {colorScheme} = useTheme()
4142

4243
return (
43-
<Base {...rest} data-portal-root>
44+
<Base {...rest} color={color} fontFamily={fontFamily} lineHeight={lineHeight} data-portal-root>
4445
<GlobalStyle colorScheme={colorScheme?.includes('dark') ? 'dark' : 'light'} />
4546
{children}
4647
</Base>
4748
)
4849
}
4950

50-
BaseStyles.defaultProps = {
51-
color: 'fg.default',
52-
fontFamily: 'normal',
53-
lineHeight: 'default',
54-
}
55-
5651
export default BaseStyles

src/Caret.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,13 @@ export type CaretProps = {
6262

6363
function Caret(props: CaretProps) {
6464
const theme = React.useContext(ThemeContext)
65-
const propsWithTheme = {...props, theme: props.theme ?? theme}
65+
const propsWithTheme = {
66+
...props,
67+
bg: props.bg || 'canvas.default',
68+
borderColor: props.borderColor || 'border.default',
69+
borderWidth: props.borderWidth || 1,
70+
theme: props.theme ?? theme,
71+
}
6672
const {bg} = getBg(propsWithTheme)
6773
const {borderColor} = getBorderColor(propsWithTheme)
6874
const {borderWidth} = getBorderWidth(propsWithTheme)
@@ -124,10 +130,4 @@ Caret.locations = [
124130
'left-bottom',
125131
]
126132

127-
Caret.defaultProps = {
128-
bg: 'canvas.default',
129-
borderColor: 'border.default',
130-
borderWidth: 1,
131-
}
132-
133133
export default Caret

src/CircleBadge.tsx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const sizeStyles = ({size, variant = 'medium'}: StyledCircleBadgeProps) => {
2626
}
2727

2828
const CircleBadge = styled.div<StyledCircleBadgeProps>`
29-
display: ${props => (props.inline ? 'inline-flex' : 'flex')};
29+
display: ${({inline = false}) => (inline ? 'inline-flex' : 'flex')};
3030
align-items: center;
3131
justify-content: center;
3232
background-color: ${get('colors.canvas.default')};
@@ -42,10 +42,6 @@ const CircleBadgeIcon = styled(StyledOcticon)`
4242
max-height: 55%;
4343
`
4444

45-
CircleBadge.defaultProps = {
46-
inline: false,
47-
}
48-
4945
CircleBadgeIcon.displayName = 'CircleBadge.Icon'
5046

5147
export type CircleBadgeProps = ComponentProps<typeof CircleBadge>

src/CircleOcticon.tsx

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ export type CircleOcticonProps = {
99
} & BoxProps
1010

1111
function CircleOcticon(props: CircleOcticonProps) {
12-
const {size, as} = props
13-
const {icon: IconComponent, bg, ...rest} = props
12+
const {size = 32, as, icon: IconComponent, bg, ...rest} = props
1413
return (
1514
<Box
1615
as={as}
@@ -22,16 +21,11 @@ function CircleOcticon(props: CircleOcticonProps) {
2221
borderStyle="solid"
2322
borderColor="border.default"
2423
>
25-
<Box display="flex" {...rest} alignItems="center" justifyContent="center">
24+
<Box display="flex" as={as} size={size} {...rest} alignItems="center" justifyContent="center">
2625
<IconComponent size={size} />
2726
</Box>
2827
</Box>
2928
)
3029
}
3130

32-
CircleOcticon.defaultProps = {
33-
...Box.defaultProps,
34-
size: 32,
35-
}
36-
3731
export default CircleOcticon

src/Dialog.tsx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ const DialogHeaderBase = styled(Box)<SxProp>`
5353
`
5454
export type DialogHeaderProps = ComponentProps<typeof DialogHeaderBase>
5555

56-
function DialogHeader({theme, children, backgroundColor = 'gray.1', ...rest}: DialogHeaderProps) {
56+
function DialogHeader({theme, children, backgroundColor = 'canvas.subtle', ...rest}: DialogHeaderProps) {
5757
if (React.Children.toArray(children).every(ch => typeof ch === 'string')) {
5858
children = (
5959
<Text theme={theme} color="fg.default" fontSize={1} fontWeight="bold" fontFamily="sans-serif">
@@ -131,10 +131,6 @@ const Dialog = forwardRef<HTMLDivElement, InternalDialogProps>(
131131
},
132132
)
133133

134-
DialogHeader.defaultProps = {
135-
backgroundColor: 'canvas.subtle',
136-
}
137-
138134
DialogHeader.propTypes = {
139135
...Box.propTypes,
140136
}

src/Flash.tsx

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import React from 'react'
12
import styled from 'styled-components'
23
import {variant} from 'styled-system'
34
import {get} from './constants'
@@ -41,12 +42,12 @@ const variants = variant({
4142
},
4243
})
4344

44-
const Flash = styled.div<
45-
{
46-
variant?: 'default' | 'warning' | 'success' | 'danger'
47-
full?: boolean
48-
} & SxProp
49-
>`
45+
type StyledFlashProps = {
46+
variant?: 'default' | 'warning' | 'success' | 'danger'
47+
full?: boolean
48+
} & SxProp
49+
50+
const StyledFlash = styled.div<StyledFlashProps>`
5051
position: relative;
5152
color: ${get('colors.fg.default')};
5253
padding: ${get('space.3')};
@@ -67,9 +68,7 @@ const Flash = styled.div<
6768
${sx};
6869
`
6970

70-
Flash.defaultProps = {
71-
variant: 'default',
72-
}
71+
export type FlashProps = ComponentProps<typeof StyledFlash>
72+
const Flash = ({variant = 'default', ...rest}: FlashProps) => <StyledFlash variant={variant} {...rest} />
7373

74-
export type FlashProps = ComponentProps<typeof Flash>
7574
export default Flash

0 commit comments

Comments
 (0)