Skip to content

Commit

Permalink
start countup when component in viewport
Browse files Browse the repository at this point in the history
  • Loading branch information
razvanMiu committed Sep 16, 2022
1 parent d078018 commit 6865929
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"dependencies": {
"@plone/scripts": "*",
"react-countup": "6.3.1",
"react-visibility-sensor": "5.1.1",
"volto-slate": "*"
},
"devDependencies": {
Expand Down
24 changes: 23 additions & 1 deletion src/StatisticBlock/View.jsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,32 @@
import React from 'react';
import cx from 'classnames';
import CountUp from 'react-countup';
import VisibilitySensor from 'react-visibility-sensor';
import { Statistic } from 'semantic-ui-react';
import { UniversalLink } from '@plone/volto/components';
import { serializeText } from '@eeacms/volto-statistic-block/helpers';

import './styles.less';

const CountUpWrapper = ({ countUpRef, start }) => {
const [visible, setVisible] = React.useState(false);

return (
<VisibilitySensor
onChange={(isVisible) => {
start();
if (isVisible && !visible) {
setVisible(true);
}
}}
active={!visible}
delayedCall
>
<span ref={countUpRef} />
</VisibilitySensor>
);
};

const View = ({ data, mode }) => {
const {
horizontal = false,
Expand Down Expand Up @@ -64,7 +84,9 @@ const View = ({ data, mode }) => {
decimals={animation.decimals > 0 ? animation.decimals : 0}
prefix={animation.prefix || ''}
suffix={animation.suffix || ''}
/>
>
{(props) => <CountUpWrapper {...props} />}
</CountUp>
</Statistic.Value>
<Statistic.Label className={cx(labelVariation)}>
{item.label}
Expand Down

0 comments on commit 6865929

Please sign in to comment.