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

use-time-machine-hook

v1.0.2

Published

A zero-dependency, ultra-lightweight React hook for Gmail-style optimistic UI and undo actions.

Readme

use-time-machine-hook ⏳

A zero-dependency, ultra-lightweight React hook for Gmail-style optimistic UI and undo actions.

Adding delayed, cancellable actions to a React app usually requires messy setTimeout cleanup, state management, and complex promise handling. use-time-machine-hook abstracts all of that into a single, perfectly typed hook with an optional beautiful UI toast.

✨ Features

  • ⚡️ Zero Dependencies: Microscopic bundle size.
  • 🔄 Optimistic UI: Update your UI instantly, before the server even responds.
  • 🛡️ Auto-Revert: If the server fails or the user clicks "Undo", the UI reverts automatically.
  • 🎨 Headless or Styled: Use the pure logic engine, or drop in our beautiful out-of-the-box Toast component.

📦 Installation

npm install use-time-machine-hook

🚀 Quick Start (With Included UI)

Drop the hook and the TimeMachineToast into any component to instantly add cancellable actions.

import { useState } from 'react';
import { useTimeMachine, TimeMachineToast } from 'use-time-machine-hook';

export default function App() {
  const [users, setUsers] = useState([{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }]);

  const { execute, undo, isPending, timeLeft } = useTimeMachine({
    mutationFn: async (userId) => {
      // Your actual API call happens here (after the delay)
      await fetch(`/api/users/${userId}`, { method: 'DELETE' });
    },
    onOptimistic: (userId) => {
      // Fires INSTANTLY: Hide the user from the UI immediately
      setUsers(prev => prev.filter(u => u.id !== userId));
    },
    onRevert: (userId) => {
      // Fires if they click 'Undo' OR if the API call fails
      alert('Action cancelled! User restored.'); 
    },
    delayMs: 5000, // 5 seconds to change their mind
  });

  return (
    <div>
      {users.map(user => (
        <div key={user.id}>
          {user.name} 
          <button onClick={() => execute(user.id)}>Delete</button>
        </div>
      ))}

      {/* The beautiful shrinking progress bar toast */}
      <TimeMachineToast 
        isPending={isPending} 
        timeLeft={timeLeft} 
        undo={undo} 
        message="User deleted." 
      />
    </div>
  );
}

🎨 Advanced Customization

Because use-time-machine is built with a Headless architecture, you have complete control over how the UI looks.

Option 1: Style the included toast with Tailwind CSS

You can easily override our default inline styles by passing a className or style prop to the TimeMachineToast.

<TimeMachineToast 
  isPending={isPending} 
  timeLeft={timeLeft} 
  undo={undo} 
  message="File moved to trash."
  // Completely overrides the look using Tailwind!
  className="bg-slate-900 border border-slate-700 text-white shadow-2xl rounded-xl"
/>

Option 2: Build a completely custom UI (Headless Mode)

If you are using a library like Shadcn, Material UI, or just want to build your own component, you can ignore our Toast completely. Just use the states returned from the hook!

import { useTimeMachine } from 'use-time-machine-hook';

export default function CustomApp() {
  const { execute, undo, isPending, timeLeft } = useTimeMachine({
    mutationFn: deleteProject,
    delayMs: 10000 
  });

  return (
    <div>
      <button onClick={execute}>Delete Project</button>

      {/* RENDER YOUR OWN CUSTOM HTML/COMPONENTS */}
      {isPending && (
        <div className="fixed top-5 right-5 bg-red-500 p-4 rounded-lg flex items-center gap-4">
          <p className="text-white font-bold">
            Project deleting in {Math.ceil(timeLeft / 1000)} seconds...
          </p>
          
          <button onClick={undo} className="bg-white text-red-500 px-3 py-1 rounded">
            STOP!
          </button>
        </div>
      )}
    </div>
  );
}

⚙️ Configuration Options

| Property | Type | Required | Description | | :--- | :--- | :--- | :--- | | mutationFn | (vars: T) => Promise<any> | Yes | The async API call to execute after the delay. | | onOptimistic| (vars: T) => void | No | Fires immediately when execute() is called. | | onRevert | (vars: T) => void | No | Fires if undo() is clicked or mutationFn throws an error. | | onSuccess | (data, vars) => void | No | Fires after mutationFn successfully completes. | | delayMs | number | No | Countdown time in milliseconds. Default: 5000. |

📜 License

MIT