GithubHelp home page GithubHelp logo

brunozoric / recoil-undo Goto Github PK

View Code? Open in Web Editor NEW

This project forked from sawyerhood/recoil-undo

0.0 0.0 0.0 405 KB

Undo functionality for the recoil state management library

HTML 9.99% TypeScript 87.67% CSS 2.34%

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 single optional property, 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.

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();
};

Roadmap

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

License

MIT © SawyerHood

recoil-undo's People

Contributors

sawyerhood avatar

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.