Skip to content

Commit

Permalink
feat(use-previous): new package
Browse files Browse the repository at this point in the history
  • Loading branch information
productdevbook committed May 3, 2023
1 parent 84620fe commit 3636d75
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 0 deletions.
10 changes: 10 additions & 0 deletions packages/core/use-previous/README.md
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
...
42 changes: 42 additions & 0 deletions packages/core/use-previous/package.json
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:^"
}
}
1 change: 1 addition & 0 deletions packages/core/use-previous/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { usePrevious } from './use-previous'
22 changes: 22 additions & 0 deletions packages/core/use-previous/src/use-previous.ts
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 }
9 changes: 9 additions & 0 deletions packages/core/use-previous/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "./../../../tsconfig.json",
"compilerOptions": {
"baseUrl": "."
},
"include": [
"./"
]
}
48 changes: 48 additions & 0 deletions packages/core/use-previous/vite.config.ts
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,
},
},
})

0 comments on commit 3636d75

Please sign in to comment.