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 types for progress #659

Merged
merged 4 commits into from
Oct 12, 2020
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
1 change: 1 addition & 0 deletions libraries/core-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
"@equinor/eds-icons": "workspace:*",
"@equinor/eds-tokens": "workspace:*",
"@testing-library/react-hooks": "^3.3.0",
"csstype": "^3.0.3",
"focus-visible": "^5.1.0",
"lodash": "^4.17.19"
},
Expand Down
3 changes: 2 additions & 1 deletion libraries/core-react/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

117 changes: 0 additions & 117 deletions libraries/core-react/src/Progress/Circular/CircularProgress.jsx

This file was deleted.

113 changes: 113 additions & 0 deletions libraries/core-react/src/Progress/Circular/CircularProgress.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import React, { forwardRef } from 'react'
import CSS from 'csstype'
import styled, { css, keyframes } from 'styled-components'
import { progress as tokens } from '../Progress.tokens'

const indeterminate = keyframes`
100% {
transform: rotate(360deg);
}
`
type ProgressRootProps = {
variant?: 'determinate' | 'indeterminate'
}

const ProgressRoot = styled.div<ProgressRootProps>`
display: inline-block;
height: 48px;
width: 48px;
color: ${tokens.linear.background};
${({ variant }) =>
variant === 'indeterminate'
? css`
animation: ${indeterminate} 1.4s linear infinite;
`
: css`
transform: rotate(-90deg);
`};
`

const BaseCircle = styled.circle`
stroke: ${tokens.linear.background};
`

const ProgressCircle = styled.circle`
stroke: ${tokens.linear.overlay};
`

type Props = {
/* Use indeterminate when there is no progress value */
variant?: 'determinate' | 'indeterminate'
className?: string
/* The value of the progress indicator for determinate variant
* Value between 0 and 100 */
value?: number
}

const CircularProgress = forwardRef<HTMLDivElement, Props>(
function CircularProgress(
{ variant = 'indeterminate', className = '', value = null, ...props },
ref,
) {
const thickness = 4
const progress = Math.round(value)
const circleStyle: CSS.Properties = {}

const rootProps = {
...props,
ref,
variant,
}

const circumference = 2 * Math.PI * ((48 - thickness) / 2)

if (variant === 'determinate') {
circleStyle.stroke = circumference.toFixed(3)
circleStyle.strokeDashoffset = `${(
((100 - progress) / 100) *
circumference
).toFixed(3)}px`

rootProps['aria-valuenow'] = progress

if (value !== undefined) {
rootProps['aria-valuenow'] = progress
rootProps['aria-valuemin'] = 0
rootProps['aria-valuemax'] = 100
}
}

return (
<ProgressRoot
{...rootProps}
role="progressbar"
className={`${className} ${variant}-progress`}
>
<svg viewBox="24 24 48 48">
<BaseCircle
style={circleStyle}
cx={48}
cy={48}
r={(48 - thickness) / 2}
fill="none"
strokeWidth={thickness}
/>
<ProgressCircle
style={circleStyle}
cx={48}
cy={48}
r={(48 - thickness) / 2}
fill="none"
strokeLinecap="round"
strokeWidth={thickness}
strokeDasharray={variant === 'determinate' ? circumference : 48}
/>
</svg>
</ProgressRoot>
)
},
)

CircularProgress.displayName = 'eds-circular-progress'

export { CircularProgress }
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const StyledProgress = styled(DotProgress)`

const { white, green } = tokens.dots

const rgbaTrim = (x) => x.split(' ').join('')
const rgbaTrim = (x: string) => x.split(' ').join('')

afterEach(cleanup)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
// @ts-nocheck
import React, { forwardRef } from 'react'
import PropTypes from 'prop-types'
import React, { forwardRef, HTMLAttributes } from 'react'
import styled, { keyframes } from 'styled-components'
import { progress as tokens } from '../Progress.tokens'

Expand Down Expand Up @@ -28,8 +26,14 @@ const Svg = styled.svg`
}
`

const DotProgress = forwardRef(function DotProgress(
{ variant, className, ...rest },
type Props = {
/* Choose between two colors */
variant?: 'white' | 'green'
className?: string
} & HTMLAttributes<SVGSVGElement>

const DotProgress = forwardRef<SVGSVGElement, Props>(function DotProgress(
{ variant = 'white', className = '', ...rest },
ref,
) {
const props = {
Expand All @@ -56,17 +60,4 @@ const DotProgress = forwardRef(function DotProgress(

DotProgress.displayName = 'eds-dot-progress'

DotProgress.propTypes = {
/** @ignore */
className: PropTypes.string,
/* Variant
* Choose between two colors */
variant: PropTypes.oneOf(['white', 'green']),
}

DotProgress.defaultProps = {
className: '',
variant: 'white',
}

export { DotProgress }
Loading