Files
heroui-vue/packages/vue/src/components/breadcrumbs/BreadcrumbsItem.vue
T

214 lines
5.9 KiB
Vue

<script lang="ts">
import {
Comment,
Fragment,
Text,
cloneVNode,
defineComponent,
h,
inject,
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 { breadcrumbsActionKey } from "./context";
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: {
id: {
type: [String, Number] as PropType<string | number>,
default: undefined,
},
href: {
type: String,
default: undefined,
},
target: {
type: String,
default: undefined,
},
rel: {
type: String,
default: undefined,
},
download: {
type: [Boolean, String] as PropType<boolean | string>,
default: undefined,
},
isDisabled: {
type: Boolean,
default: false,
},
/** Identity supplied by the parent from id/key/index. */
itemKey: {
type: [String, Number] as PropType<string | number>,
default: undefined,
},
/** 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 action = inject(breadcrumbsActionKey, undefined);
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 onClick = linkAttrs.onClick;
linkAttrs.onClick = [
...(Array.isArray(onClick) ? onClick : onClick ? [onClick] : []),
(event: MouseEvent) => {
if (
!event.defaultPrevented &&
!disabled.value &&
props.itemKey !== undefined
)
action?.(props.itemKey);
},
];
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,
id: props.id == null ? undefined : String(props.id),
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>