@raxium/vue-addons-shared 0.1.2 → 0.1.3

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.
@@ -0,0 +1,30 @@
1
+ import type { Component, PropType } from 'vue';
2
+ export type AsTag = 'a' | 'button' | 'div' | 'form' | 'h2' | 'h3' | 'img' | 'input' | 'label' | 'li' | 'nav' | 'ol' | 'p' | 'span' | 'svg' | 'ul' | 'template' | ({} & string);
3
+ export interface PrimitiveProps {
4
+ asChild?: boolean;
5
+ as?: AsTag | Component;
6
+ }
7
+ export declare const Primitive: import("vue").DefineComponent<import("vue").ExtractPropTypes<{
8
+ asChild: {
9
+ type: BooleanConstructor;
10
+ default: boolean;
11
+ };
12
+ as: {
13
+ type: PropType<AsTag | Component>;
14
+ default: string;
15
+ };
16
+ }>, () => import("vue").VNode<import("vue").RendererNode, import("vue").RendererElement, {
17
+ [key: string]: any;
18
+ }>, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<import("vue").ExtractPropTypes<{
19
+ asChild: {
20
+ type: BooleanConstructor;
21
+ default: boolean;
22
+ };
23
+ as: {
24
+ type: PropType<AsTag | Component>;
25
+ default: string;
26
+ };
27
+ }>> & Readonly<{}>, {
28
+ asChild: boolean;
29
+ as: AsTag | Component;
30
+ }, {}, {}, {}, string, import("vue").ComponentProvideOptions, true, {}, any>;
@@ -0,0 +1,54 @@
1
+ import { cloneVNode, defineComponent, h, mergeProps } from 'vue';
2
+ import { renderSlotFragments } from '../utils/renderSlotFragments';
3
+ const SELF_CLOSING_TAGS = ['area', 'img', 'input'];
4
+ const Slot = defineComponent({
5
+ name: 'PrimitiveSlot',
6
+ inheritAttrs: false,
7
+ setup(_, { attrs, slots }) {
8
+ return () => {
9
+ if (!slots.default)
10
+ return null;
11
+ const children = renderSlotFragments(slots.default());
12
+ const firstNonCommentChildrenIndex = children.findIndex(child => child.type !== Comment);
13
+ if (firstNonCommentChildrenIndex === -1)
14
+ return children;
15
+ const firstNonCommentChildren = children[firstNonCommentChildrenIndex];
16
+ // Remove props ref from being inferred
17
+ delete firstNonCommentChildren.props?.ref;
18
+ // Manually merge props to ensure `firstNonCommentChildren.props`
19
+ // has higher priority than `attrs` and can override `attrs`.
20
+ // Otherwise `cloneVNode(firstNonCommentChildren, attrs)` will
21
+ // prioritize `attrs` and override `firstNonCommentChildren.props`.
22
+ const mergedProps = firstNonCommentChildren.props
23
+ ? mergeProps(attrs, firstNonCommentChildren.props)
24
+ : attrs;
25
+ const cloned = cloneVNode({ ...firstNonCommentChildren, props: {} }, mergedProps);
26
+ if (children.length === 1)
27
+ return cloned;
28
+ children[firstNonCommentChildrenIndex] = cloned;
29
+ return children;
30
+ };
31
+ },
32
+ });
33
+ export const Primitive = defineComponent({
34
+ name: 'Primitive',
35
+ inheritAttrs: false,
36
+ props: {
37
+ asChild: {
38
+ type: Boolean,
39
+ default: false,
40
+ },
41
+ as: {
42
+ type: [String, Object],
43
+ default: 'div',
44
+ },
45
+ },
46
+ setup(props, { attrs, slots }) {
47
+ const asTag = props.asChild ? 'template' : props.as;
48
+ if (typeof asTag === 'string' && SELF_CLOSING_TAGS.includes(asTag))
49
+ return () => h(asTag, attrs);
50
+ if (asTag !== 'template')
51
+ return () => h(props.as, attrs, { default: slots.default });
52
+ return () => h(Slot, attrs, { default: slots.default });
53
+ },
54
+ });
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ export * from './components/Primitive';
1
2
  export * from './composables/useForwardExpose';
2
3
  export * from './composables/useForwardProps';
3
4
  export * from './composables/useFowardPropsEmits';
package/dist/index.js CHANGED
@@ -1,3 +1,4 @@
1
+ export * from './components/Primitive';
1
2
  export * from './composables/useForwardExpose';
2
3
  export * from './composables/useForwardProps';
3
4
  export * from './composables/useFowardPropsEmits';
@@ -0,0 +1,2 @@
1
+ import type { VNode } from 'vue';
2
+ export declare function renderSlotFragments(children?: VNode[]): VNode[];
@@ -0,0 +1,10 @@
1
+ import { Fragment } from 'vue';
2
+ export function renderSlotFragments(children) {
3
+ if (!children)
4
+ return [];
5
+ return children.flatMap((child) => {
6
+ if (child.type === Fragment)
7
+ return renderSlotFragments(child.children);
8
+ return [child];
9
+ });
10
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@raxium/vue-addons-shared",
3
3
  "type": "module",
4
- "version": "0.1.2",
4
+ "version": "0.1.3",
5
5
  "description": "Shared utilities for Raxium Vue Addons",
6
6
  "author": {
7
7
  "name": "Hwacc",