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

@structyl/hooks

v1.1.0

Published

SSR-safe, tree-shakeable React hooks for state, refs, DOM, browser APIs, and performance, including useControllableState for headless UI

Downloads

213

Readme

@structyl/hooks

A collection of SSR-safe, tree-shakeable React hooks for state, refs, the DOM, browser APIs, and performance.

npm license

@structyl/hooks is the shared hook layer of structyl, the React UI library with structure. It packages the small, single-responsibility hooks that the primitives and styled components rely on, such as useControllableState for controlled/uncontrolled state and useComposedRefs for merging refs. Every hook is SSR-safe (no unguarded window/document access) and tree-shakeable, so it is equally useful on its own in any React 18 or 19 application.

Installation

# pnpm
pnpm add @structyl/hooks

# npm
npm install @structyl/hooks

# yarn
yarn add @structyl/hooks

React 18 or 19 (and react-dom) are peer dependencies.

Usage

import {
  useControllableState,
  useDebounce,
  useLocalStorage,
} from '@structyl/hooks';

function SearchField({ value, defaultValue = '', onChange }: {
  value?: string;
  defaultValue?: string;
  onChange?: (next: string) => void;
}) {
  // Works in both controlled and uncontrolled modes.
  const [query, setQuery] = useControllableState({
    prop: value,
    defaultProp: defaultValue,
    onChange,
  });

  // Only react to the value after the user stops typing.
  const debouncedQuery = useDebounce(query ?? '', 300);

  // Persist the last search across reloads (SSR-safe).
  const [recent, setRecent] = useLocalStorage<string[]>('recent-searches', []);

  return (
    <input
      type="search"
      value={query ?? ''}
      onChange={(event) => setQuery(event.target.value)}
      onBlur={() => debouncedQuery && setRecent([debouncedQuery, ...recent])}
      placeholder="Search…"
    />
  );
}

Features

  • Controllable stateuseControllableState is the cornerstone hook for building components that work in both controlled and uncontrolled modes.
  • SSR-safe by default — browser-bound hooks guard window/document access and degrade gracefully on the server.
  • Tree-shakeable — named exports and sideEffects: false mean you only ship the hooks you import.
  • Ref compositionuseComposedRefs / composeRefs merge multiple refs onto a single node, the foundation of asChild patterns.
  • TypeScript-first — fully typed signatures with generics and tuple returns; no any.
  • ESM + CJS — ships both module formats plus type declarations.

API

State

| Hook | Description | | --- | --- | | useControllableState | Manage state that may be controlled by a parent or held internally. | | useToggle | Boolean state with a toggle and explicit setter [value, toggle, set]. | | useBoolean | Boolean state with { value, on, off, toggle, set } helpers. | | useCounter | Numeric state with increment, decrement, reset, and set. | | usePrevious | Returns the value from the previous render. |

Refs

| Hook | Description | | --- | --- | | useComposedRefs / composeRefs | Merge multiple refs (callback or object) onto one node. | | useCallbackRef | Stabilize a callback as a ref to avoid re-renders. | | useLatest | Keep a ref pointing at the latest value without re-rendering. |

DOM

| Hook | Description | | --- | --- | | useClickOutside | Run a handler when a click/touch lands outside a ref. | | useEventListener | Typed addEventListener for window, document, or an element. | | useKeyPress | Run a handler when a specific key is pressed. |

Browser

| Hook | Description | | --- | --- | | useMediaQuery | Subscribe to a CSS media query, with an SSR default. | | useLocalStorage | JSON-backed localStorage state [value, set, remove], synced across tabs. | | useCopyToClipboard | Copy text and track copied state via the Clipboard API. | | useDarkMode | Boolean for the prefers-color-scheme: dark query. |

Performance

| Hook | Description | | --- | --- | | useDebounce | Debounce a value by a delay (default 300ms). | | useThrottle | Throttle a value by a delay (default 300ms). |

Utility

| Hook | Description | | --- | --- | | useId | SSR-safe stable ID wrapping React.useId with an optional prefix. | | useMount | Run a callback once on mount. | | useUnmount | Run a callback on unmount. | | useUpdateEffect | Like useEffect, but skips the first run. | | useIsomorphicLayoutEffect | useLayoutEffect on the client, useEffect on the server. | | useWindowSize | Track { width, height } of the window. | | useHotkeys | Bind keyboard shortcuts (e.g. mod+k) with modifier support. |

Part of structyl

Part of structyl — see the full documentation at www.structyl.com.

License

MIT