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
16 changes: 15 additions & 1 deletion src/Button/ButtonBase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {merge, SxProp} from '../sx'
import {useTheme} from '../ThemeProvider'
import {ButtonProps, StyledButton} from './types'
import {getVariantStyles, getSizeStyles, getButtonStyles} from './styles'
import {useRefObjectAsForwardedRef} from '../hooks/useRefObjectAsForwardedRef'
declare let __DEV__: boolean

const defaultSxProp = {}
const iconWrapStyles = {
Expand All @@ -18,6 +20,9 @@ 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>(null)
useRefObjectAsForwardedRef(forwardedRef, innerRef)

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

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

Comment on lines +34 to +42
Copy link
Collaborator

@mattcosta7 mattcosta7 Dec 8, 2022

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
Collaborator

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={innerRef}>
{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