MCPcopy Index your code
hub / github.com/CodebuffAI/codebuff / HeadingWithCopyLink

Function HeadingWithCopyLink

web/src/components/docs/mdx/mdx-components.tsx:31–182  ·  view source on GitHub ↗
({
    className,
    children,
    ...props
  }: HTMLAttributes<HTMLHeadingElement>)

Source from the content-addressed store, hash-verified

29 defaultClasses: string,
30) => {
31 const HeadingWithCopyLink = ({
32 className,
33 children,
34 ...props
35 }: HTMLAttributes<HTMLHeadingElement>) => {
36 const [copied, setCopied] = useState(false)
37 const [showMobileBadge, setShowMobileBadge] = useState(false)
38 const isMobile = useIsMobile()
39
40 useEffect(() => {
41 if (copied) {
42 const timer = setTimeout(() => setCopied(false), 2000)
43 return () => clearTimeout(timer)
44 }
45 return undefined
46 }, [copied])
47
48 // Auto-hide mobile badge after 3 seconds
49 useEffect(() => {
50 if (isMobile && showMobileBadge) {
51 const timer = setTimeout(() => setShowMobileBadge(false), 3000)
52 return () => clearTimeout(timer)
53 }
54 return undefined
55 }, [isMobile, showMobileBadge])
56
57 const title = children?.toString()
58
59 // Generate hierarchical ID by including heading level context
60 const generateHierarchicalId = (text: string, level: string) => {
61 const baseId = text
62 ?.toLowerCase()
63 .replace(/\s+/g, '-')
64 .replace(/[^\w-]/g, '')
65
66 // Use heading level to create meaningful hierarchy
67 const levelNum = parseInt(level.replace('h', ''))
68
69 // For h1, use as-is. For h2+, prefix with level to ensure uniqueness
70 // This creates URLs like: #overview (h1), #h2-overview (h2), etc.
71 return levelNum === 1 ? baseId : `${level}-${baseId}`
72 }
73
74 const id = title
75 ? generateHierarchicalId(title, HeadingComponent)
76 : undefined
77
78 if (!title) {
79 return (
80 <HeadingComponent
81 {...props}
82 className={cn(
83 'hover:cursor-pointer hover:underline scroll-m-20',
84 defaultClasses,
85 className,
86 )}
87 >
88 {children}

Callers

nothing calls this directly

Calls 4

useIsMobileFunction · 0.90
cnFunction · 0.90
setTimeoutFunction · 0.85
generateHierarchicalIdFunction · 0.85

Tested by

no test coverage detected