|
| 1 | +import React from "react"; |
| 2 | +import Link from "@docusaurus/Link"; |
| 3 | +import clsx from "clsx"; |
| 4 | + |
| 5 | +type BlogCategory = { |
| 6 | + label?: string; |
| 7 | + permalink?: string; |
| 8 | +} | null; |
| 9 | + |
| 10 | +type BreadcrumbItem = { |
| 11 | + label: string; |
| 12 | + href?: string; |
| 13 | +}; |
| 14 | + |
| 15 | +type BlogBreadcrumbsProps = { |
| 16 | + permalink?: string; |
| 17 | + title?: string; |
| 18 | + category?: BlogCategory; |
| 19 | + className?: string; |
| 20 | +}; |
| 21 | + |
| 22 | +const getBreadcrumbItems = ({ |
| 23 | + permalink, |
| 24 | + title, |
| 25 | + category, |
| 26 | +}: { |
| 27 | + permalink: string; |
| 28 | + title: string; |
| 29 | + category?: BlogCategory; |
| 30 | +}): BreadcrumbItem[] => [ |
| 31 | + { label: "Blog", href: "/blog" }, |
| 32 | + ...(category?.label && category?.permalink |
| 33 | + ? [{ label: category.label, href: category.permalink }] |
| 34 | + : []), |
| 35 | + { label: title, href: permalink }, |
| 36 | +]; |
| 37 | + |
| 38 | +export const BlogBreadcrumbs = ({ |
| 39 | + permalink, |
| 40 | + title, |
| 41 | + category, |
| 42 | + className, |
| 43 | +}: BlogBreadcrumbsProps) => { |
| 44 | + if (!title || !permalink) { |
| 45 | + return null; |
| 46 | + } |
| 47 | + |
| 48 | + const items = getBreadcrumbItems({ permalink, title, category }); |
| 49 | + |
| 50 | + return ( |
| 51 | + <nav |
| 52 | + className={clsx("not-prose", "w-full", "min-w-0", className)} |
| 53 | + aria-label="Blog breadcrumbs" |
| 54 | + > |
| 55 | + <ol |
| 56 | + className={clsx( |
| 57 | + "m-0", |
| 58 | + "p-0", |
| 59 | + "list-none", |
| 60 | + "flex", |
| 61 | + "w-full", |
| 62 | + "min-w-0", |
| 63 | + "flex-col", |
| 64 | + "items-start", |
| 65 | + "gap-1", |
| 66 | + "blog-sm:flex-row", |
| 67 | + "blog-sm:flex-wrap", |
| 68 | + "blog-sm:items-center", |
| 69 | + "blog-sm:gap-0", |
| 70 | + "text-sm", |
| 71 | + "leading-5", |
| 72 | + "font-normal", |
| 73 | + "tracking-[-0.007em]", |
| 74 | + )} |
| 75 | + > |
| 76 | + {items.map((item, index) => { |
| 77 | + const isLast = index === items.length - 1; |
| 78 | + const itemTextClass = isLast |
| 79 | + ? "text-zinc-400 dark:text-zinc-400" |
| 80 | + : "text-zinc-700 dark:text-zinc-300"; |
| 81 | + const itemContainerClass = isLast |
| 82 | + ? "blog-sm:flex-1" |
| 83 | + : "blog-sm:w-auto blog-sm:shrink-0"; |
| 84 | + const itemLabelClass = clsx( |
| 85 | + "block", |
| 86 | + "min-w-0", |
| 87 | + "max-w-full", |
| 88 | + "break-words", |
| 89 | + isLast ? "blog-sm:truncate" : "blog-sm:whitespace-nowrap", |
| 90 | + ); |
| 91 | + |
| 92 | + return ( |
| 93 | + <li |
| 94 | + key={`${item.label}-${item.href ?? "current"}`} |
| 95 | + className={clsx( |
| 96 | + "flex", |
| 97 | + "items-center", |
| 98 | + "min-w-0", |
| 99 | + "max-w-full", |
| 100 | + "w-full", |
| 101 | + itemContainerClass, |
| 102 | + )} |
| 103 | + > |
| 104 | + {index > 0 && ( |
| 105 | + <span |
| 106 | + className={clsx( |
| 107 | + "hidden", |
| 108 | + "blog-sm:inline", |
| 109 | + "blog-sm:mx-2", |
| 110 | + "text-zinc-300", |
| 111 | + "dark:text-zinc-700", |
| 112 | + )} |
| 113 | + aria-hidden="true" |
| 114 | + > |
| 115 | + / |
| 116 | + </span> |
| 117 | + )} |
| 118 | + {item.href && !isLast ? ( |
| 119 | + <Link |
| 120 | + to={item.href} |
| 121 | + className={clsx( |
| 122 | + "no-underline", |
| 123 | + "hover:no-underline", |
| 124 | + itemLabelClass, |
| 125 | + itemTextClass, |
| 126 | + "hover:text-zinc-700", |
| 127 | + "dark:hover:text-zinc-300", |
| 128 | + )} |
| 129 | + > |
| 130 | + {item.label} |
| 131 | + </Link> |
| 132 | + ) : ( |
| 133 | + <span |
| 134 | + className={clsx(itemLabelClass, itemTextClass)} |
| 135 | + aria-current={isLast ? "page" : undefined} |
| 136 | + > |
| 137 | + {item.label} |
| 138 | + </span> |
| 139 | + )} |
| 140 | + <span |
| 141 | + className={clsx( |
| 142 | + "ml-2", |
| 143 | + "text-zinc-300", |
| 144 | + "dark:text-zinc-700", |
| 145 | + "blog-sm:hidden", |
| 146 | + )} |
| 147 | + aria-hidden="true" |
| 148 | + > |
| 149 | + / |
| 150 | + </span> |
| 151 | + </li> |
| 152 | + ); |
| 153 | + })} |
| 154 | + </ol> |
| 155 | + </nav> |
| 156 | + ); |
| 157 | +}; |
0 commit comments