@cutting/use-get-parent-size

中文文档
2.15.1发布于 2026/10/27MITTypeScriptES Module构建来源已验证近 30 天下载 2708

从一个被观测到的 DOM 元素的高度,宽度等大小观测器中获取调整大小的事件 .

中文使用文档
文档目录7 节
  1. 安装
  2. 使用亲子关系
  3. 使用量
  4. 选项
  5. 使用量
  6. 用于正确调整 svg 大小
  7. 只使用回调

@ 剪切/ use- get- parent- small -- 从一个缩放的观测器上调整所观察到的 DOM 元素的高度、 宽度等的大小 .

npm version code style: oxfmt

安装

pnpm add @cutting/use-get-parent-size

# or

npm install @cutting/use-get-parent-size

使用亲子关系

一个反应钩子,允许您使用一个缩放观察员来测量元素大小.

使用量

const { width, height } = useParentSize(ref, options);

useParentSize 进行反应 参考对象 和可选 options 对象.

返回所观察到元素的内容矩形 :

interface DOMRectReadOnly {
  readonly bottom: number;
  readonly height: number;
  readonly left: number;
  readonly right: number;
  readonly top: number;
  readonly width: number;
  readonly x: number;
  readonly y: number;
  toJSON(): any;
}

选项

export interface UseParentSizeOptions {
  debounceDelay: number;
  initialValues: Partial<ResizeObserverContentRect>;
  transformFunc?: (entry: Partial<ResizeObserverContentRect>) => Partial<ResizeObserverContentRect>;
  maxDifference?: number;
  transformFunc?: ({ width, height }: Dimensions) => Dimensions;
  callback?(entry: ResizeObserverContentRect): void;
}
  • debounceDelay - 默认500毫秒可选 number 它将拖动将事件重新分解到调用码的速度.
  • initialValues - 起初 ref 将会是 null 没有 width 或者说 height 值可以返回到挂起为止。 这个 initialValues 选项可以返回特定的 width 财务报告和审定财务报表 height 数值直到 ref 实际引用有效的 DOM 节点。 譬如说. const { width, height } = useParentSize(ref, { width: 100, height: 50})· ;

默认为空 Domrect :

{
 bottom: undefined,
 height: undefined,
 left: undefined,
 width: undefined,
 right: undefined,
 top: undefined,
 x: undefined,
 y: undefined,
};
  • transformFunc 可选函数可转换结果,例如将母体的大小减半

    transformFunc: ({ width, height }) => ({
      width: width / 2,
      height: height / 2,
    });
    

    默认是身份, (x) => x

  • maxDifference (default 10) - 使用 ParrentSize 将当前 Domrect 值存储在内存中,如果事件发生器的大小调整,这些值会在调整后与新的 Domrect 值相核对. 如果两个值的相差大于 maxDifference 然后通知客户端.

  • callback 可以提供每次调整大小时执行的函数.

使用量

用于正确调整 svg 大小

import { useRef } from 'react';
import type { UseParentSizeOptions } from '@cutting/use-get-parent-size';

export function ResponsiveSVG({
  children
}): JSX.Element => {
  const ref = useRef<HTMLDivElement>(null);
  const { width, height } = useParentSize(ref, options);
  const aspect = width / height;

  const adjustedHeight = Math.ceil(width / aspect);

  return (
    <div ref={ref}>
      <svg
        viewBox={`${origin.x} ${origin.y} ${width} ${adjustedHeight}`}
      >
        {children}
      </svg>
    </div>
  );
};

只使用回调

const Pipeline = () => {
  const controller = useVisualizationController();
  const containerRef = useRef<HTMLDivElement>(null);

  useParentSize(containerRef, {
    callback: () => {
      controller.getGraph().fit(70);
    },
    debounceDelay: 500,
  });

版本与发布记录

@cutting/use-get-parent-size 共发布 96 个版本,首次发布于 2021/05/31;过去 12 个月发布了 12 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
2.15.1latest2026/10/2714 天前
2.15.02026/10/2121 天前
2.14.02026/09/102 个月前
2.13.02026/09/092 个月前
2.12.02026/09/062 个月前
2.11.12026/07/213 个月前

关于 @cutting/use-get-parent-size 的常见问题

@cutting/use-get-parent-size 是什么?

从一个被观测到的 DOM 元素的高度,宽度等大小观测器中获取调整大小的事件 .

如何安装 @cutting/use-get-parent-size?

在项目目录运行 npm install @cutting/use-get-parent-size,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(19.3.1)、three(0.186.1)、react-dom(19.3.1)、react-router(8.5.0)、use-debounce(10.1.1)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@cutting/use-get-parent-size 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@cutting/use-get-parent-size 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@cutting/use-get-parent-size 可以商用吗?

@cutting/use-get-parent-size 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@cutting/use-get-parent-size 的下载量有多少?

根据 npm 官方下载统计,@cutting/use-get-parent-size 在 2026/10/11 – 2026/11/09 这 30 天内被下载 2,708 次,日均约 90 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@cutting/use-get-parent-size 还在维护吗?

最新版本 2.15.1 发布于 2026/10/27(14 天前);至今共发布 96 个版本,其中过去 12 个月发布了 12 个;首次发布于 2021/05/31。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。