@cutting/use-get-parent-size

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

Get resize events of an observed DOM element's height, width etc. from a resize-observer.

原始使用文档
文档目录7 节
  1. install
  2. useParentSize
  3. usage
  4. Options
  5. Usage
  6. Used to properly resize an svg
  7. Used with callback only

@cutting/use-get-parent-size - Get resize events of an observed DOM element's height, width etc. from a resize-observer.

npm version code style: oxfmt

install

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

# or

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

useParentSize

A React hook that allows you to use a ResizeObserver to measure an element's size.

usage

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

useParentSize takes a react ref object and an optional options object.

Returns the contentRect of the observed element:

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;
}

Options

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 - default 500ms. an optional number that will throttle the speed at which reize events are raised to the calling code.
  • initialValues - initially, the ref will be null and no width or height values can be returned until it is mounted. The initialValues option can return a specific width and height value until the ref actually references a valid DOM node. e.g. const { width, height } = useParentSize(ref, { width: 100, height: 50});

Default is an empty DomRect:

{
 bottom: undefined,
 height: undefined,
 left: undefined,
 width: undefined,
 right: undefined,
 top: undefined,
 x: undefined,
 y: undefined,
};
  • transformFunc optional function to transform the results, e.g. to halve the size of the parent

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

    Default is identity, (x) => x

  • maxDifference (default 10) - useParentSize stores the current DomRect values in memory and if a resize event occurrs, these values are checked against the new DomRect values after the resize. If the difference between the two values is greater than the maxDifference option then clients are notified.

  • callback a function can be provided that is executed on each resize.

Usage

Used to properly resize an 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>
  );
};

Used with callback only

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 是什么?

Get resize events of an observed DOM element's height, width etc. from a resize-observer.

如何安装 @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 和提交记录判断。