use-undoable
Advanced tools
Comparing version 2.0.0 to 2.1.0
@@ -112,4 +112,11 @@ var react = require('react'); | ||
}; | ||
var defaultOptions = { | ||
behavior: 'mergePast' | ||
}; | ||
var useUndoable = function useUndoable(initialPresent) { | ||
var useUndoable = function useUndoable(initialPresent, options) { | ||
if (options === void 0) { | ||
options = defaultOptions; | ||
} | ||
var _useReducer = react.useReducer(reducer, _extends({}, initialState, { | ||
@@ -157,3 +164,3 @@ present: initialPresent | ||
if (mutationBehavior === void 0) { | ||
mutationBehavior = 'mergePast'; | ||
mutationBehavior = options.behavior; | ||
} | ||
@@ -160,0 +167,0 @@ |
@@ -112,4 +112,11 @@ import { useReducer, useCallback } from 'react'; | ||
}; | ||
var defaultOptions = { | ||
behavior: 'mergePast' | ||
}; | ||
var useUndoable = function useUndoable(initialPresent) { | ||
var useUndoable = function useUndoable(initialPresent, options) { | ||
if (options === void 0) { | ||
options = defaultOptions; | ||
} | ||
var _useReducer = useReducer(reducer, _extends({}, initialState, { | ||
@@ -157,3 +164,3 @@ present: initialPresent | ||
if (mutationBehavior === void 0) { | ||
mutationBehavior = 'mergePast'; | ||
mutationBehavior = options.behavior; | ||
} | ||
@@ -160,0 +167,0 @@ |
@@ -13,2 +13,5 @@ declare type ActionType = 'undo' | 'redo' | 'set' | 'update' | 'reset'; | ||
} | ||
export type { ActionType, MutationBehavior, Action, State }; | ||
interface Options { | ||
behavior: MutationBehavior; | ||
} | ||
export type { ActionType, MutationBehavior, Action, State, Options }; |
@@ -1,2 +0,3 @@ | ||
declare const useUndoable: (initialPresent: any) => any[]; | ||
import { Options } from './types'; | ||
declare const useUndoable: (initialPresent: any, options?: Options) => any[]; | ||
export default useUndoable; |
{ | ||
"name": "use-undoable", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "React hook for undo/redo functionality without the hassle.", | ||
@@ -5,0 +5,0 @@ "private": false, |
@@ -5,2 +5,4 @@ # useUndoable | ||
[**Live Demo**](https://codesandbox.io/s/use-undoable-zi0b4) | ||
## Installation | ||
@@ -17,2 +19,4 @@ | ||
``` | ||
import useUndoable from 'use-undoable'; | ||
const MyComponent = () => { | ||
@@ -28,3 +32,3 @@ const initialState = 0; | ||
} | ||
] = useUndoable(initialState); | ||
] = useUndoable(initialStates); | ||
@@ -35,2 +39,12 @@ return <p>{count}</p> | ||
## Features | ||
(and how `useuUndoable` improves on existing packages) | ||
- Familiar API similar to `useState` | ||
- You can choose how you'd like state changes to be reflected, with `mergePast`, `destroyFuture`, or `keepFuture` | ||
- Ability to set default options so that each call to `setState` doesn't need to pass the same mutation behavior. | ||
- Zero dependencies | ||
- Tiny; less than 35 kB unpacked | ||
### Docs | ||
@@ -53,8 +67,33 @@ | ||
canRedo, | ||
set, | ||
reset | ||
} | ||
] = useUndoable(0); | ||
] = useUndoable(initialState, options); | ||
``` | ||
### `options` | ||
If you want to set a specific mutation behavior for all state changes, you can declare the options object like so: | ||
``` | ||
const options = { | ||
behavior: 'destroyFuture' | ||
} | ||
``` | ||
This way, all calls to `setCount` will destroy the future array on new state changes. You **can** override this behavior with each call individually, if you'd like. | ||
Therefore: | ||
``` | ||
setCount(0); | ||
``` | ||
will use the `destroyFuture` behavior and | ||
``` | ||
setCount(0, 'keepFuture'); | ||
``` | ||
will keep the future array. | ||
### `count` | ||
@@ -208,3 +247,2 @@ | ||
canRedo: canRedoWithCustomName, | ||
set: updateTheValueAndRemoveTheExistingFuture, | ||
reset: deleteEverythingYo | ||
@@ -216,3 +254,3 @@ } | ||
updateTheValueAndRemoveTheExistingFuture({ | ||
deleteEverythingYo({ | ||
state: 'My new state' | ||
@@ -219,0 +257,0 @@ }); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
33985
344
264