Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve scrolling and performance #88

Merged
merged 2 commits into from
Sep 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ function Root({
const keyboardIsOpen = React.useRef(false);
const previousDiffFromInitial = React.useRef(0);
const drawerRef = React.useRef<HTMLDivElement>(null);
const drawerHeightRef = React.useRef(drawerRef.current?.getBoundingClientRect().height || 0);
const { onDrag: changeThemeColorOnDrag, onRelease: themeTransitionOnRelease } = useSafariThemeColor(
drawerRef,
overlayRef,
Expand Down Expand Up @@ -130,7 +131,7 @@ function Root({
if (!dismissible) return;

if (drawerRef.current && !drawerRef.current.contains(event.target as Node)) return;

drawerHeightRef.current = drawerRef.current?.getBoundingClientRect().height || 0;
setIsDragging(true);
dragStartTime.current = new Date();

Expand All @@ -146,6 +147,10 @@ function Root({
const highlightedText = window.getSelection()?.toString();
const swipeAmount = drawerRef.current ? getTranslateY(drawerRef.current) : null;

if (swipeAmount > 0) {
return true;
}

// Don't drag if there's highlighted text
if (highlightedText && highlightedText.length > 0) {
return false;
Expand Down Expand Up @@ -175,7 +180,7 @@ function Root({
return false;
}

if (isDraggingDown && element !== document.body && !swipeAmount && swipeAmount !== 0) {
if (isDraggingDown && element !== document.body && !swipeAmount && swipeAmount >= 0) {
lastTimeDragPrevented.current = new Date();

// Element is scrolled to the top, but we are dragging down so we should allow scrolling
Expand All @@ -199,8 +204,6 @@ function Root({

if (!shouldDrag(event.target, isDraggingDown)) return;

const drawerHeight = drawerRef.current?.getBoundingClientRect().height || 0;

set(drawerRef.current, {
transition: 'none',
});
Expand All @@ -216,7 +219,6 @@ function Root({
// Run this only if snapPoints are not defined or if we are at the last snap point (highest one)
if (draggedDistance > 0 && !snapPoints) {
const dampenedDraggedDistance = dampenValue(draggedDistance);
console.log(Math.min(dampenedDraggedDistance * -1, 0), 0);

set(drawerRef.current, {
transform: `translate3d(0, ${Math.min(dampenedDraggedDistance * -1, 0)}px, 0)`,
Expand All @@ -228,7 +230,7 @@ function Root({
const absDraggedDistance = Math.abs(draggedDistance);
const wrapper = document.querySelector('[vaul-drawer-wrapper]');

let percentageDragged = absDraggedDistance / drawerHeight;
let percentageDragged = absDraggedDistance / drawerHeightRef.current;
const snapPointPercentageDragged = getSnapPointsPercentageDragged(absDraggedDistance, isDraggingDown);

if (snapPointPercentageDragged !== null) {
Expand Down