@sheinx/hooks 3.7.0-beta.10 → 3.7.0-beta.12
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/cjs/common/use-position-style/check-element-size.d.ts +10 -0
- package/cjs/common/use-position-style/check-element-size.d.ts.map +1 -0
- package/cjs/common/use-position-style/check-element-size.js +65 -0
- package/cjs/common/use-position-style/index.d.ts.map +1 -1
- package/cjs/common/use-position-style/index.js +5 -1
- package/esm/common/use-position-style/check-element-size.d.ts +10 -0
- package/esm/common/use-position-style/check-element-size.d.ts.map +1 -0
- package/esm/common/use-position-style/check-element-size.js +59 -0
- package/esm/common/use-position-style/index.d.ts.map +1 -1
- package/esm/common/use-position-style/index.js +5 -1
- package/package.json +1 -1
@@ -0,0 +1,10 @@
|
|
1
|
+
export interface Size {
|
2
|
+
width?: number;
|
3
|
+
height?: number;
|
4
|
+
}
|
5
|
+
interface UseCheckElementSizeOptions {
|
6
|
+
enable?: boolean;
|
7
|
+
}
|
8
|
+
export declare const useCheckElementSize: (targetElementRef: React.RefObject<HTMLElement>, options?: UseCheckElementSizeOptions) => Size | null;
|
9
|
+
export {};
|
10
|
+
//# sourceMappingURL=check-element-size.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"check-element-size.d.ts","sourceRoot":"","sources":["check-element-size.ts"],"names":[],"mappings":"AAEA,MAAM,WAAW,IAAI;IACnB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,UAAU,0BAA0B;IAElC,MAAM,CAAC,EAAE,OAAO,CAAC;CAClB;AAED,eAAO,MAAM,mBAAmB,qBACZ,MAAM,SAAS,CAAC,WAAW,CAAC,YACrC,0BAA0B,KAClC,IAAI,GAAG,IAmDT,CAAC"}
|
@@ -0,0 +1,65 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports.useCheckElementSize = void 0;
|
7
|
+
var _react = require("react");
|
8
|
+
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
9
|
+
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
10
|
+
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
11
|
+
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
12
|
+
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
|
13
|
+
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
14
|
+
var useCheckElementSize = exports.useCheckElementSize = function useCheckElementSize(targetElementRef) {
|
15
|
+
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
16
|
+
var enable = options.enable;
|
17
|
+
var _useState = (0, _react.useState)({
|
18
|
+
width: 0,
|
19
|
+
height: 0
|
20
|
+
}),
|
21
|
+
_useState2 = _slicedToArray(_useState, 2),
|
22
|
+
size = _useState2[0],
|
23
|
+
setSize = _useState2[1];
|
24
|
+
var lastSize = (0, _react.useRef)({
|
25
|
+
width: 0,
|
26
|
+
height: 0
|
27
|
+
});
|
28
|
+
var checkSize = (0, _react.useCallback)(function () {
|
29
|
+
if (targetElementRef.current) {
|
30
|
+
var rect = targetElementRef.current.getBoundingClientRect();
|
31
|
+
var newSize = {
|
32
|
+
width: rect.width,
|
33
|
+
height: rect.height
|
34
|
+
};
|
35
|
+
if (newSize.width !== lastSize.current.width || newSize.height !== lastSize.current.height) {
|
36
|
+
setSize(newSize);
|
37
|
+
lastSize.current = newSize;
|
38
|
+
}
|
39
|
+
}
|
40
|
+
}, [targetElementRef]);
|
41
|
+
(0, _react.useEffect)(function () {
|
42
|
+
var _window;
|
43
|
+
if (!enable) return;
|
44
|
+
var element = targetElementRef.current;
|
45
|
+
if (!element) return;
|
46
|
+
|
47
|
+
// 初始检查
|
48
|
+
checkSize();
|
49
|
+
var resizeObserver = null;
|
50
|
+
if ((_window = window) !== null && _window !== void 0 && _window.ResizeObserver) {
|
51
|
+
resizeObserver = new ResizeObserver(checkSize);
|
52
|
+
resizeObserver.observe(element);
|
53
|
+
}
|
54
|
+
|
55
|
+
// 清理函数
|
56
|
+
return function () {
|
57
|
+
var _resizeObserver;
|
58
|
+
(_resizeObserver = resizeObserver) === null || _resizeObserver === void 0 || _resizeObserver.disconnect();
|
59
|
+
};
|
60
|
+
}, [enable, targetElementRef, checkSize]);
|
61
|
+
if (!enable || !targetElementRef) {
|
62
|
+
return null;
|
63
|
+
}
|
64
|
+
return size;
|
65
|
+
};
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AAAA,OAAO,KAA8B,MAAM,OAAO,CAAC;
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AAAA,OAAO,KAA8B,MAAM,OAAO,CAAC;AAUnD,MAAM,MAAM,kBAAkB,GAC1B,aAAa,GACb,UAAU,GACV,cAAc,GACd,WAAW,GACX,MAAM,GACN,OAAO,CAAC;AACZ,MAAM,MAAM,gBAAgB,GACxB,aAAa,GACb,cAAc,GACd,UAAU,GACV,WAAW,GACX,KAAK,GACL,QAAQ,CAAC;AAYb,KAAK,YAAY,GAAG,kBAAkB,GAAG,gBAAgB,GAAG,OAAO,CAAC;AACpE,MAAM,WAAW,mBAAmB;IAClC,QAAQ,EAAE,YAAY,GAAG,SAAS,CAAC;IACnC,QAAQ,EAAE,OAAO,CAAC;IAClB,IAAI,EAAE,OAAO,CAAC;IAEd,WAAW,EAAE,KAAK,CAAC,SAAS,CAAC,WAAW,CAAC,CAAC;IAE1C,UAAU,EAAE,KAAK,CAAC,SAAS,CAAC,WAAW,CAAC,CAAC;IAEzC,WAAW,CAAC,EAAE,KAAK,CAAC,SAAS,CAAC,WAAW,CAAC,CAAC;IAC3C,YAAY,EAAE,MAAM,WAAW,GAAG,IAAI,GAAG,SAAS,CAAC;IACnD,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,UAAU,CAAC,EAAE,OAAO,GAAG,KAAK,CAAC;IAC7B,SAAS,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IAC5B,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,MAAM,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAC3B;AAOD,eAAO,MAAM,gBAAgB,WAAY,mBAAmB;;CAwR3D,CAAC;AAEF,eAAe,gBAAgB,CAAC"}
|
@@ -10,6 +10,7 @@ var _react = _interopRequireWildcard(require("react"));
|
|
10
10
|
var _getPositionStyle = require("./get-position-style");
|
11
11
|
var _checkPosition = require("./check-position");
|
12
12
|
var _checkBorder = require("./check-border");
|
13
|
+
var _checkElementSize = require("./check-element-size");
|
13
14
|
var _shallowEqual = _interopRequireDefault(require("../../utils/shallow-equal"));
|
14
15
|
var _usePersistFn = _interopRequireDefault(require("../use-persist-fn"));
|
15
16
|
var _utils = require("../../utils");
|
@@ -76,6 +77,9 @@ var usePositionStyle = exports.usePositionStyle = function usePositionStyle(conf
|
|
76
77
|
var parentElBorderWidth = (0, _checkBorder.useCheckElementBorderWidth)(parentElRef, {
|
77
78
|
direction: 'horizontal'
|
78
79
|
});
|
80
|
+
var popupElSize = (0, _checkElementSize.useCheckElementSize)(popupElRef, {
|
81
|
+
enable: show
|
82
|
+
});
|
79
83
|
var adjustPosition = function adjustPosition(position) {
|
80
84
|
var winHeight = _utils.docSize.height;
|
81
85
|
if (!verticalPosition.includes(position)) return position;
|
@@ -312,7 +316,7 @@ var usePositionStyle = exports.usePositionStyle = function usePositionStyle(conf
|
|
312
316
|
// 当父元素的滚动容器滚动时,判断是否需要更新弹出层位置,包括是否隐藏弹出层(通过hideStyle隐藏,不是show状态)
|
313
317
|
context.prevParentPosition = parentElNewPosition;
|
314
318
|
});
|
315
|
-
(0, _react.useEffect)(updateStyle, [show, position, absolute, updateKey, fixedWidth, parentElNewPosition]);
|
319
|
+
(0, _react.useEffect)(updateStyle, [show, position, absolute, updateKey, fixedWidth, parentElNewPosition, popupElSize]);
|
316
320
|
return {
|
317
321
|
style: style
|
318
322
|
};
|
@@ -0,0 +1,10 @@
|
|
1
|
+
export interface Size {
|
2
|
+
width?: number;
|
3
|
+
height?: number;
|
4
|
+
}
|
5
|
+
interface UseCheckElementSizeOptions {
|
6
|
+
enable?: boolean;
|
7
|
+
}
|
8
|
+
export declare const useCheckElementSize: (targetElementRef: React.RefObject<HTMLElement>, options?: UseCheckElementSizeOptions) => Size | null;
|
9
|
+
export {};
|
10
|
+
//# sourceMappingURL=check-element-size.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"check-element-size.d.ts","sourceRoot":"","sources":["check-element-size.ts"],"names":[],"mappings":"AAEA,MAAM,WAAW,IAAI;IACnB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,UAAU,0BAA0B;IAElC,MAAM,CAAC,EAAE,OAAO,CAAC;CAClB;AAED,eAAO,MAAM,mBAAmB,qBACZ,MAAM,SAAS,CAAC,WAAW,CAAC,YACrC,0BAA0B,KAClC,IAAI,GAAG,IAmDT,CAAC"}
|
@@ -0,0 +1,59 @@
|
|
1
|
+
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
2
|
+
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
3
|
+
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
4
|
+
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
5
|
+
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
|
6
|
+
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
7
|
+
import { useState, useEffect, useRef, useCallback } from 'react';
|
8
|
+
export var useCheckElementSize = function useCheckElementSize(targetElementRef) {
|
9
|
+
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
10
|
+
var enable = options.enable;
|
11
|
+
var _useState = useState({
|
12
|
+
width: 0,
|
13
|
+
height: 0
|
14
|
+
}),
|
15
|
+
_useState2 = _slicedToArray(_useState, 2),
|
16
|
+
size = _useState2[0],
|
17
|
+
setSize = _useState2[1];
|
18
|
+
var lastSize = useRef({
|
19
|
+
width: 0,
|
20
|
+
height: 0
|
21
|
+
});
|
22
|
+
var checkSize = useCallback(function () {
|
23
|
+
if (targetElementRef.current) {
|
24
|
+
var rect = targetElementRef.current.getBoundingClientRect();
|
25
|
+
var newSize = {
|
26
|
+
width: rect.width,
|
27
|
+
height: rect.height
|
28
|
+
};
|
29
|
+
if (newSize.width !== lastSize.current.width || newSize.height !== lastSize.current.height) {
|
30
|
+
setSize(newSize);
|
31
|
+
lastSize.current = newSize;
|
32
|
+
}
|
33
|
+
}
|
34
|
+
}, [targetElementRef]);
|
35
|
+
useEffect(function () {
|
36
|
+
var _window;
|
37
|
+
if (!enable) return;
|
38
|
+
var element = targetElementRef.current;
|
39
|
+
if (!element) return;
|
40
|
+
|
41
|
+
// 初始检查
|
42
|
+
checkSize();
|
43
|
+
var resizeObserver = null;
|
44
|
+
if ((_window = window) !== null && _window !== void 0 && _window.ResizeObserver) {
|
45
|
+
resizeObserver = new ResizeObserver(checkSize);
|
46
|
+
resizeObserver.observe(element);
|
47
|
+
}
|
48
|
+
|
49
|
+
// 清理函数
|
50
|
+
return function () {
|
51
|
+
var _resizeObserver;
|
52
|
+
(_resizeObserver = resizeObserver) === null || _resizeObserver === void 0 || _resizeObserver.disconnect();
|
53
|
+
};
|
54
|
+
}, [enable, targetElementRef, checkSize]);
|
55
|
+
if (!enable || !targetElementRef) {
|
56
|
+
return null;
|
57
|
+
}
|
58
|
+
return size;
|
59
|
+
};
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AAAA,OAAO,KAA8B,MAAM,OAAO,CAAC;
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AAAA,OAAO,KAA8B,MAAM,OAAO,CAAC;AAUnD,MAAM,MAAM,kBAAkB,GAC1B,aAAa,GACb,UAAU,GACV,cAAc,GACd,WAAW,GACX,MAAM,GACN,OAAO,CAAC;AACZ,MAAM,MAAM,gBAAgB,GACxB,aAAa,GACb,cAAc,GACd,UAAU,GACV,WAAW,GACX,KAAK,GACL,QAAQ,CAAC;AAYb,KAAK,YAAY,GAAG,kBAAkB,GAAG,gBAAgB,GAAG,OAAO,CAAC;AACpE,MAAM,WAAW,mBAAmB;IAClC,QAAQ,EAAE,YAAY,GAAG,SAAS,CAAC;IACnC,QAAQ,EAAE,OAAO,CAAC;IAClB,IAAI,EAAE,OAAO,CAAC;IAEd,WAAW,EAAE,KAAK,CAAC,SAAS,CAAC,WAAW,CAAC,CAAC;IAE1C,UAAU,EAAE,KAAK,CAAC,SAAS,CAAC,WAAW,CAAC,CAAC;IAEzC,WAAW,CAAC,EAAE,KAAK,CAAC,SAAS,CAAC,WAAW,CAAC,CAAC;IAC3C,YAAY,EAAE,MAAM,WAAW,GAAG,IAAI,GAAG,SAAS,CAAC;IACnD,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,UAAU,CAAC,EAAE,OAAO,GAAG,KAAK,CAAC;IAC7B,SAAS,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IAC5B,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,MAAM,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAC3B;AAOD,eAAO,MAAM,gBAAgB,WAAY,mBAAmB;;CAwR3D,CAAC;AAEF,eAAe,gBAAgB,CAAC"}
|
@@ -9,6 +9,7 @@ import React, { useEffect, useState } from 'react';
|
|
9
9
|
import { getPositionStyle } from "./get-position-style";
|
10
10
|
import { useCheckElementPosition } from "./check-position";
|
11
11
|
import { useCheckElementBorderWidth } from "./check-border";
|
12
|
+
import { useCheckElementSize } from "./check-element-size";
|
12
13
|
import shallowEqual from "../../utils/shallow-equal";
|
13
14
|
import usePersistFn from "../use-persist-fn";
|
14
15
|
import { getCurrentCSSZoom } from "../../utils";
|
@@ -67,6 +68,9 @@ export var usePositionStyle = function usePositionStyle(config) {
|
|
67
68
|
var parentElBorderWidth = useCheckElementBorderWidth(parentElRef, {
|
68
69
|
direction: 'horizontal'
|
69
70
|
});
|
71
|
+
var popupElSize = useCheckElementSize(popupElRef, {
|
72
|
+
enable: show
|
73
|
+
});
|
70
74
|
var adjustPosition = function adjustPosition(position) {
|
71
75
|
var winHeight = docSize.height;
|
72
76
|
if (!verticalPosition.includes(position)) return position;
|
@@ -303,7 +307,7 @@ export var usePositionStyle = function usePositionStyle(config) {
|
|
303
307
|
// 当父元素的滚动容器滚动时,判断是否需要更新弹出层位置,包括是否隐藏弹出层(通过hideStyle隐藏,不是show状态)
|
304
308
|
context.prevParentPosition = parentElNewPosition;
|
305
309
|
});
|
306
|
-
useEffect(updateStyle, [show, position, absolute, updateKey, fixedWidth, parentElNewPosition]);
|
310
|
+
useEffect(updateStyle, [show, position, absolute, updateKey, fixedWidth, parentElNewPosition, popupElSize]);
|
307
311
|
return {
|
308
312
|
style: style
|
309
313
|
};
|