@sheinx/hooks 3.9.4 → 3.9.5-beta.2

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.
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AAAA,OAAO,KAA+C,MAAM,OAAO,CAAC;AAWpE,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,QAAQ,CAAC,EAAE,CAAC,QAAQ,EAAE,kBAAkB,GAAG,gBAAgB,KAAK,IAAI,CAAC;IAErE,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB,MAAM,CAAC,EAAE,C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Q,CAAC,EAAE,CAAC,QAAQ,EAAE,kBAAkB,GAAG,gBAAgB,KAAK,IAAI,CAAC;IAErE,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB,MAAM,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC1B,QAAQ,CAAC,EAAE,MAAM,WAAW,GAAG,IAAI,CAAC;IACpC,cAAc,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,CAAC,aAAa,KAAK,IAAI,CAAC;CACpD;AAsBD,eAAO,MAAM,gBAAgB,WAAY,mBAAmB;;CAmW3D,CAAC;AAEF,eAAe,gBAAgB,CAAC"}
@@ -333,19 +333,23 @@ var usePositionStyle = exports.usePositionStyle = function usePositionStyle(conf
333
333
  }
334
334
  var _getAbsolutePositionS = getAbsolutePositionStyle(rect, position),
335
335
  style = _getAbsolutePositionS.style;
336
- var currentCSSZoom = (0, _utils.getCurrentCSSZoom)();
337
- if (currentCSSZoom && currentCSSZoom !== 1) {
336
+
337
+ // 计算从触发元素到挂载容器之间的相对 zoom
338
+ var rootContainer = getContainer() || document.body;
339
+ var relativeZoom = (0, _utils.getRelativeZoom)(parentElRef.current, rootContainer);
340
+ if (relativeZoom && relativeZoom !== 1) {
341
+ var zoomRatio = 1 / relativeZoom;
338
342
  if (style.left && typeof style.left === 'number') {
339
- style.left = style.left * (1 / currentCSSZoom);
343
+ style.left = style.left * zoomRatio;
340
344
  }
341
345
  if (style.top && typeof style.top === 'number') {
342
- style.top = style.top * (1 / currentCSSZoom);
346
+ style.top = style.top * zoomRatio;
343
347
  }
344
348
  if (style.right && typeof style.right === 'number') {
345
- style.right = style.right * (1 / currentCSSZoom);
349
+ style.right = style.right * zoomRatio;
346
350
  }
347
351
  if (style.bottom && typeof style.bottom === 'number') {
348
- style.bottom = style.bottom * (1 / currentCSSZoom);
352
+ style.bottom = style.bottom * zoomRatio;
349
353
  }
350
354
  }
351
355
  return {
@@ -45,7 +45,7 @@ var usePagination = function usePagination(props) {
45
45
  var handleChange = (0, _usePersistFn.usePersistFn)(function (c, size) {
46
46
  if (c === current && size === undefined) return;
47
47
  setCurrent(c);
48
- setPageSize(size || pageSizeProp);
48
+ setPageSize(size || pageSize);
49
49
  if (onChange) {
50
50
  var sizeChange = size !== undefined && pageSize !== size;
51
51
  onChange(c, size || pageSize, sizeChange);
@@ -1,4 +1,12 @@
1
- export declare const getCurrentCSSZoom: () => number;
1
+ /**
2
+ * 计算容器的累积 zoom 值
3
+ * 当弹出层通过 position: absolute 定位在容器内时,需要用容器的 zoom 值来修正坐标
4
+ * 因为 getBoundingClientRect() 返回的是应用 zoom 后的视口坐标,
5
+ * 但 CSS 的 left/top 值会在容器的 zoom 坐标系中解释
6
+ * @param container - 容器元素,默认为 document.body
7
+ * @returns 容器的累积 zoom 值
8
+ */
9
+ export declare const getRelativeZoom: (_element: HTMLElement | null, container?: HTMLElement | null) => number;
2
10
  export declare const getScrollPosition: (element?: HTMLElement | null) => {
3
11
  scrollTop: number;
4
12
  scrollLeft: number;
@@ -1 +1 @@
1
- {"version":3,"file":"document.d.ts","sourceRoot":"","sources":["document.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,iBAAiB,QAAO,MAqBpC,CAAA;AA6BD,eAAO,MAAM,iBAAiB,aAAc,WAAW,GAAG,IAAI;;;CAc7D,CAAC"}
1
+ {"version":3,"file":"document.d.ts","sourceRoot":"","sources":["document.ts"],"names":[],"mappings":"AAIA;;;;;;;GAOG;AACH,eAAO,MAAM,eAAe,aAAc,WAAW,GAAG,IAAI,cAAa,WAAW,GAAG,IAAI,KAAmB,MA8B7G,CAAC;AAEF,eAAO,MAAM,iBAAiB,aAAc,WAAW,GAAG,IAAI;;;CAc7D,CAAC"}
@@ -3,51 +3,47 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.getScrollPosition = exports.getCurrentCSSZoom = void 0;
7
- var cachedZoom = 0;
8
- var getCurrentCSSZoom = exports.getCurrentCSSZoom = function getCurrentCSSZoom() {
9
- if (cachedZoom) return cachedZoom;
10
- if (typeof window === 'undefined' || typeof navigator === 'undefined' || !document.body) {
11
- return 1;
12
- }
13
- var currentCSSZoom = Math.round(document.body.getBoundingClientRect().width) / document.body.clientWidth;
14
- if (window.ResizeObserver) {
15
- // 监听document.body的变化,更新缓存的zoom
16
- var resizeObserver = new ResizeObserver(function () {
17
- cachedZoom = Math.round(document.body.getBoundingClientRect().width) / document.body.clientWidth;
18
- });
19
- resizeObserver.observe(document.body);
20
- }
21
- cachedZoom = currentCSSZoom;
22
- return currentCSSZoom;
23
- };
24
-
25
- // export const getZoomBoundingClientRect = (element: HTMLElement) => {
26
- // const currentCSSZoom = getCurrentCSSZoom()
27
- // if (currentCSSZoom === 1 || !currentCSSZoom) {
28
- // return element.getBoundingClientRect()
29
- // }
30
- // const isNotZoom = currentCSSZoom === 1 || !currentCSSZoom
6
+ exports.getScrollPosition = exports.getRelativeZoom = void 0;
7
+ // 缓存容器的 zoom 值,避免重复计算
8
+ // 使用 WeakMap 确保容器元素被销毁时缓存也会被清理
9
+ var zoomCache = new WeakMap();
31
10
 
32
- // if (isNotZoom) {
33
- // return element.getBoundingClientRect()
34
- // }
11
+ /**
12
+ * 计算容器的累积 zoom 值
13
+ * 当弹出层通过 position: absolute 定位在容器内时,需要用容器的 zoom 值来修正坐标
14
+ * 因为 getBoundingClientRect() 返回的是应用 zoom 后的视口坐标,
15
+ * 但 CSS 的 left/top 值会在容器的 zoom 坐标系中解释
16
+ * @param container - 容器元素,默认为 document.body
17
+ * @returns 容器的累积 zoom 值
18
+ */
19
+ var getRelativeZoom = exports.getRelativeZoom = function getRelativeZoom(_element) {
20
+ var container = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : document.body;
21
+ if (!container || typeof window === 'undefined') return 1;
35
22
 
36
- // const zoomRatio = 1 / currentCSSZoom
37
- // const rect = element.getBoundingClientRect()
23
+ // 检查缓存
24
+ var cached = zoomCache.get(container);
25
+ if (cached !== undefined) {
26
+ return cached;
27
+ }
28
+ var zoom = 1;
29
+ var current = container;
38
30
 
39
- // return {
40
- // x: rect.x * zoomRatio,
41
- // y: rect.y * zoomRatio,
42
- // top: rect.top * zoomRatio,
43
- // right: rect.right * zoomRatio,
44
- // bottom: rect.bottom * zoomRatio,
45
- // left: rect.left * zoomRatio,
46
- // width: rect.width * zoomRatio,
47
- // height: rect.height * zoomRatio,
48
- // }
49
- // }
31
+ // 从容器向上遍历到 document,累积所有的 zoom 值
32
+ while (current && current !== document.documentElement) {
33
+ var computedStyle = window.getComputedStyle(current);
34
+ // zoom 不是标准 CSS 属性,需要通过索引访问
35
+ var zoomValue = computedStyle.zoom || '1';
36
+ var currentZoom = parseFloat(zoomValue);
37
+ if (!isNaN(currentZoom) && currentZoom !== 0) {
38
+ zoom *= currentZoom;
39
+ }
40
+ current = current.parentElement;
41
+ }
50
42
 
43
+ // 缓存计算结果
44
+ zoomCache.set(container, zoom);
45
+ return zoom;
46
+ };
51
47
  var getScrollPosition = exports.getScrollPosition = function getScrollPosition(element) {
52
48
  if (!element) return {
53
49
  scrollTop: 0,
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AAAA,OAAO,KAA+C,MAAM,OAAO,CAAC;AAWpE,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,CAA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AAQ,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,QAAQ,CAAC,EAAE,CAAC,QAAQ,EAAE,kBAAkB,GAAG,gBAAgB,KAAK,IAAI,CAAC;IAErE,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB,MAAM,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC1B,QAAQ,CAAC,EAAE,MAAM,WAAW,GAAG,IAAI,CAAC;IACpC,cAAc,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,CAAC,aAAa,KAAK,IAAI,CAAC;CACpD;AAsBD,eAAO,MAAM,gBAAgB,WAAY,mBAAmB;;CAmW3D,CAAC;AAEF,eAAe,gBAAgB,CAAC"}
@@ -13,7 +13,7 @@ import { useCheckElementBorderWidth } from "./check-border";
13
13
  import { useCheckElementSize } from "./check-element-size";
14
14
  import shallowEqual from "../../utils/shallow-equal";
15
15
  import usePersistFn from "../use-persist-fn";
16
- import { getCurrentCSSZoom, getScrollPosition } from "../../utils";
16
+ import { getScrollPosition, getRelativeZoom } from "../../utils";
17
17
  import { docSize } from "../../utils";
18
18
  var horizontalPosition = ['left-bottom', 'left-top', 'right-bottom', 'right-top', 'left', 'right'];
19
19
  var verticalPosition = ['bottom-left', 'bottom-right', 'top-left', 'top-right', 'bottom', 'top'];
@@ -324,19 +324,23 @@ export var usePositionStyle = function usePositionStyle(config) {
324
324
  }
325
325
  var _getAbsolutePositionS = getAbsolutePositionStyle(rect, position),
326
326
  style = _getAbsolutePositionS.style;
327
- var currentCSSZoom = getCurrentCSSZoom();
328
- if (currentCSSZoom && currentCSSZoom !== 1) {
327
+
328
+ // 计算从触发元素到挂载容器之间的相对 zoom
329
+ var rootContainer = getContainer() || document.body;
330
+ var relativeZoom = getRelativeZoom(parentElRef.current, rootContainer);
331
+ if (relativeZoom && relativeZoom !== 1) {
332
+ var zoomRatio = 1 / relativeZoom;
329
333
  if (style.left && typeof style.left === 'number') {
330
- style.left = style.left * (1 / currentCSSZoom);
334
+ style.left = style.left * zoomRatio;
331
335
  }
332
336
  if (style.top && typeof style.top === 'number') {
333
- style.top = style.top * (1 / currentCSSZoom);
337
+ style.top = style.top * zoomRatio;
334
338
  }
335
339
  if (style.right && typeof style.right === 'number') {
336
- style.right = style.right * (1 / currentCSSZoom);
340
+ style.right = style.right * zoomRatio;
337
341
  }
338
342
  if (style.bottom && typeof style.bottom === 'number') {
339
- style.bottom = style.bottom * (1 / currentCSSZoom);
343
+ style.bottom = style.bottom * zoomRatio;
340
344
  }
341
345
  }
342
346
  return {
@@ -39,7 +39,7 @@ var usePagination = function usePagination(props) {
39
39
  var handleChange = usePersistFn(function (c, size) {
40
40
  if (c === current && size === undefined) return;
41
41
  setCurrent(c);
42
- setPageSize(size || pageSizeProp);
42
+ setPageSize(size || pageSize);
43
43
  if (onChange) {
44
44
  var sizeChange = size !== undefined && pageSize !== size;
45
45
  onChange(c, size || pageSize, sizeChange);
@@ -1,4 +1,12 @@
1
- export declare const getCurrentCSSZoom: () => number;
1
+ /**
2
+ * 计算容器的累积 zoom 值
3
+ * 当弹出层通过 position: absolute 定位在容器内时,需要用容器的 zoom 值来修正坐标
4
+ * 因为 getBoundingClientRect() 返回的是应用 zoom 后的视口坐标,
5
+ * 但 CSS 的 left/top 值会在容器的 zoom 坐标系中解释
6
+ * @param container - 容器元素,默认为 document.body
7
+ * @returns 容器的累积 zoom 值
8
+ */
9
+ export declare const getRelativeZoom: (_element: HTMLElement | null, container?: HTMLElement | null) => number;
2
10
  export declare const getScrollPosition: (element?: HTMLElement | null) => {
3
11
  scrollTop: number;
4
12
  scrollLeft: number;
@@ -1 +1 @@
1
- {"version":3,"file":"document.d.ts","sourceRoot":"","sources":["document.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,iBAAiB,QAAO,MAqBpC,CAAA;AA6BD,eAAO,MAAM,iBAAiB,aAAc,WAAW,GAAG,IAAI;;;CAc7D,CAAC"}
1
+ {"version":3,"file":"document.d.ts","sourceRoot":"","sources":["document.ts"],"names":[],"mappings":"AAIA;;;;;;;GAOG;AACH,eAAO,MAAM,eAAe,aAAc,WAAW,GAAG,IAAI,cAAa,WAAW,GAAG,IAAI,KAAmB,MA8B7G,CAAC;AAEF,eAAO,MAAM,iBAAiB,aAAc,WAAW,GAAG,IAAI;;;CAc7D,CAAC"}
@@ -1,47 +1,43 @@
1
- var cachedZoom = 0;
2
- export var getCurrentCSSZoom = function getCurrentCSSZoom() {
3
- if (cachedZoom) return cachedZoom;
4
- if (typeof window === 'undefined' || typeof navigator === 'undefined' || !document.body) {
5
- return 1;
6
- }
7
- var currentCSSZoom = Math.round(document.body.getBoundingClientRect().width) / document.body.clientWidth;
8
- if (window.ResizeObserver) {
9
- // 监听document.body的变化,更新缓存的zoom
10
- var resizeObserver = new ResizeObserver(function () {
11
- cachedZoom = Math.round(document.body.getBoundingClientRect().width) / document.body.clientWidth;
12
- });
13
- resizeObserver.observe(document.body);
14
- }
15
- cachedZoom = currentCSSZoom;
16
- return currentCSSZoom;
17
- };
18
-
19
- // export const getZoomBoundingClientRect = (element: HTMLElement) => {
20
- // const currentCSSZoom = getCurrentCSSZoom()
21
- // if (currentCSSZoom === 1 || !currentCSSZoom) {
22
- // return element.getBoundingClientRect()
23
- // }
24
- // const isNotZoom = currentCSSZoom === 1 || !currentCSSZoom
1
+ // 缓存容器的 zoom 值,避免重复计算
2
+ // 使用 WeakMap 确保容器元素被销毁时缓存也会被清理
3
+ var zoomCache = new WeakMap();
25
4
 
26
- // if (isNotZoom) {
27
- // return element.getBoundingClientRect()
28
- // }
5
+ /**
6
+ * 计算容器的累积 zoom 值
7
+ * 当弹出层通过 position: absolute 定位在容器内时,需要用容器的 zoom 值来修正坐标
8
+ * 因为 getBoundingClientRect() 返回的是应用 zoom 后的视口坐标,
9
+ * 但 CSS 的 left/top 值会在容器的 zoom 坐标系中解释
10
+ * @param container - 容器元素,默认为 document.body
11
+ * @returns 容器的累积 zoom 值
12
+ */
13
+ export var getRelativeZoom = function getRelativeZoom(_element) {
14
+ var container = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : document.body;
15
+ if (!container || typeof window === 'undefined') return 1;
29
16
 
30
- // const zoomRatio = 1 / currentCSSZoom
31
- // const rect = element.getBoundingClientRect()
17
+ // 检查缓存
18
+ var cached = zoomCache.get(container);
19
+ if (cached !== undefined) {
20
+ return cached;
21
+ }
22
+ var zoom = 1;
23
+ var current = container;
32
24
 
33
- // return {
34
- // x: rect.x * zoomRatio,
35
- // y: rect.y * zoomRatio,
36
- // top: rect.top * zoomRatio,
37
- // right: rect.right * zoomRatio,
38
- // bottom: rect.bottom * zoomRatio,
39
- // left: rect.left * zoomRatio,
40
- // width: rect.width * zoomRatio,
41
- // height: rect.height * zoomRatio,
42
- // }
43
- // }
25
+ // 从容器向上遍历到 document,累积所有的 zoom 值
26
+ while (current && current !== document.documentElement) {
27
+ var computedStyle = window.getComputedStyle(current);
28
+ // zoom 不是标准 CSS 属性,需要通过索引访问
29
+ var zoomValue = computedStyle.zoom || '1';
30
+ var currentZoom = parseFloat(zoomValue);
31
+ if (!isNaN(currentZoom) && currentZoom !== 0) {
32
+ zoom *= currentZoom;
33
+ }
34
+ current = current.parentElement;
35
+ }
44
36
 
37
+ // 缓存计算结果
38
+ zoomCache.set(container, zoom);
39
+ return zoom;
40
+ };
45
41
  export var getScrollPosition = function getScrollPosition(element) {
46
42
  if (!element) return {
47
43
  scrollTop: 0,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sheinx/hooks",
3
- "version": "3.9.4",
3
+ "version": "3.9.5-beta.2",
4
4
  "description": "",
5
5
  "keywords": [],
6
6
  "license": "MIT",