-
Notifications
You must be signed in to change notification settings - Fork 667
Refine 'as' prop type in button component and fix name of aria type #2659
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
Changes from 4 commits
c791a50
57a6fc6
91e89e9
e8036cb
d28127f
d4ab06e
cc8e2d0
a0da738
a2eec8f
e4c1103
17e6150
7f32cfc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| 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' | ||
|
|
@@ -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>() | ||
| const {theme} = useTheme() | ||
| const baseStyles = useMemo(() => { | ||
| return merge.all([getButtonStyles(theme), getSizeStyles(size, variant, false), getVariantStyles(variant, theme)]) | ||
|
|
@@ -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.
Outdated
|
||
| } | ||
| }, [innerRef]) | ||
|
|
||
|
Comment on lines
+34
to
+42
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 /> | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 forwardedRefThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AMAZING thank you!