-
Notifications
You must be signed in to change notification settings - Fork 648
Add aria attributes to the progress bar #3517
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 all commits
814bf19
b318e2c
7bbd044
80e41b7
48aafd7
5f03895
d8dcf82
7dc7fe9
fb50b97
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,7 @@ | ||
| --- | ||
| '@primer/react': patch | ||
| --- | ||
|
|
||
| Adding aria-attributes and role to the ProgressBar component | ||
|
|
||
| <!-- Changed components: ProgressBar --> |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,8 +1,9 @@ | ||||||
| import React from 'react' | ||||||
| import React, {forwardRef} from 'react' | ||||||
| import styled from 'styled-components' | ||||||
| import {width, WidthProps} from 'styled-system' | ||||||
| import {get} from '../constants' | ||||||
| import sx, {SxProp} from '../sx' | ||||||
| import {warning} from '../utils/warning' | ||||||
|
|
||||||
| type ProgressProp = {progress?: string | number} | ||||||
|
|
||||||
|
|
@@ -37,22 +38,37 @@ const ProgressContainer = styled.span<StyledProgressContainerProps>` | |||||
| ${sx}; | ||||||
| ` | ||||||
|
|
||||||
| export type ProgressBarProps = React.PropsWithChildren & {bg?: string} & StyledProgressContainerProps & ProgressProp | ||||||
|
|
||||||
| export const ProgressBar = ({ | ||||||
| progress, | ||||||
| bg = 'success.emphasis', | ||||||
| barSize = 'default', | ||||||
| children, | ||||||
| ...rest | ||||||
| }: ProgressBarProps) => { | ||||||
| if (children && progress) { | ||||||
| throw new Error('You should pass `progress` or children, not both.') | ||||||
| } | ||||||
|
|
||||||
| return ( | ||||||
| <ProgressContainer barSize={barSize} {...rest}> | ||||||
| {children ?? <Item progress={progress} sx={{backgroundColor: bg}} />} | ||||||
| </ProgressContainer> | ||||||
| ) | ||||||
| } | ||||||
| export type ProgressBarProps = React.HTMLAttributes<HTMLSpanElement> & {bg?: string} & StyledProgressContainerProps & | ||||||
|
Contributor
Author
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. @joshblack I added |
||||||
| ProgressProp | ||||||
|
|
||||||
| export const ProgressBar = forwardRef<HTMLSpanElement, ProgressBarProps>( | ||||||
| ({progress, bg = 'success.emphasis', barSize = 'default', children, ...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, | ||||||
|
Member
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.
Suggested change
Don't think we need to
Contributor
Author
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. Oh good point! I added the math.round before I used
Contributor
Author
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. Oh wait... I need it in the case that the typeof the value is a
Member
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. Ah I see! My bad!
Member
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. @siddharthkp one note with
Member
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. Oh wow, good note! |
||||||
| 'aria-valuemin': 0, | ||||||
| 'aria-valuemax': 100, | ||||||
| 'aria-busy': progressAsNumber ? progressAsNumber !== 100 : false, | ||||||
| } | ||||||
|
|
||||||
| return ( | ||||||
| <ProgressContainer ref={forwardRef} role="progressbar" barSize={barSize} {...ariaAttributes} {...rest}> | ||||||
| {children ?? <Item progress={progress} sx={{backgroundColor: bg}} />} | ||||||
| </ProgressContainer> | ||||||
| ) | ||||||
| }, | ||||||
| ) | ||||||
|
|
||||||
| ProgressBar.displayName = 'ProgressBar' | ||||||
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.
We also have ProgressBar with multiple items like here and we don't get aria attributes in these I realised. There is no usage of
ProgressBar.Itemthough at dotcom so not sure if it would worth the effort. What do you think??Uh oh!
There was an error while loading. Please reload this page.
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.
Good catch!!
To deal with multiple I now throw an error if children are passed in without
aria-valuenowor'aria-valuetext:What do you think?
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.
Oh I see! I have a different take on this but I am not 100% sure, let me know what you think!
Seems like we are handling these aria attributes internally and I think it is a tad inconsistent to ask this from consumers when there are
ProgressBar.Itemchildren.I am thinking maybe we would want to calculate the total progress internally and update the aria attributes as we do for the single progress item? Something along those lines (not a complete code)
And use that
totalProgressto calculate the value of aria attributes.cc @siddharthkp @joshblack I would love their opinion before we decide anything concrete.
Uh oh!
There was an error while loading. Please reload this page.
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.
Hi!
We could definitely loop through the children if the usage is predictable. However...
I couldn't find any usage of ProgressBar with multiple items, so we might be able to slide it under the rug and deprecate it safely without any remediation :)