portfolio/components/ui/Heading.tsx
Ryan Freeman 42a46b93d3
All checks were successful
Build And Publish / BuildAndPublish (push) Successful in 3m8s
Update heading component
2025-03-27 15:05:21 +00:00

28 lines
776 B
TypeScript

'use client'
import {ElementType, ReactNode, useEffect, useRef} from 'react'
import {createSlug} from '@/lib/createSlug'
import Link from 'next/link'
type HeadingProps = {
as?: ElementType
children: ReactNode
}
export function Heading({as: Component = 'h1', children = null}: HeadingProps) {
const ref = useRef<HTMLAnchorElement>(null);
const headingText = children?.toString() || ''
useEffect(() => {
if (ref.current) {
ref.current.innerHTML = '#'
}
}, [])
return (
<Component id={createSlug(headingText)} className="flex group">
{children}
<Link className="sr-only group-hover:not-sr-only !ml-1.5" href={`#${createSlug(headingText)}`} ref={ref}></Link>
</Component>
)
}