Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 677
ProgressBar: Adjust ProgressBar.Item for accessibility#4878
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
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
16795e3
Adjust `ProgressBar.Item` for accessibility
TylerJDev 747354d
Move warning
TylerJDev b2aa4c3
Include if progressAsNumber === 0
TylerJDev 3cbd244
Update snapshots, tests, move aria-* attributes
TylerJDev 993204c
Update warning
TylerJDev 6bb59e3
Merge branch 'main' into tylerjdev/progress-bar-a11y
TylerJDev d73c566
Update `ProgressBar.Item` props
TylerJDev 344cf29
Merge branch 'main' into tylerjdev/progress-bar-a11y
TylerJDev 0ca44f2
Account for `0`
TylerJDev c07904e
Add changeset
TylerJDev ecd8774
Merge branch 'main' into tylerjdev/progress-bar-a11y
TylerJDev b6afe92
Change children conditional
TylerJDev b153fc9
Remove warning, add default 0
TylerJDev 3a0f019
Fix lint, test
TylerJDev 4823c8e
Update packages/react/src/ProgressBar/ProgressBar.tsx
TylerJDev a2ca83b
Update packages/react/src/ProgressBar/ProgressBar.tsx
TylerJDev 8b67e67
Update packages/react/src/ProgressBar/ProgressBar.tsx
TylerJDev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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". |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
| @@ -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'}`)}; | ||
| @@ -34,8 +33,6 @@ export const Item = styled.span<ProgressProp & SxProp>` | ||
| ${sx}; | ||
| ` | ||
| Item.displayName = 'ProgressBar.Item' | ||
| const sizeMap = { | ||
| small: '5px', | ||
| large: '10px', | ||
| @@ -60,37 +57,78 @@ 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), | ||
| 'aria-valuemin': 0, | ||
| 'aria-valuemax': 100, | ||
| 'aria-valuetext': ariaValueText, | ||
| } | ||
| return ( | ||
| <ProgressItem | ||
| {...rest} | ||
| role="progressbar" | ||
| aria-label={ariaLabel} | ||
| ref={forwardRef} | ||
| progress={progress} | ||
| {...ariaAttributes} | ||
| /> | ||
| ) | ||
| }, | ||
| ) | ||
| 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, | ||
| } | ||
| // Get the number of non-empty nodes passed as children, this will exclude | ||
| // booleans, null, and undefined | ||
| const validChildren = React.Children.toArray(children).length | ||
| 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 | ||
joshblack marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| ) : ( | ||
| <Item | ||
| data-animated={animated} | ||
| progress={progress} | ||
| aria-label={ariaLabel} | ||
| aria-valuenow={ariaValueNow} | ||
| aria-valuetext={ariaValueText} | ||
| bg={bg} | ||
| /> | ||
| )} | ||
| </ProgressContainer> | ||
| ) | ||
| }, | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 5 additions & 6 deletions
11 packages/react/src/__tests__/__snapshots__/ProgressBar.test.tsx.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.