Align.js
5.7 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
var _react = _interopRequireDefault(require("react"));
var _ref2 = require("rc-util/lib/ref");
var _isVisible = _interopRequireDefault(require("rc-util/lib/Dom/isVisible"));
var _domAlign = require("dom-align");
var _addEventListener = _interopRequireDefault(require("rc-util/lib/Dom/addEventListener"));
var _util = require("./util");
var _useBuffer3 = _interopRequireDefault(require("./hooks/useBuffer"));
/**
* Removed props:
* - childrenProps
*/
function getElement(func) {
if (typeof func !== 'function') return null;
return func();
}
function getPoint(point) {
if ((0, _typeof2.default)(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.default.useRef({});
var nodeRef = _react.default.useRef();
var childNode = _react.default.Children.only(children); // ===================== Align ======================
// We save the props here to avoid closure makes props ood
var forceAlignPropsRef = _react.default.useRef({});
forceAlignPropsRef.current.disabled = disabled;
forceAlignPropsRef.current.target = target;
forceAlignPropsRef.current.onAlign = onAlign;
var _useBuffer = (0, _useBuffer3.default)(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 && (0, _isVisible.default)(element)) {
result = (0, _domAlign.alignElement)(source, element, align);
} else if (point) {
result = (0, _domAlign.alignPoint)(source, point, align);
}
(0, _util.restoreFocus)(activeElement, source);
if (latestOnAlign && result) {
latestOnAlign(source, result);
}
return true;
}
return false;
}, monitorBufferTime),
_useBuffer2 = (0, _slicedToArray2.default)(_useBuffer, 2),
_forceAlign = _useBuffer2[0],
cancelForceAlign = _useBuffer2[1]; // ===================== Effect =====================
// Listen for target updated
var resizeMonitor = _react.default.useRef({
cancel: function cancel() {}
}); // Listen for source updated
var sourceResizeMonitor = _react.default.useRef({
cancel: function cancel() {}
});
_react.default.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 = (0, _util.monitorResize)(nodeRef.current, _forceAlign);
}
if (cacheRef.current.element !== element || !(0, _util.isSamePoint)(cacheRef.current.point, point)) {
_forceAlign(); // Add resize observer
if (resizeMonitor.current.element !== element) {
resizeMonitor.current.cancel();
resizeMonitor.current.element = element;
resizeMonitor.current.cancel = (0, _util.monitorResize)(element, _forceAlign);
}
}
}); // Listen for disabled change
_react.default.useEffect(function () {
if (!disabled) {
_forceAlign();
} else {
cancelForceAlign();
}
}, [disabled]); // Listen for window resize
var winResizeRef = _react.default.useRef(null);
_react.default.useEffect(function () {
if (monitorWindowResize) {
if (!winResizeRef.current) {
winResizeRef.current = (0, _addEventListener.default)(window, 'resize', _forceAlign);
}
} else if (winResizeRef.current) {
winResizeRef.current.remove();
winResizeRef.current = null;
}
}, [monitorWindowResize]); // Clear all if unmount
_react.default.useEffect(function () {
return function () {
resizeMonitor.current.cancel();
sourceResizeMonitor.current.cancel();
if (winResizeRef.current) winResizeRef.current.remove();
cancelForceAlign();
};
}, []); // ====================== Ref =======================
_react.default.useImperativeHandle(ref, function () {
return {
forceAlign: function forceAlign() {
return _forceAlign(true);
}
};
}); // ===================== Render =====================
if (_react.default.isValidElement(childNode)) {
childNode = _react.default.cloneElement(childNode, {
ref: (0, _ref2.composeRef)(childNode.ref, nodeRef)
});
}
return childNode;
};
var RefAlign = _react.default.forwardRef(Align);
RefAlign.displayName = 'Align';
var _default = RefAlign;
exports.default = _default;