feat: Add center direction & unified transitions for true responsive dialogs
#10
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Introducing a
centerdirection and refactors the CSS/JS positioning logic to enable smooth, morphing transitions between all drawer directions (e.g.,bottom<->center).This allows developers to create responsive dialogs that maintain state (like form inputs) when resizing the window, offering a significantly better UX than the current standard practice.
Problem
Currently, the standard approach for "Responsive Dialogs" (as seen in the shadcn/ui examples) relies on conditional rendering:
Issues with this approach:
Drawerunmounts and theDialogmounts. Any state (e.g., text typed into an<input>is lost.💡 The Solution
Since
vaulis built on top of Radix/Base UI Dialog primitives, we can handle the "Desktop Modal" state directly withinvaulby adding acenterdirection.By unifying the CSS
transformlogic for all directions, we can now simply change thedirectionprop dynamically:🛠️ Changes
1. Unified Coordinate System
Previously, different directions used different CSS properties (
top,bottom,left,right) which made CSS transitions between them impossible.I refactored
style.cssso that all directions (including the newcenter) use a unifiedtransform: translate3d(...)approach relative to consistent offsets (e.g.,50%).2.
centerDirectionAdded support for
direction="center". This mimics a standard modal dialog behavior but keeps thevaulcontext alive.3. Morphing vs. Entry Animation
Added a
data-vaul-drawer-morphingattribute.4. Drag Logic Update
Updated the JS drag calculations (
onDrag,resetDrawer) to respect the new CSS offsets, ensuring dragging works correctly.Demo
I have added an responsive example to the example page.