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

ProgressBar: Adjust ProgressBar.Item for accessibility #4878

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 14 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/lucky-horses-kiss.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/react': minor
---

Move `aria-*` attributes to `ProgressBar.Item` and marks `ProgressBar.Item` as `role="progressbar".
80 changes: 58 additions & 22 deletions packages/react/src/ProgressBar/ProgressBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {width} from 'styled-system'
import {get} from '../constants'
import type {SxProp} from '../sx'
import sx from '../sx'
import {warning} from '../utils/warning'

type ProgressProp = {
progress?: string | number
Expand All @@ -17,7 +16,7 @@ const shimmer = keyframes`
to { mask-position: 0%; }
`

export const Item = styled.span<ProgressProp & SxProp>`
const ProgressItem = styled.span<ProgressProp & SxProp>`
width: ${props => (props.progress ? `${props.progress}%` : 0)};
background-color: ${props => get(`colors.${props.bg || 'success.emphasis'}`)};

Expand All @@ -34,8 +33,6 @@ export const Item = styled.span<ProgressProp & SxProp>`
${sx};
`

Item.displayName = 'ProgressBar.Item'

const sizeMap = {
small: '5px',
large: '10px',
Expand All @@ -60,37 +57,76 @@ const ProgressContainer = styled.span<StyledProgressContainerProps>`
${sx};
`

export type ProgressBarItems = React.HTMLAttributes<HTMLSpanElement> & {'aria-label'?: string} & ProgressProp & SxProp

export const Item = forwardRef<HTMLSpanElement, ProgressBarItems>(
(
{progress, 'aria-label': ariaLabel, 'aria-valuenow': ariaValueNow, 'aria-valuetext': ariaValueText, ...rest},
forwardRef,
) => {
const progressAsNumber = typeof progress === 'string' ? parseInt(progress, 10) : progress

const ariaAttributes = {
'aria-valuenow':
ariaValueNow || (progressAsNumber !== undefined && progressAsNumber >= 0 ? Math.round(progressAsNumber) : 0),
TylerJDev marked this conversation as resolved.
Show resolved Hide resolved
'aria-valuemin': 0,
'aria-valuemax': 100,
'aria-valuetext': ariaValueText,
}

return (
<ProgressItem
role="progressbar"
aria-label={ariaLabel}
ref={forwardRef}
progress={progress}
{...ariaAttributes}
{...rest}
TylerJDev marked this conversation as resolved.
Show resolved Hide resolved
/>
)
},
)

Item.displayName = 'ProgressBar.Item'

export type ProgressBarProps = React.HTMLAttributes<HTMLSpanElement> & {bg?: string} & StyledProgressContainerProps &
ProgressProp

export const ProgressBar = forwardRef<HTMLSpanElement, ProgressBarProps>(
(
{animated, progress, bg = 'success.emphasis', barSize = 'default', children, ...rest}: ProgressBarProps,
{
animated,
progress,
bg = 'success.emphasis',
barSize = 'default',
children,
'aria-label': ariaLabel,
'aria-valuenow': ariaValueNow,
'aria-valuetext': ariaValueText,
...rest
}: ProgressBarProps,
forwardRef,
) => {
if (children && progress) {
throw new Error('You should pass `progress` or children, not both.')
}

warning(
children &&
typeof (rest as React.AriaAttributes)['aria-valuenow'] === 'undefined' &&
typeof (rest as React.AriaAttributes)['aria-valuetext'] === 'undefined',
'Expected `aria-valuenow` or `aria-valuetext` to be provided to <ProgressBar>. Provide one of these values so screen reader users can determine the current progress. This warning will become an error in the next major release.',
)

const progressAsNumber = typeof progress === 'string' ? parseInt(progress, 10) : progress

const ariaAttributes = {
'aria-valuenow': progressAsNumber ? Math.round(progressAsNumber) : undefined,
'aria-valuemin': 0,
'aria-valuemax': 100,
'aria-valuetext': progressAsNumber ? `${Math.round(progressAsNumber)}%` : undefined,
}
const validChildren = React.Children.toArray(children).length
Copy link
Member

Choose a reason for hiding this comment

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

Would https://react.dev/reference/react/Children#children-count work here as well for getting the length?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried this but ran into an issue where non-components would be included in the count:

<ProgressBar barSize="small" aria-label="Upload test.png">
  {false}
  {false}
  {false}
</ProgressBar>

This resulted in React.Children.count(children) to equal 3 😕 This isn't really a big deal, but I noticed this via a conditional in dotcom that was causing an error, and it was because the children were booleans instead of actual components 😅


return (
<ProgressContainer ref={forwardRef} role="progressbar" barSize={barSize} {...ariaAttributes} {...rest}>
{children ?? <Item data-animated={animated} progress={progress} bg={bg} />}
<ProgressContainer ref={forwardRef} barSize={barSize} {...rest}>
{validChildren ? (
children
Copy link
Member

Choose a reason for hiding this comment

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

For this check, would it be enough to check for the presence of children and toggle or is the count important / used somewhere else?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right now, you can use the ProgressBar alone, or with children. If you use it alone, you can apply all the props directly to <ProgressBar>, but with children, you need to apply them to the individual items.

I think I understand the ask, are you asking if we can switch up the current conditional? 🤔

) : (
<Item
data-animated={animated}
progress={progress}
aria-label={ariaLabel}
aria-valuenow={ariaValueNow}
aria-valuetext={ariaValueText}
bg={bg}
/>
)}
</ProgressContainer>
)
},
Expand Down
48 changes: 47 additions & 1 deletion packages/react/src/__tests__/ProgressBar.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import axe from 'axe-core'
import {FeatureFlags} from '../FeatureFlags'

describe('ProgressBar', () => {
behavesAsComponent({Component: ProgressBar})
behavesAsComponent({Component: ProgressBar, toRender: () => <ProgressBar aria-valuenow={10} progress={0} />})

checkExports('ProgressBar', {
default: undefined,
Expand Down Expand Up @@ -72,4 +72,50 @@ describe('ProgressBar', () => {
it('respects the "progress" prop', () => {
expect(render(<ProgressBar progress={80} aria-label="Upload test.png" />)).toMatchSnapshot()
})

it('passed the `aria-label` down to the progress bar', () => {
const {getByRole, getByLabelText} = HTMLRender(<ProgressBar progress={80} aria-label="Upload test.png" />)
expect(getByRole('progressbar')).toHaveAttribute('aria-label', 'Upload test.png')
expect(getByLabelText('Upload test.png')).toBeInTheDocument()
})

it('passed the `aria-valuenow` down to the progress bar', () => {
const {getByRole} = HTMLRender(<ProgressBar progress={80} aria-valuenow={80} />)
expect(getByRole('progressbar')).toHaveAttribute('aria-valuenow', '80')
})

it('passed the `aria-valuetext` down to the progress bar', () => {
const {getByRole} = HTMLRender(<ProgressBar aria-valuetext="80 percent" />)
expect(getByRole('progressbar')).toHaveAttribute('aria-valuetext', '80 percent')
})

it('does not pass the `aria-label` down to the progress bar if there are multiple items', () => {
const {getByRole} = HTMLRender(
<ProgressBar aria-label="Upload test.png">
<ProgressBar.Item progress={80} />
</ProgressBar>,
)
expect(getByRole('progressbar')).not.toHaveAttribute('aria-label')
})

it('passes aria attributes to the progress bar item', () => {
const {getByRole} = HTMLRender(
<ProgressBar>
<ProgressBar.Item progress={50} aria-label="Progress" ria-valuenow="50"></ProgressBar.Item>
</ProgressBar>,
)
expect(getByRole('progressbar')).toHaveAttribute('aria-valuenow', '50')
expect(getByRole('progressbar')).toHaveAttribute('aria-label', 'Progress')
})

it('provides `aria-valuenow` to the progress bar item if it is not already provided', () => {
const {getByRole} = HTMLRender(<ProgressBar progress={50} />)
expect(getByRole('progressbar')).toHaveAttribute('aria-valuenow', '50')
})

it('applies `0` as a value for `aria-valuenow`', () => {
const {getByRole} = HTMLRender(<ProgressBar progress={0} aria-valuenow={0} aria-label="Upload text.png" />)

expect(getByRole('progressbar')).toHaveAttribute('aria-valuenow', '0')
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,15 @@ exports[`ProgressBar respects the "progress" prop 1`] = `
}

<span
aria-label="Upload test.png"
aria-valuemax={100}
aria-valuemin={0}
aria-valuenow={80}
aria-valuetext="80%"
className="c0"
role="progressbar"
>
<span
aria-label="Upload test.png"
aria-valuemax={100}
aria-valuemin={0}
aria-valuenow={80}
className="c1"
role="progressbar"
/>
</span>
`;
Loading