-
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 2 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: _none_ --> | ||
|
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. 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
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. Good catch!! To deal with multiple I now throw an error if children are passed in without What do you think?
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 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 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) if (children) {
let totalProgress = 0
React.Children.toArray(children).forEach(child => {
if (React.isValidElement(child) && child.props.progress) {
totalProgress += parseInt(child.props.progress)
}
})
}And use that cc @siddharthkp @joshblack I would love their opinion before we decide anything concrete.
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. 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 :) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| import React from 'react' | ||
| import React, {forwardRef} from 'react' | ||
| import styled from 'styled-components' | ||
| import {width, WidthProps} from 'styled-system' | ||
| import {get} from '../constants' | ||
|
|
@@ -23,6 +23,7 @@ const sizeMap = { | |
| type StyledProgressContainerProps = { | ||
| inline?: boolean | ||
| barSize?: keyof typeof sizeMap | ||
| tabIndex?: number | ||
|
||
| } & WidthProps & | ||
| SxProp | ||
|
|
||
|
|
@@ -39,20 +40,24 @@ const ProgressContainer = styled.span<StyledProgressContainerProps>` | |
|
|
||
| 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 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.') | ||
| } | ||
|
|
||
| const ariaAttributes = { | ||
| 'aria-valuenow': typeof progress === 'number' ? Math.round(progress) : undefined, | ||
| 'aria-valuetext': typeof progress === 'string' ? progress : undefined, | ||
| 'aria-valuemin': 0, | ||
| 'aria-valuemax': 100, | ||
| 'aria-busy': progress !== 100, | ||
|
||
| } | ||
|
|
||
| return ( | ||
| <ProgressContainer ref={forwardRef} role="progressbar" barSize={barSize} {...ariaAttributes} {...rest}> | ||
| {children ?? <Item progress={progress} sx={{backgroundColor: bg}} />} | ||
| </ProgressContainer> | ||
| ) | ||
| }, | ||
| ) | ||

Uh oh!
There was an error while loading. Please reload this page.