Skip to content

Button should respect polymorphic as prop #2166

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

Merged
merged 11 commits into from
Jul 27, 2022
Merged
Show file tree
Hide file tree
Changes from all 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/odd-donuts-raise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/react': patch
---

button should be polymorphic
8 changes: 8 additions & 0 deletions docs/content/Button.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,15 @@ Native `<button>` HTML attributes are forwarded to the underlying React `button`
type="Component"
description="provide an octicon. It will be placed after the button text"
/>
<PropsTableAsRow defaultElementType="button" />
<PropsTableSxRow />
<PropsTablePassthroughPropsRow
elementName="button"
passthroughPropsLink={
<Link href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attributes">MDN</Link>
}
isPolymorphic
/>
</PropsTable>

### Button.Counter
Expand Down
100 changes: 50 additions & 50 deletions src/Button/Button.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {EyeClosedIcon, EyeIcon, SearchIcon, TriangleDownIcon, XIcon} from '@primer/octicons-react'
import {EyeClosedIcon, EyeIcon, SearchIcon, TriangleDownIcon, XIcon, TriangleRightIcon} from '@primer/octicons-react'
import {Meta} from '@storybook/react'
import React, {useState} from 'react'
import React, {useState, forwardRef} from 'react'
import {Button, ButtonProps, IconButton} from '.'
import {BaseStyles, ThemeProvider} from '..'
import Box from '../Box'
Expand Down Expand Up @@ -199,52 +199,52 @@ export const DisabledButton = ({...args}: ButtonProps) => {
)
}

// type ReactRouterLikeLinkProps = {to: string; children: React.ReactNode}
// const ReactRouterLikeLink = forwardRef<HTMLAnchorElement, ReactRouterLikeLinkProps>(
// ({to, ...props}: {to: string; children: React.ReactNode}, ref) => {
// // eslint-disable-next-line jsx-a11y/anchor-has-content
// return <a ref={ref} href={to} {...props} />
// }
// )
type ReactRouterLikeLinkProps = {to: string; children: React.ReactNode}
const ReactRouterLikeLink = forwardRef<HTMLAnchorElement, ReactRouterLikeLinkProps>(
({to, ...props}: {to: string; children: React.ReactNode}, ref) => {
// eslint-disable-next-line jsx-a11y/anchor-has-content
return <a ref={ref} href={to} {...props} />
}
)

// export const linkButton = ({...args}: ButtonProps) => {
// return (
// <>
// <Box mb={2} display="flex">
// <LinkButton href="https://primer.style/" {...args}>
// Link to Primer
// </LinkButton>
// </Box>
// <Box mb={2} display="flex">
// <LinkButton href="https://primer.style/" variant="danger" {...args}>
// Link to Primer
// </LinkButton>
// </Box>
// <Box mb={2} display="flex">
// <LinkButton href="https://primer.style/" variant="primary" {...args}>
// Link to Primer
// </LinkButton>
// </Box>
// <Box mb={2} display="flex">
// <LinkButton href="https://primer.style/" variant="outline" {...args}>
// Link to Primer
// </LinkButton>
// </Box>
// <Box mb={2} display="flex">
// <LinkButton href="https://primer.style/" variant="invisible" {...args}>
// Link to Primer
// </LinkButton>
// </Box>
// <Box mb={2} display="flex">
// <LinkButton href="https://primer.style/" variant="primary" trailingIcon={TriangleRightIcon} {...args}>
// Link to Primer
// </LinkButton>
// </Box>
// <Box mb={2} display="flex">
// <LinkButton to="/dummy" as={ReactRouterLikeLink} variant="primary" trailingIcon={TriangleRightIcon} {...args}>
// Link to Primer
// </LinkButton>
// </Box>
// </>
// )
// }
export const linkButton = ({...args}: ButtonProps) => {
return (
<>
<Box mb={2} display="flex">
<Button as="a" href="https://primer.style/" {...args}>
Link to Primer
</Button>
</Box>
<Box mb={2} display="flex">
<Button as="a" href="https://primer.style/" variant="danger" {...args}>
Link to Primer
</Button>
</Box>
<Box mb={2} display="flex">
<Button as="a" href="https://primer.style/" variant="primary" {...args}>
Link to Primer
</Button>
</Box>
<Box mb={2} display="flex">
<Button as="a" href="https://primer.style/" variant="outline" {...args}>
Link to Primer
</Button>
</Box>
<Box mb={2} display="flex">
<Button as="a" href="https://primer.style/" variant="invisible" {...args}>
Link to Primer
</Button>
</Box>
<Box mb={2} display="flex">
<Button as="a" href="https://primer.style/" variant="primary" trailingIcon={TriangleRightIcon} {...args}>
Link to Primer
</Button>
</Box>
<Box mb={2} display="flex">
<Button to="/dummy" as={ReactRouterLikeLink} variant="primary" trailingIcon={TriangleRightIcon} {...args}>
Link to Primer
</Button>
</Box>
</>
)
}
2 changes: 1 addition & 1 deletion src/Button/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {ButtonBase} from './ButtonBase'
const ButtonComponent = forwardRef<HTMLButtonElement, ButtonProps>(
({children, ...props}, forwardedRef): JSX.Element => {
return (
<ButtonBase ref={forwardedRef} {...props} as="button">
<ButtonBase ref={forwardedRef} as="button" {...props}>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

allow as to be overwritten by passed in props

{children}
</ButtonBase>
)
Expand Down
5 changes: 4 additions & 1 deletion src/Button/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ export type VariantType = 'default' | 'primary' | 'invisible' | 'danger' | 'outl

export type Size = 'small' | 'medium' | 'large'

type StyledButtonProps = ComponentPropsWithRef<typeof StyledButton>
/**
* Remove styled-components polymorphic as prop, which conflicts with radix's
*/
type StyledButtonProps = Omit<ComponentPropsWithRef<typeof StyledButton>, 'as'>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

the crux of the fix here seems like the as prop applied by styled-components differs from the types of that applied using the Radix polymorphic helper types an an incompatible way. By omitting the styled component version of this type from the types that get passed around, we can rely on the radix version and avoid weird issues when trying to apply the as prop to the button base to the left of the spread restProps


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

Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/Button.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {SearchIcon} from '@primer/octicons-react'
expect.extend(toHaveNoViolations)

describe('Button', () => {
behavesAsComponent({Component: Button, options: {skipAs: true}})
behavesAsComponent({Component: Button})

it('renders a <button>', () => {
const container = render(<Button>Default</Button>)
Expand Down