|
| 1 | +--- |
| 2 | +/** |
| 3 | + * Breadcrumbs.astro |
| 4 | + * |
| 5 | + * Auto-generates ancestor breadcrumb trail from the current URL path. |
| 6 | + * Always shows at least the section name (e.g. "Components", "Get Started"). |
| 7 | + * Use parentGroup for logical parents not reflected in the URL (e.g. component category). |
| 8 | + */ |
| 9 | +
|
| 10 | +interface Props { |
| 11 | + /** Optional logical parent not reflected in the URL (e.g. component category) */ |
| 12 | + parentGroup?: { label: string; url?: string }; |
| 13 | +} |
| 14 | +
|
| 15 | +const { parentGroup } = Astro.props; |
| 16 | +const pathname = Astro.url.pathname.replace(/\/$/, ''); |
| 17 | +const segments = pathname.split('/').filter(Boolean); |
| 18 | +
|
| 19 | +// Show on any page within a section |
| 20 | +const showBreadcrumbs = segments.length >= 1; |
| 21 | +
|
| 22 | +// Known labels for path segments |
| 23 | +const SEGMENT_LABELS: Record<string, string> = { |
| 24 | + "get-started": "Get Started", |
| 25 | + "foundations": "Foundations", |
| 26 | + "components": "Components", |
| 27 | + "tokens": "Tokens", |
| 28 | + "examples": "Examples", |
| 29 | + "designers": "Designers", |
| 30 | + "developers": "Developers", |
| 31 | +}; |
| 32 | +
|
| 33 | +function segmentToLabel(segment: string): string { |
| 34 | + if (SEGMENT_LABELS[segment]) return SEGMENT_LABELS[segment]; |
| 35 | + // Fallback: kebab-case to sentence case |
| 36 | + return segment.split('-').map((w, i) => |
| 37 | + i === 0 ? w.charAt(0).toUpperCase() + w.slice(1) : w |
| 38 | + ).join(' '); |
| 39 | +} |
| 40 | +
|
| 41 | +// Build ancestor crumbs: all segments except the last, but always include the section |
| 42 | +const crumbs: Array<{ label: string; url: string }> = []; |
| 43 | +let path = ''; |
| 44 | +const crumbCount = Math.max(1, segments.length - 1); |
| 45 | +for (let i = 0; i < crumbCount; i++) { |
| 46 | + path += '/' + segments[i]; |
| 47 | + crumbs.push({ label: segmentToLabel(segments[i]), url: path }); |
| 48 | +} |
| 49 | +--- |
| 50 | + |
| 51 | +{showBreadcrumbs && ( |
| 52 | + <nav class="breadcrumbs" aria-label="Breadcrumb"> |
| 53 | + <ol> |
| 54 | + <li> |
| 55 | + <a href="/">Design System</a> |
| 56 | + <span class="separator" aria-hidden="true">/</span> |
| 57 | + </li> |
| 58 | + {crumbs.map((crumb, i) => ( |
| 59 | + <li> |
| 60 | + {/* TODO: Swap to <goa-link> once underline control lands (#3504) */} |
| 61 | + <a href={crumb.url}>{crumb.label}</a> |
| 62 | + <span class="separator" aria-hidden="true">/</span> |
| 63 | + </li> |
| 64 | + ))} |
| 65 | + {parentGroup && ( |
| 66 | + <li> |
| 67 | + {parentGroup.url ? ( |
| 68 | + <a href={parentGroup.url}>{parentGroup.label}</a> |
| 69 | + ) : ( |
| 70 | + <span>{parentGroup.label}</span> |
| 71 | + )} |
| 72 | + <span class="separator" aria-hidden="true">/</span> |
| 73 | + </li> |
| 74 | + )} |
| 75 | + <li class="sr-only" aria-current="page"> |
| 76 | + {segmentToLabel(segments[segments.length - 1])} |
| 77 | + </li> |
| 78 | + </ol> |
| 79 | + </nav> |
| 80 | +)} |
| 81 | + |
| 82 | +<style> |
| 83 | + .breadcrumbs { |
| 84 | + margin-bottom: var(--goa-space-l); |
| 85 | + } |
| 86 | + |
| 87 | + .breadcrumbs ol { |
| 88 | + display: flex; |
| 89 | + flex-wrap: wrap; |
| 90 | + align-items: center; |
| 91 | + gap: var(--goa-space-2xs); |
| 92 | + list-style: none; |
| 93 | + margin: 0; |
| 94 | + padding: 0; |
| 95 | + } |
| 96 | + |
| 97 | + .breadcrumbs li { |
| 98 | + display: flex; |
| 99 | + align-items: center; |
| 100 | + gap: var(--goa-space-2xs); |
| 101 | + font: var(--goa-typography-body-xs); |
| 102 | + color: var(--goa-color-greyscale-400); |
| 103 | + } |
| 104 | + |
| 105 | + .breadcrumbs a { |
| 106 | + color: var(--goa-color-greyscale-500); |
| 107 | + text-decoration: none; |
| 108 | + font: var(--goa-typography-body-xs); |
| 109 | + } |
| 110 | + |
| 111 | + .breadcrumbs a:hover { |
| 112 | + color: var(--goa-color-interactive-default); |
| 113 | + text-decoration: underline; |
| 114 | + } |
| 115 | + |
| 116 | + .breadcrumbs a:focus { |
| 117 | + outline: none; |
| 118 | + } |
| 119 | + |
| 120 | + .breadcrumbs a:focus-visible { |
| 121 | + outline: 2px solid var(--goa-color-interactive-default); |
| 122 | + outline-offset: 2px; |
| 123 | + border-radius: var(--goa-border-radius-xs); |
| 124 | + } |
| 125 | + |
| 126 | + .separator { |
| 127 | + color: var(--goa-color-greyscale-300); |
| 128 | + font: var(--goa-typography-body-xs); |
| 129 | + } |
| 130 | + |
| 131 | + .sr-only { |
| 132 | + position: absolute; |
| 133 | + width: 1px; |
| 134 | + height: 1px; |
| 135 | + padding: 0; |
| 136 | + margin: -1px; |
| 137 | + overflow: hidden; |
| 138 | + clip: rect(0, 0, 0, 0); |
| 139 | + white-space: nowrap; |
| 140 | + border: 0; |
| 141 | + } |
| 142 | +</style> |
0 commit comments