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

@liberfi.io/hooks

v0.1.89

Published

A collection of framework-agnostic React utility hooks used across the LiberFi frontend. Provides common patterns — boolean state, stable callback refs, resize observation, periodic ticking, and a shared event bus — so that consuming packages don't reinve

Downloads

10,061

Readme

@liberfi.io/hooks

A collection of framework-agnostic React utility hooks used across the LiberFi frontend. Provides common patterns — boolean state, stable callback refs, resize observation, periodic ticking, and a shared event bus — so that consuming packages don't reinvent them.

Design Philosophy

  • One hook, one file — each hook lives in its own module with a single, focused responsibility.
  • Stable references — actions and callbacks are memoised via useCallback / useRef to prevent unnecessary consumer re-renders.
  • SSR-safeuseSafeLayoutEffect guards useLayoutEffect behind a runtime check; other hooks use only useEffect.
  • Shared timeruseTick multiplexes all subscribers through a single GlobalTimer singleton, avoiding per-hook intervals.
  • Minimal dependencies — only eventemitter3 and @liberfi.io/utils at runtime; throttle is implemented internally.

Installation

pnpm add @liberfi.io/hooks

Peer dependencies

The consumer must provide:

| Package | Version | | ------- | ------- | | react | >=18 |

API Reference

Hooks

useBoolean

Manage a boolean state with convenience setters.

function useBoolean(initialValue?: boolean): UseBooleanReturn;

type UseBooleanReturn = [
  boolean,
  { setTrue: () => void; setFalse: () => void; toggle: () => void },
];

useCallbackRef

Persist a callback reference that always points to the latest function, while returning a stable callable.

function useCallbackRef<T extends (...args: any[]) => any>(
  fn: T | undefined,
  deps?: React.DependencyList,
): T;

useIsMounted

Returns a function that reports whether the component is currently mounted.

function useIsMounted(): () => boolean;

useValueRef

Returns a ref that always reflects the latest value (updated synchronously on render).

function useValueRef<T>(value: T): RefObject<T>;

useSafeLayoutEffect

useLayoutEffect in the browser, useEffect on the server. Drop-in replacement to suppress SSR warnings.

const useSafeLayoutEffect: typeof useLayoutEffect;

useTick

Calls a callback at a specified interval, powered by a shared GlobalTimer singleton.

function useTick(callback: TickCallback, interval?: number): void;

type TickEvent = { delta: number; now: number };
type TickCallback = (event: TickEvent) => void;

| Parameter | Type | Default | Description | | ---------- | -------------- | ------- | -------------------------------------- | | callback | TickCallback | — | Called each tick with { delta, now } | | interval | number | 1000 | Milliseconds between ticks |

useTickAge

Returns the elapsed milliseconds since a given birthday, updated every tick.

function useTickAge(birthday?: number | Date, interval?: number): number;

| Parameter | Type | Default | Description | | ---------- | ---------------- | ------------ | ------------------- | | birthday | number \| Date | Date.now() | Reference timestamp | | interval | number | 1000 | Tick interval in ms |

useResizeObserver

Observes an element's size via the ResizeObserver API.

function useResizeObserver<T extends HTMLElement>(
  options: UseResizeObserverOptions<T>,
): Size;

When onResize is provided, the hook delegates to it instead of updating internal state (no re-renders).

useThrottledResizeObserver

Throttled variant of useResizeObserver.

function useThrottledResizeObserver<T extends HTMLElement>(
  options: UseThrottledResizeObserverOptions<T>,
): Size;

useEventEmitter

Returns a global EventEmitter singleton (from eventemitter3) managed by an internal DI container.

function useEventEmitter(): EventEmitter;

Types

| Type | Description | | -------------------------------------- | ----------------------------------------------------------------------------------- | | UseBooleanReturn | Return type of useBoolean | | Size | { width: number \| undefined; height: number \| undefined } | | UseResizeObserverOptions<T> | Options for useResizeObserver: ref, onResize?, box? | | UseThrottledResizeObserverOptions<T> | Options for useThrottledResizeObserver: ref, box?, throttleMs? (default 50) | | TickEvent | Tick payload: { delta: number; now: number } | | TickCallback | (event: TickEvent) => void |

Constants

| Name | Type | Description | | --------- | -------- | ------------------------------------ | | version | string | Current package version ("0.1.24") |

Usage Examples

Basic boolean toggle

import { useBoolean } from "@liberfi.io/hooks";

function Panel() {
  const [isOpen, { toggle, setFalse }] = useBoolean(false);
  return (
    <div>
      <button onClick={toggle}>Toggle</button>
      {isOpen && <div onClose={setFalse}>Content</div>}
    </div>
  );
}

Periodic countdown

import { useTickAge } from "@liberfi.io/hooks";

function Timer({ startedAt }: { startedAt: number }) {
  const elapsed = useTickAge(startedAt, 1000);
  const seconds = Math.floor(elapsed / 1000);
  return <span>{seconds}s elapsed</span>;
}

Resize-aware component

import { useRef } from "react";
import { useResizeObserver } from "@liberfi.io/hooks";

function Card() {
  const ref = useRef<HTMLDivElement>(null);
  const { width = 0 } = useResizeObserver({ ref });
  return <div ref={ref}>{width > 600 ? <WideLayout /> : <NarrowLayout />}</div>;
}

Cross-component event bus

import { useEffect } from "react";
import { useEventEmitter } from "@liberfi.io/hooks";

function Sender() {
  const ee = useEventEmitter();
  return <button onClick={() => ee.emit("refresh")}>Refresh</button>;
}

function Receiver() {
  const ee = useEventEmitter();
  useEffect(() => {
    const handler = () => console.log("refreshed");
    ee.on("refresh", handler);
    return () => {
      ee.off("refresh", handler);
    };
  }, [ee]);
  return null;
}

Future Improvements

  • Evaluate whether version.ts top-level side effect should be lazy-initialised.