npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@ohkit/measure

v0.0.6

Published

可观测dom尺寸变化的高阶容器组件

Readme

@ohkit/measure

React组件尺寸测量工具,用于获取子元素的内容区域尺寸信息

安装

npm install @ohkit/measure

功能特性

  • 测量子元素的尺寸信息(宽/高/位置等)
  • 支持响应式尺寸变化监测
  • 提供简洁的API获取测量结果
  • 兼容多种React组件类型

基本用法

import Measure from '@ohkit/measure';

function Example() {
  return (
    <Measure>
      {({ measureRef, contentRect }) => (
        <div ref={measureRef}>
          <p>测量区域内容</p>
          {contentRect && (
            <div>
              宽度: {contentRect.client.width}px
            </div>
          )}
        </div>
      )}
    </Measure>
  );
}

详见示例

API 说明

Props

| 参数 | 说明 | 类型 | 默认值 | |------|------|------|--------| | children | 渲染函数,接收测量相关参数 | ({ measureRef, measure, contentRect }) => ReactNode | - | | innerRef | 获取被测量元素的ref | RefObject | (instance: HTMLElement | null) => void | - | | onResize | 尺寸变化回调函数 | (contentRect: IContentRect) => void | - | | throttleMs | resize事件节流时间(ms) | number | 200 | | client | 是否测量client尺寸 | boolean | true | | offset | 是否测量offset尺寸 | boolean | true | | scroll | 是否测量scroll尺寸 | boolean | false | | bounds | 是否测量边界位置 | boolean | false | | position | 是否测量相对位置与bounds相比少了宽高 | boolean | false | | margin | 是否测量margin | boolean | false | | triggerResizeInit | 是否在组件挂载时立即触发一次resize事件 | boolean | true |

测量参数

children函数接收一个对象参数,包含以下属性:

  • measureRef: 需要绑定到被测量元素的ref
  • measure: 手动触发测量的函数
  • contentRect: 测量结果对象,包含以下属性:
    • entry: ResizeObserver返回的原始尺寸 {width, height, top, right, bottom, left, x, y}
    • client: {width, height, top, left}
    • offset: {width, height, top, left}
    • scroll: {width, height, top, left}
    • bounds: {top, right, bottom, left, width, height}
    • position: {top, right, bottom, left}
    • margin: {top, right, bottom, left}

高级用法

自定义测量属性

<Measure client={true} offset={false} onResize={(contentRect) => {
    console.log(`Client Width: ${contentRect.client.width}px`);
    console.log(`Offset Width: ${contentRect.client.width}px`);
}}>
  {({ measureRef, contentRect }) => (
    <div ref={measureRef}>
      <p>测量区域内容</p>
    </div>
  )}
</Measure>

监测尺寸变化

<Measure client>
  {({ measureRef, contentRect }) => {
    if (contentRect) {
      console.log('client尺寸变化:', contentRect.client.width);
    }
    return <div ref={measureRef}>...</div>;
  }}
</Measure>

注意事项

  1. 测量元素必须具有确定的尺寸(非display:none)
  2. 频繁测量可能影响性能,建议合理使用
  3. 初次渲染时contentRect可能为undefined