Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # 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 2 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`
const ProgressContainer = styled.span<
{
inline?: boolean
barSize?: keyof typeof sizeMap
} & WidthProps &
SystemCommonProps &
SxProp
>`
Copy link

Choose a reason for hiding this comment

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

What do you think about creating an interface at the top of the file for situations like this where it includes multiple lines? My hunch is that for more complex components like this one it can get a little tricky to visually parse. That seems to be what other applications at GH do, might make it easier to read when jumping between codebases. Curious what @smockle or @T-Hugs thinks

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I agree. I'd love to come up with a naming convention for this kind of props type that's passed to styled-components but not exported and doesn't contain all the props accepted by the returned component. Maybe Styled[component-name]Props (e.g. StyledProgressContainerProps)?

Copy link
Member

Choose a reason for hiding this comment

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

What do you think about creating an interface at the top of the file for situations like this where it includes multiple lines? My hunch is that for more complex components like this one it can get a little tricky to visually parse.

Great idea! Incidentally, organizing prop types this way helped with the namespace components issue described in #999 (comment)

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.