Skip to content
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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions UNRELEASED.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,8 @@
- Changed `aria-labelledby` to always exist on `TextField` ([#2401](https://github.com/Shopify/polaris-react/pull/2401))
- Converted `ButtonGroup > Item` into a functional component ([#2441](https://github.com/Shopify/polaris-react/pull/2441))
- Refactored BulkActions to make use of `ButtonGroup` ([#2441](https://github.com/Shopify/polaris-react/pull/2441))
- Migrated `Loading` to use hooks ([#2303](https://github.com/Shopify/polaris-react/pull/2303))

### Deprecations

### Deprecations
101 changes: 40 additions & 61 deletions src/components/Frame/components/Loading/Loading.tsx
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) {
Copy link
Member

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.

Copy link
Contributor Author

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 and previousProgress 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 and previousProgress are not equal which save redundant re-rendering. if we set currentProgress in state, it'd re-render more often (due its decimals changes with each requestAnimationFrame call)

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);
Copy link
Member

Choose a reason for hiding this comment

The 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
animation = requestAnimationFrame(increment);
return requestAnimationFrame(increment);

Then below set the value of animation

const animation = increment();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should track latest animation for calling cancelAnimationFrame in un-mounting, I'm not sure if const animation = increment(); will works.
should we use ref?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just chiming in const animation = increment(); won't keep track of the recursed animation as the assignment will

};

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>
);
}
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';
29 changes: 25 additions & 4 deletions src/components/Frame/components/Loading/tests/Loading.test.tsx
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++) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this a good approach?
we can also change 100 to 100,000 to have a 100% code coverage but it took ~7 seconds in my computer...

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);
});
});
2 changes: 1 addition & 1 deletion src/components/Frame/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export {

export {ToastManager, ToastManagerProps} from './ToastManager';

export {Loading, LoadingProps} from './Loading';
export {Loading} from './Loading';

export {ContextualSaveBar} from './ContextualSaveBar';

Expand Down