feat: add Typography EmptyState and Breadcrumbs

This commit is contained in:
maoyingjie
2026-10-22 13:07:13 +08:00
parent bcadbeaf8c
commit e4d817ce55
50 changed files with 3130 additions and 90 deletions
@@ -0,0 +1,95 @@
<script lang="ts">
import {
Fragment,
cloneVNode,
defineComponent,
h,
isVNode,
provide,
useAttrs,
type VNode,
type VNodeChild,
} from "vue";
import { breadcrumbsVariants } from "@heroui-vue/styles";
import { disabledContextKey, useInheritedDisabled } from "../field/disabled";
import BreadcrumbsItem from "./BreadcrumbsItem.vue";
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 isBreadcrumbsItem(node: VNodeChild): node is VNode {
return isVNode(node) && node.type === BreadcrumbsItem;
}
export default defineComponent({
name: "Breadcrumbs",
inheritAttrs: false,
props: {
ariaLabel: {
type: String,
default: "Breadcrumbs",
},
isDisabled: {
type: Boolean,
default: false,
},
/** Render the list as a custom element instead of `ol`. */
as: {
type: String,
default: "ol",
},
},
setup(props, { attrs, slots }) {
const variants = breadcrumbsVariants();
// Merge with ancestor Fieldset/disabled scopes; never shadow parent true.
const disabled = useInheritedDisabled(() => props.isDisabled);
provide(disabledContextKey, disabled);
function render() {
const nodes = flattenChildren((slots.default?.() ?? []) as VNodeChild[]);
const total = nodes.filter(isBreadcrumbsItem).length;
let index = 0;
const children = nodes.map((node) => {
if (!isBreadcrumbsItem(node)) return node;
const isLast = ++index === total;
return cloneVNode(node, {
isLast,
separatorSlot: slots.separator,
});
});
return h(
props.as,
{
...attrs,
class: [variants.base(), attrs.class],
"aria-label": props.ariaLabel || "Breadcrumbs",
"data-slot": "breadcrumbs",
},
children,
);
}
return render;
},
});
</script>
@@ -0,0 +1,187 @@
<script lang="ts">
import {
Comment,
Fragment,
Text,
cloneVNode,
defineComponent,
h,
isVNode,
useAttrs,
type PropType,
type VNodeChild,
} from "vue";
import { breadcrumbsVariants } from "@heroui-vue/styles";
import { useInheritedDisabled } from "../field/disabled";
import Link from "../link/Link.vue";
import BreadcrumbsSeparatorIcon from "./BreadcrumbsSeparatorIcon.vue";
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;
}
function toArray(content: VNodeChild): VNodeChild[] {
if (content == null || content === false || content === true) return [];
return Array.isArray(content) ? content : [content];
}
export default defineComponent({
name: "BreadcrumbsItem",
inheritAttrs: false,
props: {
href: {
type: String,
default: undefined,
},
target: {
type: String,
default: "_self",
},
rel: {
type: String,
default: undefined,
},
download: {
type: [Boolean, String] as PropType<boolean | string>,
default: undefined,
},
isDisabled: {
type: Boolean,
default: false,
},
/** Set by Breadcrumbs for the final item. */
isLast: {
type: Boolean,
default: false,
},
/** Set by Breadcrumbs from the `separator` slot. */
separatorSlot: {
type: Function as PropType<() => VNodeChild>,
default: undefined,
},
/** Render the item as a custom element instead of `li`. */
as: {
type: String,
default: "li",
},
},
setup(props, { attrs, slots }) {
const variants = breadcrumbsVariants();
const disabled = useInheritedDisabled(
() => props.isDisabled || props.isLast,
);
function renderSeparator(): VNodeChild {
const nodes = flattenChildren(
toArray(props.separatorSlot?.() as VNodeChild),
);
const meaningful = nodes.filter(isMeaningful);
if (meaningful.length === 0) {
return h(BreadcrumbsSeparatorIcon, {
class: variants.separator(),
"data-slot": "breadcrumbs-separator",
});
}
if (meaningful.length === 1 && isVNode(meaningful[0])) {
return cloneVNode(meaningful[0], {
class: variants.separator(),
"data-slot": "breadcrumbs-separator",
});
}
return meaningful;
}
function render() {
const current = props.isLast;
// RAC Link renders a span when disabled or when no href is provided.
const nonNavigable = current || disabled.value || !props.href;
const callerClass = attrs.class;
const rootAttrs: Record<string, unknown> = {};
const linkAttrs: Record<string, unknown> = {};
for (const [key, value] of Object.entries(attrs)) {
if (key === "class") continue;
if (/^on[A-Z]/.test(key)) linkAttrs[key] = value;
else rootAttrs[key] = value;
}
const content =
slots.default?.({
isCurrent: current,
isDisabled: disabled.value,
}) ?? [];
const link = h(
Link,
{
// Reka Primitive picks the Slot/element branch once in setup.
// A stable key per structural mode remounts Link when switching
// between navigable <a> and non-navigable <span>.
key: nonNavigable
? "breadcrumbs-link-static"
: "breadcrumbs-link-nav",
...linkAttrs,
asChild: nonNavigable,
...(current ? { "aria-current": "page" } : {}),
...(current ? { "data-current": "true" } : {}),
...(disabled.value ? { "data-disabled": "true" } : {}),
...(nonNavigable ? {} : { href: props.href }),
...(nonNavigable ? {} : { target: props.target }),
...(nonNavigable ? {} : { rel: props.rel }),
...(nonNavigable ? {} : { download: props.download }),
isDisabled: current || disabled.value,
class: [variants.link(), callerClass],
},
{
default: () => (nonNavigable ? h("span", content) : content),
},
);
return h(
props.as,
{
...rootAttrs,
class: [variants.item(), callerClass],
"data-slot": "breadcrumbs-item",
"data-current": current ? "true" : undefined,
"data-disabled": disabled.value ? "true" : undefined,
},
[link, current ? null : renderSeparator()],
);
}
return render;
},
});
</script>
@@ -0,0 +1,18 @@
<template>
<svg
aria-hidden="true"
fill="none"
height="16"
role="presentation"
viewBox="0 0 16 16"
width="16"
xmlns="http://www.w3.org/2000/svg"
>
<path
clip-rule="evenodd"
d="M5.47 2.97a.75.75 0 0 1 1.06 0l4.5 4.5a.75.75 0 0 1 0 1.06l-4.5 4.5a.75.75 0 1 1-1.06-1.06L9.44 8 5.47 4.03a.75.75 0 0 1 0-1.06Z"
fill="currentColor"
fill-rule="evenodd"
/>
</svg>
</template>