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

use-flexible-undo

v0.3.0

Published

React hook that lets you use undomundo's branching undo/redo functionality independently of how you structure your application state.

Downloads

12

Readme

use-flexible-undo

React hook that lets you use undomundo's branching undo/redo functionality independently of how you structure your application state. Note that for most use cases it's probably better to use undomundo directly, e.g. to use undomundo's makeUndoableReducer in combination with React's useReducer. That will integrate your app state with the undo history state and enables multi-user undo/redo. See undomundo for more details.

import { FC, useState } from 'react';
import { useFlexibleUndo } from 'use-flexible-undo';

// utilities for manipulating vectors:
import { vAdd, vScale } from 'vec-la-fp';

type Vector2d = [number, number];

type PayloadConfigByType = {
  setColor: {
    payload: string;
    // By default actions are considered to be absolute.
  };
  moveBy: {
    payload: Vector2d;
    // If you need to you can define custom/relative actions.
    isCustom: true;
  };
};

export const MyFunctionComponent: FC = () => {
  // Manage state however you like:
  // - separate useState calls
  // - state object in single useState
  // - useReducer
  // - external state management solution
  // - a combination of the above, etc...

  // In this example we prefix the non-undoable setters
  // with _ to differentiate them from the undoable versions,
  // but you're free to use any naming convention.
  const [color, _setColor] = useState('red');
  const [position, _setPosition] = useState<Vector2d>([0, 0]);

  const {
    undoables,
    canUndo,
    undo,
    canRedo,
    redo,
    history,
    timeTravel,
    switchToBranch,
  } = useFlexibleUndo<PayloadConfigByType>({
    // All arguments / options are static, changes after
    // initialization will be ignored.
    actionConfigs:{
      setColor: {
        updateState: _setColor,
      },
      moveBy: {
        updateState: delta => _setPosition(vAdd(delta)),
        // We have no inverse action defined for 'moveBy',
        // so for undo we keep the same action type and negate the payload:
        makeActionForUndo: ({ type, payload }) => ({
          type,
          payload: vScale(-1, payload),
        }),
      },
    },
    options: {
      useBranchingHistory: true, // defaults to false
      maxHistoryLength: 100, // defaults to Infinity
    },
  });

  const { setColor, moveBy } = undoables;

  return (
    <>
      // For an absolute action you need to provide the undo value
      // (current state) and the redo value (new state):
      <button onClick={() => setColor(color, 'black')}>Paint it black!</button>

      // For a relative action you only need to provide the delta:
      <button onClick={() => moveBy([100, 0])}>Move aside!</button>

      <button disabled={!canUndo()} onClick={undo}>undo</button>
      // etc..
    </>
  )
};