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

Add segments to progressbar #3114

Merged
merged 19 commits into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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/dry-weeks-compete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@primer/react": patch
---

Adds the option to declare multiple segments in a ProgressBar.
13 changes: 13 additions & 0 deletions docs/content/ProgressBar.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ If you'd like to use ProgressBar inline, pass the `inline` boolean prop & **be s
</>
```

### Multiple Segments

If you want to show multiple segments in a ProgressBar, pass separate `Item`s as children **instead** of a `progress` value.

```jsx live
<>
<ProgressBar>
<ProgressBar.Item progress={33} />
<ProgressBar.Item progress={33} sx={{backgroundColor: 'danger.emphasis'}} />
</ProgressBar>
</>
```

## Props

<ComponentProps data={data} />
Expand Down
10 changes: 9 additions & 1 deletion src/ProgressBar/ProgressBar.features.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react'
import {ComponentMeta} from '@storybook/react'
import ProgressBar from './ProgressBar'
import {ProgressBar} from '..'

export default {
title: 'Components/ProgressBar/Features',
Expand All @@ -17,3 +17,11 @@ export const SizeLarge = () => <ProgressBar progress="66" barSize="large" />
export const Inline = () => <ProgressBar inline progress="66" sx={{width: '100px'}} />

export const Color = () => <ProgressBar progress="66" bg="done.emphasis" />

export const MultipleItems = () => (
<ProgressBar>
<ProgressBar.Item progress={33} />
<ProgressBar.Item progress={23} sx={{backgroundColor: 'danger.emphasis'}} />
<ProgressBar.Item progress={14} sx={{backgroundColor: 'severe.emphasis'}} />
</ProgressBar>
)
2 changes: 1 addition & 1 deletion src/ProgressBar/ProgressBar.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react'
import {Meta, ComponentStory} from '@storybook/react'
import ProgressBar from './ProgressBar'
import {ProgressBar} from '..'

export default {
title: 'Components/ProgressBar',
Expand Down
24 changes: 17 additions & 7 deletions src/ProgressBar/ProgressBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import sx, {SxProp} from '../sx'

type ProgressProp = {progress?: string | number}

const Bar = styled.span<ProgressProp & SxProp>`
export const Item = styled.span<ProgressProp & SxProp>`
width: ${props => (props.progress ? `${props.progress}%` : 0)};

background-color: ${get('colors.success.emphasis')};
${sx};
`

Item.displayName = 'ProgressBar.Item'

const sizeMap = {
small: '5px',
large: '10px',
Expand All @@ -35,14 +37,22 @@ const ProgressContainer = styled.span<StyledProgressContainerProps>`
${sx};
`

export type ProgressBarProps = {bg?: string} & StyledProgressContainerProps & ProgressProp
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.')
}

function ProgressBar({progress, bg = 'success.emphasis', barSize = 'default', ...rest}: ProgressBarProps) {
return (
<ProgressContainer barSize={barSize} {...rest}>
<Bar progress={progress} sx={{bg}} />
{children ?? <Item progress={progress} sx={{backgroundColor: bg}} />}
</ProgressContainer>
)
}

export default ProgressBar
9 changes: 8 additions & 1 deletion src/ProgressBar/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
export {default, ProgressBarProps} from './ProgressBar'
import {ProgressBar as Bar, Item} from './ProgressBar'

export type {ProgressBarProps} from './ProgressBar'

/**
* Collection of ProgressBar related components.
*/
export const ProgressBar = Object.assign(Bar, {Item})
3 changes: 2 additions & 1 deletion src/__tests__/ProgressBar.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ describe('ProgressBar', () => {
behavesAsComponent({Component: ProgressBar})

checkExports('ProgressBar', {
default: ProgressBar,
default: undefined,
ProgressBar,
})

it('should have no axe violations', async () => {
Expand Down
2 changes: 2 additions & 0 deletions src/__tests__/__snapshots__/ProgressBar.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ exports[`ProgressBar renders consistently 1`] = `
.c1 {
width: 0;
background-color: #1f883d;
background-color: #1f883d;
}

.c0 {
Expand All @@ -30,6 +31,7 @@ exports[`ProgressBar respects the "progress" prop 1`] = `
.c1 {
width: 80%;
background-color: #1f883d;
background-color: #1f883d;
}

.c0 {
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export {default as Popover} from './Popover'
export type {PopoverProps, PopoverContentProps} from './Popover'
export {default as Portal, registerPortalRoot} from './Portal'
export type {PortalProps} from './Portal'
export {default as ProgressBar} from './ProgressBar'
export {ProgressBar} from './ProgressBar'
export type {ProgressBarProps} from './ProgressBar'
export {default as RadioGroup} from './RadioGroup'
export type {RelativeTimeProps} from './RelativeTime'
Expand Down