pl-fe: adopt PTR library code

Signed-off-by: nicole mikołajczyk <git@mkljczk.pl>
This commit is contained in:
nicole mikołajczyk
2025-11-22 17:37:05 +01:00
parent 26c39ffa12
commit 02062ccc52
7 changed files with 307 additions and 51 deletions

View File

@ -126,7 +126,6 @@
"react-router-dom": "^5.3.4", "react-router-dom": "^5.3.4",
"react-router-dom-v5-compat": "^6.28.1", "react-router-dom-v5-compat": "^6.28.1",
"react-router-scroll-4": "^1.0.0-beta.2", "react-router-scroll-4": "^1.0.0-beta.2",
"react-simple-pull-to-refresh": "^1.3.3",
"react-sparklines": "^1.7.0", "react-sparklines": "^1.7.0",
"react-sticky-box": "^2.0.5", "react-sticky-box": "^2.0.5",
"react-swipeable-views": "^0.14.0", "react-swipeable-views": "^0.14.0",

View File

@ -9,8 +9,8 @@ const InlineStyle: React.FC<IInlineStyle> = ({ children }) => {
const sheet = useRef(document.adoptedStyleSheets ? new CSSStyleSheet() : document.createElement('style')); const sheet = useRef(document.adoptedStyleSheets ? new CSSStyleSheet() : document.createElement('style'));
useEffect(() => { useEffect(() => {
if (sheet.current) { const stylesheet = sheet.current;
const stylesheet = sheet.current; if (stylesheet) {
if (stylesheet instanceof CSSStyleSheet) { if (stylesheet instanceof CSSStyleSheet) {
stylesheet.replaceSync(children); stylesheet.replaceSync(children);
} else { } else {

View File

@ -1,43 +1,243 @@
import React from 'react'; /* eslint-disable compat/compat */
import PTRComponent from 'react-simple-pull-to-refresh'; /*
Copyright 2019 GUIBERT THOMAS
import Spinner from 'pl-fe/components/ui/spinner'; Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
interface IPullToRefresh { The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
// Adapted from [react-simple-pull-to-refresh](https://github.com/thmsgbrt/react-simple-pull-to-refresh)
import React, { useRef, useEffect } from 'react';
import Spinner from './ui/spinner';
enum DIRECTION {
UP = -0b01,
DOWN = 0b01,
}
const isOverflowScrollable = (element: HTMLElement): boolean => {
const overflowType: string = getComputedStyle(element).overflowY;
if (element === document.scrollingElement && overflowType === 'visible') {
return true;
}
if (overflowType !== 'scroll' && overflowType !== 'auto') {
return false;
}
return true;
};
const isScrollable = (element: HTMLElement, direction: DIRECTION): boolean => {
if (!isOverflowScrollable(element)) {
return false;
}
if (direction === DIRECTION.DOWN) {
const bottomScroll = element.scrollTop + element.clientHeight;
return bottomScroll < element.scrollHeight;
}
if (direction === DIRECTION.UP) {
return element.scrollTop > 0;
}
throw new Error('unsupported direction');
};
/**
* Returns whether a given element or any of its ancestors (up to rootElement) is scrollable in a given direction.
*/
const isTreeScrollable = (element: HTMLElement, direction: DIRECTION): boolean => {
if (isScrollable(element, direction)) {
return true;
}
if (element.parentElement === null) {
return false;
}
return isTreeScrollable(element.parentElement, direction);
};
interface PullToRefreshProps {
isPullable?: boolean;
onRefresh?: () => Promise<any>; onRefresh?: () => Promise<any>;
refreshingContent?: JSX.Element | string; refreshingContent?: JSX.Element | string;
pullingContent?: JSX.Element | string; pullingContent?: JSX.Element | string;
children: React.ReactNode; children: JSX.Element;
pullDownThreshold?: number;
maxPullDownDistance?: number;
resistance?: number;
backgroundColor?: string;
className?: string;
} }
/** const PullToRefresh: React.FC<PullToRefreshProps> = ({
* PullToRefresh: isPullable = true,
* Wrapper around a third-party PTR component with pl-fe defaults. onRefresh,
*/ refreshingContent = onRefresh ? <Spinner size={30} withText={false} /> : <></>,
const PullToRefresh: React.FC<IPullToRefresh> = ({ children, onRefresh, ...rest }): JSX.Element => { pullingContent = <></>,
const handleRefresh = () => { children,
if (onRefresh) { pullDownThreshold = 67,
return onRefresh(); maxPullDownDistance = 95, // max distance to scroll to trigger refresh
} else { resistance = 2,
// If not provided, do nothing backgroundColor,
return Promise.resolve(); className = '',
}) => {
const containerRef = useRef<HTMLDivElement>(null);
const childrenRef = useRef<HTMLDivElement>(null);
const pullDownRef = useRef<HTMLDivElement>(null);
let pullToRefreshThresholdBreached: boolean = false;
let isDragging: boolean = false;
let startY: number = 0;
let currentY: number = 0;
useEffect(() => {
if (!isPullable || !childrenRef || !childrenRef.current) return;
const childrenEl = childrenRef.current;
childrenEl.addEventListener('touchstart', onTouchStart, { passive: true });
childrenEl.addEventListener('mousedown', onTouchStart);
childrenEl.addEventListener('touchmove', onTouchMove, { passive: false });
childrenEl.addEventListener('mousemove', onTouchMove);
childrenEl.addEventListener('touchend', onEnd);
childrenEl.addEventListener('mouseup', onEnd);
document.body.addEventListener('mouseleave', onEnd);
return () => {
childrenEl.removeEventListener('touchstart', onTouchStart);
childrenEl.removeEventListener('mousedown', onTouchStart);
childrenEl.removeEventListener('touchmove', onTouchMove);
childrenEl.removeEventListener('mousemove', onTouchMove);
childrenEl.removeEventListener('touchend', onEnd);
childrenEl.removeEventListener('mouseup', onEnd);
document.body.removeEventListener('mouseleave', onEnd);
};
}, [
children,
isPullable,
onRefresh,
pullDownThreshold,
maxPullDownDistance,
]);
const initContainer = (): void => {
requestAnimationFrame(() => {
/**
* Reset Styles
*/
if (childrenRef.current) {
childrenRef.current.style.overflowX = 'hidden';
childrenRef.current.style.overflowY = 'auto';
childrenRef.current.style.transform = 'unset';
}
if (pullDownRef.current) {
pullDownRef.current.style.opacity = '0';
}
if (containerRef.current) {
containerRef.current.classList.remove('ptr--pull-down-treshold-breached');
containerRef.current.classList.remove('ptr--dragging');
containerRef.current.classList.remove('ptr--fetch-more-treshold-breached');
}
if (pullToRefreshThresholdBreached) pullToRefreshThresholdBreached = false;
});
};
const onTouchStart = (e: MouseEvent | TouchEvent): void => {
isDragging = false;
if (e instanceof MouseEvent) {
startY = e.pageY;
} }
if (window.TouchEvent && e instanceof TouchEvent) {
startY = e.touches[0].pageY;
}
currentY = startY;
// Check if element can be scrolled
if (e.type === 'touchstart' && isTreeScrollable(e.target as HTMLElement, DIRECTION.UP)) {
return;
}
// Top non visible so cancel
if (childrenRef.current!.getBoundingClientRect().top < 0) {
return;
}
isDragging = true;
};
const onTouchMove = (e: MouseEvent | TouchEvent): void => {
if (!isDragging) {
return;
}
if (window.TouchEvent && e instanceof TouchEvent) {
currentY = e.touches[0].pageY;
} else {
currentY = (e as MouseEvent).pageY;
}
containerRef.current!.classList.add('ptr--dragging');
if (currentY < startY) {
isDragging = false;
return;
}
if (e.cancelable) {
e.preventDefault();
}
const yDistanceMoved = Math.min((currentY - startY) / resistance, maxPullDownDistance);
// Limit to trigger refresh has been breached
if (yDistanceMoved >= pullDownThreshold) {
isDragging = true;
pullToRefreshThresholdBreached = true;
containerRef.current!.classList.remove('ptr--dragging');
containerRef.current!.classList.add('ptr--pull-down-treshold-breached');
}
// maxPullDownDistance breached, stop the animation
if (yDistanceMoved >= maxPullDownDistance) {
return;
}
pullDownRef.current!.style.opacity = ((yDistanceMoved) / 65).toString();
childrenRef.current!.style.overflow = 'visible';
childrenRef.current!.style.transform = `translate(0px, ${yDistanceMoved}px)`;
pullDownRef.current!.style.visibility = 'visible';
};
const onEnd = (): void => {
isDragging = false;
startY = 0;
currentY = 0;
// Container has not been dragged enough, put it back to it's initial state
if (!pullToRefreshThresholdBreached) {
if (pullDownRef.current) pullDownRef.current.style.visibility = 'hidden';
initContainer();
return;
}
if (childrenRef.current) {
childrenRef.current.style.overflow = 'visible';
childrenRef.current.style.transform = `translate(0px, ${pullDownThreshold}px)`;
}
onRefresh?.().then(initContainer).catch(initContainer);
}; };
return ( return (
<PTRComponent <div className={`ptr ${className}`} style={{ backgroundColor }} ref={containerRef}>
onRefresh={handleRefresh} <div className='ptr__pull-down' ref={pullDownRef}>
pullingContent={<></>} <div className='ptr__loader ptr__pull-down--loading'>{refreshingContent}</div>
// `undefined` will fallback to the default, while `<></>` will render nothing <div className='ptr__pull-down--pull-more'>{pullingContent}</div>
refreshingContent={onRefresh ? <Spinner size={30} withText={false} /> : <></>} </div>
pullDownThreshold={67} <div className='ptr__children' ref={childrenRef}>
maxPullDownDistance={95} {children}
resistance={2} </div>
{...rest} </div>
>
{/* This thing really wants a single JSX element as its child (TypeScript), so wrap it in one */}
<>{children}</>
</PTRComponent>
); );
}; };

View File

@ -17,5 +17,6 @@
@use 'utilities'; @use 'utilities';
@use 'components/datepicker'; @use 'components/datepicker';
@use 'mfm'; @use 'mfm';
@use 'ptr';
@use 'new/index'; @use 'new/index';

View File

@ -53,8 +53,3 @@
@apply w-12 h-12 border-0 opacity-0 bg-transparent; @apply w-12 h-12 border-0 opacity-0 bg-transparent;
} }
} }
.ptr,
.ptr__children {
@apply overflow-visible #{!important};
}

View File

@ -0,0 +1,75 @@
// Adapted from react-simple-pull-to-refresh, licensed under MIT License.
// https://github.com/thmsgbrt/react-simple-pull-to-refresh/blob/master/LICENCE
.ptr,
.ptr__children {
height: 100%;
width: 100%;
overflow: visible;
-webkit-overflow-scrolling: touch;
position: relative;
}
/**
* Pull down transition
*/
.ptr__children,
.ptr__pull-down {
transition: transform 0.2s cubic-bezier(0, 0, 0.31, 1);
}
.ptr__pull-down {
position: absolute;
overflow: hidden;
left: 0;
right: 0;
top: 0;
visibility: hidden;
> div {
display: none;
}
.spinner {
margin-top: 0.5rem;
}
}
.ptr--dragging {
/**
* Hide PullMore content is treshold breached
*/
&.ptr--pull-down-treshold-breached {
.ptr__pull-down--pull-more {
display: none;
}
}
/**
* Otherwize, display content
*/
.ptr__pull-down--pull-more {
display: block;
}
}
.ptr--pull-down-treshold-breached {
/**
* Force opacity to 1 is pull down trashold breached
*/
.ptr__pull-down {
opacity: 1 !important;
}
/**
* And display loader
*/
.ptr__pull-down--loading {
display: block;
}
}
.ptr__loader {
margin: 0 auto;
text-align: center;
}

14
pnpm-lock.yaml generated
View File

@ -379,9 +379,6 @@ importers:
react-router-scroll-4: react-router-scroll-4:
specifier: ^1.0.0-beta.2 specifier: ^1.0.0-beta.2
version: 1.0.0-beta.2(prop-types@15.8.1)(react-dom@18.3.1(react@18.3.1))(react-router-dom@5.3.4(react@18.3.1))(react@18.3.1) version: 1.0.0-beta.2(prop-types@15.8.1)(react-dom@18.3.1(react@18.3.1))(react-router-dom@5.3.4(react@18.3.1))(react@18.3.1)
react-simple-pull-to-refresh:
specifier: ^1.3.3
version: 1.3.3(react-dom@18.3.1(react@18.3.1))(react@18.3.1)
react-sparklines: react-sparklines:
specifier: ^1.7.0 specifier: ^1.7.0
version: 1.7.0(react-dom@18.3.1(react@18.3.1))(react@18.3.1) version: 1.7.0(react-dom@18.3.1(react@18.3.1))(react@18.3.1)
@ -5541,12 +5538,6 @@ packages:
peerDependencies: peerDependencies:
react: '>=16.8' react: '>=16.8'
react-simple-pull-to-refresh@1.3.3:
resolution: {integrity: sha512-6qXsa5RtNVmKJhLWvDLIX8UK51HFtCEGjdqQGf+M1Qjrcc4qH4fki97sgVpGEFBRwbY7DiVDA5N5p97kF16DTw==}
peerDependencies:
react: ^16.10.2 || ^17.0.0 || ^18.0.0
react-dom: ^16.10.2 || ^17.0.0 || ^18.0.0
react-sparklines@1.7.0: react-sparklines@1.7.0:
resolution: {integrity: sha512-bJFt9K4c5Z0k44G8KtxIhbG+iyxrKjBZhdW6afP+R7EnIq+iKjbWbEFISrf3WKNFsda+C46XAfnX0StS5fbDcg==} resolution: {integrity: sha512-bJFt9K4c5Z0k44G8KtxIhbG+iyxrKjBZhdW6afP+R7EnIq+iKjbWbEFISrf3WKNFsda+C46XAfnX0StS5fbDcg==}
peerDependencies: peerDependencies:
@ -12335,11 +12326,6 @@ snapshots:
'@remix-run/router': 1.23.0 '@remix-run/router': 1.23.0
react: 18.3.1 react: 18.3.1
react-simple-pull-to-refresh@1.3.3(react-dom@18.3.1(react@18.3.1))(react@18.3.1):
dependencies:
react: 18.3.1
react-dom: 18.3.1(react@18.3.1)
react-sparklines@1.7.0(react-dom@18.3.1(react@18.3.1))(react@18.3.1): react-sparklines@1.7.0(react-dom@18.3.1(react@18.3.1))(react@18.3.1):
dependencies: dependencies:
prop-types: 15.8.1 prop-types: 15.8.1