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

feat(shared-types,ui-progress): add customization options for progressbar #1808

Merged
merged 1 commit into from
Dec 6, 2024
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
1 change: 1 addition & 0 deletions packages/shared-types/src/ComponentThemeVariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1014,6 +1014,7 @@ export type ProgressBarTheme = {
trackBottomBorderWidth: Border['widthSmall']
trackBottomBorderColor: Colors['contrasts']['grey1214']
trackBottomBorderColorInverse: Colors['contrasts']['white1010']
borderRadius: string
}

export type ProgressCircleTheme = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,36 @@ describe('<ProgressBar />', () => {
const axeCheck = await runAxeCheck(container)
expect(axeCheck).toBe(true)
})

it('should not render the value inside when the prop is set to false', () => {
const valueNow = 33
const { container } = render(
<ProgressBar
screenReaderLabel="Chapters read"
valueMax={100}
valueNow={valueNow}
renderValueInside={false}
renderValue={`${valueNow}%`}
/>
)
const progressMeter = container.querySelector('[class$="-progressBar__trackValue"]')

expect(progressMeter).not.toHaveTextContent('33%')
})

it('should render the value inside when the prop is set', () => {
const valueNow = 33
const { container } = render(
<ProgressBar
screenReaderLabel="Chapters read"
valueMax={100}
valueNow={valueNow}
renderValueInside
renderValue={`${valueNow}%`}
/>
)
const progressMeter = container.querySelector('[class$="-progressBar__trackValue"]')

expect(progressMeter).toHaveTextContent('33%')
})
})
5 changes: 3 additions & 2 deletions packages/ui-progress/src/ProgressBar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ class ProgressBar extends Component<ProgressBarProps> {
size,
color,
meterColor,
renderValueInside,
styles,
...props
} = this.props
Expand Down Expand Up @@ -130,11 +131,11 @@ class ProgressBar extends Component<ProgressBarProps> {
/>

<span css={styles?.track} role="presentation" aria-hidden="true">
<span css={styles?.trackValue}></span>
<span css={styles?.trackValue}>{renderValueInside && value}</span>
</span>
</span>

{value && (
{value && !renderValueInside && (
Comment on lines +134 to +138
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you try to write a test for this? e.g if renderValueInside is true then the value text should be inside the track's DOM

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

<span css={styles?.value} aria-hidden="true">
{value}
</span>
Expand Down
11 changes: 10 additions & 1 deletion packages/ui-progress/src/ProgressBar/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,14 @@ type ProgressBarOwnProps = {
* Set the element type of the component's root
*/
as?: AsElementType

/**
* If true, displays the `renderValue` inside the progress meter for customization.
*
* Note: This should not be used in most cases. When enabled, ensure `renderValue` is styled for proper
* legibility and alignment across themes and sizes.
*/
renderValueInside?: boolean
}

type PropKeys = keyof ProgressBarOwnProps
Expand Down Expand Up @@ -148,7 +156,8 @@ const propTypes: PropValidators<PropKeys> = {
shouldAnimate: PropTypes.bool,
margin: ThemeablePropTypes.spacing,
elementRef: PropTypes.func,
as: PropTypes.elementType
as: PropTypes.elementType,
renderValueInside: PropTypes.bool,
}

const allowedProps: AllowedPropKeys = [
Expand Down
6 changes: 5 additions & 1 deletion packages/ui-progress/src/ProgressBar/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,15 @@ const generateStyle = (
fontFamily: componentTheme.fontFamily,
fontWeight: componentTheme.fontWeight,
lineHeight: componentTheme.lineHeight,
fontSize: componentTheme.fontSize
fontSize: componentTheme.fontSize,
borderRadius: componentTheme.borderRadius
},

trackLayout: {
label: 'progressBar__trackLayout',
position: 'relative',
flex: 1,
borderRadius: 'inherit',

...colorVariants[color!].trackLayout
},
Expand All @@ -138,6 +140,7 @@ const generateStyle = (
borderBottomWidth: componentTheme.trackBottomBorderWidth,
borderBottomStyle: 'solid',
background: 'transparent',
borderRadius: 'inherit',

...sizeVariants[size!].track,
...colorVariants[color!].trackBorder
Expand All @@ -150,6 +153,7 @@ const generateStyle = (
height: '100%',
width: currentValuePercent,
maxWidth: '100%',
borderRadius: 'inherit',

...(shouldAnimate && { transition: 'all 0.5s' }),
...(meterColorClassName &&
Expand Down
4 changes: 3 additions & 1 deletion packages/ui-progress/src/ProgressBar/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ const generateComponentTheme = (theme: Theme): ProgressBarTheme => {
trackColorInverse: 'transparent',
trackBottomBorderWidth: borders?.widthSmall,
trackBottomBorderColor: colors?.contrasts?.grey3045,
trackBottomBorderColorInverse: colors?.contrasts?.white1010
trackBottomBorderColorInverse: colors?.contrasts?.white1010,

borderRadius: '0px'
}

return {
Expand Down
Loading