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 🙏

© 2024 – Pkg Stats / Ryan Hefner

recoil-undo

v0.0.7

Published

Undo functionality for the recoil state management library

Downloads

48

Readme

recoil-undo

Undo functionality for the recoil state management library

NPM

Notice

This is an incredibly early library and much like recoil itself the api will almost certainly change. Right now the functionality is very basic, but expect it to come much more robust over time and those changes might not be backwards compatible at the moment.

Install

recoil-undo relies on both React and Recoil installed as peer dependencies so make sure they are installed as well.

npm install --save recoil-undo

or

yarn add recoil-undo

Usage

Make sure that you include you put RecoilUndoRoot under RecoilRoot. From there you can use the useUndo hook that will return a callback that will undo the last state change. The library is written in typescript and ts support work out of the box.

import React from 'react';
import { RecoilRoot, atom, useRecoilState } from 'recoil';
import { RecoilUndoRoot, useUndo, useRedo } from 'recoil-undo';

const COUNT = atom({
  default: 0,
  key: 'count',
});

const App = () => {
  return (
    <RecoilRoot>
      <RecoilUndoRoot>
        <Counter />
      </RecoilUndoRoot>
    </RecoilRoot>
  );
};

function Counter() {
  const [count, setCount] = useRecoilState(COUNT);
  const undo = useUndo();
  const redo = useRedo();
  return (
    <div>
      <div>
        <button onClick={() => setCount((count) => count - 1)}>-</button>
        {count}
        <button onClick={() => setCount((count) => count + 1)}>+</button>
      </div>
      <button onClick={undo}>Undo</button>
      <button onClick={redo}>Redo</button>
    </div>
  );
}

Api

RecoilUndoRoot

This component is exported from recoil-undo and should be placed right under the RecoilRoot provider in the application. It is responsible for keeping track of the undo history from your recoil state. At the moment it takes a few optional properties:

  • trackedAtoms which is an array of RecoilState (the value that is returned from atom in recoil). If trackedAtoms is passed into RecoilUndoRoot the undo stack will only apply to the atoms provided, all other atoms will be ignored when undoing / redoing. Note: there is no reason to track selectors, as their values will be updated as the atoms change.
  • trackingByDefault which is a boolean value (default is true) where you can skip history tracking if required

If trackedAtoms is not passed to RecoilUndoState all atoms will be tracked by recoil-undo.

useUndo

This hook returns a function that when called will move all tracked atoms to the previous history state.

useRedo

This hook returns a function that when called will move all tracked atoms to the next history state.

useBatching

This hook returns an object with two properties startBatch and endBatch. There are many situations where you might want to turn mutliple user interactions into a single item in the undo stack. Example: suppose a user is dragging an object across the screen, you don't want to record every mouse move as an undoable operation. Instead, you only want to record the start and end position on the undo stack. In cases like these, you can call startBatch and endBatch to make sure multiple atom updates only add a single item to the undo stack.

const { startBatch, endBatch } = useBatching();

const onMouseDown = () => {
  startBatch();
};

const onMouseMove = () => {
  // Update item position
};

const onMouseUp = () => {
  endBatch();
};

useIsTrackingHistory

This will start / stop tracking history if required.

const {getIsTrackingHistory, setIsTrackingHistory} = useIsTrackingHistory();

// getIsTrackingHistory() === false
setIsTrackingHistory(true);
// ... after a re-render
// getIsTrackingHistory() === true

Roadmap

  • Undo scoping (keep multiple undo stacks in a single application)

License

MIT © SawyerHood