-
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
84620fe
commit 3636d75
Showing
6 changed files
with
132 additions
and
0 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,10 @@ | ||
# `@oku-ui/use-previous` | ||
|
||
## Installation | ||
|
||
```sh | ||
$ pnpm add @oku-ui/use-previous | ||
``` | ||
|
||
## Usage | ||
... |
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,42 @@ | ||
{ | ||
"name": "@oku-ui/use-previous", | ||
"type": "module", | ||
"version": "1.0.0", | ||
"license": "MIT", | ||
"source": "src/index.ts", | ||
"funding": "https://github.com/sponsors/productdevbook", | ||
"homepage": "https://oku-ui.com/primitives", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/oku-ui/primitives.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/oku-ui/primitives/issues" | ||
}, | ||
"exports": { | ||
".": { | ||
"types": "./dist/types/index.d.ts", | ||
"require": "./dist/use-previous.cjs", | ||
"import": "./dist/use-previous.js" | ||
} | ||
}, | ||
"main": "dist/use-previous.cjs", | ||
"module": "dist/use-previous.js", | ||
"types": "dist/types/index.d.ts", | ||
"files": [ | ||
"dist", | ||
"README.md" | ||
], | ||
"scripts": { | ||
"clean": "rm -rf dist", | ||
"build": "vite build --mode production", | ||
"lint": "eslint .", | ||
"lint:fix": "eslint . --fix" | ||
}, | ||
"peerDependencies": { | ||
"vue": "^3.2.47" | ||
}, | ||
"devDependencies": { | ||
"tsconfig": "workspace:^" | ||
} | ||
} |
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 @@ | ||
export { usePrevious } from './use-previous' |
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,22 @@ | ||
import { getCurrentInstance, ref, watch } from 'vue' | ||
|
||
function usePrevious<T>(value: T) { | ||
// We compare values before making an update to ensure that | ||
// a change has been made. This ensures the previous value is | ||
// persisted correctly between renders. | ||
|
||
const _ref = ref({ value, previous: value }) | ||
const instance = getCurrentInstance() | ||
if (!instance) | ||
throw new Error('usePrevious must be called within a setup function.') | ||
|
||
watch(() => value, () => { | ||
if (_ref.value.value !== value) { | ||
_ref.value.previous = _ref.value.value | ||
_ref.value.value = value as any | ||
} | ||
}) | ||
return _ref.value.previous | ||
} | ||
|
||
export { usePrevious } |
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,9 @@ | ||
{ | ||
"extends": "./../../../tsconfig.json", | ||
"compilerOptions": { | ||
"baseUrl": "." | ||
}, | ||
"include": [ | ||
"./" | ||
] | ||
} |
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,48 @@ | ||
import path, { resolve } from 'node:path' | ||
|
||
import Vue from '@vitejs/plugin-vue' | ||
import { defineConfig } from 'vite' | ||
|
||
// https://github.com/qmhc/vite-plugin-dts | ||
import dtsPlugin from 'vite-plugin-dts' | ||
|
||
// https://github.com/sxzz/unplugin-vue-macros | ||
import VueMacros from 'unplugin-vue-macros/vite' | ||
|
||
import * as pkg from './package.json' | ||
|
||
const externals = [ | ||
...Object.keys(pkg.peerDependencies || {}), | ||
...Object.keys(pkg.dependencies || {}), | ||
] | ||
export default defineConfig({ | ||
plugins: [ | ||
dtsPlugin({ | ||
include: ['./src/**/*.ts', './src/**/*.tsx', './src/**/*.vue'], | ||
skipDiagnostics: false, | ||
staticImport: true, | ||
outputDir: ['./dist/types'], | ||
cleanVueFileName: false, | ||
}), | ||
VueMacros({ | ||
plugins: { | ||
vue: Vue(), | ||
}, | ||
}), | ||
], | ||
resolve: { | ||
alias: { | ||
'@': resolve(__dirname, 'src'), | ||
}, | ||
}, | ||
build: { | ||
target: 'modules', | ||
lib: { | ||
entry: path.resolve(__dirname, './src/index.ts'), | ||
formats: ['es', 'cjs'], | ||
}, | ||
rollupOptions: { | ||
external: externals, | ||
}, | ||
}, | ||
}) |