Skip to content

Commit

Permalink
hookify Frame Loading
Browse files Browse the repository at this point in the history
  • Loading branch information
sijad committed Nov 23, 2019
1 parent 89d0f27 commit 5b04ed5
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 62 deletions.
103 changes: 42 additions & 61 deletions src/components/Frame/components/Loading/Loading.tsx
Original file line number Diff line number Diff line change
@@ -1,78 +1,59 @@
import React from 'react';
import debounce from 'lodash/debounce';
import React, {useEffect, useState} from 'react';
import styles from './Loading.scss';

export interface LoadingProps {}

interface State {
progress: number;
step: number;
animation: number | null;
}

const INITIAL_STEP = 10;
const STUCK_THRESHOLD = 99;

export class Loading extends React.Component<LoadingProps, State> {
state: State = {
progress: 0,
step: INITIAL_STEP,
animation: null,
};
export function Loading() {
const [progress, setProgress] = useState(0);

private ariaValuenow = debounce(() => {
const {progress} = this.state;
return Math.floor(progress / 10) * 10;
}, 15);
useEffect(() => {
let animation: number | undefined;
let step = INITIAL_STEP;
let currentProgress = 0;
let previousProgress = 0;

componentDidMount() {
this.increment();
}
const increment = () => {
if (currentProgress >= STUCK_THRESHOLD) {
return;
}

componentWillUnmount() {
const {animation} = this.state;
currentProgress = Math.min(currentProgress + step, 100);
const nextProgress = Math.floor(currentProgress) / 100;

if (animation != null) {
cancelAnimationFrame(animation);
}
}
if (nextProgress !== previousProgress) {
setProgress(nextProgress);
previousProgress = nextProgress;
}

render() {
const {progress} = this.state;
step = INITIAL_STEP ** -(currentProgress / 25);

const customStyles = {
transform: `scaleX(${Math.floor(progress) / 100})`,
animation = requestAnimationFrame(increment);
};

const ariaValuenow = this.ariaValuenow();
increment();

return (
<div className={styles.Loading}>
<div
className={styles.Level}
style={customStyles}
aria-valuenow={ariaValuenow}
aria-valuemin={0}
aria-valuemax={100}
role="progressbar"
/>
</div>
);
}

private increment() {
const {progress, step} = this.state;

if (progress >= STUCK_THRESHOLD) {
return;
}
return () => {
if (animation) {
cancelAnimationFrame(animation);
}
};
}, []);

const animation = requestAnimationFrame(() => this.increment());
const customStyles = {
transform: `scaleX(${progress})`,
};

this.setState({
progress: Math.min(progress + step, 100),
step: INITIAL_STEP ** -(progress / 25),
animation,
});
}
return (
<div className={styles.Loading}>
<div
className={styles.Level}
style={customStyles}
aria-valuenow={progress * 100}
aria-valuemin={0}
aria-valuemax={100}
role="progressbar"
/>
</div>
);
}
2 changes: 1 addition & 1 deletion src/components/Frame/components/Loading/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export {Loading, LoadingProps} from './Loading';
export {Loading} from './Loading';

0 comments on commit 5b04ed5

Please sign in to comment.