-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Frame] hookify Loading #2303
[Frame] hookify Loading #2303
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,78 +1,57 @@ | ||||||
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.1); | ||||||
|
||||||
private ariaValuenow = debounce(() => { | ||||||
const {progress} = this.state; | ||||||
return Math.floor(progress / 10) * 10; | ||||||
}, 15); | ||||||
useEffect(() => { | ||||||
let animation: number; | ||||||
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); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Setting the value of a variable outside of the current scope is surpising and we should try and avoid such mutation if possible. Instead of setting the value of an variable outside the current scope, return the value from requestAnimationFrame
Suggested change
Then below set the value of animation
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should track latest There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just chiming in |
||||||
}; | ||||||
|
||||||
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 () => { | ||||||
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> | ||||||
); | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export {Loading, LoadingProps} from './Loading'; | ||
export {Loading} from './Loading'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,38 @@ | ||
import React from 'react'; | ||
import {mountWithAppProvider} from 'test-utilities/legacy'; | ||
import {animationFrame} from '@shopify/jest-dom-mocks'; | ||
import {act} from 'react-dom/test-utils'; | ||
import {Loading} from '../Loading'; | ||
|
||
describe('<Loading />', () => { | ||
const loading = mountWithAppProvider(<Loading />); | ||
beforeEach(() => { | ||
animationFrame.mock(); | ||
}); | ||
|
||
afterEach(() => { | ||
animationFrame.restore(); | ||
}); | ||
|
||
it('increases over time', () => { | ||
const loading = mountWithAppProvider(<Loading />); | ||
|
||
it('mounts', () => { | ||
expect(loading.exists()).toBe(true); | ||
for (let i = 0; i <= 100; i++) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this a good approach? |
||
act(() => animationFrame.runFrame()); | ||
} | ||
|
||
loading.update(); | ||
|
||
expect(loading.find('.Level').prop('aria-valuenow')).toBe(26); | ||
}); | ||
|
||
it('unmounts safely', () => { | ||
it('cancels the animationFrame on unmount', () => { | ||
const cancelAnimationFrameSpy = jest.spyOn(window, 'cancelAnimationFrame'); | ||
const loading = mountWithAppProvider(<Loading />); | ||
|
||
expect(() => { | ||
loading.unmount(); | ||
}).not.toThrow(); | ||
|
||
expect(cancelAnimationFrameSpy).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You seem to have changed the progress from being an integer between 0 an 100, to a number rounded to 2 decimal places between 0 and 1. I'm ok with that, but make sure that you update the stuck threshold and anything else like how the next step is calculated to account for that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
currentProgress
still go from 0 - 100 with not limited decimal point (e.g. 20, 21.332232332, 22.12321, 23.5565, etc)nextProgress
andpreviousProgress
goes from 0 - 1 with only two decimal point (e.g. 0.12, 0.13, 0.14, 0.15)with this approach set state called only when
nextProgress
andpreviousProgress
are not equal which save redundant re-rendering. if we setcurrentProgress
in state, it'd re-render more often (due its decimals changes with eachrequestAnimationFrame
call)