publ-echo 0.0.45 → 0.0.46
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/lib/GridItem/GridItem.js +84 -3
- package/dist/lib/GridItem/utils/autoResizeUtils.d.ts +24 -0
- package/dist/lib/GridItem/utils/autoResizeUtils.js +51 -0
- package/dist/lib/GridLayoutEditor/ReactGridLayout.js +20 -0
- package/dist/lib/hooks/useIsMounted.d.ts +9 -0
- package/dist/lib/hooks/useIsMounted.js +19 -0
- package/dist/lib/hooks/useResizeobserver.d.ts +38 -0
- package/dist/lib/hooks/useResizeobserver.js +116 -0
- package/package.json +1 -1
|
@@ -27,15 +27,22 @@ import classNames from "../../external-lib/classnames";
|
|
|
27
27
|
import { calcGridColWidth, calcGridItemPosition, calcGridItemWHPx, calcWH, calcXY, clamp, resolveRowHeight, } from "./utils/calculateUtils";
|
|
28
28
|
import { perc, setTopLeft, setTransform, } from "../GridLayoutEditor/utils/renderHelpers";
|
|
29
29
|
import { Resizable } from "../Resizable";
|
|
30
|
+
import { useResizeObserver } from "../hooks/useResizeobserver";
|
|
31
|
+
import { shouldAutoResize, shouldBlockResize } from "./utils/autoResizeUtils";
|
|
30
32
|
var GridItem = function (_a) {
|
|
31
33
|
var children = _a.children, props = __rest(_a, ["children"]);
|
|
32
|
-
var resizeHandle = props.resizeHandle, cols = props.cols, isDraggable = props.isDraggable, _b = props.transformScale, transformScale = _b === void 0 ? 1 : _b, isResizable = props.isResizable, useCSSTransforms = props.useCSSTransforms, className = props.className, _c = props.cancel, cancel = _c === void 0 ? "" : _c, _d = props.handle, handle = _d === void 0 ? "" : _d, x = props.x, y = props.y, z = props.z, w = props.w, h = props.h, _e = props.minH, minH = _e === void 0 ? 1 : _e, _f = props.minW, minW = _f === void 0 ? 1 : _f, _g = props.maxH, maxH = _g === void 0 ? Infinity : _g, _h = props.maxW, maxW = _h === void 0 ? Infinity : _h, i = props.i, _j = props.isHiddenVisibility, isHiddenVisibility = _j === void 0 ? false : _j, containerWidth = props.containerWidth, customColWidth = props.customColWidth;
|
|
34
|
+
var resizeHandle = props.resizeHandle, cols = props.cols, isDraggable = props.isDraggable, _b = props.transformScale, transformScale = _b === void 0 ? 1 : _b, isResizable = props.isResizable, useCSSTransforms = props.useCSSTransforms, className = props.className, _c = props.cancel, cancel = _c === void 0 ? "" : _c, _d = props.handle, handle = _d === void 0 ? "" : _d, x = props.x, y = props.y, z = props.z, w = props.w, h = props.h, _e = props.minH, minH = _e === void 0 ? 1 : _e, _f = props.minW, minW = _f === void 0 ? 1 : _f, _g = props.maxH, maxH = _g === void 0 ? Infinity : _g, _h = props.maxW, maxW = _h === void 0 ? Infinity : _h, i = props.i, _j = props.isHiddenVisibility, isHiddenVisibility = _j === void 0 ? false : _j, containerWidth = props.containerWidth, customColWidth = props.customColWidth, rowHeight = props.rowHeight;
|
|
35
|
+
var shouldAutoShrink = useRef({ shouldAutoShrink: false, initialHeight: 0 });
|
|
33
36
|
var _k = useState(), resizing = _k[0], setResizing = _k[1];
|
|
34
37
|
var _l = useState(), dragging = _l[0], setDragging = _l[1];
|
|
35
38
|
var _m = useState(), dragStart = _m[0], setDragStart = _m[1];
|
|
36
39
|
var _o = useState(false), isDragging = _o[0], setIsDragging = _o[1];
|
|
37
40
|
var _p = useState(false), isResizing = _p[0], setIsResizing = _p[1];
|
|
38
41
|
var elementRef = useRef(null);
|
|
42
|
+
var _q = useResizeObserver({
|
|
43
|
+
ref: elementRef,
|
|
44
|
+
box: 'border-box',
|
|
45
|
+
}, [isDragging, isResizing]), _r = _q.width, width = _r === void 0 ? 0 : _r, _s = _q.height, height = _s === void 0 ? 0 : _s, _t = _q.clientHeight, clientHeight = _t === void 0 ? 0 : _t, _u = _q.scrollHeight, scrollHeight = _u === void 0 ? 0 : _u, textContentHeight = _q.textContentHeight;
|
|
39
46
|
// const moveDroppingItem = (prevProps: GridItemProps) => {
|
|
40
47
|
// const { droppingPosition } = props;
|
|
41
48
|
// if (!droppingPosition) return;
|
|
@@ -173,9 +180,9 @@ var GridItem = function (_a) {
|
|
|
173
180
|
if (isBounded) {
|
|
174
181
|
var offsetParent = node.offsetParent;
|
|
175
182
|
if (offsetParent) {
|
|
176
|
-
var margin = props.margin,
|
|
183
|
+
var margin = props.margin, rowHeight_1 = props.rowHeight;
|
|
177
184
|
// const colWidth = calcGridColWidth(positionParams);
|
|
178
|
-
var rowHeightNumber = resolveRowHeight(
|
|
185
|
+
var rowHeightNumber = resolveRowHeight(rowHeight_1, colWidth);
|
|
179
186
|
var bottomBoundary = offsetParent.clientHeight -
|
|
180
187
|
calcGridItemWHPx(h, rowHeightNumber, margin[1]);
|
|
181
188
|
top = clamp(top, 0, bottomBoundary);
|
|
@@ -253,7 +260,16 @@ var GridItem = function (_a) {
|
|
|
253
260
|
* @param {Object} callbackData an object with node and size information
|
|
254
261
|
*/
|
|
255
262
|
var onResizeStart = function (e, callbackData) {
|
|
263
|
+
var _a;
|
|
264
|
+
var isOverflowingVertically = scrollHeight > clientHeight;
|
|
265
|
+
var isHandleVertical = callbackData.handle.includes('n') || callbackData.handle.includes('s');
|
|
266
|
+
// const isOverflowingHorizontally = scrollWidth > clientWidth;
|
|
267
|
+
if (isOverflowingVertically && isHandleVertical) {
|
|
268
|
+
(_a = elementRef.current) === null || _a === void 0 ? void 0 : _a.classList.add("resize-notallowed-".concat(callbackData.handle));
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
256
271
|
onResizeHandler(e, callbackData, "onResizeStart");
|
|
272
|
+
shouldAutoShrink.current.initialHeight = height;
|
|
257
273
|
};
|
|
258
274
|
/**
|
|
259
275
|
* onResize event handler
|
|
@@ -262,6 +278,53 @@ var GridItem = function (_a) {
|
|
|
262
278
|
*/
|
|
263
279
|
var onResize = function (e, callbackData) {
|
|
264
280
|
setIsResizing(true);
|
|
281
|
+
var _a = getDirectionOfMouseEvent(e), dirX = _a.dirX, dirY = _a.dirY;
|
|
282
|
+
var shouldBlock = shouldBlockResize({
|
|
283
|
+
handle: callbackData.handle,
|
|
284
|
+
direction: dirY,
|
|
285
|
+
contentHeight: textContentHeight,
|
|
286
|
+
gridItemHeight: callbackData.size.height,
|
|
287
|
+
shouldAutoShrink: shouldAutoShrink.current.shouldAutoShrink,
|
|
288
|
+
initialHeight: shouldAutoShrink.current.initialHeight,
|
|
289
|
+
colHeight: rowHeight
|
|
290
|
+
});
|
|
291
|
+
if (resizing === null || resizing === void 0 ? void 0 : resizing.top) {
|
|
292
|
+
console.log(resizing === null || resizing === void 0 ? void 0 : resizing.top);
|
|
293
|
+
callbackData.size.top = resizing === null || resizing === void 0 ? void 0 : resizing.top;
|
|
294
|
+
}
|
|
295
|
+
var shouldResize = shouldAutoResize({
|
|
296
|
+
handle: callbackData.handle,
|
|
297
|
+
dirX: dirX,
|
|
298
|
+
dirY: dirY,
|
|
299
|
+
contentHeight: textContentHeight,
|
|
300
|
+
gridItemHeight: callbackData.size.height,
|
|
301
|
+
shouldAutoShrink: shouldAutoShrink.current.shouldAutoShrink,
|
|
302
|
+
initialHeight: shouldAutoShrink.current.initialHeight
|
|
303
|
+
});
|
|
304
|
+
if (shouldResize !== 'EXPAND' && shouldBlock === 'BLOCK') {
|
|
305
|
+
callbackData.size.height = textContentHeight;
|
|
306
|
+
}
|
|
307
|
+
if (shouldResize === 'EXPAND') {
|
|
308
|
+
callbackData.size.height = textContentHeight;
|
|
309
|
+
shouldAutoShrink.current.shouldAutoShrink = true;
|
|
310
|
+
// NOTE: 배포전에 무조건 변경
|
|
311
|
+
if (callbackData.handle === 'nw') {
|
|
312
|
+
callbackData.handle = 'w';
|
|
313
|
+
}
|
|
314
|
+
if (callbackData.handle === 'ne') {
|
|
315
|
+
callbackData.handle = 'se';
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
if (shouldResize === 'SHRINK') {
|
|
319
|
+
callbackData.size.height = Math.max(shouldAutoShrink.current.initialHeight, textContentHeight);
|
|
320
|
+
// NOTE: 배포전에 무조건 변경
|
|
321
|
+
if (callbackData.handle === 'nw') {
|
|
322
|
+
callbackData.handle = 'sw';
|
|
323
|
+
}
|
|
324
|
+
if (callbackData.handle === 'ne') {
|
|
325
|
+
callbackData.handle = 'se';
|
|
326
|
+
}
|
|
327
|
+
}
|
|
265
328
|
onResizeHandler(e, callbackData, "onResize");
|
|
266
329
|
};
|
|
267
330
|
/**
|
|
@@ -272,6 +335,10 @@ var GridItem = function (_a) {
|
|
|
272
335
|
var onResizeStop = function (e, callbackData) {
|
|
273
336
|
setIsResizing(false);
|
|
274
337
|
onResizeHandler(e, callbackData, "onResizeStop");
|
|
338
|
+
shouldAutoShrink.current = {
|
|
339
|
+
shouldAutoShrink: false,
|
|
340
|
+
initialHeight: 0,
|
|
341
|
+
};
|
|
275
342
|
};
|
|
276
343
|
/**
|
|
277
344
|
* Wrapper around drag events to provide more useful data.
|
|
@@ -297,6 +364,11 @@ var GridItem = function (_a) {
|
|
|
297
364
|
h = clamp(h, minH, maxH);
|
|
298
365
|
setResizing(handlerName === "onResizeStop" ? undefined : size);
|
|
299
366
|
var _c = getResizableXYPosition(handle, w, h, prevW, prevH), newX = _c.newX, newY = _c.newY;
|
|
367
|
+
if (newY !== y) {
|
|
368
|
+
// Y 변화가 있다면 자동줄이기는 하지않는다.
|
|
369
|
+
shouldAutoShrink.current.shouldAutoShrink = false;
|
|
370
|
+
shouldAutoShrink.current.initialHeight = 0;
|
|
371
|
+
}
|
|
300
372
|
handler(i, w, h, { e: e, node: node, size: size }, newX, newY);
|
|
301
373
|
};
|
|
302
374
|
var pos = calcGridItemPosition(getPositionParams(), x, y, z, w, h, {
|
|
@@ -325,3 +397,12 @@ var GridItem = function (_a) {
|
|
|
325
397
|
return newChild;
|
|
326
398
|
};
|
|
327
399
|
export default GridItem;
|
|
400
|
+
// NOTE: 이동해야할 util
|
|
401
|
+
var getDirectionOfMouseEvent = function (e) {
|
|
402
|
+
var dirX = e.movementX > 0 ? 'right' : e.movementX < 0 ? 'left' : null;
|
|
403
|
+
var dirY = e.movementY > 0 ? 'down' : e.movementY < 0 ? 'up' : null;
|
|
404
|
+
return {
|
|
405
|
+
dirX: dirX,
|
|
406
|
+
dirY: dirY
|
|
407
|
+
};
|
|
408
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export type Handle = 'n' | 's' | 'w' | 'e' | 'nw' | 'ne' | 'sw' | 'se';
|
|
2
|
+
export type DirectionX = 'right' | 'left';
|
|
3
|
+
export type DirectionY = 'up' | 'down';
|
|
4
|
+
type Props = {
|
|
5
|
+
handle: Handle;
|
|
6
|
+
dirX: DirectionX;
|
|
7
|
+
dirY: DirectionY;
|
|
8
|
+
contentHeight: number;
|
|
9
|
+
gridItemHeight: number;
|
|
10
|
+
shouldAutoShrink: boolean;
|
|
11
|
+
initialHeight: number;
|
|
12
|
+
};
|
|
13
|
+
export declare function shouldAutoResize({ handle, dirX, dirY, contentHeight, gridItemHeight, shouldAutoShrink, initialHeight }: Props): "SHRINK" | "EXPAND" | "NONE";
|
|
14
|
+
type BlockDetectProp = {
|
|
15
|
+
handle: Handle;
|
|
16
|
+
direction: DirectionY;
|
|
17
|
+
contentHeight: number;
|
|
18
|
+
gridItemHeight: number;
|
|
19
|
+
shouldAutoShrink: boolean;
|
|
20
|
+
initialHeight: number;
|
|
21
|
+
colHeight: number;
|
|
22
|
+
};
|
|
23
|
+
export declare function shouldBlockResize({ handle, direction, contentHeight, gridItemHeight, shouldAutoShrink, initialHeight, colHeight }: BlockDetectProp): "NONE" | "BLOCK";
|
|
24
|
+
export {};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
export function shouldAutoResize(_a) {
|
|
2
|
+
var handle = _a.handle, dirX = _a.dirX, dirY = _a.dirY, contentHeight = _a.contentHeight, gridItemHeight = _a.gridItemHeight, shouldAutoShrink = _a.shouldAutoShrink, initialHeight = _a.initialHeight;
|
|
3
|
+
var shouldExpand = contentHeight > gridItemHeight;
|
|
4
|
+
var shouldShrink = gridItemHeight > contentHeight;
|
|
5
|
+
if (handle === 'nw') {
|
|
6
|
+
// if (dirY === 'up' || dirY === 'down') {
|
|
7
|
+
// return 'NONE'
|
|
8
|
+
// }
|
|
9
|
+
if (dirX === 'left' && shouldShrink && shouldAutoShrink) {
|
|
10
|
+
return 'SHRINK';
|
|
11
|
+
}
|
|
12
|
+
if (dirX === 'right' && shouldExpand) {
|
|
13
|
+
return 'EXPAND';
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
if (handle.includes('e') && dirX === 'left' && shouldExpand) {
|
|
17
|
+
return 'EXPAND';
|
|
18
|
+
}
|
|
19
|
+
if (handle.includes('e') && dirX === 'right' && shouldShrink && shouldAutoShrink) {
|
|
20
|
+
return 'SHRINK';
|
|
21
|
+
}
|
|
22
|
+
if (handle.includes('w') && dirX === 'right' && shouldExpand) {
|
|
23
|
+
return 'EXPAND';
|
|
24
|
+
}
|
|
25
|
+
if (handle.includes('w') && dirX === 'left' && shouldShrink && shouldAutoShrink) {
|
|
26
|
+
return 'SHRINK';
|
|
27
|
+
}
|
|
28
|
+
return 'NONE';
|
|
29
|
+
}
|
|
30
|
+
export function shouldBlockResize(_a) {
|
|
31
|
+
var handle = _a.handle, direction = _a.direction, contentHeight = _a.contentHeight, gridItemHeight = _a.gridItemHeight, shouldAutoShrink = _a.shouldAutoShrink, initialHeight = _a.initialHeight, colHeight = _a.colHeight;
|
|
32
|
+
var nextGridHeight = gridItemHeight - colHeight;
|
|
33
|
+
var shouldBlock = contentHeight > nextGridHeight;
|
|
34
|
+
var shouldShrink = gridItemHeight > contentHeight;
|
|
35
|
+
if (handle.includes('n') && direction === 'down' && shouldBlock) {
|
|
36
|
+
return 'BLOCK';
|
|
37
|
+
}
|
|
38
|
+
if (handle.includes('s') && direction === 'up' && shouldBlock) {
|
|
39
|
+
return 'BLOCK';
|
|
40
|
+
}
|
|
41
|
+
// if (handle.includes('e') && direction === 'right' && shouldShrink && shouldAutoShrink) {
|
|
42
|
+
// return 'SHRINK';
|
|
43
|
+
// }
|
|
44
|
+
// if (handle.includes('w') && direction === 'right' && shouldExpand) {
|
|
45
|
+
// return 'EXPAND';
|
|
46
|
+
// }
|
|
47
|
+
// if (handle.includes('w') && direction === 'left' && shouldShrink && shouldAutoShrink) {
|
|
48
|
+
// return 'SHRINK';
|
|
49
|
+
// }
|
|
50
|
+
return 'NONE';
|
|
51
|
+
}
|
|
@@ -357,6 +357,26 @@ var ReactGridLayout = function (_a) {
|
|
|
357
357
|
}
|
|
358
358
|
return (_jsx(GridItem, { w: activeDrag.w, h: activeDrag.h, x: activeDrag.x, y: activeDrag.y, z: activeDrag.z || 0, i: activeDrag.i, className: "placeholder", containerWidth: width, cols: cols, margin: margin, containerPadding: containerPadding || margin, maxRows: maxRows, rowHeight: rowHeight, isDraggable: false, isResizable: false, isBounded: false, useCSSTransforms: useCSSTransforms, transformScale: transformScale, children: item && item }));
|
|
359
359
|
};
|
|
360
|
+
var placeholder2 = function () {
|
|
361
|
+
if (!activeDrag)
|
|
362
|
+
return;
|
|
363
|
+
// {...activeDrag} is pretty slow, actually
|
|
364
|
+
var l = getLayoutItem(props.layout, String(activeDrag.i));
|
|
365
|
+
if (!l) {
|
|
366
|
+
return null;
|
|
367
|
+
}
|
|
368
|
+
// NOTE: toChildren changes key
|
|
369
|
+
// LINK: https://legacy.reactjs.org/docs/react-api.html
|
|
370
|
+
var items = React.Children.toArray(children);
|
|
371
|
+
var item = items.find(function (child) {
|
|
372
|
+
var originalKey = String(child.key).split(".$");
|
|
373
|
+
return originalKey[1] === l.i;
|
|
374
|
+
});
|
|
375
|
+
if (!item) {
|
|
376
|
+
return null;
|
|
377
|
+
}
|
|
378
|
+
return (_jsx(GridItem, { w: activeDrag.w, h: activeDrag.h, x: activeDrag.x, y: activeDrag.y, z: activeDrag.z || 0, i: activeDrag.i, className: "placeholder", containerWidth: width, cols: cols, margin: margin, containerPadding: containerPadding || margin, maxRows: maxRows, rowHeight: rowHeight, isDraggable: false, isResizable: false, isBounded: false, useCSSTransforms: useCSSTransforms, transformScale: transformScale, children: item && item }));
|
|
379
|
+
};
|
|
360
380
|
/**
|
|
361
381
|
* Given a grid item, set its style attributes & surround in a <Draggable>.
|
|
362
382
|
* @param {Element} child React element.
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Custom hook for determining if the component is currently mounted.
|
|
3
|
+
* @returns {() => boolean} A function that returns a boolean value indicating whether the component is mounted.
|
|
4
|
+
* @see [Documentation](https://usehooks-ts.com/react-hook/use-is-mounted)
|
|
5
|
+
* @example
|
|
6
|
+
* const isComponentMounted = useIsMounted();
|
|
7
|
+
* // Use isComponentMounted() to check if the component is currently mounted before performing certain actions.
|
|
8
|
+
*/
|
|
9
|
+
export declare function useIsMounted(): () => boolean;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { useCallback, useEffect, useRef } from 'react';
|
|
2
|
+
/**
|
|
3
|
+
* Custom hook for determining if the component is currently mounted.
|
|
4
|
+
* @returns {() => boolean} A function that returns a boolean value indicating whether the component is mounted.
|
|
5
|
+
* @see [Documentation](https://usehooks-ts.com/react-hook/use-is-mounted)
|
|
6
|
+
* @example
|
|
7
|
+
* const isComponentMounted = useIsMounted();
|
|
8
|
+
* // Use isComponentMounted() to check if the component is currently mounted before performing certain actions.
|
|
9
|
+
*/
|
|
10
|
+
export function useIsMounted() {
|
|
11
|
+
var isMounted = useRef(false);
|
|
12
|
+
useEffect(function () {
|
|
13
|
+
isMounted.current = true;
|
|
14
|
+
return function () {
|
|
15
|
+
isMounted.current = false;
|
|
16
|
+
};
|
|
17
|
+
}, []);
|
|
18
|
+
return useCallback(function () { return isMounted.current; }, []);
|
|
19
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { RefObject } from 'react';
|
|
2
|
+
type Size = {
|
|
3
|
+
width: number | undefined;
|
|
4
|
+
height: number | undefined;
|
|
5
|
+
scrollHeight: number | undefined;
|
|
6
|
+
clientHeight: number | undefined;
|
|
7
|
+
textContentHeight: number | undefined;
|
|
8
|
+
};
|
|
9
|
+
type ResizeHandler = (size: Size) => void;
|
|
10
|
+
type BoxOptions = 'border-box' | 'content-box' | 'device-pixel-content-box';
|
|
11
|
+
type UseResizeObserverOptions<T extends HTMLElement = HTMLElement> = {
|
|
12
|
+
ref: RefObject<T>;
|
|
13
|
+
onResize?: ResizeHandler;
|
|
14
|
+
box?: BoxOptions;
|
|
15
|
+
};
|
|
16
|
+
type UseResizeObserverResult = Size;
|
|
17
|
+
/**
|
|
18
|
+
* Custom hook for observing the size of an element using the ResizeObserver API.
|
|
19
|
+
*
|
|
20
|
+
* @template T - The type of the element to observe.
|
|
21
|
+
* @param {UseResizeObserverOptions<T>} options - The options for the ResizeObserver. (default is `{}`).
|
|
22
|
+
* @param {RefObject<T>} options.ref - The ref of the element to observe.
|
|
23
|
+
* @param {ResizeHandler} [options.onResize] - When using `onResize`, the hook doesn't re-render on element size changes; it delegates handling to the provided callback . (default is `undefined`).
|
|
24
|
+
* @param {string} [options.box] - The box model to use for the ResizeObserver. (default is `'content-box'`).
|
|
25
|
+
* @returns {UseResizeObserverResult} - The size of the observed element.
|
|
26
|
+
* @see [Documentation](https://usehooks-ts.com/react-hook/use-resize-observer)
|
|
27
|
+
* @see [MDN ResizeObserver API](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver)
|
|
28
|
+
* @example
|
|
29
|
+
* const myRef = useRef(null);
|
|
30
|
+
* const { width = 0, height = 0 } = useResizeObserver({
|
|
31
|
+
* ref: myRef,
|
|
32
|
+
* box: 'content-box',
|
|
33
|
+
* });
|
|
34
|
+
*
|
|
35
|
+
* <div ref={myRef}>Hello, world!</div>
|
|
36
|
+
*/
|
|
37
|
+
export declare function useResizeObserver<T extends HTMLElement = HTMLElement>(options: UseResizeObserverOptions<T>, deps: any[]): UseResizeObserverResult;
|
|
38
|
+
export {};
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
var __assign = (this && this.__assign) || function () {
|
|
2
|
+
__assign = Object.assign || function(t) {
|
|
3
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
4
|
+
s = arguments[i];
|
|
5
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
6
|
+
t[p] = s[p];
|
|
7
|
+
}
|
|
8
|
+
return t;
|
|
9
|
+
};
|
|
10
|
+
return __assign.apply(this, arguments);
|
|
11
|
+
};
|
|
12
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
13
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
14
|
+
if (ar || !(i in from)) {
|
|
15
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
16
|
+
ar[i] = from[i];
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
20
|
+
};
|
|
21
|
+
import { useEffect, useRef, useState } from 'react';
|
|
22
|
+
import { useIsMounted } from './useIsMounted';
|
|
23
|
+
var initialSize = {
|
|
24
|
+
width: undefined,
|
|
25
|
+
height: undefined,
|
|
26
|
+
scrollHeight: undefined,
|
|
27
|
+
clientHeight: undefined,
|
|
28
|
+
textContentHeight: undefined,
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Custom hook for observing the size of an element using the ResizeObserver API.
|
|
32
|
+
*
|
|
33
|
+
* @template T - The type of the element to observe.
|
|
34
|
+
* @param {UseResizeObserverOptions<T>} options - The options for the ResizeObserver. (default is `{}`).
|
|
35
|
+
* @param {RefObject<T>} options.ref - The ref of the element to observe.
|
|
36
|
+
* @param {ResizeHandler} [options.onResize] - When using `onResize`, the hook doesn't re-render on element size changes; it delegates handling to the provided callback . (default is `undefined`).
|
|
37
|
+
* @param {string} [options.box] - The box model to use for the ResizeObserver. (default is `'content-box'`).
|
|
38
|
+
* @returns {UseResizeObserverResult} - The size of the observed element.
|
|
39
|
+
* @see [Documentation](https://usehooks-ts.com/react-hook/use-resize-observer)
|
|
40
|
+
* @see [MDN ResizeObserver API](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver)
|
|
41
|
+
* @example
|
|
42
|
+
* const myRef = useRef(null);
|
|
43
|
+
* const { width = 0, height = 0 } = useResizeObserver({
|
|
44
|
+
* ref: myRef,
|
|
45
|
+
* box: 'content-box',
|
|
46
|
+
* });
|
|
47
|
+
*
|
|
48
|
+
* <div ref={myRef}>Hello, world!</div>
|
|
49
|
+
*/
|
|
50
|
+
export function useResizeObserver(options, deps) {
|
|
51
|
+
var ref = options.ref, _a = options.box, box = _a === void 0 ? 'content-box' : _a;
|
|
52
|
+
var _b = useState(initialSize), _c = _b[0], width = _c.width, height = _c.height, scrollHeight = _c.scrollHeight, clientHeight = _c.clientHeight, textContentHeight = _c.textContentHeight, setSize = _b[1];
|
|
53
|
+
var isMounted = useIsMounted();
|
|
54
|
+
var previousSize = useRef(__assign({}, initialSize));
|
|
55
|
+
var onResize = useRef(undefined);
|
|
56
|
+
onResize.current = options === null || options === void 0 ? void 0 : options.onResize;
|
|
57
|
+
useEffect(function () {
|
|
58
|
+
if (!(ref === null || ref === void 0 ? void 0 : ref.current))
|
|
59
|
+
return;
|
|
60
|
+
var placeholder = document.querySelector('.placeholder');
|
|
61
|
+
if (!placeholder) {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
if (typeof window === 'undefined' || !('ResizeObserver' in window))
|
|
65
|
+
return;
|
|
66
|
+
var observer = new ResizeObserver(function (_a) {
|
|
67
|
+
var _b, _c, _d;
|
|
68
|
+
var entry = _a[0];
|
|
69
|
+
var boxProp = box === 'border-box'
|
|
70
|
+
? 'borderBoxSize'
|
|
71
|
+
: box === 'device-pixel-content-box'
|
|
72
|
+
? 'devicePixelContentBoxSize'
|
|
73
|
+
: 'contentBoxSize';
|
|
74
|
+
var newWidth = extractSize(entry, boxProp, 'inlineSize');
|
|
75
|
+
var newHeight = extractSize(entry, boxProp, 'blockSize');
|
|
76
|
+
var newScrollHeight = entry.target.scrollHeight;
|
|
77
|
+
var newClientHeight = entry.target.clientHeight;
|
|
78
|
+
var newTextContentHeight = (_d = (_c = (_b = entry.target.children[0]) === null || _b === void 0 ? void 0 : _b.children[0]) === null || _c === void 0 ? void 0 : _c.clientHeight) !== null && _d !== void 0 ? _d : 0;
|
|
79
|
+
var hasChanged = previousSize.current.width !== newWidth ||
|
|
80
|
+
previousSize.current.height !== newHeight ||
|
|
81
|
+
previousSize.current.scrollHeight !== newScrollHeight ||
|
|
82
|
+
previousSize.current.clientHeight !== newClientHeight ||
|
|
83
|
+
previousSize.current.textContentHeight !== newTextContentHeight;
|
|
84
|
+
if (hasChanged) {
|
|
85
|
+
var newSize = { width: newWidth, height: newHeight, scrollHeight: newScrollHeight, clientHeight: newClientHeight, textContentHeight: newTextContentHeight };
|
|
86
|
+
previousSize.current.width = newWidth;
|
|
87
|
+
previousSize.current.height = newHeight;
|
|
88
|
+
if (onResize === null || onResize === void 0 ? void 0 : onResize.current) {
|
|
89
|
+
onResize.current(newSize);
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
if (isMounted()) {
|
|
93
|
+
setSize(newSize);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
observer.observe(placeholder.children[0], { box: box });
|
|
99
|
+
return function () {
|
|
100
|
+
observer.disconnect();
|
|
101
|
+
};
|
|
102
|
+
}, __spreadArray([box, ref, isMounted], deps, true));
|
|
103
|
+
return { width: width, height: height, scrollHeight: scrollHeight, clientHeight: clientHeight, textContentHeight: textContentHeight };
|
|
104
|
+
}
|
|
105
|
+
function extractSize(entry, box, sizeType) {
|
|
106
|
+
if (!entry[box]) {
|
|
107
|
+
if (box === 'contentBoxSize') {
|
|
108
|
+
return entry.contentRect[sizeType === 'inlineSize' ? 'width' : 'height'];
|
|
109
|
+
}
|
|
110
|
+
return undefined;
|
|
111
|
+
}
|
|
112
|
+
return Array.isArray(entry[box])
|
|
113
|
+
? entry[box][0][sizeType]
|
|
114
|
+
: // @ts-ignore Support Firefox's non-standard behavior
|
|
115
|
+
entry[box][sizeType];
|
|
116
|
+
}
|