vue-unwrap 1.0.0 → 1.0.1
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/dist/index.d.ts +5 -4
- package/dist/index.js +17 -3
- package/package.json +3 -2
package/dist/index.d.ts
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
import * as _$vue from "vue";
|
|
2
|
-
import { ComponentPublicInstance, SetupContext } from "vue";
|
|
2
|
+
import { ComponentPublicInstance, SetupContext, VNode, VNodeArrayChildren } from "vue";
|
|
3
3
|
|
|
4
4
|
//#region src/useUnwrap.d.ts
|
|
5
5
|
type NodeRef = Element | ComponentPublicInstance | null;
|
|
6
6
|
declare function getNodeElement<N = NodeRef>(node: N): Element | null;
|
|
7
|
+
declare function unwrapVNodes(vnodes: VNodeArrayChildren): VNode[];
|
|
7
8
|
declare function useUnwrap<N = NodeRef, P = Record<string, any>>(propTypes?: Record<string, any>): {
|
|
8
9
|
$el: _$vue.ShallowRef<Element | null, Element | null>;
|
|
9
10
|
children: _$vue.ShallowReactive<N[]>;
|
|
10
11
|
Unwrap: {
|
|
11
|
-
(props: P, context: SetupContext):
|
|
12
|
+
(props: P, context: SetupContext): VNode<_$vue.RendererNode, _$vue.RendererElement, {
|
|
12
13
|
[key: string]: any;
|
|
13
|
-
}> |
|
|
14
|
+
}> | VNode<_$vue.RendererNode, _$vue.RendererElement, {
|
|
14
15
|
[key: string]: any;
|
|
15
16
|
}>[];
|
|
16
17
|
inheritAttrs: boolean;
|
|
@@ -27,4 +28,4 @@ declare const __VLS_export: _$vue.DefineComponent<{}, {
|
|
|
27
28
|
}, {}, {}, {}, _$vue.ComponentOptionsMixin, _$vue.ComponentOptionsMixin, {}, string, _$vue.PublicProps, Readonly<{}> & Readonly<{}>, {}, {}, {}, {}, string, _$vue.ComponentProvideOptions, true, {}, any>;
|
|
28
29
|
declare const _default: typeof __VLS_export;
|
|
29
30
|
//#endregion
|
|
30
|
-
export { NodeRef, _default as Unwrap, getNodeElement, useUnwrap };
|
|
31
|
+
export { NodeRef, _default as Unwrap, getNodeElement, unwrapVNodes, useUnwrap };
|
package/dist/index.js
CHANGED
|
@@ -1,9 +1,23 @@
|
|
|
1
|
-
import { cloneVNode, createBlock, defineComponent, h, openBlock, shallowReactive, shallowRef, unref, useSlots } from "vue";
|
|
1
|
+
import { Comment, Fragment, cloneVNode, createBlock, defineComponent, h, isVNode, openBlock, shallowReactive, shallowRef, unref, useSlots } from "vue";
|
|
2
2
|
//#region src/useUnwrap.ts
|
|
3
3
|
function getNodeElement(node) {
|
|
4
4
|
const el = node?.$el || node;
|
|
5
5
|
return el instanceof Element ? el : null;
|
|
6
6
|
}
|
|
7
|
+
function unwrapVNodes(vnodes) {
|
|
8
|
+
const nodes = [];
|
|
9
|
+
for (const node of vnodes) {
|
|
10
|
+
if (node == null || typeof node === "boolean") continue;
|
|
11
|
+
if (Array.isArray(node)) {
|
|
12
|
+
nodes.push(...unwrapVNodes(node));
|
|
13
|
+
continue;
|
|
14
|
+
}
|
|
15
|
+
if (!isVNode(node) || node.type === Comment) continue;
|
|
16
|
+
if (node.type === Fragment && Array.isArray(node.children)) nodes.push(...unwrapVNodes(node.children));
|
|
17
|
+
else nodes.push(node);
|
|
18
|
+
}
|
|
19
|
+
return nodes;
|
|
20
|
+
}
|
|
7
21
|
function useUnwrap(propTypes) {
|
|
8
22
|
const $el = shallowRef(null);
|
|
9
23
|
const children = shallowReactive([]);
|
|
@@ -12,7 +26,7 @@ function useUnwrap(propTypes) {
|
|
|
12
26
|
const trackWrapperRef = (node) => $el.value = getNodeElement(node);
|
|
13
27
|
function Unwrap(props, context) {
|
|
14
28
|
const { is, ...attrs } = context.attrs;
|
|
15
|
-
const nodes = (slots.default?.(props) ?? []).map((node, i) => cloneVNode(node, { ref: trackChildRef(i) }, true));
|
|
29
|
+
const nodes = unwrapVNodes(slots.default?.(props) ?? []).map((node, i) => cloneVNode(node, { ref: trackChildRef(i) }, true));
|
|
16
30
|
const wrapper = is ? cloneVNode(h(is, attrs, nodes), { ref: trackWrapperRef }, true) : null;
|
|
17
31
|
children.splice(nodes.length);
|
|
18
32
|
return wrapper || nodes;
|
|
@@ -41,4 +55,4 @@ var Unwrap_default = /* @__PURE__ */ defineComponent({
|
|
|
41
55
|
}
|
|
42
56
|
});
|
|
43
57
|
//#endregion
|
|
44
|
-
export { Unwrap_default as Unwrap, getNodeElement, useUnwrap };
|
|
58
|
+
export { Unwrap_default as Unwrap, getNodeElement, unwrapVNodes, useUnwrap };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vue-unwrap",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.0.
|
|
4
|
+
"version": "1.0.1",
|
|
5
5
|
"description": "Conditional wrapper component for Vue 3 with child ref aggregation.",
|
|
6
6
|
"author": "Andrea 'Fiad' Fiadone <hello@fiad.one>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -30,7 +30,8 @@
|
|
|
30
30
|
"test": "vitest",
|
|
31
31
|
"typecheck": "vue-tsc --noEmit",
|
|
32
32
|
"release": "bumpp",
|
|
33
|
-
"prepublishOnly": "npm run build"
|
|
33
|
+
"prepublishOnly": "npm run build",
|
|
34
|
+
"commit": "cz"
|
|
34
35
|
},
|
|
35
36
|
"peerDependencies": {
|
|
36
37
|
"vue": "^3.5.0"
|