feat: add Typography EmptyState and Breadcrumbs
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
<script lang="ts">
|
||||
import {
|
||||
Comment,
|
||||
Fragment,
|
||||
Text,
|
||||
defineComponent,
|
||||
h,
|
||||
isVNode,
|
||||
type VNodeChild,
|
||||
} from "vue";
|
||||
import { emptyStateVariants } from "@heroui-vue/styles";
|
||||
|
||||
function flattenChildren(
|
||||
nodes: VNodeChild[],
|
||||
out: VNodeChild[] = [],
|
||||
): VNodeChild[] {
|
||||
for (const child of nodes) {
|
||||
if (Array.isArray(child)) {
|
||||
flattenChildren(child, out);
|
||||
continue;
|
||||
}
|
||||
if (
|
||||
isVNode(child) &&
|
||||
child.type === Fragment &&
|
||||
Array.isArray(child.children)
|
||||
) {
|
||||
flattenChildren(child.children as VNodeChild[], out);
|
||||
continue;
|
||||
}
|
||||
out.push(child);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
function isMeaningful(node: VNodeChild): boolean {
|
||||
if (node == null || node === false || node === true) return false;
|
||||
if (typeof node === "string") return node.trim() !== "";
|
||||
if (typeof node === "number") return true;
|
||||
if (!isVNode(node)) return true;
|
||||
if (node.type === Comment) return false;
|
||||
if (node.type === Text) return String(node.children ?? "").trim() !== "";
|
||||
return true;
|
||||
}
|
||||
|
||||
export default defineComponent({
|
||||
name: "EmptyState",
|
||||
inheritAttrs: false,
|
||||
props: {
|
||||
/** Render a custom root element instead of `div`. */
|
||||
as: {
|
||||
type: String,
|
||||
default: "div",
|
||||
},
|
||||
},
|
||||
setup(props, { attrs, slots }) {
|
||||
/** Re-read the slot each render so conditional content updates its fallback. */
|
||||
function render() {
|
||||
const children = flattenChildren(
|
||||
(slots.default?.() ?? []) as VNodeChild[],
|
||||
);
|
||||
const content: VNodeChild[] = children.some(isMeaningful)
|
||||
? children
|
||||
: ["No results found"];
|
||||
|
||||
return h(
|
||||
props.as,
|
||||
{
|
||||
...attrs,
|
||||
class: [emptyStateVariants(), attrs.class],
|
||||
"data-slot": "empty-state",
|
||||
},
|
||||
content,
|
||||
);
|
||||
}
|
||||
|
||||
return render;
|
||||
},
|
||||
});
|
||||
</script>
|
||||
Reference in New Issue
Block a user