Align.js
5.08 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
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import _typeof from "@babel/runtime/helpers/esm/typeof";
/**
* Removed props:
* - childrenProps
*/
import React from 'react';
import { composeRef } from "rc-util/es/ref";
import isVisible from "rc-util/es/Dom/isVisible";
import { alignElement, alignPoint } from 'dom-align';
import addEventListener from "rc-util/es/Dom/addEventListener";
import { isSamePoint, restoreFocus, monitorResize } from './util';
import useBuffer from './hooks/useBuffer';
function getElement(func) {
if (typeof func !== 'function') return null;
return func();
}
function getPoint(point) {
if (_typeof(point) !== 'object' || !point) return null;
return point;
}
var Align = function Align(_ref, ref) {
var children = _ref.children,
disabled = _ref.disabled,
target = _ref.target,
align = _ref.align,
onAlign = _ref.onAlign,
monitorWindowResize = _ref.monitorWindowResize,
_ref$monitorBufferTim = _ref.monitorBufferTime,
monitorBufferTime = _ref$monitorBufferTim === void 0 ? 0 : _ref$monitorBufferTim;
var cacheRef = React.useRef({});
var nodeRef = React.useRef();
var childNode = React.Children.only(children); // ===================== Align ======================
// We save the props here to avoid closure makes props ood
var forceAlignPropsRef = React.useRef({});
forceAlignPropsRef.current.disabled = disabled;
forceAlignPropsRef.current.target = target;
forceAlignPropsRef.current.onAlign = onAlign;
var _useBuffer = useBuffer(function () {
var _forceAlignPropsRef$c = forceAlignPropsRef.current,
latestDisabled = _forceAlignPropsRef$c.disabled,
latestTarget = _forceAlignPropsRef$c.target,
latestOnAlign = _forceAlignPropsRef$c.onAlign;
if (!latestDisabled && latestTarget) {
var source = nodeRef.current;
var result;
var element = getElement(latestTarget);
var point = getPoint(latestTarget);
cacheRef.current.element = element;
cacheRef.current.point = point; // IE lose focus after element realign
// We should record activeElement and restore later
var _document = document,
activeElement = _document.activeElement; // We only align when element is visible
if (element && isVisible(element)) {
result = alignElement(source, element, align);
} else if (point) {
result = alignPoint(source, point, align);
}
restoreFocus(activeElement, source);
if (latestOnAlign && result) {
latestOnAlign(source, result);
}
return true;
}
return false;
}, monitorBufferTime),
_useBuffer2 = _slicedToArray(_useBuffer, 2),
_forceAlign = _useBuffer2[0],
cancelForceAlign = _useBuffer2[1]; // ===================== Effect =====================
// Listen for target updated
var resizeMonitor = React.useRef({
cancel: function cancel() {}
}); // Listen for source updated
var sourceResizeMonitor = React.useRef({
cancel: function cancel() {}
});
React.useEffect(function () {
var element = getElement(target);
var point = getPoint(target);
if (nodeRef.current !== sourceResizeMonitor.current.element) {
sourceResizeMonitor.current.cancel();
sourceResizeMonitor.current.element = nodeRef.current;
sourceResizeMonitor.current.cancel = monitorResize(nodeRef.current, _forceAlign);
}
if (cacheRef.current.element !== element || !isSamePoint(cacheRef.current.point, point)) {
_forceAlign(); // Add resize observer
if (resizeMonitor.current.element !== element) {
resizeMonitor.current.cancel();
resizeMonitor.current.element = element;
resizeMonitor.current.cancel = monitorResize(element, _forceAlign);
}
}
}); // Listen for disabled change
React.useEffect(function () {
if (!disabled) {
_forceAlign();
} else {
cancelForceAlign();
}
}, [disabled]); // Listen for window resize
var winResizeRef = React.useRef(null);
React.useEffect(function () {
if (monitorWindowResize) {
if (!winResizeRef.current) {
winResizeRef.current = addEventListener(window, 'resize', _forceAlign);
}
} else if (winResizeRef.current) {
winResizeRef.current.remove();
winResizeRef.current = null;
}
}, [monitorWindowResize]); // Clear all if unmount
React.useEffect(function () {
return function () {
resizeMonitor.current.cancel();
sourceResizeMonitor.current.cancel();
if (winResizeRef.current) winResizeRef.current.remove();
cancelForceAlign();
};
}, []); // ====================== Ref =======================
React.useImperativeHandle(ref, function () {
return {
forceAlign: function forceAlign() {
return _forceAlign(true);
}
};
}); // ===================== Render =====================
if (React.isValidElement(childNode)) {
childNode = React.cloneElement(childNode, {
ref: composeRef(childNode.ref, nodeRef)
});
}
return childNode;
};
var RefAlign = React.forwardRef(Align);
RefAlign.displayName = 'Align';
export default RefAlign;