vue-use-forward-ref 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026-present, REFINIST
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,104 @@
1
+ # vue-forwardref
2
+
3
+ [![npm](https://img.shields.io/npm/v/vue-forwardref.svg?colorA=42b883&colorB=35495e)](https://npmjs.com/package/vue-forwardref) [![Unit Test](https://img.shields.io/github/actions/workflow/status/refinist/vue-forwardref/unit-test.yml?colorA=42b883&colorB=35495e&label=Unit%20Test)](https://github.com/refinist/vue-forwardref/actions/workflows/unit-test.yml) [![codecov](https://img.shields.io/codecov/c/github/refinist/vue-forwardref?colorA=42b883&colorB=35495e)](https://codecov.io/github/refinist/vue-forwardref) [![size](https://img.badgesize.io/https://unpkg.com/vue-forwardref?compression=gzip&labelColor=42b883&color=35495e)](https://unpkg.com/vue-forwardref)
4
+
5
+ A lightweight Vue 3 composable to forward component ref and merge the child's exposed API onto the parent instance. Parent's ref then exposes both the child's `expose()` and optional extra fields in one place.
6
+
7
+ ## ✨ Features
8
+
9
+ - **Lightweight** – Zero dependencies, minimal footprint
10
+ - **Type Safe** – Full TypeScript support
11
+ - **Simple API** – `useForwardRef(ext?)` returns `{ forwardRef }`, pass it to child's `ref`
12
+ - **Merge with ext** – Optionally merge extra fields onto the parent's exposed object
13
+
14
+ ## 📦 Installation
15
+
16
+ ```bash
17
+ # npm
18
+ npm install vue-forwardref
19
+
20
+ # yarn
21
+ yarn add vue-forwardref
22
+
23
+ # pnpm
24
+ pnpm add vue-forwardref
25
+
26
+ # bun
27
+ bun add vue-forwardref
28
+ ```
29
+
30
+ ## 🚀 Basic Usage
31
+
32
+ ### Forward child ref to parent
33
+
34
+ Use `forwardRef` as the child's `ref`. The parent's ref (e.g. from a grandparent) will then receive the child's exposed API.
35
+
36
+ ```vue
37
+ <template>
38
+ <Child ref="forwardRef" />
39
+ </template>
40
+
41
+ <script setup lang="ts">
42
+ import { ref } from 'vue';
43
+ import { useForwardRef } from 'vue-forwardref';
44
+ import Child from './Child.vue';
45
+
46
+ const { forwardRef } = useForwardRef();
47
+ </script>
48
+ ```
49
+
50
+ When a parent mounts this component with `<Wrapper ref="parentRef" />`, `parentRef` will expose the same API as `Child` (whatever `Child` passed to `expose()`).
51
+
52
+ ### Merge extra fields with `useForwardRef(ext)`
53
+
54
+ You can merge additional fields onto the exposed object so the parent ref sees both the child's API and your extra data.
55
+
56
+ ```vue
57
+ <template>
58
+ <Child ref="forwardRef" />
59
+ </template>
60
+
61
+ <script setup lang="ts">
62
+ import { ref } from 'vue';
63
+ import { useForwardRef } from 'vue-forwardref';
64
+ import Child from './Child.vue';
65
+
66
+ const count = ref(0);
67
+ const { forwardRef } = useForwardRef({
68
+ count,
69
+ reset: () => {
70
+ count.value = 0;
71
+ }
72
+ });
73
+ </script>
74
+ ```
75
+
76
+ The parent's ref will then have both `Child`'s exposed API and `count` / `reset`.
77
+
78
+ ## 📚 API
79
+
80
+ ### `useForwardRef<E>(ext?: E)`
81
+
82
+ Must be called in a component's `setup()` (uses `getCurrentInstance()`).
83
+
84
+ **Parameters:**
85
+
86
+ - `ext` (optional) – Object to merge onto the current component's `exposed` / `exposeProxy`. When the ref callback runs (child mounted), both the child instance (or element) and `ext` are assigned onto the current instance's exposed object.
87
+
88
+ **Returns:**
89
+
90
+ ```ts
91
+ { forwardRef: (originRef: OriginRef) => void }
92
+ ```
93
+
94
+ - `forwardRef` – Ref callback. Use it as the child's `ref` (e.g. `ref="forwardRef"` or `:ref="forwardRef"`). When the child is mounted, `originRef` is the child instance or DOM element; when unmounted, it is `null`. The current component's exposed object is updated so that the parent's ref sees the merged API.
95
+
96
+ **Types:**
97
+
98
+ - `OriginRef = Element | ComponentPublicInstance | null`
99
+
100
+ ## 📄 License
101
+
102
+ [MIT](./LICENSE)
103
+
104
+ Copyright (c) 2025-present REFINIST
@@ -0,0 +1,11 @@
1
+ import { ComponentPublicInstance } from "vue";
2
+
3
+ //#region src/types.d.ts
4
+ type OriginRef = Element | ComponentPublicInstance | null;
5
+ //#endregion
6
+ //#region src/index.d.ts
7
+ declare function useForwardRef<E extends Record<string, any>>(ext?: E): {
8
+ forwardRef: (originRef: OriginRef) => void;
9
+ };
10
+ //#endregion
11
+ export { useForwardRef };
package/dist/index.mjs ADDED
@@ -0,0 +1 @@
1
+ import{getCurrentInstance as e}from"vue";function t(t){let n=e();function r(e){n.exposed=Object.assign(e||{},t),n.exposeProxy=Object.assign(e||{},t)}return{forwardRef:r}}export{t as useForwardRef};
package/package.json ADDED
@@ -0,0 +1,76 @@
1
+ {
2
+ "name": "vue-use-forward-ref",
3
+ "type": "module",
4
+ "version": "0.0.0",
5
+ "packageManager": "pnpm@10.30.3",
6
+ "description": "A lightweight Vue 3 composable to forward component ref and merge child exposed API onto parent instance",
7
+ "author": "REFINIST",
8
+ "license": "MIT",
9
+ "homepage": "https://github.com/refinist/vue-forwardref#readme",
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/refinist/vue-forwardref.git"
13
+ },
14
+ "bugs": {
15
+ "url": "https://github.com/refinist/vue-forwardref/issues"
16
+ },
17
+ "keywords": [
18
+ "vue",
19
+ "vue3",
20
+ "composable",
21
+ "forward-ref",
22
+ "ref-forwarding",
23
+ "expose",
24
+ "composition-api",
25
+ "typescript",
26
+ "component-ref"
27
+ ],
28
+ "exports": {
29
+ ".": "./dist/index.mjs",
30
+ "./package.json": "./package.json"
31
+ },
32
+ "types": "./dist/index.d.mts",
33
+ "files": [
34
+ "dist"
35
+ ],
36
+ "engines": {
37
+ "node": ">=20.19.0"
38
+ },
39
+ "scripts": {
40
+ "build": "tsdown",
41
+ "lint": "eslint --cache .",
42
+ "lint:fix": "pnpm run lint --fix",
43
+ "format": "prettier --cache --write .",
44
+ "prepublishOnly": "pnpm run build",
45
+ "release": "bumpp && pnpm publish",
46
+ "test": "vitest run",
47
+ "test:coverage": "vitest run --coverage",
48
+ "test:watch": "vitest",
49
+ "typecheck": "tsgo --noEmit"
50
+ },
51
+ "peerDependencies": {
52
+ "vue": ">=3"
53
+ },
54
+ "devDependencies": {
55
+ "@refinist/eslint-config": "^1.2.0",
56
+ "@refinist/prettier-config": "^1.1.1",
57
+ "@typescript/native-preview": "7.0.0-dev.20260228.1",
58
+ "@vitejs/plugin-vue": "^6.0.1",
59
+ "@vitejs/plugin-vue-jsx": "^5.1.4",
60
+ "@vitest/coverage-istanbul": "3.2.4",
61
+ "@vue/test-utils": "^2.4.6",
62
+ "bumpp": "^10.4.1",
63
+ "eslint": "^9.39.3",
64
+ "jsdom": "^26.1.0",
65
+ "prettier": "^3.8.1",
66
+ "tsdown": "^0.20.3",
67
+ "typescript": "^5.9.3",
68
+ "vitest": "^4.0.18",
69
+ "vue": "^3.5.21"
70
+ },
71
+ "prettier": "@refinist/prettier-config",
72
+ "publishConfig": {
73
+ "access": "public",
74
+ "registry": "https://registry.npmjs.org/"
75
+ }
76
+ }