Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

next
tsx 78 lines 1.96 KB
Raw
1 'use client';
2
3 import { cn } from '@/lib/utils';
4 import { Link, Check } from 'lucide-react';
5 import { useState, type ComponentPropsWithoutRef } from 'react';
6
7 type HeadingProps = ComponentPropsWithoutRef<'h1'> & {
8 as?: 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6';
9 };
10
11 export function Heading({ as, className, ...props }: HeadingProps) {
12 const As = as ?? 'h1';
13
14 if (!props.id) return <As className={className} {...props} />;
15
16 return (
17 <As
18 className={cn(
19 'flex scroll-m-28 flex-row items-center gap-2',
20 className,
21 )}
22 {...props}
23 >
24 <HeadingAnchor id={props.id}>{props.children}</HeadingAnchor>
25 </As>
26 );
27 }
28
29 function HeadingAnchor({ id, children }: { id: string; children: React.ReactNode }) {
30 const [copied, setCopied] = useState(false);
31
32 const copyLink = () => {
33 const url = `${window.location.origin}${window.location.pathname}${window.location.search}#${id}`;
34 window.history.replaceState(null, '', `#${id}`);
35 navigator.clipboard.writeText(url).then(() => {
36 setCopied(true);
37 setTimeout(() => setCopied(false), 2000);
38 }).catch(() => {});
39 };
40
41 const handleClick = (e: React.MouseEvent<HTMLAnchorElement>) => {
42 e.preventDefault();
43 copyLink();
44 };
45
46 const handleKeyDown = (e: React.KeyboardEvent<HTMLAnchorElement>) => {
47 if (e.key === 'Enter' || e.key === ' ') {
48 e.preventDefault();
49 copyLink();
50 }
51 };
52
53 return (
54 <>
55 <a
56 data-card=""
57 href={`#${id}`}
58 className="peer"
59 onClick={handleClick}
60 onKeyDown={handleKeyDown}
61 aria-label={`Copy link to ${id} section`}
62 >
63 {children}
64 </a>
65 {copied ? (
66 <Check
67 aria-hidden
68 className="size-3.5 shrink-0 text-green-500 transition-opacity"
69 />
70 ) : (
71 <Link
72 aria-hidden
73 className="size-3.5 shrink-0 text-fd-muted-foreground opacity-0 transition-opacity peer-hover:opacity-100"
74 />
75 )}
76 </>
77 );
78 }