Commit f8adadd1 authored by AFOC-1709091830\echou's avatar AFOC-1709091830\echou

improve animation

parent b163a558
import * as React from "react";
import {PullToRefresh} from "../src";
export interface BasicProps {
}
export interface BasicState {
}
export class Basic extends React.Component<BasicProps, BasicState> {
private onRefresh() {
return new Promise((resolve) => {
setTimeout(resolve, 2000);
});
}
public render() {
return (
<div style={{overflow: "scroll"}}>
<PullToRefresh
pullDownContent={<div style={{backgroundColor: "red"}}>pullDownContent</div>}
releaseContent={<div style={{backgroundColor: "red"}}>releaseContent</div>}
refreshContent={<div style={{backgroundColor: "red", height: "200px"}}>refreshContent</div>}
pullDownThreshold={200}
onRefresh={this.onRefresh}
>
<div style={{height: "95vh", textAlign: "center"}}>
PullToRefresh
</div>
</PullToRefresh>
</div>
);
}
}
...@@ -68,7 +68,6 @@ export class PullToRefresh extends React.Component<PullToRefreshProps, PullToRef ...@@ -68,7 +68,6 @@ export class PullToRefresh extends React.Component<PullToRefreshProps, PullToRef
this.startY = e["pageY"] || e.touches[0].pageY; this.startY = e["pageY"] || e.touches[0].pageY;
this.currentY = this.startY; this.currentY = this.startY;
this.container.style["willChange"] = "transform";
this.container.style.transition = "transform 0.2s cubic-bezier(0,0,0.31,1)"; this.container.style.transition = "transform 0.2s cubic-bezier(0,0,0.31,1)";
} }
...@@ -89,7 +88,7 @@ export class PullToRefresh extends React.Component<PullToRefreshProps, PullToRef ...@@ -89,7 +88,7 @@ export class PullToRefresh extends React.Component<PullToRefreshProps, PullToRef
}); });
} }
if (this.currentY - this.startY > this.state.maxPullDownDistance) { if (this.currentY - this.startY > this.state.maxPullDownDistance * 1.2) {
return; return;
} }
...@@ -103,25 +102,43 @@ export class PullToRefresh extends React.Component<PullToRefreshProps, PullToRef ...@@ -103,25 +102,43 @@ export class PullToRefresh extends React.Component<PullToRefreshProps, PullToRef
this.startY = 0; this.startY = 0;
this.currentY = 0; this.currentY = 0;
if (!this.state.pullToRefreshThresholdBreached) {
this.initContainer();
return;
}
this.container.style.overflow = "visible";
this.container.style.transform = `translate3d(0px, ${this.props.pullDownThreshold}px, 0px)`;
this.setState({ this.setState({
pullToRefreshThresholdBreached: false, pullToRefreshThresholdBreached: false,
onRefreshing: true, onRefreshing: true,
}, () => { }, () => {
this.props.onRefresh().then(() => { this.props.onRefresh().then(() => {
this.setState({onRefreshing: false}); this.setState({onRefreshing: false});
requestAnimationFrame(() => { this.initContainer();
this.container.style.overflow = "auto";
this.container.style.transform = "none";
this.container.style["willChange"] = "none";
});
}); });
}); });
} }
private initContainer() {
requestAnimationFrame(() => {
this.container.style.overflow = "auto";
this.container.style.transform = "none";
});
}
private renderPullDownContent() { private renderPullDownContent() {
const {releaseContent, pullDownContent, refreshContent} = this.props; const {releaseContent, pullDownContent, refreshContent, pullDownThreshold} = this.props;
const {onRefreshing, pullToRefreshThresholdBreached} = this.state; const {onRefreshing, pullToRefreshThresholdBreached} = this.state;
return onRefreshing ? refreshContent : pullToRefreshThresholdBreached ? releaseContent : pullDownContent; const content = onRefreshing ? refreshContent : pullToRefreshThresholdBreached ? releaseContent : pullDownContent;
const contentStyle: React.CSSProperties = {
height: `${pullDownThreshold}px`,
};
return (
<div style={contentStyle}>
{content}
</div>
);
} }
public render() { public render() {
...@@ -129,7 +146,7 @@ export class PullToRefresh extends React.Component<PullToRefreshProps, PullToRef ...@@ -129,7 +146,7 @@ export class PullToRefresh extends React.Component<PullToRefreshProps, PullToRef
const containerStyle: React.CSSProperties = { const containerStyle: React.CSSProperties = {
height: "auto", height: "auto",
overflow: "auto", overflow: "hidden",
WebkitOverflowScrolling: "touch", WebkitOverflowScrolling: "touch",
}; };
...@@ -137,7 +154,7 @@ export class PullToRefresh extends React.Component<PullToRefreshProps, PullToRef ...@@ -137,7 +154,7 @@ export class PullToRefresh extends React.Component<PullToRefreshProps, PullToRef
position: "absolute", position: "absolute",
left: 0, left: 0,
right: 0, right: 0,
top: (-1 * maxPullDownDistance), top: (-maxPullDownDistance),
}; };
return ( return (
...@@ -147,7 +164,9 @@ export class PullToRefresh extends React.Component<PullToRefreshProps, PullToRef ...@@ -147,7 +164,9 @@ export class PullToRefresh extends React.Component<PullToRefreshProps, PullToRef
{this.renderPullDownContent()} {this.renderPullDownContent()}
</div> </div>
</div> </div>
{this.props.children} <div style={{position: "relative", zIndex: 1}}>
{this.props.children}
</div>
</div> </div>
); );
} }
......
...@@ -2,44 +2,25 @@ ...@@ -2,44 +2,25 @@
import * as React from "react"; import * as React from "react";
import {render} from "react-dom"; import {render} from "react-dom";
import {AppContainer} from "react-hot-loader"; import {AppContainer} from "react-hot-loader";
import {PullToRefresh} from "./PullToRefresh"; import {Basic} from "../examples/Basic";
const onRefresh = () => { const renderExample = () => {
return new Promise((reslove) => {
setTimeout(reslove, 1000);
});
};
const renderMasterLayout = () => {
const root = document.getElementById("sample"); const root = document.getElementById("sample");
render( render(
<AppContainer> <AppContainer>
<div> <Basic />
<div>Header</div>
<PullToRefresh
pullDownContent={<div style={{backgroundColor: "red", height: "200px"}}>pullDownContent</div>}
releaseContent={<div style={{backgroundColor: "red", height: "200px"}}>releaseContent</div>}
refreshContent={<div style={{backgroundColor: "red", height: "200px"}}>refreshContent</div>}
pullDownThreshold={100}
onRefresh={onRefresh}
>
<div style={{backgroundColor: "green", height: "500px", color: "white", textAlign: "center"}}>
PullToRefresh
</div>
</PullToRefresh>
</div>
</AppContainer>, </AppContainer>,
root, root,
); );
}; };
renderMasterLayout(); renderExample();
// Hot Module Replacement API // Hot Module Replacement API
declare const module: { hot: any }; declare const module: { hot: any };
if (module.hot) { if (module.hot) {
module.hot.accept("./PullToRefresh", () => { module.hot.accept("./PullToRefresh", () => {
renderMasterLayout(); renderExample();
}); });
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment