-
-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
160 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Meta, Story, ArgsTable } from '@storybook/addon-docs/blocks'; | ||
import { Count } from '../../src'; | ||
|
||
<Meta title="Docs/Utils/Count" /> | ||
|
||
# Count | ||
|
||
--- | ||
|
||
Count from one number to another. | ||
|
||
## API | ||
|
||
### [Count](https://github.com/reaviz/reaviz/blob/master/src/common/Count/Count.tsx) | ||
<ArgsTable of={Count} /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import React from 'react'; | ||
import { Count } from './Count'; | ||
|
||
export default { | ||
title: 'Utils/Count', | ||
component: Count | ||
}; | ||
|
||
export const Simple = () => ( | ||
<h1 style={{ color: 'white ' }}> | ||
<Count from={0} to={50} /> | ||
</h1> | ||
); | ||
|
||
export const NonZeroStart = () => ( | ||
<h1 style={{ color: 'white ' }}> | ||
<Count from={33} to={50} /> | ||
</h1> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import React, { FC } from 'react'; | ||
import { CountProps, useCount } from './useCount'; | ||
|
||
export const Count: FC<CountProps> = (props) => { | ||
const ref = useCount(props); | ||
return <span ref={ref} />; | ||
}; | ||
|
||
Count.defaultProps = { | ||
from: 0, | ||
duration: 1, | ||
delay: 0, | ||
localize: true, | ||
decimalPlaces: 0 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './Count'; | ||
export * from './useCount'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { animate } from 'framer-motion'; | ||
import { useEffect, useRef } from 'react'; | ||
|
||
export interface CountProps { | ||
/** | ||
* Number to animate to | ||
*/ | ||
to: number; | ||
|
||
/** | ||
* Number to animate from. Defaults 0. | ||
*/ | ||
from?: number; | ||
|
||
/** | ||
* Duration of the animation in seconds. Defaults 1. | ||
*/ | ||
duration?: number; | ||
|
||
/** | ||
* Delay of the animation. Defaults 0. | ||
*/ | ||
delay?: number; | ||
|
||
/** | ||
* Localize the number. Defaults true. | ||
*/ | ||
format?: boolean; | ||
|
||
/** | ||
* Number of decimal places. Defaults 0. | ||
*/ | ||
decimalPlaces?: number; | ||
} | ||
|
||
export const COUNT_DEFAULTS = { | ||
from: 0, | ||
duration: 1, | ||
delay: 0, | ||
format: true, | ||
decimalPlaces: 0 | ||
}; | ||
|
||
export const useCount = ({ | ||
from, | ||
to, | ||
duration, | ||
delay, | ||
decimalPlaces, | ||
format | ||
}: CountProps) => { | ||
const nodeRef = useRef<any | null>(null); | ||
|
||
from = from || COUNT_DEFAULTS.from; | ||
duration = duration || COUNT_DEFAULTS.duration; | ||
delay = delay || COUNT_DEFAULTS.delay; | ||
format = format || COUNT_DEFAULTS.format; | ||
decimalPlaces = decimalPlaces || COUNT_DEFAULTS.decimalPlaces; | ||
|
||
useEffect(() => { | ||
const node = nodeRef.current; | ||
|
||
const controls = animate(from, to, { | ||
duration, | ||
delay, | ||
onUpdate(value) { | ||
let formatted: number | string = value; | ||
if (decimalPlaces) { | ||
formatted = Number(value.toFixed(decimalPlaces)); | ||
} else { | ||
formatted = Number(value.toFixed(0)); | ||
} | ||
|
||
if (format) { | ||
formatted = formatted.toLocaleString(); | ||
} | ||
|
||
node.textContent = formatted as string; | ||
} | ||
}); | ||
|
||
return () => controls.stop(); | ||
}, [from, to, duration, delay, decimalPlaces, format]); | ||
|
||
return nodeRef; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters