feat: pull to close modal window
This commit is contained in:
1 parent
5f5a7b9d87
commit
a903752f9d
3 files changed
+116
-23
No files matched your search
@@ -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<InstanceProps<void>> = ({ onResolve }) => {
|
||||
<div className='relative'>
|
||||
<Input
|
||||
ref={inputRef}
|
||||
autoFocus
|
||||
placeholder='Start entering title...'
|
||||
value={title}
|
||||
onChange={handleChangeTitle}
|
||||
@@ -50,6 +49,7 @@ const Search: FC<InstanceProps<void>> = ({ onResolve }) => {
|
||||
/>
|
||||
<Button
|
||||
variant='outline'
|
||||
type='button'
|
||||
size='icon'
|
||||
onClick={handleReset}
|
||||
disabled={!query.length}
|
||||
@@ -58,7 +58,7 @@ const Search: FC<InstanceProps<void>> = ({ onResolve }) => {
|
||||
<XCircle />
|
||||
</Button>
|
||||
</div>
|
||||
{Boolean(results?.pages.length) && (
|
||||
{Boolean(results?.pages.length && results?.pages?.[0]?.results?.length) && (
|
||||
<div className='mt-3'>
|
||||
<List
|
||||
pages={results!.pages as unknown as MovieDBResponse[]}
|
||||
|
||||
+86
-20
@@ -4,6 +4,7 @@ import * as React from 'react';
|
||||
import { useState } from 'react';
|
||||
import * as DialogPrimitive from '@radix-ui/react-dialog';
|
||||
|
||||
import useScrollDelta from '@/hooks/useScrollDelta';
|
||||
import { cn } from '@/lib/utils';
|
||||
|
||||
const Dialog = DialogPrimitive.Root;
|
||||
@@ -21,7 +22,7 @@ const DialogOverlay = React.forwardRef<
|
||||
<DialogPrimitive.Overlay
|
||||
ref={ref}
|
||||
className={cn(
|
||||
'fixed inset-0 z-50 overflow-auto bg-black/50 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0',
|
||||
'fixed inset-0 z-50 overflow-auto data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0',
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
@@ -33,24 +34,87 @@ DialogOverlay.displayName = DialogPrimitive.Overlay.displayName;
|
||||
|
||||
const DialogContent = React.forwardRef<
|
||||
React.ElementRef<typeof DialogPrimitive.Content>,
|
||||
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Content>
|
||||
>(({ className, children, ...props }, ref) => (
|
||||
<DialogPortal>
|
||||
<DialogOverlay>
|
||||
<DialogPrimitive.Content
|
||||
ref={ref}
|
||||
{...props}
|
||||
className={cn(
|
||||
'absolute top-[20vh] max-w-[1000px] w-[calc(100%-24px)] left-[50%] rounded-xl translate-x-[-50%] gap-4 shadow-lg data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[state=closed]:slide-out-to-left-1/2 data-[state=open]:slide-in-from-left-1/2 bg-background',
|
||||
className
|
||||
)}
|
||||
>
|
||||
{children}
|
||||
<div className='absolute top-full block w-10 h-[20vh] pointer-events-none' />
|
||||
</DialogPrimitive.Content>
|
||||
</DialogOverlay>
|
||||
</DialogPortal>
|
||||
));
|
||||
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Content> & { onClose?: () => void }
|
||||
>(({ className, children, onClose, ...props }, ref) => {
|
||||
const position = React.useRef(0);
|
||||
const close = React.useRef(false);
|
||||
const wrapperRef = React.useRef<HTMLDivElement>(null);
|
||||
const overlayRef = React.useRef<HTMLDivElement>(null);
|
||||
const contentRef = React.useRef<HTMLDivElement | null>(null);
|
||||
|
||||
const { delta, onScroll } = useScrollDelta();
|
||||
|
||||
const handleTouchStart = (event: React.TouchEvent<HTMLDivElement>) => {
|
||||
position.current = event.touches[0].clientY;
|
||||
};
|
||||
|
||||
const handleTouchMove = (event: React.TouchEvent<HTMLDivElement>) => {
|
||||
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 (
|
||||
<DialogPortal>
|
||||
<div className='fixed inset-0 w-full h-full bg-black/50 z-50' ref={wrapperRef}>
|
||||
<DialogOverlay ref={overlayRef} onScroll={onScroll}>
|
||||
<DialogPrimitive.Content
|
||||
onTouchStart={handleTouchStart}
|
||||
onTouchMove={handleTouchMove}
|
||||
onTouchEnd={handleTouchEnd}
|
||||
ref={handleRef}
|
||||
{...props}
|
||||
className={cn(
|
||||
'absolute top-[20vh] max-w-[1000px] w-[calc(100%-24px)] left-[50%] rounded-xl translate-x-[-50%] gap-4 shadow-lg data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[state=closed]:slide-out-to-left-1/2 data-[state=open]:slide-in-from-left-1/2 bg-background',
|
||||
className
|
||||
)}
|
||||
>
|
||||
{children}
|
||||
<div className='absolute top-full block w-10 h-[20vh] pointer-events-none' />
|
||||
</DialogPrimitive.Content>
|
||||
</DialogOverlay>
|
||||
</div>
|
||||
</DialogPortal>
|
||||
);
|
||||
});
|
||||
DialogContent.displayName = DialogPrimitive.Content.displayName;
|
||||
|
||||
const DialogHeader = ({ className, ...props }: React.HTMLAttributes<HTMLDivElement>) => (
|
||||
@@ -97,7 +161,9 @@ const Modal: React.FC<{
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={handleClose}>
|
||||
<DialogContent className={className}>{children}</DialogContent>
|
||||
<DialogContent forceMount onClose={handleClose} className={className}>
|
||||
{children}
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
};
|
||||
|
||||
@@ -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<HTMLDivElement, 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;
|
||||
}
|
||||
}, [])
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user