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>
@@ -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>
+1 -1
View File
@@ -259,7 +259,7 @@ defineExpose({
:as-child="asChild"
v-bind="passthroughAttrs"
:href="href"
:target="target"
:target="href ? target : undefined"
:rel="rel"
:download="downloadAttr"
:class="slots.base()"
@@ -0,0 +1,68 @@
<script setup lang="ts">
import { computed, useAttrs } from "vue";
import {
typographyVariants,
type TypographyVariants,
} from "@heroui-vue/styles";
defineOptions({ inheritAttrs: false });
type TypographyType = NonNullable<TypographyVariants["type"]>;
const props = withDefaults(
defineProps<{
type?: TypographyType;
align?: NonNullable<TypographyVariants["align"]>;
color?: NonNullable<TypographyVariants["color"]>;
weight?: TypographyVariants["weight"];
truncate?: boolean;
/** Render a different element while keeping the visual type. */
as?: string;
}>(),
{
type: "body",
align: "start",
color: "default",
truncate: false,
},
);
const defaultElementByType: Record<TypographyType, string> = {
body: "p",
"body-sm": "p",
"body-xs": "p",
code: "code",
h1: "h1",
h2: "h2",
h3: "h3",
h4: "h4",
h5: "h5",
h6: "h6",
};
const attrs = useAttrs();
const element = computed(() => props.as ?? defaultElementByType[props.type]);
const baseClass = computed(() =>
typographyVariants({
align: props.align,
color: props.color,
truncate: props.truncate,
type: props.type,
weight: props.weight,
}).base(),
);
</script>
<template>
<component
:is="element"
v-bind="attrs"
:class="baseClass"
data-slot="typography"
:data-type="type"
>
<slot />
</component>
</template>
@@ -0,0 +1,35 @@
<script setup lang="ts">
import type { TypographyVariants } from "@heroui-vue/styles";
import Typography from "./Typography.vue";
defineOptions({ inheritAttrs: false });
withDefaults(
defineProps<{
align?: NonNullable<TypographyVariants["align"]>;
color?: NonNullable<TypographyVariants["color"]>;
weight?: TypographyVariants["weight"];
truncate?: boolean;
as?: string;
}>(),
{
align: "start",
color: "default",
truncate: false,
},
);
</script>
<template>
<Typography
v-bind="$attrs"
type="code"
:align="align"
:color="color"
:weight="weight"
:truncate="truncate"
:as="as"
>
<slot />
</Typography>
</template>
@@ -0,0 +1,42 @@
<script setup lang="ts">
import { computed } from "vue";
import type { TypographyVariants } from "@heroui-vue/styles";
import Typography from "./Typography.vue";
defineOptions({ inheritAttrs: false });
const props = withDefaults(
defineProps<{
level?: 1 | 2 | 3 | 4 | 5 | 6;
align?: NonNullable<TypographyVariants["align"]>;
color?: NonNullable<TypographyVariants["color"]>;
weight?: TypographyVariants["weight"];
truncate?: boolean;
as?: string;
}>(),
{
level: 1,
align: "start",
color: "default",
truncate: false,
},
);
const type = computed(
() => `h${props.level}` as NonNullable<TypographyVariants["type"]>,
);
</script>
<template>
<Typography
v-bind="$attrs"
:type="type"
:align="align"
:color="color"
:weight="weight"
:truncate="truncate"
:as="as"
>
<slot />
</Typography>
</template>
@@ -0,0 +1,47 @@
<script setup lang="ts">
import { computed } from "vue";
import type { TypographyVariants } from "@heroui-vue/styles";
import Typography from "./Typography.vue";
defineOptions({ inheritAttrs: false });
type ParagraphSize = "base" | "sm" | "xs";
const props = withDefaults(
defineProps<{
size?: ParagraphSize;
align?: NonNullable<TypographyVariants["align"]>;
color?: NonNullable<TypographyVariants["color"]>;
weight?: TypographyVariants["weight"];
truncate?: boolean;
as?: string;
}>(),
{
size: "base",
align: "start",
color: "default",
truncate: false,
},
);
const type = computed(
() =>
(props.size === "base" ? "body" : `body-${props.size}`) as NonNullable<
TypographyVariants["type"]
>,
);
</script>
<template>
<Typography
v-bind="$attrs"
:type="type"
:align="align"
:color="color"
:weight="weight"
:truncate="truncate"
:as="as"
>
<slot />
</Typography>
</template>
@@ -0,0 +1,15 @@
<script setup lang="ts">
import { useAttrs } from "vue";
import { typographyVariants } from "@heroui-vue/styles";
defineOptions({ inheritAttrs: false });
const attrs = useAttrs();
const slots = typographyVariants();
</script>
<template>
<div v-bind="attrs" :class="slots.prose()" data-slot="prose">
<slot />
</div>
</template>
+29
View File
@@ -262,3 +262,32 @@ export {
type RadioVariants,
type RadioGroupVariants,
} from "@heroui-vue/styles";
export { default as Typography } from "./components/typography/Typography.vue";
export { default as TypographyHeading } from "./components/typography/TypographyHeading.vue";
export { default as TypographyParagraph } from "./components/typography/TypographyParagraph.vue";
export { default as TypographyCode } from "./components/typography/TypographyCode.vue";
export { default as TypographyProse } from "./components/typography/TypographyProse.vue";
export type {
TypographyProps,
TypographyHeadingProps,
TypographyParagraphProps,
TypographyCodeProps,
TypographyProseProps,
} from "./types";
export { default as EmptyState } from "./components/empty-state/EmptyState.vue";
export type { EmptyStateProps } from "./types";
export { default as Breadcrumbs } from "./components/breadcrumbs/Breadcrumbs.vue";
export { default as BreadcrumbsItem } from "./components/breadcrumbs/BreadcrumbsItem.vue";
export type { BreadcrumbsProps, BreadcrumbsItemProps } from "./types";
export {
typographyVariants,
breadcrumbsVariants,
emptyStateVariants,
type TypographyVariants,
type BreadcrumbsVariants,
type EmptyStateVariants,
} from "@heroui-vue/styles";
+28
View File
@@ -119,6 +119,34 @@ export type FieldsetActionsProps = InstanceType<
import type SearchField from "./components/search-field/SearchField.vue";
export type SearchFieldProps = InstanceType<typeof SearchField>["$props"];
import type Typography from "./components/typography/Typography.vue";
import type TypographyHeading from "./components/typography/TypographyHeading.vue";
import type TypographyParagraph from "./components/typography/TypographyParagraph.vue";
import type TypographyCode from "./components/typography/TypographyCode.vue";
import type TypographyProse from "./components/typography/TypographyProse.vue";
import type EmptyState from "./components/empty-state/EmptyState.vue";
import type Breadcrumbs from "./components/breadcrumbs/Breadcrumbs.vue";
import type BreadcrumbsItem from "./components/breadcrumbs/BreadcrumbsItem.vue";
export type TypographyProps = InstanceType<typeof Typography>["$props"];
export type TypographyHeadingProps = InstanceType<
typeof TypographyHeading
>["$props"];
export type TypographyParagraphProps = InstanceType<
typeof TypographyParagraph
>["$props"];
export type TypographyCodeProps = InstanceType<typeof TypographyCode>["$props"];
export type TypographyProseProps = InstanceType<
typeof TypographyProse
>["$props"];
export type EmptyStateProps = InstanceType<typeof EmptyState>["$props"];
export type BreadcrumbsProps = InstanceType<typeof Breadcrumbs>["$props"];
/** Public item props; Root injects `isLast` / `separatorSlot` internally. */
export type BreadcrumbsItemProps = Omit<
InstanceType<typeof BreadcrumbsItem>["$props"],
"isLast" | "separatorSlot"
>;
import type SearchFieldGroup from "./components/search-field/SearchFieldGroup.vue";
export type SearchFieldGroupProps = InstanceType<
typeof SearchFieldGroup