From d2cff7023c8ef3eb71106fc334b53e5b96c74839 Mon Sep 17 00:00:00 2001 From: meriadec Date: Fri, 14 Dec 2018 18:14:40 +0100 Subject: [PATCH] Implement ProgressBar & ProgressCircle --- src/components/ProgressBar/index.js | 65 ++++++++++++++++ src/components/ProgressBar/stories.js | 16 ++++ src/components/ProgressCircle/index.js | 98 ++++++++++++++++++++++++ src/components/ProgressCircle/stories.js | 16 ++++ 4 files changed, 195 insertions(+) create mode 100644 src/components/ProgressBar/index.js create mode 100644 src/components/ProgressBar/stories.js create mode 100644 src/components/ProgressCircle/index.js create mode 100644 src/components/ProgressCircle/stories.js diff --git a/src/components/ProgressBar/index.js b/src/components/ProgressBar/index.js new file mode 100644 index 00000000..51424539 --- /dev/null +++ b/src/components/ProgressBar/index.js @@ -0,0 +1,65 @@ +// @flow + +import React, { PureComponent } from 'react' +import styled, { css, keyframes } from 'styled-components' + +import { colors } from 'styles/theme' + +const animIndeterminate = keyframes` + 0% { + transform: scaleX(0) translate3d(0, 0, 0); + } + 50% { + transform: scaleX(1) translate3d(100%, 0, 0); + } + 100% { + transform: scaleX(0) translate3d(0, 0, 0); + } +` + +const Outer = styled.div` + background-color: ${colors.fog}; + border-radius: 3px; + overflow: hidden; + height: 10px; + width: ${p => p.width}px; + position: relative; +` + +const Inner = styled.div` + position: absolute; + top: 0; + left: 0; + right: 0; + bottom: 0; + background-color: ${colors.wallet}; + transform-origin: center left; + + ${p => + p.progress === 0 + ? css` + animation: ${animIndeterminate} 2s cubic-bezier(0.61, 0.01, 0.39, 1.03) infinite; + ` + : css` + transform: scaleX(${p => p.progress}); + transition: 150ms ease-out transform; + `}; +` + +type Props = { + progress: number, + width: number, +} + +class ProgressBar extends PureComponent { + render() { + const { progress, width } = this.props + return ( + + + + ) + } +} + +export default ProgressBar diff --git a/src/components/ProgressBar/stories.js b/src/components/ProgressBar/stories.js new file mode 100644 index 00000000..83e97e42 --- /dev/null +++ b/src/components/ProgressBar/stories.js @@ -0,0 +1,16 @@ +// @flow + +import React from 'react' +import { storiesOf } from '@storybook/react' +import { number } from '@storybook/addon-knobs' + +import ProgressBar from 'components/ProgressBar' + +const stories = storiesOf('Components', module) + +stories.add('ProgressBar', () => ( + +)) diff --git a/src/components/ProgressCircle/index.js b/src/components/ProgressCircle/index.js new file mode 100644 index 00000000..e50756a8 --- /dev/null +++ b/src/components/ProgressCircle/index.js @@ -0,0 +1,98 @@ +// @flow + +import React, { PureComponent } from 'react' +import styled, { css, keyframes } from 'styled-components' + +import { colors } from 'styles/theme' + +import Text from 'components/base/Text' + +const STROKE_WIDTH = 10 + +type Props = { + progress: number, + size: number, +} + +const animIndeterminate = keyframes` + 0% { + } + 50% { + } + 100% { + } +` + +const InnerCircle = styled.circle` + transform-origin: 50% 50%; + ${p => + p.progress === 0 + ? css` + animation: ${animIndeterminate} 3s cubic-bezier(0.61, 0.01, 0.39, 1.03) infinite; + ` + : css` + transition: stroke-dashoffset 0.35s; + transform: rotate(-90deg); + `}; +` + +const Container = styled.div` + position: relative; + width: ${p => p.size}px; + height: ${p => p.size}px; +` + +const TextContainer = styled.div` + position: absolute; + top: 0; + left: 0; + right: 0; + bottom: 0; + display: flex; + align-items: center; + justify-content: center; +` + +class ProgressCircle extends PureComponent { + render() { + const { size, progress } = this.props + const radius = size / 2 + const normalizedRadius = radius - STROKE_WIDTH / 2 + const circumference = normalizedRadius * 2 * Math.PI + const strokeDashoffset = circumference - progress * circumference + + return ( + + + + {`${Math.round(progress * 100)}%`} + + + + + + + + ) + } +} + +export default ProgressCircle diff --git a/src/components/ProgressCircle/stories.js b/src/components/ProgressCircle/stories.js new file mode 100644 index 00000000..48df92c2 --- /dev/null +++ b/src/components/ProgressCircle/stories.js @@ -0,0 +1,16 @@ +// @flow + +import React from 'react' +import { storiesOf } from '@storybook/react' +import { number } from '@storybook/addon-knobs' + +import ProgressCircle from 'components/ProgressCircle' + +const stories = storiesOf('Components', module) + +stories.add('ProgressCircle', () => ( + +))