-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Expand file tree
/
Copy pathgrid-item.tsx
More file actions
179 lines (169 loc) · 5.14 KB
/
grid-item.tsx
File metadata and controls
179 lines (169 loc) · 5.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/**
* External dependencies
*/
import { useSortable } from '@dnd-kit/sortable';
import clsx from 'clsx';
/**
* WordPress dependencies
*/
import { useState, useRef, useLayoutEffect } from '@wordpress/element';
import { useMergeRefs } from '@wordpress/compose';
/**
* Internal dependencies
*/
import ResizeHandle from './resize-handle';
import type { GridItemProps, ResizeDelta } from './types';
import styles from './grid-item.module.css';
export function GridItem( {
item,
maxColumns,
disabled = false,
verticalResizable = true,
interacting = false,
children,
actionableArea = null,
onResize,
onResizeEnd,
renderResizeHandle,
}: GridItemProps ) {
const [ previewDelta, setPreviewDelta ] = useState< ResizeDelta | null >(
null
);
const itemRef = useRef< HTMLDivElement >( null );
// Tile bounding rect at the first resize frame. The cursor `delta`
// from the handle is anchored to the gesture start, but the
// overlay needs to track the cursor against the *current* tile
// edge — which has shifted whenever the width/height stepped a
// column/row. Re-anchor locally by subtracting the tile growth.
const initialResizeRectRef = useRef< DOMRect | null >( null );
// Latest cursor delta from the resize handle. Reading this in a
// `useLayoutEffect` lets the overlay re-measure the tile rect
// *after* React commits a width step but before paint, so the
// frame that follows a column step never renders the overlay
// at the pre-step offset.
const lastResizeDeltaRef = useRef< ResizeDelta | null >( null );
const { attributes, listeners, setNodeRef, isDragging } = useSortable( {
id: item.key,
disabled,
} );
const mergedRef = useMergeRefs( [ itemRef, setNodeRef ] );
/*
* With `<DragOverlay>` handling the cursor-following clone, the
* sortable item stays put in its grid cell and acts as a
* placeholder. No `transform` is applied here — applying one
* would double-move the placeholder alongside the overlay.
*/
const style = {
gridColumnEnd: `span ${
item.width === 'full'
? maxColumns
: Math.min(
typeof item.width === 'number' ? item.width : 1,
maxColumns
)
}`,
gridRowEnd: `span ${ item.height || 1 }`,
cursor: disabled ? 'default' : 'grab',
};
const itemClassName = clsx(
styles.item,
isDragging && styles[ 'is-dragging' ]
);
const handleResize = ( delta: ResizeDelta ) => {
const clamped = {
width: delta.width,
height: verticalResizable ? delta.height : 0,
};
const node = itemRef.current;
if ( node && ! initialResizeRectRef.current ) {
initialResizeRectRef.current = node.getBoundingClientRect();
}
lastResizeDeltaRef.current = clamped;
onResize( item.key, clamped );
// Provisional preview against the pre-commit rect; the
// `useLayoutEffect` below refines it once React commits the
// new tile size so a column step never paints with the
// stale offset.
if ( node && initialResizeRectRef.current ) {
const currentRect = node.getBoundingClientRect();
const offsetX =
currentRect.right - initialResizeRectRef.current.right;
const offsetY =
currentRect.bottom - initialResizeRectRef.current.bottom;
setPreviewDelta( {
width: clamped.width - offsetX,
height: clamped.height - ( verticalResizable ? offsetY : 0 ),
} );
}
};
useLayoutEffect( () => {
const lastDelta = lastResizeDeltaRef.current;
const initialRect = initialResizeRectRef.current;
const node = itemRef.current;
if ( ! lastDelta || ! initialRect || ! node ) {
return;
}
const currentRect = node.getBoundingClientRect();
const offsetX = currentRect.right - initialRect.right;
const offsetY = currentRect.bottom - initialRect.bottom;
const next = {
width: lastDelta.width - offsetX,
height: lastDelta.height - ( verticalResizable ? offsetY : 0 ),
};
// Use the updater form so the effect doesn't need `previewDelta`
// in its deps. Returning `prev` when nothing changed lets React
// bail out without a re-render.
setPreviewDelta( ( prev ) =>
next.width === prev?.width && next.height === prev?.height
? prev
: next
);
}, [ item.width, item.height, verticalResizable ] );
const handleResizeEnd = () => {
setPreviewDelta( null );
initialResizeRectRef.current = null;
lastResizeDeltaRef.current = null;
onResizeEnd();
};
const previewOverlay = previewDelta ? (
<div
className={ styles[ 'preview-overlay' ] }
style={ {
insetInlineEnd: -previewDelta.width,
bottom: -previewDelta.height,
} }
/>
) : null;
return (
<div
ref={ mergedRef }
className={ itemClassName }
style={ style }
{ ...attributes }
>
{ actionableArea ? (
<div
style={ { display: 'contents' } }
{ ...( interacting ? { inert: '' } : {} ) }
>
{ actionableArea }
</div>
) : null }
<div { ...listeners } style={ { height: '100%' } }>
<div className={ styles[ 'item-content' ] }>
{ children }
{ ! disabled && (
<ResizeHandle
itemId={ item.key }
verticalResizable={ verticalResizable }
onResize={ handleResize }
onResizeEnd={ handleResizeEnd }
renderResizeHandle={ renderResizeHandle }
/>
) }
</div>
{ previewOverlay }
</div>
</div>
);
}