Skip to content

Commit

Permalink
feat: add static progress to circular #progress - also add example to…
Browse files Browse the repository at this point in the history
… the docs
  • Loading branch information
tujoworker committed May 19, 2019
1 parent 6b99a2b commit 39bae67
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,19 @@ class Example extends PureComponent {
render() {
return (
<Fragment>
<ComponentBox>
<ComponentBox caption="Default circular progress">
{/* @jsx */ `
<Progress />
`}
</ComponentBox>
<ComponentBox
caption="Shows a large circular progress with a static 50% in progress"
data-dnb-test="progress-circular--primary"
>
{/* @jsx */ `
<Progress
// label="Primary button with text only"
data-dnb-test="progress-circular--primary"
progress="50"
size="large"
/>
`}
</ComponentBox>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ draft: true
| Properties | Description |
| -------------- | --------------------------------------------------------------------------------------------------------------------------------------------------- |
| `label` | _(optional)_ if a text label is needed. Defaults to `null`. |
| `progress` | _(optional)_ to visualize a static **percentage** (0-100) as a progress state. Defaults to `null`. |
| `visible` | _(optional)_ defines the visibility of the progress. Toggeling the `visible` property to false will force a fade-out animation. Defaults to `true`. |
| `type` | _(optional)_ defines the **type** of progress, like `circular`. Defaults to `circular`. |
| `no_animation` | _(optional)_ disables the fade-in and fade-out animation. Defaults to false. |
Expand Down
70 changes: 38 additions & 32 deletions packages/dnb-ui-lib/src/components/progress/Progress.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,47 @@

import React, { PureComponent } from 'react'
import PropTypes from 'prop-types'
// import classnames from 'classnames'
// import keycode from 'keycode'
import classnames from 'classnames'
import {
registerElement
// validateDOMAttributes,
// dispatchCustomElementEvent
} from '../../shared/component-helper'
import ProgressCircular from './ProgressCircular'

const renderProps = {
render: null
}
const renderProps = {}

export const propTypes = {
label: PropTypes.string,
// label: PropTypes.string,
visible: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
type: PropTypes.oneOf(['circular']),
no_animation: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
min_time: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
variant: PropTypes.oneOf(['primary', 'secondary']),
size: PropTypes.oneOf(['large', 'medium']),
// type: PropTypes.oneOf(['circular']),
// no_animation: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
// min_time: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
// variant: PropTypes.oneOf(['primary', 'secondary']),
size: PropTypes.oneOf(['small', 'medium', 'large', 'huge']),
progress: PropTypes.number
// id: PropTypes.string,
// class: PropTypes.string,
/** React props */
className: PropTypes.string,
children: PropTypes.oneOfType([
PropTypes.object,
PropTypes.node,
PropTypes.func
]),
// className: PropTypes.string,
// children: PropTypes.oneOfType([
// PropTypes.object,
// PropTypes.node,
// PropTypes.func
// ]),

// Web Component props
render: PropTypes.func
}

export const defaultProps = {
label: null,
// label: null,
visible: true,
type: 'circular',
no_animation: false,
min_time: null,
variant: 'primary',
// type: 'circular',
// no_animation: false,
// min_time: null,
// variant: 'primary',
size: 'medium',
progress: null,
// id: null,
// class: null,

Expand All @@ -70,8 +68,11 @@ export default class Progress extends PureComponent {

static getDerivedStateFromProps(props, state) {
if (state._listenForPropChanges) {
if (props.visible && state._visible !== props.visible) {
state.visible = state._visible = props.visible
if (props.visible) {
state.visible = Boolean(props.visible)
}
if (parseFloat(props.progress) > -1) {
state.visible = props.progress
}
}
state._listenForPropChanges = true
Expand All @@ -88,23 +89,28 @@ export default class Progress extends PureComponent {
this.state = {
_listenForPropChanges: true,
visible,
_visible: visible
progress: props.progress
}

// this._tablistRef = React.createRef()
}

render() {
const {
size,
progress: _progress, //eslint-disable-line
visible: _visible //eslint-disable-line
// ...props
// ...attributes
} = this.props

// const { visible } = this.state
const { progress, visible } = this.state

return (
<div className="dnb-progress">
<ProgressCircular />
<div
className={classnames(
'dnb-progress',
!visible && 'dnb-progress--hidden'
)}
>
<ProgressCircular size={size} progress={progress} />
</div>
)
}
Expand Down
53 changes: 48 additions & 5 deletions packages/dnb-ui-lib/src/components/progress/ProgressCircular.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,67 @@

import React, { PureComponent } from 'react'
import PropTypes from 'prop-types'
import classnames from 'classnames'

export const propTypes = {
size: PropTypes.string,
progress: PropTypes.number,
maxOffset: PropTypes.number
}
export const defaultProps = {
size: null,
progress: null,
maxOffset: 88
}

export default class ProgressCircular extends PureComponent {
static propTypes = propTypes
static defaultProps = defaultProps
render() {
const { size, maxOffset, progress } = this.props
const strokeDashoffset = -((maxOffset / 100) * progress)
const hasProgress = parseFloat(progress) > -1
return (
<div className="dnb-progress__circular">
<Circle className="dnb-progress__circular__line dark paused" />
<Circle className="dnb-progress__circular__line light" />
<Circle className="dnb-progress__circular__line dark" />
<div
className={classnames(
'dnb-progress__circular',
size && `dnb-progress__circular--${size}`
)}
>
<Circle
className={classnames(
'dnb-progress__circular__line',
'dark',
'paused'
)}
/>
<Circle
className={classnames(
'dnb-progress__circular__line',
'light',
hasProgress && 'paused'
)}
style={hasProgress ? { strokeDashoffset } : {}}
/>
<Circle
className={classnames(
'dnb-progress__circular__line',
'dark',
hasProgress && 'paused'
)}
style={hasProgress ? { strokeDashoffset: -maxOffset } : {}}
/>
</div>
)
}
}

const Circle = ({ className }) => (
const Circle = ({ className, ...rest }) => (
<svg
className={className}
viewBox="0 0 32 32"
shapeRendering="optimizeSpeed"
{...rest}
>
<circle
className="dnb-progress__circular__circle"
Expand Down
22 changes: 19 additions & 3 deletions packages/dnb-ui-lib/src/components/progress/style/_progress.scss
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,25 @@
&__circular {
position: relative;

// medium
width: 2rem;
height: 2rem;

&--small {
width: 1rem;
height: 1rem;
}

&--large {
width: 4rem;
height: 4rem;
}

&--huge {
width: 20rem;
height: 20rem;
}

// since SVG is starting 90deg from top
transform: rotate(-90deg);

Expand Down Expand Up @@ -49,7 +65,7 @@
var(--progress-circular-circle);
stroke-dashoffset: var(--progress-circular-circle-offset--min);
}
&__line.dark.paused {
&__line.paused {
// z-index: 1;
animation-play-state: paused;
}
Expand All @@ -58,10 +74,10 @@
stroke-linecap: round;
}
&__line.light &__circle {
stroke: var(--color-mint-green);
stroke: grey;
}
&__line.dark &__circle {
stroke: var(--color-emerald-green);
stroke: black;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,29 @@

@import '../../../../style/themes/imports.scss';

// .dnb-progress {
// }
/*
* Progress complightnt
*
*/

:root {
--progress-circular-circle: 88;
--progress-circular-circle-offset--min: -1;
--progress-circular-circle-offset--max: -88;
}

.dnb-progress {
// circular variant
&__circular {
&__line {
animation-duration: 2s;
}

&__line.light &__circle {
stroke: var(--color-mint-green);
}
&__line.dark &__circle {
stroke: var(--color-emerald-green);
}
}
}
5 changes: 4 additions & 1 deletion packages/dnb-ui-lib/stories/componentsStories.js
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,10 @@ stories.push([
() => (
<Wrapper>
<Box>
<Progress />
<Progress size="huge" />
</Box>
<Box>
<Progress progress={88} size="huge" />
</Box>
</Wrapper>
)
Expand Down

0 comments on commit 39bae67

Please sign in to comment.