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

Migrate ProgressBar to TypeScript #987

Merged
merged 5 commits into from
Feb 4, 2021
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/friendly-carrots-accept.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@primer/components": patch
---

Migrate `ProgressBar` to TypeScript
31 changes: 21 additions & 10 deletions src/ProgressBar.js → src/ProgressBar.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import React from 'react'
// @ts-ignore @styled-system/prop-types does not provide type definitions
colebemis marked this conversation as resolved.
Show resolved Hide resolved
import systemPropTypes from '@styled-system/prop-types'
import PropTypes from 'prop-types'
import React from 'react'
import styled from 'styled-components'
import {layout} from 'styled-system'
import systemPropTypes from '@styled-system/prop-types'
import {width, WidthProps} from 'styled-system'
import {COMMON, get, SystemCommonProps} from './constants'
import sx, {SxProp} from './sx'
import theme from './theme'
import {COMMON, get} from './constants'
import sx from './sx'
import {ComponentProps} from './utils/types'

const Bar = styled.span`
const Bar = styled.span<{progress?: string | number} & SystemCommonProps>`
width: ${props => (props.progress ? `${props.progress}%` : 0)};
${COMMON}
`
Expand All @@ -18,18 +20,27 @@ const sizeMap = {
default: '8px'
}

const ProgressContainer = styled.span`
type StyledProgressContainerProps = {
inline?: boolean
barSize?: keyof typeof sizeMap
} & WidthProps &
SystemCommonProps &
SxProp

const ProgressContainer = styled.span<StyledProgressContainerProps>`
display: ${props => (props.inline ? 'inline-flex' : 'flex')};
overflow: hidden;
background-color: ${get('colors.gray.2')};
border-radius: ${get('radii.1')};
height: ${props => sizeMap[props.barSize]};
height: ${props => sizeMap[props.barSize || 'default']};
Copy link

Choose a reason for hiding this comment

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

Is this necessary if we're setting the default value in defaultProps?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It shouldn't be necessary. But defaultProps + styled-components + TypeScript don't play well together in this case. TypeScript was complaining about props.barSize possibly being undefined. I'm open to other solutions :)

${COMMON}
${layout.width}
${width}
${sx};
`

const ProgressBar = ({progress, bg, theme, ...rest}) => {
export type ProgressBarProps = ComponentProps<typeof ProgressContainer> & ComponentProps<typeof Bar>

function ProgressBar({progress, bg, theme, ...rest}: ProgressBarProps) {
return (
<ProgressContainer theme={theme} {...rest}>
<Bar progress={progress} bg={bg} theme={theme} />
Expand Down
File renamed without changes.