Skip to content

Commit

Permalink
refactor!: deprecate proxyWithHistory (#839)
Browse files Browse the repository at this point in the history
* docs: update proxyWithHistory website page

Adds reference to new npm package and codesandbox demo

* docs: update proxyWithHistory example with new package

* refactor!: deprecate proxyWithHistory
  • Loading branch information
lwhiteley authored Jan 6, 2024
1 parent 58f17ba commit f03dcc5
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 12 deletions.
4 changes: 2 additions & 2 deletions docs/api/utils/proxyWithHistory.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ description: ''
This is a utility function to create a proxy with snapshot history.

```js
import { proxyWithHistory } from 'valtio/utils'
import { proxyWithHistory } from 'valtio-history'

const state = proxyWithHistory({ count: 0 })
console.log(state.value) // ---> { count: 0 }
Expand All @@ -26,4 +26,4 @@ console.log(state.value) // ---> { count: 1 }

## Codesandbox demo

https://codesandbox.io/s/admiring-wright-2oh2x?file=/src/App.tsx
https://codesandbox.io/s/valtio-history-example-v0-m353xc?file=/src/App.tsx
3 changes: 2 additions & 1 deletion examples/editor-proxyWithHistory/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"dependencies": {
"react": "17.0.2",
"react-dom": "17.0.2",
"valtio": "1.2.11"
"valtio": "1.2.11",
"valtio-history": "0.0.1"
},
"devDependencies": {
"@types/react": "17.0.20",
Expand Down
19 changes: 13 additions & 6 deletions examples/editor-proxyWithHistory/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
import './styles.css'
import React from 'react'
import { useSnapshot } from 'valtio'
import { proxyWithHistory } from 'valtio/utils'
import { proxyWithHistory } from 'valtio-history'

const textProxy = proxyWithHistory({
text: 'Add some test to this initial value and then undo/redo',
text: 'Add some text to this initial value and then undo/redo',
})
const update = (e: React.ChangeEvent<HTMLTextAreaElement>) =>
(textProxy.value.text = e.target.value)

const update = (event: React.ChangeEvent<HTMLTextAreaElement>) =>
(textProxy.value.text = event.target.value)

export default function App() {
const { value, undo, redo, history, canUndo, canRedo } =
const { value, undo, redo, history, canUndo, canRedo, getCurrentChangeDate } =
useSnapshot(textProxy)

return (
<div className="App">
<h2>Editor with history</h2>
<small>{history.index} changes</small>
<div className="info">
<span>
change {history.index + 1} / {history.nodes.length}
</span>
<span>|</span>
<span>{getCurrentChangeDate().toISOString()}</span>
</div>
<div className="editor">
<textarea value={value.text} rows={4} onChange={update} />
</div>
Expand Down
10 changes: 10 additions & 0 deletions examples/editor-proxyWithHistory/src/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@
text-align: center;
}

.info {
display: flex;
justify-content: center;
padding: 16px;

& > span {
margin-right: 32px;
}
}

.editor {
display: flex;
margin: 0 auto;
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ For more information, check out [this guide](./docs/guides/computed-properties.m
This is a utility function to create a proxy with snapshot history.
```js
import { proxyWithHistory } from 'valtio/utils'
import { proxyWithHistory } from 'valtio-history'

const state = proxyWithHistory({ count: 0 })
console.log(state.value) // ---> { count: 0 }
Expand Down
2 changes: 1 addition & 1 deletion src/vanilla/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ export { devtools } from './utils/devtools.ts'
export { derive, underive, unstable_deriveSubscriptions } from 'derive-valtio'
export { addComputed_DEPRECATED as addComputed } from './utils/addComputed.ts'
export { proxyWithComputed_DEPRECATED as proxyWithComputed } from './utils/proxyWithComputed.ts'
export { proxyWithHistory } from './utils/proxyWithHistory.ts'
export { proxyWithHistory_DEPRECATED as proxyWithHistory } from './utils/proxyWithHistory.ts'
export { proxySet } from './utils/proxySet.ts'
export { proxyMap } from './utils/proxyMap.ts'
13 changes: 12 additions & 1 deletion src/vanilla/utils/proxyWithHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,19 @@ const deepClone = <T>(obj: T): T => {
* const state = proxyWithHistory({
* count: 1,
* })
*
* @deprecated Please use the `valtio-history` package. eg.
* import { proxyWithHistory } from 'valtio-history'
*/
export function proxyWithHistory<V>(initialValue: V, skipSubscribe = false) {
export function proxyWithHistory_DEPRECATED<V>(
initialValue: V,
skipSubscribe = false,
) {
if (import.meta.env?.MODE !== 'production') {
console.warn(
'proxyWithHistory is deprecated. Please use the "valtio-history" package; refer to the docs',
)
}
const proxyObject = proxy({
value: initialValue,
history: ref({
Expand Down

1 comment on commit f03dcc5

@vercel
Copy link

@vercel vercel bot commented on f03dcc5 Jan 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

valtio – ./

valtio-pmndrs.vercel.app
valtio-git-main-pmndrs.vercel.app
valtio.vercel.app
valtio.pmnd.rs

Please sign in to comment.