Skip to content

Commit

Permalink
feat(shared-types,ui-progress): add customization options for progres…
Browse files Browse the repository at this point in the history
…sbar
  • Loading branch information
balzss committed Dec 6, 2024
1 parent ee9cafd commit 56308de
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 5 deletions.
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 && (
<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

0 comments on commit 56308de

Please sign in to comment.