({
children,
tag,
label,
level,
anchor = true,
...props
}: React.ComponentPropsWithoutRef<`h${Level}`> & {
id: string;
tag?: string;
label?: string;
level?: Level;
anchor?: boolean;
})
| 69 | } |
| 70 | |
| 71 | export function Heading<Level extends 2 | 3>({ |
| 72 | children, |
| 73 | tag, |
| 74 | label, |
| 75 | level, |
| 76 | anchor = true, |
| 77 | ...props |
| 78 | }: React.ComponentPropsWithoutRef<`h${Level}`> & { |
| 79 | id: string; |
| 80 | tag?: string; |
| 81 | label?: string; |
| 82 | level?: Level; |
| 83 | anchor?: boolean; |
| 84 | }) { |
| 85 | level = level ?? (2 as Level); |
| 86 | let Component = `h${level}` as 'h2' | 'h3'; |
| 87 | let ref = useRef<HTMLHeadingElement>(null); |
| 88 | let registerHeading = useSectionStore(s => s.registerHeading); |
| 89 | |
| 90 | let inView = useInView(ref, { |
| 91 | margin: `${remToPx(-3.5)}px 0px 0px 0px`, |
| 92 | amount: 'all' |
| 93 | }); |
| 94 | |
| 95 | const stepPattern = /^Step\s*#?\s*(\d+)(?:\s*:\s*)?(.*)$/i; |
| 96 | const textOnlyHeading = typeof children === 'string'; |
| 97 | const hasSteps = textOnlyHeading ? children.match(stepPattern) : null; |
| 98 | const stepNumber = hasSteps && hasSteps[1]; |
| 99 | const title = hasSteps ? hasSteps[2] : children; |
| 100 | |
| 101 | useEffect(() => { |
| 102 | if (level === 2) { |
| 103 | registerHeading({ |
| 104 | id: props.id, |
| 105 | ref, |
| 106 | offsetRem: tag || label ? 8 : 6 |
| 107 | }); |
| 108 | } |
| 109 | }); |
| 110 | |
| 111 | return ( |
| 112 | <> |
| 113 | <Eyebrow tag={tag} label={label} /> |
| 114 | <Component |
| 115 | ref={ref} |
| 116 | className={ |
| 117 | tag || label |
| 118 | ? 'relative mt-2 scroll-mt-32' |
| 119 | : 'relative scroll-mt-24' |
| 120 | } |
| 121 | {...props} |
| 122 | > |
| 123 | {!anchor && ( |
| 124 | <> |
| 125 | <Step stepNumber={stepNumber} /> |
| 126 | {title} |
| 127 | </> |
| 128 | )} |
nothing calls this directly
no test coverage detected