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-semantic-tree

v0.1.2

Published

Decorate your React component tree with semantic context.

Readme

react-semantic-tree

npm version MIT License

Zero-dependency React library that turns your component tree into a semantic path for event logging.

npm install react-semantic-tree

Usage

1. Provider — the adapter receives every event. The path arrives as an array of segment labels, so the adapter can keep them structured or join them however it likes:

const adapter: LoggerAdapter = (labels, metadata) =>
  Sentry.addBreadcrumb({
    category: "user-action",
    message: labels.join(" > "),
    data: metadata,
  });

<SemanticConfig adapter={adapter}>
  <Branch label="AppRoot" metadata={{ appVersion: "1.0.0" }}>
    <App />
  </Branch>
</SemanticConfig>;

Without an adapter it logs to console.debug.

Tracking is best-effort: an adapter that throws (or returns a rejecting promise) cannot affect the application. The failure is contained to that event, dropped rather than retried, and reported to the console in development. In production it is swallowed silently.

2. Branches — each label appends a path segment, and metadata / events merge down the tree:

<Branch label="UserSettings" metadata={{ user: user.email }}>
  <ProfileForm />
</Branch>

3. Leaves — attach the returned ref; the hook logs the interactions:

const { ref } = useLeaf<HTMLButtonElement>("save_button");
return (
  <button ref={ref} onClick={save}>
    Save
  </button>
);

4. Portals — a leaf resolves its context by finding the nearest data-semantic-branch ancestor in the DOM, then looking up the branch data registered against that element in memory. A portal moves the DOM node but not the React one, so a leaf rendered through a portal has no branch to read, and only reports itself. Render a <Branch> inside the portal to register the inherited path, metadata and flags against a DOM node the leaf can find. Unlabeled and empty is the minimal form: it adds no path segment, only the inherited context.

<Dialog open={open}>
  {/* the dialog renders its children through a portal */}
  <Branch>{/* leaves here resolve as "User Grants > …" */}</Branch>
</Dialog>

It has to sit inside the portalled content, not wrap the component that creates the portal: a <Branch> around <Dialog> publishes its anchor outside the portal, where the leaf cannot find it.

5. withBranch — the same branch without touching call sites:

export default withBranch("Team Settings", {
  metadata: { hello: "world" },
})(TeamSettings);

Returns a forwardRef component taking the wrapped component's props. Static properties are not copied, and parents that introspect children (tabs, selects) see the wrapper rather than the original element.

What gets logged

A leaf resolves everything through its nearest data-semantic-branch ancestor: the branch registers the path prefix, metadata and event flags against that element in memory, and the leaf looks them up after finding it in the DOM. Leaves never read React context, so context has to be registered on a DOM node — including inside a portal, which moves the node but not the React tree (see Portals under Usage).

Each event carries the resolved path as an array of segment labels, root first and the leaf's own label last, plus the merged metadata: branch metadata first, then the leaf's, then the library-generated eventType and tagName. Keeping the segments separate lets the adapter choose how to render them — join with a separator, keep them as an array, or use them as a hierarchy.

Metadata is read at event time, not render time, so state and derived values are current:

useLeaf("Create Invite Link", { prefix });

It is always plain data: values are never called. Reading a useLeaf call tells you exactly what it captures, and nothing it captures was computed somewhere else or read off the DOM at event time. Anything that needs deriving is computed at the call site:

useLeaf("docs link", { href: link.href, surface: "docs" });

| Event | Logged when | | --------- | ------------------------------------------------------------- | | mount | the element attaches. | | visible | it enters the viewport (IntersectionObserver, 10% threshold). | | hover | the pointer enters it (mouseenter). | | focus | the element receives focus. | | click | a click reaches the element. |

click is on by default; every other event is off unless you enable it.

API

<SemanticConfig>

| Prop | Type | Description | | ---------- | ----------------- | ----------------------------------------------------------------------------------------------------------------- | | adapter | LoggerAdapter | Optional. Receives one event; see the payload contract. Defaults to consoleLogAdapter. | | events | TrackingOptions | Optional. Which events to track, for the whole app. | | children | React.ReactNode | The application. |

<Branch>

| Prop | Type | Description | | ---------- | ----------------- | ----------------------------------------------------------------------------------------------------------------------------------------- | | label | string | Optional. Adds a path segment; omit it to only re-materialise the inherited path, metadata and events on a DOM node (useful for portals). | | metadata | JsonObject | Optional. Merged with parent metadata. | | events | TrackingOptions | Optional. Event overrides for this subtree; a leaf's own events wins. | | children | React.ReactNode | The part of the tree this branch applies to. |

useLeaf(label, metadata?, events?)

const { ref, element } = useLeaf<T>(label, metadata?, events?);
  • label (required) — the leaf's path segment.
  • metadata — merged last; plain data (JsonObject), read at event time.
  • events — per-leaf overrides, winning over branch and global config. Compared by value, so a fresh object each render is fine.
  • ref — callback ref to attach to the element.
  • element — the tracked node, or null when unmounted. Roughly ref.current of a ref object, which this ref is not.

Types

LoggerAdapter, TrackingOptions, BranchProps, JsonObject, JsonValue.

License

MIT — see LICENSE.