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

@patch-kit/history

v2.1.0

Published

History Manager for React

Readme

History Manager

A self-contained undo/redo system for using the Command Pattern.

Interactive demo: Default


Overview

Each user action is recorded as a command with undo() and redo() functions, enabling full history traversal. The factory pattern ensures each history instance is independently scoped and fully typed.

Example usage


Setup

Call createHistory() once at module level and export the bound pair. Both HistoryProvider and useHistory must come from the same call — they share the same context instance.

import { createHistory } from '@patch-kit/history';

export const { HistoryProvider, useHistory } = createHistory();

For multiple independent history stacks, each call creates its own isolated instance:

export const { HistoryProvider: HistoryA, useHistory: useHistoryA } = createHistory();
export const { HistoryProvider: HistoryB, useHistory: useHistoryB } = createHistory();

Basic Usage

<HistoryProvider>
  <App />
</HistoryProvider>
const { addHistory, undo, redo, canUndo, canRedo } = useHistory();

addHistory({
  name: 'History Command',
  undo() {
    // Undoes the action (called on undo)
  }
  redo() {
    // Re-applies the action (called on redo)
  },
});

immediate

Pass true as the second argument to apply the action immediately on record, instead of applying it manually beforehand:

// without immediate — apply manually first, then record
applyChange(next);
addHistory({ undo: () => applyChange(prev), redo: () => applyChange(next) });

// with immediate — record and apply in one call
addHistory({ undo: () => applyChange(prev), redo: () => applyChange(next) }, true);

Typed Return Values

Commands can return a value from undo() and redo(). When they do, the Provider's onUndo/onRedo callbacks receive that value — letting you centralise any shared logic that would otherwise be repeated at every call site.

type HistoryValue = { type: string; payload: unknown };

export const { HistoryProvider, useHistory } = createHistory<HistoryValue>();
<HistoryProvider
  onUndo={(value) => { /* handle value centrally */ }}
  onRedo={(value) => { /* handle value centrally */ }}
>
  <App />
</HistoryProvider>
addHistory({
  name: 'Update',
  undo() { return { type: 'update', payload: previous }; },
  redo() { return { type: 'update', payload: next }; }
});

If no return value is needed, undo() and redo() can be void — onUndo/onRedo are optional and only relevant when commands return data.


API

createHistory<T = any>()

Factory function. Call at module level, outside any component.

Returns { HistoryProvider, useHistory } bound to the same context instance.

<HistoryProvider>

| Prop | Type | Default | Description | |------|------|---------|-------------| | limit | number | 64 | Max number of history entries | | onUndo | (value: T) => void | — | Called after undo() with its return value | | onRedo | (value: T) => void | — | Called after redo() with its return value |

useHistory()

Must be called inside the matching HistoryProvider.

const {
  addHistory,   // (command: Command<T>, immediate?: boolean) => void
  undo,         // () => void
  redo,         // () => void
  resetHistory, // () => void
  canUndo,      // boolean
  canRedo,      // boolean
} = useHistory();

Command<T>

type Command<T = any> = {
  name: string;
  undo: () => T;  // return value passed to onUndo
  redo: () => T;  // return value passed to onRedo
}

Patterns

Capture state before mutating

Always snapshot the previous state before applying the change:

const previous = structuredClone(item);
applyChange(next);

addHistory({
  name: 'Update',
  undo() { applyChange(previous); },
  redo() { applyChange(next); }
});

Batch related operations

Multiple related changes should be a single history entry:

// ✅ one entry, one undo
addHistory({
  name: `Move ${ids.length} items`,
  undo() { ids.forEach(id => move(id, prevPos[id])); },
  redo() { ids.forEach(id => move(id, nextPos[id])); }
});

// ❌ N entries, N undos
ids.forEach(id => addHistory({ ... }));

Delayed commit (drag, resize, etc.)

For continuous interactions, accumulate changes and commit once on release:

const pending = useRef({ initial: null, final: null });

onDragStart(() => { pending.current.initial = getPosition(); });
onDragMove((pos) => { pending.current.final = pos; });
onDragEnd(() => {
  const { initial, final } = pending.current;
  addHistory({
    name: 'Move',
    undo() { setPosition(initial); },
    redo() { setPosition(final); }
  });
});

Updating state on every move event causes re-renders that feed back into the drag — commit once at the end instead.