|
| 1 | +Use a wrapped `DocBreadcrumbs` theme component. That’s the smallest, most upgrade-friendly hook that’s used on every docs MDX page. |
| 2 | + |
| 3 | +### 1. Create your custom “right of breadcrumbs” component |
| 4 | + |
| 5 | +For example: |
| 6 | + |
| 7 | +`src/components/BreadcrumbRightControls.tsx`: |
| 8 | + |
| 9 | +```tsx |
| 10 | +import React from 'react'; |
| 11 | + |
| 12 | +export default function BreadcrumbRightControls() { |
| 13 | + // Replace with your real UI: framework selector, version dropdown, etc. |
| 14 | + return ( |
| 15 | + <div> |
| 16 | + {/* Example content */} |
| 17 | + <button type="button">Do something</button> |
| 18 | + </div> |
| 19 | + ); |
| 20 | +} |
| 21 | +``` |
| 22 | + |
| 23 | +### 2. Swizzle `DocBreadcrumbs` with `--wrap` |
| 24 | + |
| 25 | +Docusaurus exposes docs breadcrumbs through the `@theme/DocBreadcrumbs` component, which is used inside the docs layout ([alanwang.site][1]). Swizzling with `--wrap` lets you enhance it without copying the whole implementation ([Docusaurus][2]). |
| 26 | + |
| 27 | +From your project root: |
| 28 | + |
| 29 | +```bash |
| 30 | +npx docusaurus swizzle @docusaurus/theme-classic DocBreadcrumbs -- --wrap |
| 31 | +# or: yarn swizzle @docusaurus/theme-classic DocBreadcrumbs --wrap |
| 32 | +# or: pnpm docusaurus swizzle @docusaurus/theme-classic DocBreadcrumbs -- --wrap |
| 33 | +``` |
| 34 | + |
| 35 | +This creates: |
| 36 | + |
| 37 | +```text |
| 38 | +src/theme/DocBreadcrumbs/index.(js|tsx) |
| 39 | +``` |
| 40 | + |
| 41 | +### 3. Implement the wrapper that adds your control on the right |
| 42 | + |
| 43 | +Replace the generated file with something like this (TypeScript version; JS is identical minus types): |
| 44 | + |
| 45 | +`src/theme/DocBreadcrumbs/index.tsx`: |
| 46 | + |
| 47 | +```tsx |
| 48 | +import React from 'react'; |
| 49 | +import clsx from 'clsx'; |
| 50 | + |
| 51 | +// Original theme implementation |
| 52 | +import DocBreadcrumbs from '@theme-original/DocBreadcrumbs'; |
| 53 | +import type { Props } from '@theme/DocBreadcrumbs'; |
| 54 | + |
| 55 | +// Your custom UI |
| 56 | +import BreadcrumbRightControls from '@site/src/components/BreadcrumbRightControls'; |
| 57 | + |
| 58 | +// Optional CSS module for layout |
| 59 | +import styles from './styles.module.css'; |
| 60 | + |
| 61 | +export default function DocBreadcrumbsWrapper( |
| 62 | + props: Props, |
| 63 | +): JSX.Element | null { |
| 64 | + const breadcrumbs = <DocBreadcrumbs {...props} />; |
| 65 | + |
| 66 | + // If breadcrumbs are disabled (config/front matter), don't render anything |
| 67 | + if (!breadcrumbs) { |
| 68 | + return null; |
| 69 | + } |
| 70 | + |
| 71 | + return ( |
| 72 | + <div className={clsx(styles.wrapper)}> |
| 73 | + <div className={styles.left}>{breadcrumbs}</div> |
| 74 | + <div className={styles.right}> |
| 75 | + <BreadcrumbRightControls /> |
| 76 | + </div> |
| 77 | + </div> |
| 78 | + ); |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +`src/theme/DocBreadcrumbs/styles.module.css`: |
| 83 | + |
| 84 | +```css |
| 85 | +.wrapper { |
| 86 | + display: flex; |
| 87 | + align-items: center; /* baseline-ish alignment with default breadcrumbs */ |
| 88 | + justify-content: space-between; |
| 89 | + gap: 0.75rem; |
| 90 | +} |
| 91 | + |
| 92 | +/* Left side: native Docusaurus breadcrumbs */ |
| 93 | +.left { |
| 94 | + min-width: 0; /* keep ellipsis / wrapping sane */ |
| 95 | +} |
| 96 | + |
| 97 | +/* Right side: your custom controls */ |
| 98 | +.right { |
| 99 | + display: inline-flex; |
| 100 | + align-items: center; |
| 101 | + gap: 0.5rem; |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +Notes: |
| 106 | + |
| 107 | +* Using `@theme-original/DocBreadcrumbs` means you keep all the default markup and schema.org breadcrumb behavior while just wrapping it in a flex container ([Docusaurus][2]). |
| 108 | +* `DocBreadcrumbs` is only used on docs pages via `DocItem/Layout`, so this affects every MDX docs page and nothing else (no blog, no plain MDX pages) ([alanwang.site][1]). |
| 109 | +* The wrapper layout is independent of Docusaurus internals: if they change the internal breadcrumb markup, you still just receive whatever `DocBreadcrumbs` returns and render it on the left. |
| 110 | + |
| 111 | +That’s it: you’ve extended Docusaurus 3 with a single wrapped theme component, and your custom controls will appear aligned to the right of the breadcrumbs on every docs MDX page. |
| 112 | + |
| 113 | +[1]: https://www.alanwang.site/en/posts/blog-guides/docusaurus-comment "Adding Comment Functionality to Docusaurus | Alan Wang" |
| 114 | +[2]: https://docusaurus.io/docs/swizzling?utm_source=chatgpt.com "Swizzling" |
0 commit comments