vue-unwrap 1.0.0 → 1.1.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/dist/index.d.ts CHANGED
@@ -1,16 +1,21 @@
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[]>;
11
+ dom: _$vue.ComputedRef<{
12
+ el: Element | null;
13
+ children: _$vue.ShallowReactive<N[]>;
14
+ }>;
10
15
  Unwrap: {
11
- (props: P, context: SetupContext): _$vue.VNode<_$vue.RendererNode, _$vue.RendererElement, {
16
+ (props: P, context: SetupContext): VNode<_$vue.RendererNode, _$vue.RendererElement, {
12
17
  [key: string]: any;
13
- }> | _$vue.VNode<_$vue.RendererNode, _$vue.RendererElement, {
18
+ }> | VNode<_$vue.RendererNode, _$vue.RendererElement, {
14
19
  [key: string]: any;
15
20
  }>[];
16
21
  inheritAttrs: boolean;
@@ -21,10 +26,29 @@ declare function useUnwrap<N = NodeRef, P = Record<string, any>>(propTypes?: Rec
21
26
  };
22
27
  //#endregion
23
28
  //#region src/Unwrap.vue.d.ts
24
- declare const __VLS_export: _$vue.DefineComponent<{}, {
29
+ declare const dom: _$vue.ComputedRef<{
30
+ el: Element | null;
31
+ children: _$vue.ShallowReactive<NodeRef[]>;
32
+ }>;
33
+ type __VLS_Slots = {
34
+ default(props: {
35
+ dom: typeof dom;
36
+ }): any;
37
+ };
38
+ declare const __VLS_base: _$vue.DefineComponent<{}, {
25
39
  $el: _$vue.ShallowRef<Element | null, Element | null>;
26
40
  children: _$vue.ShallowReactive<NodeRef[]>;
41
+ dom: _$vue.ComputedRef<{
42
+ el: Element | null;
43
+ children: _$vue.ShallowReactive<NodeRef[]>;
44
+ }>;
27
45
  }, {}, {}, {}, _$vue.ComponentOptionsMixin, _$vue.ComponentOptionsMixin, {}, string, _$vue.PublicProps, Readonly<{}> & Readonly<{}>, {}, {}, {}, {}, string, _$vue.ComponentProvideOptions, true, {}, any>;
46
+ declare const __VLS_export: __VLS_WithSlots<typeof __VLS_base, __VLS_Slots>;
28
47
  declare const _default: typeof __VLS_export;
48
+ type __VLS_WithSlots<T, S> = T & {
49
+ new (): {
50
+ $slots: S;
51
+ };
52
+ };
29
53
  //#endregion
30
- export { NodeRef, _default as Unwrap, getNodeElement, useUnwrap };
54
+ export { NodeRef, _default as Unwrap, getNodeElement, unwrapVNodes, useUnwrap };
package/dist/index.js CHANGED
@@ -1,18 +1,36 @@
1
- import { cloneVNode, createBlock, defineComponent, h, openBlock, shallowReactive, shallowRef, unref, useSlots } from "vue";
1
+ import { Comment, Fragment, cloneVNode, computed, 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([]);
24
+ const dom = computed(() => ({
25
+ el: $el.value,
26
+ children
27
+ }));
10
28
  const slots = useSlots();
11
29
  const trackChildRef = (index) => (node) => children[index] = node;
12
30
  const trackWrapperRef = (node) => $el.value = getNodeElement(node);
13
31
  function Unwrap(props, context) {
14
32
  const { is, ...attrs } = context.attrs;
15
- const nodes = (slots.default?.(props) ?? []).map((node, i) => cloneVNode(node, { ref: trackChildRef(i) }, true));
33
+ const nodes = unwrapVNodes(slots.default?.(props) ?? []).map((node, i) => cloneVNode(node, { ref: trackChildRef(i) }, true));
16
34
  const wrapper = is ? cloneVNode(h(is, attrs, nodes), { ref: trackWrapperRef }, true) : null;
17
35
  children.splice(nodes.length);
18
36
  return wrapper || nodes;
@@ -22,6 +40,7 @@ function useUnwrap(propTypes) {
22
40
  return {
23
41
  $el,
24
42
  children,
43
+ dom,
25
44
  Unwrap
26
45
  };
27
46
  }
@@ -30,15 +49,16 @@ function useUnwrap(propTypes) {
30
49
  var Unwrap_default = /* @__PURE__ */ defineComponent({
31
50
  __name: "Unwrap",
32
51
  setup(__props, { expose: __expose }) {
33
- const { $el, children, Unwrap } = useUnwrap();
52
+ const { $el, children, dom, Unwrap } = useUnwrap();
34
53
  __expose({
35
54
  $el,
36
- children
55
+ children,
56
+ dom
37
57
  });
38
58
  return (_ctx, _cache) => {
39
- return openBlock(), createBlock(unref(Unwrap));
59
+ return openBlock(), createBlock(unref(Unwrap), { dom: unref(dom) }, null, 8, ["dom"]);
40
60
  };
41
61
  }
42
62
  });
43
63
  //#endregion
44
- export { Unwrap_default as Unwrap, getNodeElement, useUnwrap };
64
+ 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.0",
4
+ "version": "1.1.0",
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"