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

fix: opacity calculation #447

Merged
merged 1 commit into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
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
6 changes: 5 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ export function Root({
const previousDiffFromInitial = React.useRef(0);
const drawerRef = React.useRef<HTMLDivElement>(null);
const drawerHeightRef = React.useRef(drawerRef.current?.getBoundingClientRect().height || 0);
const drawerWidthRef = React.useRef(drawerRef.current?.getBoundingClientRect().width || 0);
const initialDrawerHeight = React.useRef(0);

const onSnapPointChange = React.useCallback((activeSnapPointIndex: number) => {
Expand Down Expand Up @@ -187,6 +188,7 @@ export function Root({
if (drawerRef.current && !drawerRef.current.contains(event.target as Node)) return;

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

Expand Down Expand Up @@ -288,9 +290,11 @@ export function Root({
// We need to capture last time when drag with scroll was triggered and have a timeout between
const absDraggedDistance = Math.abs(draggedDistance);
const wrapper = document.querySelector('[data-vaul-drawer-wrapper]');
const drawerDimension =
direction === 'bottom' || direction === 'top' ? drawerHeightRef.current : drawerWidthRef.current;

// Calculate the percentage dragged, where 1 is the closed position
let percentageDragged = absDraggedDistance / drawerHeightRef.current;
let percentageDragged = absDraggedDistance / drawerDimension;
const snapPointPercentageDragged = getSnapPointsPercentageDragged(absDraggedDistance, isDraggingInDirection);

if (snapPointPercentageDragged !== null) {
Expand Down
2 changes: 1 addition & 1 deletion test/src/app/scrollable-with-inputs/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Drawer } from 'vaul';
export default function Page() {
return (
<div className="w-screen h-screen bg-white p-8 flex justify-center items-center" data-vaul-drawer-wrapper="">
<Drawer.Root autoFocus={true}>
<Drawer.Root>
<Drawer.Trigger asChild>
<button>Open Drawer</button>
</Drawer.Trigger>
Expand Down
Loading