@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
package/dist/index.js
CHANGED