Git Product home page Git Product logo

recoil-undo's Introduction

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

recoil-undo's People

Contributors

brunozoric avatar dependabot[bot] avatar sawyerhood avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

recoil-undo's Issues

Start batch when tracking atoms

Heya!

I found an issue with startBatch when using track atoms functionality.
When you run startBatch() it takes present state and pushes it to the history past and if there are no changed atoms history state remains like there were some changes. When you go "undo" you actually do not undo since previous state is same as current state. And if you started/ended batch few (dozen times) you have dozens of equal states in history.past array.

I'll try to fix, but if you have an idea how to do it, I'm all ears :)

How can I determine if undo/redo is available?

For better UX it would be nice to have the information wether there are any undo or redo states available before attempting to execute them, so UI can reflect that by for example disabling or hiding the appropriate buttons.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.