-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
index.js
185 lines (168 loc) · 4.86 KB
/
index.js
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
180
181
182
183
184
185
/**
* External dependencies
*/
import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { useMergeRefs } from '@wordpress/compose';
import { Popover } from '@wordpress/components';
import {
forwardRef,
useMemo,
useReducer,
useLayoutEffect,
} from '@wordpress/element';
/**
* Internal dependencies
*/
import { __unstableUseBlockElement as useBlockElement } from '../block-list/use-block-props/use-block-refs';
import usePopoverScroll from './use-popover-scroll';
const MAX_POPOVER_RECOMPUTE_COUNTER = Number.MAX_SAFE_INTEGER;
function BlockPopover(
{
clientId,
bottomClientId,
children,
__unstableRefreshSize,
__unstableCoverTarget = false,
__unstablePopoverSlot,
__unstableContentRef,
shift = true,
...props
},
ref
) {
const selectedElement = useBlockElement( clientId );
const lastSelectedElement = useBlockElement( bottomClientId ?? clientId );
const mergedRefs = useMergeRefs( [
ref,
usePopoverScroll( __unstableContentRef ),
] );
const [
popoverDimensionsRecomputeCounter,
forceRecomputePopoverDimensions,
] = useReducer(
// Module is there to make sure that the counter doesn't overflow.
( s ) => ( s + 1 ) % MAX_POPOVER_RECOMPUTE_COUNTER,
0
);
// When blocks are moved up/down, they are animated to their new position by
// updating the `transform` property manually (i.e. without using CSS
// transitions or animations). The animation, which can also scroll the block
// editor, can sometimes cause the position of the Popover to get out of sync.
// A MutationObserver is therefore used to make sure that changes to the
// selectedElement's attribute (i.e. `transform`) can be tracked and used to
// trigger the Popover to rerender.
useLayoutEffect( () => {
if ( ! selectedElement ) {
return;
}
const observer = new window.MutationObserver(
forceRecomputePopoverDimensions
);
observer.observe( selectedElement, { attributes: true } );
return () => {
observer.disconnect();
};
}, [ selectedElement ] );
const style = useMemo( () => {
if (
// popoverDimensionsRecomputeCounter is by definition always equal or greater
// than 0. This check is only there to satisfy the correctness of the
// exhaustive-deps rule for the `useMemo` hook.
popoverDimensionsRecomputeCounter < 0 ||
! selectedElement ||
lastSelectedElement !== selectedElement
) {
return {};
}
return {
position: 'absolute',
width: selectedElement.offsetWidth,
height: selectedElement.offsetHeight,
};
}, [
selectedElement,
lastSelectedElement,
__unstableRefreshSize,
popoverDimensionsRecomputeCounter,
] );
const popoverAnchor = useMemo( () => {
if (
// popoverDimensionsRecomputeCounter is by definition always equal or greater
// than 0. This check is only there to satisfy the correctness of the
// exhaustive-deps rule for the `useMemo` hook.
popoverDimensionsRecomputeCounter < 0 ||
! selectedElement ||
( bottomClientId && ! lastSelectedElement )
) {
return undefined;
}
return {
getBoundingClientRect() {
const selectedBCR = selectedElement.getBoundingClientRect();
const lastSelectedBCR =
lastSelectedElement?.getBoundingClientRect();
// Get the biggest rectangle that encompasses completely the currently
// selected element and the last selected element:
// - for top/left coordinates, use the smaller numbers
// - for the bottom/right coordinates, use the largest numbers
const left = Math.min(
selectedBCR.left,
lastSelectedBCR?.left ?? Infinity
);
const top = Math.min(
selectedBCR.top,
lastSelectedBCR?.top ?? Infinity
);
const right = Math.max(
selectedBCR.right,
lastSelectedBCR.right ?? -Infinity
);
const bottom = Math.max(
selectedBCR.bottom,
lastSelectedBCR.bottom ?? -Infinity
);
const width = right - left;
const height = bottom - top;
return new window.DOMRect( left, top, width, height );
},
contextElement: selectedElement,
};
}, [
bottomClientId,
lastSelectedElement,
selectedElement,
popoverDimensionsRecomputeCounter,
] );
if ( ! selectedElement || ( bottomClientId && ! lastSelectedElement ) ) {
return null;
}
return (
<Popover
ref={ mergedRefs }
animate={ false }
focusOnMount={ false }
anchor={ popoverAnchor }
// Render in the old slot if needed for backward compatibility,
// otherwise render in place (not in the default popover slot).
__unstableSlotName={ __unstablePopoverSlot }
inline={ ! __unstablePopoverSlot }
placement="top-start"
resize={ false }
flip={ false }
shift={ shift }
{ ...props }
className={ classnames(
'block-editor-block-popover',
props.className
) }
variant="unstyled"
>
{ __unstableCoverTarget && <div style={ style }>{ children }</div> }
{ ! __unstableCoverTarget && children }
</Popover>
);
}
export default forwardRef( BlockPopover );