mirror of
https://github.com/supabase/supabase.git
synced 2026-12-03 23:18:57 +00:00
## Problem - kb article headings have no link to their section, so a reader cannot share a link to one part of a guide - docs and www headings show a `#` link that only jumps to the heading, and studio report headers use their own copy link button ## Solution - adds `CopyHeadingLinkButton` to `ui`, with the studio look, so one copy link button serves all surfaces - replaces the `#` link in `Heading` with the button, so docs, www and `ui-patterns` markdown headings copy a link on click - adds the button to the h2 to h4 headings of kb guides, faqs and troubleshooting articles - changes the studio report section headers to use the shared button, and removes their local copy logic | before | after | | -------|------| | <img width="795" height="378" alt="image" src="https://github.com/user-attachments/assets/68302dc8-829a-4af1-b1c3-5ec68d8c39e3" /> | <img width="795" height="378" alt="image" src="https://github.com/user-attachments/assets/b21d363e-3f27-43b0-aa0c-0efd92693d7e" /> | <!-- ## Preview links If relevant, include links to changed pages for easy review access. Copy the preview base URL from the Vercel bot comment on this PR. Use the following table as an example template. | Site | Live | Preview | Search for | | -------------- | ------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------ | ----------------------------- | | WWW | [/blog/your-post](https://supabase.com/blog/your-post) | [/blog/your-post](https://zone-www-dot-com-git-branch-name-supabase.vercel.app/blog/your-post) | unique phrase from the change | | Docs | [/docs/guides/your-page](https://supabase.com/docs/guides/your-page) | [/docs/guides/your-page](https://docs-git-branch-name-supabase.vercel.app/docs/guides/your-page) | unique phrase from the change | | Studio | [/dashboard](https://supabase.com/dashboard) | [/dashboard](https://studio-git-branch-name-supabase.vercel.app/dashboard) | unique phrase from the change | | Design system | [/design-system](https://supabase.com/design-system) | [/design-system](https://design-system-git-branch-name-supabase.vercel.app/design-system) | unique phrase from the change | | UI library | [/library](https://supabase.com/library) | [/library](https://ui-library-git-branch-name-supabase.vercel.app/library) | unique phrase from the change | | Knowledge base | [/kb/guides/your-page](https://supabase.com/kb/guides/your-page) | [/kb/guides/your-page](https://kb-git-branch-name-supabase.vercel.app/kb/guides/your-page) | unique phrase from the change | --> <!-- ## Additional context Optionally add any other context or screenshots. --> ## Review instructions 1. open [/kb/guides/migrate-from-mysql](https://kb-git-anthonylio-fdbkin-47106-add-heading-links-to-knowledge-base-troubleshooting-guides-supabase.vercel.app/kb/guides/migrate-from-mysql) and hover the "Retrieve your MySQL database credentials" heading 3. click the icon and check that the clipboard holds the page url with `#retrieve-your-mysql-database-credentials` 4. open [/docs/guides/database/overview](https://docs-git-anthonylio-fdbkin-47106-add-heading-links-to-knowledge-base-troubleshooting-guides-supabase.vercel.app/docs/guides/database/overview), hover an h2 and do steps 2 and 3 again 5. repeat 3 6. open [/blog/introducing-dbarena](https://zone-www-dot-com-git-anthonylio-fdbkin-47106-add-heading-links-to-knowledge-base-troubleshooting-guides-supabase.vercel.app/blog/introducing-dbarena), hover an h2 and do steps 2 and 3 again 7. open [/dashboard/project/_/observability/auth](https://studio-git-anthonylio-fdbkin-47106-add-heading-links-to-knowledge-base-troubleshooting-guides-supabase.vercel.app/dashboard/project/_/observability/auth), select a project, change a filter, and copy the "Usage" section link 8. make sure that the copied url keeps the filter query string and ends with `#usage` ## Checklist Check all before review: - [x] I have read [CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md) - [ ] If I wrote a new docs topic or edited an existing topic, I used the `/write-the-docs` or `/edit-the-docs` skill, which applies the docs [style guide](https://github.com/supabase/supabase/tree/master/apps/docs/style-guide)
124 lines
3.9 KiB
TypeScript
124 lines
3.9 KiB
TypeScript
'use client'
|
|
|
|
import { Feedback } from '~/components/Feedback'
|
|
import { useBreakpoint } from 'common'
|
|
import { Menu } from 'lucide-react'
|
|
import type { HTMLAttributes } from 'react'
|
|
import { useEffect, useState } from 'react'
|
|
import { cn, Separator, Sheet, SheetContent, SheetHeader, SheetTrigger } from 'ui'
|
|
|
|
interface TocItem extends HTMLAttributes<HTMLElement> {
|
|
label: string
|
|
anchor: string
|
|
}
|
|
|
|
export function ContributingToc({ className }: { className?: string }) {
|
|
const mobileToc = useBreakpoint('lg')
|
|
const [tocItems, setTocItems] = useState<Array<TocItem>>([])
|
|
|
|
useEffect(() => {
|
|
const headings = [
|
|
...document.querySelectorAll(
|
|
'article.prose > h2:not(#feedback-title),h3:not(#feedback-title)'
|
|
),
|
|
] as Array<HTMLHeadingElement>
|
|
const tocItems = headings
|
|
.filter((heading) => !!heading.id && heading.textContent)
|
|
.map((heading) => ({
|
|
label: heading.textContent!,
|
|
anchor: heading.id,
|
|
}))
|
|
setTocItems(tocItems)
|
|
}, [])
|
|
|
|
return mobileToc ? (
|
|
<MobileToc items={tocItems} />
|
|
) : (
|
|
<TocBase
|
|
items={tocItems}
|
|
className={cn(
|
|
'[--local-top-spacing:5rem]',
|
|
'border-l thin-scrollbar overflow-y-auto px-2 hidden lg:block',
|
|
'col-span-3 self-start sticky',
|
|
'top-[calc(var(--header-height)+2rem)] max-h-[calc(100vh-var(--header-height)-3rem)]',
|
|
className
|
|
)}
|
|
/>
|
|
)
|
|
}
|
|
|
|
function MobileToc({ items, className }: { items: Array<TocItem>; className?: string }) {
|
|
const [open, setOpen] = useState(false)
|
|
|
|
useEffect(() => {
|
|
const onHashChanged = () => setOpen(false)
|
|
|
|
window.addEventListener('hashchange', onHashChanged)
|
|
return () => {
|
|
window.removeEventListener('hashchange', onHashChanged)
|
|
}
|
|
}, [])
|
|
|
|
return (
|
|
<Sheet open={open} onOpenChange={setOpen}>
|
|
<SheetTrigger
|
|
className={cn(
|
|
'fixed z-0 inset-0 top-auto w-full rounded-t-lg border border-b-0 p-4 bg-studio flex items-center gap-2 text-foreground-light text-sm',
|
|
className
|
|
)}
|
|
>
|
|
<Menu size={16} strokeWidth={1.5} className="text-foreground-light" />
|
|
<span className="sr-only">
|
|
{open ? 'Close table of contents' : 'Open table of contents'}
|
|
</span>
|
|
<span>On this page</span>
|
|
</SheetTrigger>
|
|
<SheetContent
|
|
side="bottom"
|
|
size="lg"
|
|
className={cn(
|
|
'w-full flex flex-col gap-0 p-0 rounded-t-lg overflow-hidden',
|
|
!open && 'top-[calc(100vh-100px)]'
|
|
)}
|
|
>
|
|
<SheetHeader className="py-0 px-4">
|
|
<SheetTrigger
|
|
className={cn(
|
|
'w-full py-4 flex items-center gap-2 text-foreground-light text-sm',
|
|
className
|
|
)}
|
|
>
|
|
<Menu size={16} strokeWidth={1.5} className="text-foreground-light" />
|
|
<span className="sr-only">
|
|
{open ? 'Close table of contents' : 'Open table of contents'}
|
|
</span>
|
|
<span>On this page</span>
|
|
</SheetTrigger>
|
|
</SheetHeader>
|
|
<div className="w-full flex-1 p-4 pb-8 overflow-y-auto thin-scrollbar">
|
|
<TocBase items={items} />
|
|
</div>
|
|
</SheetContent>
|
|
</Sheet>
|
|
)
|
|
}
|
|
|
|
function TocBase({ items, className }: { items: Array<TocItem>; className?: string }) {
|
|
return (
|
|
<nav aria-label="Table of contents" className={cn('text-foreground-lighter', className)}>
|
|
<span className="hidden lg:block font-mono text-xs uppercase text-foreground px-5 mb-6">
|
|
On this page
|
|
</span>
|
|
<ul className="toc-menu list-none lg:pl-5 text-[0.8rem] grid gap-2">
|
|
{items.map((item) => (
|
|
<li key={item.anchor} className="overflow-hidden truncate">
|
|
<a href={`#${item.anchor}`}>{item.label}</a>
|
|
</li>
|
|
))}
|
|
</ul>
|
|
<Separator className="lg:w-[calc(100%-2rem)] lg:ml-5 my-4 lg:my-8" />
|
|
<Feedback className="pl-0 lg:pl-5" />
|
|
</nav>
|
|
)
|
|
}
|