mirror of
https://github.com/r-freeman/portfolio.git
synced 2025-04-25 18:44:36 +00:00
All checks were successful
Build And Publish / BuildAndPublish (push) Successful in 3m15s
28 lines
767 B
TypeScript
28 lines
767 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="group">
|
|
{children}
|
|
<Link className="ml-1.5 invisible group-hover:visible" href={`#${createSlug(headingText)}`} ref={ref}></Link>
|
|
</Component>
|
|
)
|
|
} |