1.0.1 โ€ข Published 9 months ago

@daveyrojo/use-ref-with-reset v1.0.1

Weekly downloads
-
License
MIT
Repository
-
Last release
9 months ago

use-ref-with-reset

๐Ÿ’ก A tiny Vue 3 composable that tracks and resets reactive values to their original state โ€” with optional deep watching, prop syncing, and helper methods like resetTo() and set().


โœจ Features

  • โœ… Tracks changes and compares against an original reference
  • ๐Ÿ” reset() support to roll back state
  • ๐Ÿง  isModified to know if value has changed
  • ๐Ÿ”„ resetTo() to redefine the original value
  • ๐Ÿ”ง set() for safe, shallow updates
  • ๐Ÿ” Optional deep watching
  • ๐Ÿ“ฆ Lightweight and tree-shakeable

๐Ÿ“ฆ Installation

npm install use-ref-with-reset
# or
pnpm add use-ref-with-reset

๐Ÿงช Quick Example

import { useRefWithReset } from 'use-ref-with-reset'

const { value, reset, isModified, set, resetTo } = useRefWithReset({ count: 1 })

value.value.count++             // Modify
console.log(isModified.value)   // true

reset()                         // Back to { count: 1 }
resetTo({ count: 100 })         // Redefine original
set({ count: 999 })             // Change value

๐Ÿ”ง Usage with Props

const props = defineProps<{
  config: { visible: boolean }
}>()

const {
  value: config,
  original,
  reset,
  isModified
} = useRefWithReset(() => props.config, {
  watchSource: () => props.config,
  deepWatch: true
})

๐Ÿงฐ API

useRefWithReset(initial, options)

argument,type,Description
initial, T | () => T | Ref<T>, The initial or reactive value
options, UseRefWithResetOptions<T>, Options for deep watch, syncing, etc.

๐Ÿงฉ Options

interface UseRefWithResetOptions<T> {
  watchSource?: MaybeRefOrGetter<T>
  syncOriginalOnSourceChange?: boolean // default: true
  deepWatch?: boolean                  // default: false
  freezeOriginal?: boolean             // default: false
}

โœ… When to Use

Use Case, โœ… Recommended?
Temporary form state, โœ… Yes
Compare props to modified local state, โœ… Yes
Undo history, โŒ No
Complex object diffs, โš ๏ธ Better with deep libraries like fast-deep-equal

๐Ÿงช Testing

npm run test

Includes a Vitest test suite in /test

๐Ÿง‘โ€๐Ÿ’ป Author

Made with โค๏ธ by David Eldridge

๐Ÿ“„ License

MIT

1.0.1

9 months ago

1.0.0

9 months ago