diff --git a/packages/pl-fe/package.json b/packages/pl-fe/package.json index 22d03e697..e0cd34ae0 100644 --- a/packages/pl-fe/package.json +++ b/packages/pl-fe/package.json @@ -126,7 +126,6 @@ "react-router-dom": "^5.3.4", "react-router-dom-v5-compat": "^6.28.1", "react-router-scroll-4": "^1.0.0-beta.2", - "react-simple-pull-to-refresh": "^1.3.3", "react-sparklines": "^1.7.0", "react-sticky-box": "^2.0.5", "react-swipeable-views": "^0.14.0", diff --git a/packages/pl-fe/src/components/inline-style.tsx b/packages/pl-fe/src/components/inline-style.tsx index 3f6d5ed70..ff711f533 100644 --- a/packages/pl-fe/src/components/inline-style.tsx +++ b/packages/pl-fe/src/components/inline-style.tsx @@ -9,8 +9,8 @@ const InlineStyle: React.FC = ({ children }) => { const sheet = useRef(document.adoptedStyleSheets ? new CSSStyleSheet() : document.createElement('style')); useEffect(() => { - if (sheet.current) { - const stylesheet = sheet.current; + const stylesheet = sheet.current; + if (stylesheet) { if (stylesheet instanceof CSSStyleSheet) { stylesheet.replaceSync(children); } else { diff --git a/packages/pl-fe/src/components/pull-to-refresh.tsx b/packages/pl-fe/src/components/pull-to-refresh.tsx index 644c772d3..33fbc4eba 100644 --- a/packages/pl-fe/src/components/pull-to-refresh.tsx +++ b/packages/pl-fe/src/components/pull-to-refresh.tsx @@ -1,43 +1,243 @@ -import React from 'react'; -import PTRComponent from 'react-simple-pull-to-refresh'; +/* eslint-disable compat/compat */ +/* +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; refreshingContent?: JSX.Element | string; pullingContent?: JSX.Element | string; - children: React.ReactNode; + children: JSX.Element; + pullDownThreshold?: number; + maxPullDownDistance?: number; + resistance?: number; + backgroundColor?: string; + className?: string; } -/** - * PullToRefresh: - * Wrapper around a third-party PTR component with pl-fe defaults. - */ -const PullToRefresh: React.FC = ({ children, onRefresh, ...rest }): JSX.Element => { - const handleRefresh = () => { - if (onRefresh) { - return onRefresh(); - } else { - // If not provided, do nothing - return Promise.resolve(); +const PullToRefresh: React.FC = ({ + isPullable = true, + onRefresh, + refreshingContent = onRefresh ? : <>, + pullingContent = <>, + children, + pullDownThreshold = 67, + maxPullDownDistance = 95, // max distance to scroll to trigger refresh + resistance = 2, + backgroundColor, + className = '', +}) => { + const containerRef = useRef(null); + const childrenRef = useRef(null); + const pullDownRef = useRef(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 ( - } - // `undefined` will fallback to the default, while `<>` will render nothing - refreshingContent={onRefresh ? : <>} - pullDownThreshold={67} - maxPullDownDistance={95} - resistance={2} - {...rest} - > - {/* This thing really wants a single JSX element as its child (TypeScript), so wrap it in one */} - <>{children} - +
+
+
{refreshingContent}
+
{pullingContent}
+
+
+ {children} +
+
); }; diff --git a/packages/pl-fe/src/styles/application.scss b/packages/pl-fe/src/styles/application.scss index 29661b3ce..1c0dd846e 100644 --- a/packages/pl-fe/src/styles/application.scss +++ b/packages/pl-fe/src/styles/application.scss @@ -17,5 +17,6 @@ @use 'utilities'; @use 'components/datepicker'; @use 'mfm'; +@use 'ptr'; @use 'new/index'; \ No newline at end of file diff --git a/packages/pl-fe/src/styles/loading.scss b/packages/pl-fe/src/styles/loading.scss index f52e8dee1..9fa0d38d2 100644 --- a/packages/pl-fe/src/styles/loading.scss +++ b/packages/pl-fe/src/styles/loading.scss @@ -53,8 +53,3 @@ @apply w-12 h-12 border-0 opacity-0 bg-transparent; } } - -.ptr, -.ptr__children { - @apply overflow-visible #{!important}; -} diff --git a/packages/pl-fe/src/styles/ptr.scss b/packages/pl-fe/src/styles/ptr.scss new file mode 100644 index 000000000..62c655cf2 --- /dev/null +++ b/packages/pl-fe/src/styles/ptr.scss @@ -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; +} \ No newline at end of file diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index faf288f5c..8b48b8a74 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -379,9 +379,6 @@ importers: react-router-scroll-4: 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) - 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: specifier: ^1.7.0 version: 1.7.0(react-dom@18.3.1(react@18.3.1))(react@18.3.1) @@ -5541,12 +5538,6 @@ packages: peerDependencies: 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: resolution: {integrity: sha512-bJFt9K4c5Z0k44G8KtxIhbG+iyxrKjBZhdW6afP+R7EnIq+iKjbWbEFISrf3WKNFsda+C46XAfnX0StS5fbDcg==} peerDependencies: @@ -12335,11 +12326,6 @@ snapshots: '@remix-run/router': 1.23.0 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): dependencies: prop-types: 15.8.1