Skip to content

Commit

Permalink
feat(compose-refs): new package
Browse files Browse the repository at this point in the history
  • Loading branch information
productdevbook committed May 3, 2023
1 parent 961fb2b commit 84620fe
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 0 deletions.
10 changes: 10 additions & 0 deletions packages/core/compose-refs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# `@oku-ui/compose-refs`

## Installation

```sh
$ pnpm add @oku-ui/compose-refs
```

## Usage
...
42 changes: 42 additions & 0 deletions packages/core/compose-refs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"name": "@oku-ui/compose-refs",
"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/compose-refs.cjs",
"import": "./dist/compose-refs.js"
}
},
"main": "dist/compose-refs.cjs",
"module": "dist/compose-refs.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:^"
}
}
34 changes: 34 additions & 0 deletions packages/core/compose-refs/src/compose-refs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import type { Ref } from 'vue'
import { onMounted, onUnmounted, ref } from 'vue'

function useComposedRefs(refs: Ref[]) {
const composedRef = ref(null)

onMounted(() => {
composedRef.value = refs.reduce((acc: any, ref) => {
if (!ref || !ref.value)
return acc
if (typeof acc === 'function') {
return (value: any) => {
ref.value = value
acc(value)
}
}
else {
ref.value = acc
return ref.value
}
}, null)
})

onUnmounted(() => {
refs.forEach((ref) => {
if (ref && ref.value)
ref.value = null
})
})

return composedRef
}

export { useComposedRefs }
1 change: 1 addition & 0 deletions packages/core/compose-refs/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { useComposedRefs } from './compose-refs'
9 changes: 9 additions & 0 deletions packages/core/compose-refs/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/compose-refs/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 84620fe

Please sign in to comment.