Skip to content

Commit 63f9472

Browse files
committed
fix: maintain same dragabble dimensions on the overlay
1 parent dc10bf4 commit 63f9472

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

packages/dragswag/src/react/drag-overlay.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,17 @@ function DragOverlay({ ref, style, children: dragElement, ...props }: React.Comp
2121
bottom: 0,
2222
left: 0,
2323
right: 0,
24+
pointerEvents: 'none' as const,
25+
zIndex: 9999,
2426
display: dragElement ? 'block' : 'none',
2527
...style,
2628
}
2729

2830
const dragWrapperStyle = {
29-
position: 'relative' as const,
31+
position: 'absolute' as const,
3032
transform: `translateX(0px) translateY(0px)`,
3133
willChange: 'transform',
34+
pointerEvents: 'none' as const,
3235
}
3336

3437
return (
@@ -44,7 +47,6 @@ const DragOverlayContext = createContext<DragOverlayContextType | null>(null)
4447

4548
export function DragOverlayProvider({ children, ...rest }: DragOverlayProviderProps) {
4649
const [dragElement, setDragElement] = useState<ReactElement | null>(null)
47-
// const [dragElement2, setDragElement2] = [null, () => {}]
4850
const dragWrapperRef = useRef<HTMLDivElement>(null)
4951

5052
const setDragElementPosition = (position: { top: number; left: number }) => {
@@ -54,12 +56,12 @@ export function DragOverlayProvider({ children, ...rest }: DragOverlayProviderPr
5456
}
5557

5658
return (
57-
<DragOverlayContext value={{ dragElement, setDragElement, setDragElementPosition }}>
59+
<DragOverlayContext.Provider value={{ dragElement, setDragElement, setDragElementPosition }}>
5860
{children}
5961
<DragOverlay ref={dragWrapperRef} {...rest}>
6062
{dragElement}
6163
</DragOverlay>
62-
</DragOverlayContext>
64+
</DragOverlayContext.Provider>
6365
)
6466
}
6567

packages/dragswag/src/react/use-draggable.tsx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export function useDraggable(config: DraggableConfig) {
1818
config,
1919
data: null as any,
2020
currentDragComponent: null as React.ReactElement | null,
21+
elementDimensions: { width: 0, height: 0 },
2122
})
2223

2324
refs.current.config = config
@@ -41,7 +42,10 @@ export function useDraggable(config: DraggableConfig) {
4142
onDragStart(props) {
4243
const current = refs.current
4344

44-
const { top, left } = current.element!.getBoundingClientRect()
45+
const { top, left, width, height } = current.element!.getBoundingClientRect()
46+
47+
// Store element dimensions
48+
current.elementDimensions = { width, height }
4549

4650
let offset
4751

@@ -174,8 +178,17 @@ export function useDraggable(config: DraggableConfig) {
174178

175179
if (current.isDragging) {
176180
let dragComponent = current.config.component?.({ data: current.data, props: child.props }) ?? child
181+
182+
// Apply dimensions from the original element
183+
const style = {
184+
...(dragComponent.props.style || {}),
185+
width: current.elementDimensions.width + 'px',
186+
height: current.elementDimensions.height + 'px',
187+
}
188+
177189
dragComponent = React.cloneElement(dragComponent, {
178190
ref: dragComponentRef,
191+
style,
179192
})
180193

181194
// Store the current drag component to use in the effect

0 commit comments

Comments
 (0)