2022-03-21 11:09:01 -07:00
|
|
|
import React from 'react';
|
|
|
|
import PTRComponent from 'react-simple-pull-to-refresh';
|
|
|
|
|
|
|
|
import { Spinner } from 'soapbox/components/ui';
|
|
|
|
|
|
|
|
interface IPullToRefresh {
|
2022-05-28 09:02:04 -07:00
|
|
|
onRefresh?: () => Promise<any>;
|
|
|
|
refreshingContent?: JSX.Element | string;
|
|
|
|
pullingContent?: JSX.Element | string;
|
2022-03-21 11:09:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PullToRefresh:
|
|
|
|
* Wrapper around a third-party PTR component with Soapbox defaults.
|
|
|
|
*/
|
2022-04-04 12:17:24 -07:00
|
|
|
const PullToRefresh: React.FC<IPullToRefresh> = ({ children, onRefresh, ...rest }): JSX.Element => {
|
2022-03-21 11:09:01 -07:00
|
|
|
const handleRefresh = () => {
|
|
|
|
if (onRefresh) {
|
|
|
|
return onRefresh();
|
|
|
|
} else {
|
|
|
|
// If not provided, do nothing
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<PTRComponent
|
|
|
|
onRefresh={handleRefresh}
|
2022-03-24 12:27:27 -07:00
|
|
|
pullingContent={<></>}
|
|
|
|
// `undefined` will fallback to the default, while `<></>` will render nothing
|
|
|
|
refreshingContent={onRefresh ? <Spinner size={30} withText={false} /> : <></>}
|
2022-03-21 11:09:01 -07:00
|
|
|
pullDownThreshold={67}
|
|
|
|
maxPullDownDistance={95}
|
|
|
|
resistance={2}
|
|
|
|
{...rest}
|
|
|
|
>
|
2022-04-04 12:17:24 -07:00
|
|
|
{/* This thing really wants a single JSX element as its child (TypeScript), so wrap it in one */}
|
|
|
|
<>{children}</>
|
2022-03-21 11:09:01 -07:00
|
|
|
</PTRComponent>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default PullToRefresh;
|