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

react-pretext-hooks

v0.1.0

Published

React hooks and components for @chenglou/pretext text measurement & layout

Readme

react-pretext-hooks

React hooks and components for @chenglou/pretext — fast, accurate text measurement & layout without touching the DOM.

Install

npm install react-pretext-hooks @chenglou/pretext react

@chenglou/pretext and react (>=18) are peer dependencies.

API

useTextLayout(text, font, width, lineHeight, options?)

All-in-one hook. Returns { height, lineCount }.

import { useTextLayout } from "react-pretext-hooks";

function MyComponent() {
  const { height, lineCount } = useTextLayout(
    "Hello world, this is a long paragraph…",
    "16px/1.5 Inter",
    300,
    24,
  );

  return <div style={{ height }}>…</div>;
}

usePrepared(text, font, options?)

Returns the raw PreparedText handle for power users who want to call layout() manually at multiple widths.

import { usePrepared } from "react-pretext-hooks";
import { layout } from "@chenglou/pretext";

function MyComponent({ widths }: { widths: number[] }) {
  const prepared = usePrepared("Some text", "16px Inter");

  const layouts = widths.map((w) => layout(prepared, w, 24));
  // …
}

useAutoLayout(text, font, lineHeight, ref, options?)

ResizeObserver-aware hook. Attach it to a container ref and it recomputes layout automatically when the container resizes.

import { useRef } from "react";
import { useAutoLayout } from "react-pretext-hooks";

function AutoBox() {
  const ref = useRef<HTMLDivElement>(null);
  const { height, lineCount, width } = useAutoLayout(
    "Resizable text…",
    "16px Inter",
    24,
    ref,
  );

  return (
    <div ref={ref} style={{ height }}>
      Resizable text…
    </div>
  );
}

<MeasuredText>

Component wrapper around useAutoLayout. Renders a container whose height is driven by the measured text layout.

import { MeasuredText } from "react-pretext-hooks";

<MeasuredText text="Hello world" font="16px Inter" lineHeight={24}>
  <p>Hello world</p>
</MeasuredText>

Props:

| Prop | Type | Description | | ------------ | ----------------------------- | ---------------------------------------------- | | text | string | The text to measure. | | font | string | CSS font shorthand (e.g. "16px Inter"). | | lineHeight | number | Line height in pixels. | | options | PrepareOptions | Optional whiteSpace / wordBreak overrides. | | children | ReactNode | Rendered inside the container (defaults to text). | | className | string | Class name for the wrapper element. | | style | CSSProperties | Additional inline styles. | | as | keyof JSX.IntrinsicElements | HTML tag to render (default "div"). |

The component also sets data-measured-height, data-measured-lines, and data-measured-width attributes on the wrapper element.

PrepareOptions

interface PrepareOptions {
  whiteSpace?: "normal" | "pre-wrap";
  wordBreak?: "normal" | "keep-all";
}

How it works

@chenglou/pretext splits text measurement into two phases:

  1. prepare() — the expensive step. Segments text and measures each segment via an offscreen canvas. This is memoized by the hooks so it only runs when text or font change.
  2. layout() — pure arithmetic (~0.0002 ms). Computes height and line count from the prepared data for a given width. This runs on every resize or parameter change with negligible cost.

License

MIT