@ark-ui/vue
Version:
A collection of unstyled, accessible UI components for Vue, utilizing state machines for seamless interaction.
37 lines (34 loc) • 1.15 kB
JavaScript
import { mergeProps } from '@zag-js/vue';
import { defineComponent, cloneVNode, Fragment } from 'vue';
const Dynamic = defineComponent({
name: "Dynamic",
inheritAttrs: false,
setup(_, { attrs, slots }) {
return () => {
if (!slots.default) return null;
const children = renderSlotFragments(slots.default());
const [firstChildren, ...otherChildren] = children;
if (Object.keys(attrs).length > 0) {
delete firstChildren.props?.ref;
const mergedProps = mergeProps(attrs, firstChildren.props ?? {});
const cloned = cloneVNode(firstChildren, mergedProps);
for (const prop in mergedProps) {
if (prop.startsWith("on")) {
cloned.props ||= {};
cloned.props[prop] = mergedProps[prop];
}
}
return children.length === 1 ? cloned : [cloned, ...otherChildren];
}
return children;
};
}
});
function renderSlotFragments(children) {
if (!children) return [];
return children.flatMap((child) => {
if (child.type === Fragment) return renderSlotFragments(child.children);
return [child];
});
}
export { Dynamic };