Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refine 'as' prop type in button component and fix name of aria type #2659

Merged
merged 12 commits into from
Dec 7, 2022
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
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
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')
kendallgassner marked this conversation as resolved.
Show resolved Hide resolved
}
}, [innerRef])

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