Skip to content

Add bg prop to ProgressBar.Item #5007

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
merged 3 commits into from
Sep 24, 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
5 changes: 5 additions & 0 deletions .changeset/slow-walls-drum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/react': minor
---

ProgressBar: Add `bg` prop to `ProgressBar.Item`
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ export const Color = () => <ProgressBar progress="66" bg="done.emphasis" aria-la

export const MultipleItems = () => (
<ProgressBar aria-valuenow={70} aria-label="Upload test.png">
<ProgressBar.Item progress={33} />
<ProgressBar.Item progress={23} sx={{backgroundColor: 'danger.emphasis'}} />
<ProgressBar.Item progress={14} sx={{backgroundColor: 'severe.emphasis'}} />
<ProgressBar.Item progress={33} sx={{bg: 'accent.emphasis'}} />
<ProgressBar.Item progress={23} bg={'danger.emphasis'} />
<ProgressBar.Item progress={14} bg={'severe.emphasis'} />
</ProgressBar>
)

Expand Down
20 changes: 9 additions & 11 deletions packages/react/src/ProgressBar/ProgressBar.figma.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,16 @@ figma.connect(
{
props: {
color: figma.enum('color', {
green: 'var(--data-green-color)',
teal: 'var(--data-teal-color)',
blue: 'var(--data-blue-color)',
purple: 'var(--data-purple-color)',
orange: 'var(--data-orange-color)',
red: 'var(--data-red-color)',
pink: 'var(--data-pink-color)',
yellow: 'var(--data-yellow-color)',
auburn: 'var(--data-auburn-color)',
gray: 'var(--data-gray-color)',
green: 'success.empahsis',
blue: 'accent.emphasis',
purple: 'done.emphasis',
orange: 'severe.empahsis',
red: 'danger.emphasis',
pink: 'sponsors.emphasis',
yellow: 'attention.emphasis',
gray: 'neutral.epmhasis',
}),
},
example: ({color}) => <ProgressBarItem sx={{background: color}} />,
example: ({color}) => <ProgressBarItem bg={color} />,
},
)
21 changes: 18 additions & 3 deletions packages/react/src/ProgressBar/ProgressBar.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import React from 'react'
import React, {useEffect} from 'react'
import type {Meta} from '@storybook/react'
import {ProgressBar, type ProgressBarProps} from '..'

const sectionColors = ['success.emphasis', 'done.emphasis', 'severe.emphasis', 'danger.emphasis', 'attention.emphasis']
const sectionColorsDefault = [
'success.emphasis',
'accent.emphasis',
'done.emphasis',
'severe.emphasis',
'danger.emphasis',
'attention.emphasis',
]

export default {
title: 'Components/ProgressBar',
Expand All @@ -12,13 +19,21 @@ export default {
export const Default = () => <ProgressBar aria-label="Upload test.png" />

export const Playground = ({sections, ...args}: ProgressBarProps & {sections: number}) => {
const [sectionColors, setSectionColors] = React.useState(sectionColorsDefault)

useEffect(() => {
if (args.bg && args.bg !== '') {
setSectionColors([args.bg, ...sectionColorsDefault])
}
}, [args.bg])

Copy link
Member

@siddharthkp siddharthkp Sep 24, 2024

Choose a reason for hiding this comment

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

(non blocker, nit picking) Didn't really understand what this block of code does, can you help me?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Needed to avoid error: "To many re-renders"

if (sections === 1) {
return <ProgressBar {...args} sx={args.inline ? {width: '100px'} : {}} aria-label="Upload test.png" />
} else {
return (
<ProgressBar aria-label="Upload test.png">
{[...Array(sections).keys()].map(i => (
<ProgressBar.Item key={i} progress={100 / sections} sx={{bg: sectionColors[i]}} />
<ProgressBar.Item key={i} progress={100 / sections} bg={sectionColors[i]} />
))}
</ProgressBar>
)
Expand Down
9 changes: 6 additions & 3 deletions packages/react/src/ProgressBar/ProgressBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import type {SxProp} from '../sx'
import sx from '../sx'
import {warning} from '../utils/warning'

type ProgressProp = {progress?: string | number}
type ProgressProp = {
progress?: string | number
bg?: string
}

const shimmer = keyframes`
from { mask-position: 200%; }
Expand All @@ -16,7 +19,7 @@ const shimmer = keyframes`

export const Item = styled.span<ProgressProp & SxProp>`
width: ${props => (props.progress ? `${props.progress}%` : 0)};
background-color: ${get('colors.success.emphasis')};
background-color: ${props => get(`colors.${props.bg || 'success.emphasis'}`)};

@media (prefers-reduced-motion: no-preference) {
&[data-animated='true'] {
Expand Down Expand Up @@ -86,7 +89,7 @@ export const ProgressBar = forwardRef<HTMLSpanElement, ProgressBarProps>(

return (
<ProgressContainer ref={forwardRef} role="progressbar" barSize={barSize} {...ariaAttributes} {...rest}>
{children ?? <Item data-animated={animated} progress={progress} sx={{backgroundColor: bg}} />}
{children ?? <Item data-animated={animated} progress={progress} bg={bg} />}
</ProgressContainer>
)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ exports[`ProgressBar respects the "progress" prop 1`] = `
.c1 {
width: 80%;
background-color: var(--bgColor-success-emphasis,var(--color-success-emphasis,#1f883d));
background-color: var(--bgColor-success-emphasis,var(--color-success-emphasis,#1f883d));
}

.c0 {
Expand Down
Loading