Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
5 changes: 5 additions & 0 deletions .changeset/giant-horses-knock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/react': patch
---

Add a console warning if the Button and IconButton as property is used incorrectly
31 changes: 23 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
"fzy.js": "0.4.1",
"history": "^5.0.0",
"react-intersection-observer": "9.4.1",
"react-merge-refs": "2.0.1",
"styled-system": "^5.1.5"
},
"devDependencies": {
Expand Down
11 changes: 10 additions & 1 deletion src/Button/ButtonBase.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, {ComponentPropsWithRef, forwardRef, useMemo} from 'react'
import {mergeRefs} from 'react-merge-refs'
import {ForwardRefComponent as PolymorphicForwardRefComponent} from '../utils/polymorphic'
import Box from '../Box'
import {merge, SxProp} from '../sx'
Expand All @@ -18,6 +19,7 @@ const trailingIconStyles = {
const ButtonBase = forwardRef<HTMLElement, ButtonProps>(
({children, as: Component = 'button', sx: sxProp = defaultSxProp, ...props}, forwardedRef): JSX.Element => {
const {leadingIcon: LeadingIcon, trailingIcon: TrailingIcon, variant = 'default', size = 'medium', ...rest} = props
const innerRef = React.useRef<HTMLElement>()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
const innerRef = React.useRef<HTMLElement>()
const innerRef = React.useRef<HTMLElement>()
useRefObjectAsForwardedRef(forwardedRef, innerRef)

Here was that convention that I was talking about! This should be available in src/hooks/useRefObjectAsForwardedRef.ts. This seems to show up in a couple components that both have a local and forwardedRef

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

AMAZING thank you!

const {theme} = useTheme()
const baseStyles = useMemo(() => {
return merge.all([getButtonStyles(theme), getSizeStyles(size, variant, false), getVariantStyles(variant, theme)])
Expand All @@ -26,8 +28,15 @@ const ButtonBase = forwardRef<HTMLElement, ButtonProps>(
return merge(baseStyles, sxProp as SxProp)
}, [baseStyles, sxProp])

React.useEffect(() => {
if (!(innerRef.current instanceof HTMLButtonElement) && !(innerRef.current instanceof HTMLAnchorElement)) {
// eslint-disable-next-line no-console
console.warn('This component should be an instanceof a semantic button or anchor')
Comment thread
kendallgassner marked this conversation as resolved.
Outdated
}
}, [innerRef])

Comment on lines +34 to +42

@mattcosta7 mattcosta7 Dec 8, 2022

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

realize this might be a little late, but should we wrap the entire effect in

if (DEV) {}

I realize that'll trip the linter, but since it's not actually conditional but instead a build time flag, I think that's worth the lint disable, which would strip a few extra bytes from production builds, and cause no actual runtime issues?

Otherwise, we'll end up still doing instanceof checks here, for no reason on every mount?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I opened a PR for this suggestion - #2666

return (
<StyledButton as={Component} sx={sxStyles} {...rest} ref={forwardedRef}>
<StyledButton as={Component} sx={sxStyles} {...rest} ref={mergeRefs([innerRef, forwardedRef])}>
{LeadingIcon && (
<Box as="span" data-component="leadingIcon" sx={iconWrapStyles}>
<LeadingIcon />
Expand Down
4 changes: 3 additions & 1 deletion src/Button/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ export type Size = 'small' | 'medium' | 'large'
*/
type StyledButtonProps = Omit<ComponentPropsWithRef<typeof StyledButton>, 'as'>

type ButtonA11yProps = {'aria-label': string; 'aria-labelby'?: never} | {'aria-label'?: never; 'aria-labelby': string}
type ButtonA11yProps =
| {'aria-label': string; 'aria-labelledby'?: never}
| {'aria-label'?: never; 'aria-labelledby': string}

export type ButtonBaseProps = {
/**
Expand Down