-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add input example and update types (#185)
- Loading branch information
Showing
3 changed files
with
64 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { temporal, type TemporalState } from 'zundo'; | ||
import { useStore, createStore } from 'zustand'; | ||
import { shallow } from 'zustand/shallow'; | ||
|
||
interface MyState { | ||
fontSize: number; | ||
changeFontSize: (fontSize: number) => void; | ||
} | ||
|
||
const withZundo = temporal<MyState>((set) => ({ | ||
fontSize: 16, | ||
changeFontSize: (fontSize) => set({ fontSize }), | ||
})); | ||
|
||
const originalStore = createStore(withZundo); | ||
|
||
const useBaseStore = <T extends unknown>( | ||
selector: (state: MyState) => T, | ||
equality?: (a: T, b: T) => boolean, | ||
) => useStore(originalStore, selector, equality); | ||
const useTemporalStore = <T extends unknown>( | ||
selector: (state: TemporalState<MyState>) => T, | ||
equality?: (a: T, b: T) => boolean, | ||
) => useStore(originalStore.temporal, selector, equality); | ||
|
||
export default function App() { | ||
const { fontSize, changeFontSize } = useBaseStore((state) => state); | ||
const { futureStates, pastStates, undo, resume, pause } = useTemporalStore( | ||
(state) => state, | ||
shallow, | ||
); | ||
|
||
return ( | ||
<div> | ||
<h1>Web</h1> | ||
<div> | ||
<input | ||
type="number" | ||
value={fontSize} | ||
onFocus={() => { | ||
changeFontSize(fontSize); | ||
pause(); | ||
}} | ||
onChange={(e) => changeFontSize(e.target.valueAsNumber)} | ||
onBlur={(e) => { | ||
resume(); | ||
changeFontSize(e.target.valueAsNumber); | ||
}} | ||
/> | ||
<span>{fontSize}</span> | ||
<div> | ||
<h2>Future States</h2> | ||
<div>{JSON.stringify(futureStates)}</div> | ||
<h2>Previous States</h2> | ||
<div>{JSON.stringify(pastStates)}</div> | ||
<button onClick={() => undo()}>Undo</button> | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters