-
-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathheading-with-anchor.tsx
141 lines (125 loc) · 3.63 KB
/
heading-with-anchor.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
'use client';
import React from 'react';
import { cn } from '@/lib/utils';
import { Slot } from '@radix-ui/react-slot';
import { cva, type VariantProps } from 'class-variance-authority';
import { LinkIcon } from 'lucide-react';
import Link from 'next/link';
type AnchorProps = {
anchor?: string;
anchorVisibility?: 'hover' | 'always' | 'never';
disableCopyToClipboard?: boolean;
};
const Anchor = ({
anchor,
disableCopyToClipboard = false,
anchorVisibility = 'always',
}: AnchorProps) => {
function copyToClipboard() {
if (disableCopyToClipboard) return;
const currentUrl = window.location.href.replace(/#.*$/, '');
const urlWithId = `${currentUrl}#${anchor}`;
void navigator.clipboard.writeText(urlWithId);
}
return (
<div
className={cn(
'ms-2 pt-1',
anchorVisibility === 'always' && 'visible',
anchorVisibility === 'never' && 'hidden',
anchorVisibility === 'hover' && 'invisible group-hover:visible',
)}
>
{/* modify `Link` to `a` if you are not using Next.js */}
<Link href={`#${anchor}`} onClick={copyToClipboard}>
<LinkIcon className="text-gray-600 hover:text-gray-400" />
</Link>
</div>
);
};
const headingVariants = cva('font-bold text-primary', {
variants: {
variant: {
h1: 'leading-14 text-4xl lg:text-5xl',
h2: 'leading-14 text-3xl lg:text-4xl',
h3: 'leading-10 text-2xl lg:text-3xl',
h4: 'leading-8 text-xl lg:text-2xl',
h5: 'leading-8 text-lg lg:text-xl',
h6: 'leading-7 text-sm lg:text-base',
p: 'leading-5 text-lg lg:text-xl font-normal',
},
},
defaultVariants: {
variant: 'h6',
},
});
type BaseHeadingProps = {
children?: React.ReactNode;
variant?: string;
className?: string;
asChild?: boolean;
anchor?: string;
anchorAlignment?: 'close' | 'spaced';
anchorVisibility?: 'hover' | 'always' | 'never';
disableCopyToClipboard?: boolean;
} & React.HTMLAttributes<HTMLHeadingElement> &
VariantProps<typeof headingVariants>;
const BaseHeading = ({
children,
className,
variant = 'h6',
asChild = false,
anchor,
anchorAlignment = 'spaced',
anchorVisibility = 'always',
disableCopyToClipboard = false,
...props
}: BaseHeadingProps) => {
const Comp = asChild ? Slot : variant;
return (
<>
<Comp
id={anchor}
{...props}
className={cn(
anchor && 'flex scroll-m-20 items-center gap-1', // modify `scroll-m-20` according to your header height.
anchorAlignment === 'spaced' && 'justify-between',
anchorVisibility === 'hover' && 'group',
headingVariants({ variant, className }),
)}
>
{children}
{anchor && (
<Anchor
anchor={anchor}
anchorVisibility={anchorVisibility}
disableCopyToClipboard={disableCopyToClipboard}
/>
)}
</Comp>
</>
);
};
type TypographyProps = Omit<BaseHeadingProps, 'variant' | 'asChild'>;
const H1 = (props: TypographyProps) => {
return <BaseHeading {...props} variant="h1" />;
};
const H2 = (props: TypographyProps) => {
return <BaseHeading {...props} variant="h2" />;
};
const H3 = (props: TypographyProps) => {
return <BaseHeading {...props} variant="h3" />;
};
const H4 = (props: TypographyProps) => {
return <BaseHeading {...props} variant="h4" />;
};
const H5 = (props: TypographyProps) => {
return <BaseHeading {...props} variant="h5" />;
};
const H6 = (props: TypographyProps) => {
return <BaseHeading {...props} variant="h6" />;
};
const P = (props: TypographyProps) => {
return <BaseHeading {...props} variant="p" />;
};
export { H1, H2, H3, H4, H5, H6, P };