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

CounterLabel should forward ref and dom props #3291

Merged
merged 3 commits into from
May 18, 2023
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/twenty-tips-attend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/react': patch
---

Counter label forwards refs and dom props
78 changes: 40 additions & 38 deletions src/CounterLabel/CounterLabel.tsx
Original file line number Diff line number Diff line change
@@ -1,47 +1,49 @@
import React from 'react'
import React, {HTMLAttributes, forwardRef} from 'react'
import Box from '../Box'
import {BetterSystemStyleObject, SxProp, merge} from '../sx'
import VisuallyHidden from '../_VisuallyHidden'
import {defaultSxProp} from '../utils/defaultSxProp'

export type CounterLabelProps = {
scheme?: 'primary' | 'secondary'
} & SxProp
export type CounterLabelProps = React.PropsWithChildren<
HTMLAttributes<HTMLSpanElement> & {
scheme?: 'primary' | 'secondary'
} & SxProp
>

const CounterLabel: React.FC<React.PropsWithChildren<CounterLabelProps>> = ({
scheme = 'secondary',
sx = {},
children,
...props
}) => {
return (
<>
<Box
as="span"
aria-hidden="true"
sx={merge<BetterSystemStyleObject>(
{
display: 'inline-block',
padding: '2px 5px',
fontSize: 0,
fontWeight: 'bold',
lineHeight: 'condensedUltra',
borderRadius: '20px',
backgroundColor: scheme === 'primary' ? 'neutral.emphasis' : 'neutral.muted',
color: scheme === 'primary' ? 'fg.onEmphasis' : 'fg.default',
'&:empty': {
display: 'none',
const CounterLabel = forwardRef<HTMLSpanElement, CounterLabelProps>(
({scheme = 'secondary', sx = defaultSxProp, children, ...props}, forwardedRef) => {
return (
<>
<Box
aria-hidden="true"
sx={merge<BetterSystemStyleObject>(
{
display: 'inline-block',
padding: '2px 5px',
fontSize: 0,
fontWeight: 'bold',
lineHeight: 'condensedUltra',
borderRadius: '20px',
backgroundColor: scheme === 'primary' ? 'neutral.emphasis' : 'neutral.muted',
color: scheme === 'primary' ? 'fg.onEmphasis' : 'fg.default',
'&:empty': {
display: 'none',
},
},
},
sx,
)}
{...props}
>
{children}
</Box>
<VisuallyHidden>&nbsp;({children})</VisuallyHidden>
</>
)
}
sx,
)}
{...props}
as="span"
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

we don't pass as through (in ts land) so avoiding it getting passed through by non-ts code

// @ts-expect-error Box is expecting a divelement, but this component forces a span element
ref={forwardedRef}
>
{children}
</Box>
<VisuallyHidden>&nbsp;({children})</VisuallyHidden>
</>
)
},
)

CounterLabel.displayName = 'CounterLabel'

Expand Down
14 changes: 13 additions & 1 deletion src/CounterLabel/CounterLabel.types.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react'
import React, {useRef} from 'react'
import CounterLabel from '../CounterLabel'

export function shouldAcceptCallWithNoProps() {
Expand All @@ -9,3 +9,15 @@ export function shouldNotAcceptSystemProps() {
// @ts-expect-error system props should not be accepted
return <CounterLabel backgroundColor="whitesmoke" />
}

export function showAcceptARef() {
function Component() {
const ref = useRef<HTMLSpanElement>(null)
return <CounterLabel ref={ref} />
}
return <Component />
}

export function shouldPassThroughSpanProps() {
return <CounterLabel data-testid="some value" aria-label="some label" />
}