Git Product home page Git Product logo

undoer's Introduction

Native undo/redo behavior for web. This lets you push native undo stack events onto your pages, so that users can use Ctrl/Cmd-Zβ€”or even use some other gesture (e.g., on iOS devices, you can shake your phone to Undo).

See a writeup on how this works or an awesome maze-based demo for more.

Usage

Install on NPM/Yarn via undoer. You can use this element as a Web Component or as pure, imperative JavaScript.

Web Component

Add the dependency to your JS and register it as a CE:

import UndoerElement from './node_modules/undoer/element.js';
customElements.define('undoer-element', UndoerElement);

Then add the element to your page, optionally adding state attribute to set its zero initial state (otherwise it will be null):

<undoer-element state="initial state"></undoer-element>

Finally, use the element's JavaScript API:

const undoerEl = document.querySelector('undoer-element');

undoerEl.addEventListener('state', (ev) => {
  console.info('user undo or redid', ev.detail);
});

// set new state with
undoerEl.state = 'new state';
undoerEl.state = /* any object */ ;

// or via attribute for string state
undoerEl.setAttribute('state', 'new state');

Imperative JavaScript

You can also use the raw Undoer class without CEs:

import {Undoer} from './node_modules/undoer/undoer.js';
// or
import {Undoer} from 'undoer';  // your build system might allow this

// construct with callback and push state
const initialState = null;  // default is null
const undoer = new Undoer((data) => {
  console.info('user undo or redid', data);
}, initialState);
undoer.push('new state');

Notes

This makes sense as a Web Component as the undo behavior works by adding a hidden <div contentEditable> to your page. In the WC case, this is as a child of the element: in the imperative case, it's added (by default) to document.body.

undoer's People

Contributors

samthor 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.