From a903752f9d0530c54777a0330ddb1e0830038a23 Mon Sep 17 00:00:00 2001 From: ner Date: Wed, 3 Apr 2024 20:20:12 +0300 Subject: [PATCH] feat: pull to close modal window --- components/Header/components/Search/index.tsx | 6 +- components/ui/dialog.tsx | 106 ++++++++++++++---- hooks/useScrollDelta.ts | 27 +++++ 3 files changed, 116 insertions(+), 23 deletions(-) create mode 100644 hooks/useScrollDelta.ts diff --git a/components/Header/components/Search/index.tsx b/components/Header/components/Search/index.tsx index 48b163f..e9eb3a5 100644 --- a/components/Header/components/Search/index.tsx +++ b/components/Header/components/Search/index.tsx @@ -1,6 +1,6 @@ 'use client'; -import { ChangeEvent, FC, useRef, useState } from 'react'; +import { ChangeEvent, FC, useEffect, useRef, useState } from 'react'; import { create, InstanceProps } from 'react-modal-promise'; import { Search as SearchIcon, XCircle } from 'lucide-react'; @@ -42,7 +42,6 @@ const Search: FC> = ({ onResolve }) => {
> = ({ onResolve }) => { />
- {Boolean(results?.pages.length) && ( + {Boolean(results?.pages.length && results?.pages?.[0]?.results?.length) && (
, - React.ComponentPropsWithoutRef ->(({ className, children, ...props }, ref) => ( - - - - {children} -
- - - -)); + React.ComponentPropsWithoutRef & { onClose?: () => void } +>(({ className, children, onClose, ...props }, ref) => { + const position = React.useRef(0); + const close = React.useRef(false); + const wrapperRef = React.useRef(null); + const overlayRef = React.useRef(null); + const contentRef = React.useRef(null); + + const { delta, onScroll } = useScrollDelta(); + + const handleTouchStart = (event: React.TouchEvent) => { + position.current = event.touches[0].clientY; + }; + + const handleTouchMove = (event: React.TouchEvent) => { + const threshold = 150; + + const { clientY } = event.touches[0]; + const content = contentRef.current; + const overlay = overlayRef.current; + const wrapper = wrapperRef.current; + const diff = clientY - position.current; + const top = overlay?.scrollTop || 0; + const isTop = top === 0; + const canBeClosed = diff > threshold; + + close.current = canBeClosed && isTop; + + if (isTop && content && wrapper) { + const opacity = `${1 - (diff * 100) / threshold / 100}`; + + content.style.opacity = opacity; + wrapper.style.opacity = opacity; + content.style.transform = `translate(-50%, ${diff}px)`; + } + + if (top <= 0 && delta.current < 0 && canBeClosed) onClose?.(); + }; + + const handleTouchEnd = () => { + if (close.current) return onClose?.(); + + close.current = false; + + if (!contentRef.current || !wrapperRef.current) return; + + contentRef.current.style.opacity = '1'; + wrapperRef.current.style.opacity = '1'; + contentRef.current.style.transform = 'translate(-50%, 0)'; + }; + + const handleRef = (element: HTMLDivElement | null) => { + if (!element) return; + + (ref as (element: HTMLElement | null) => void)?.(element); + contentRef.current = element; + }; + + return ( + +
+ + + {children} +
+ + +
+ + ); +}); DialogContent.displayName = DialogPrimitive.Content.displayName; const DialogHeader = ({ className, ...props }: React.HTMLAttributes) => ( @@ -97,7 +161,9 @@ const Modal: React.FC<{ return ( - {children} + + {children} + ); }; diff --git a/hooks/useScrollDelta.ts b/hooks/useScrollDelta.ts new file mode 100644 index 0000000..90ea1f0 --- /dev/null +++ b/hooks/useScrollDelta.ts @@ -0,0 +1,27 @@ +import { useCallback, useRef } from 'react'; + +export default function useScrollDelta() { + const lastKnownScrollPosition = useRef(0); + const deltaY = useRef(0); + + return { + delta: deltaY, + onScroll: useCallback((scroll: React.UIEvent) => { + let ticking = false; + + if (!ticking) { + window.requestAnimationFrame(() => { + const top = (scroll.target as HTMLDivElement).scrollTop; + + deltaY.current = top - lastKnownScrollPosition.current; + + lastKnownScrollPosition.current = top; + + ticking = false; + }); + + ticking = true; + } + }, []) + }; +}