Skip to content

Instantly share code, notes, and snippets.

@DarlonHenrique
Created July 6, 2023 17:29
Show Gist options
  • Save DarlonHenrique/d83606299e33ac4e07ade113e3f135f6 to your computer and use it in GitHub Desktop.
Save DarlonHenrique/d83606299e33ac4e07ade113e3f135f6 to your computer and use it in GitHub Desktop.
Heading Component
interface HeadingProps {
children: React.ReactNode
}
import { Slot } from '@radix-ui/react-slot'
import {cva, type VariantProps} from 'class-variance-authority'
import { cn } from "@/lib/utils"
import React from 'react'
const headingVariants = cva(
'scroll-m-20 tracking-tight',
{
variants: {
variant: {
h1: 'text-4xl font-extrabold lg:text-5xl',
h2: 'border-b pb-2 text-3xl font-semibold transition-colors first:mt-0',
h3: 'text-2xl font-semibold',
h4: 'text-xl font-semibold'
}
},
defaultVariants: {
variant: 'h1'
}
}
)
interface HeadingProps extends React.HTMLAttributes<HTMLHeadingElement>, VariantProps<typeof headingVariants> {
asChild?: boolean
}
const Heading = React.forwardRef<HTMLHeadingElement, HeadingProps>(
({ className, variant, asChild = false, ...props }, ref) => {
const Comp = asChild ? Slot : 'h1'
return (
<Comp
className={cn(headingVariants({ variant, className }))}
ref={ref}
{...props}
/>
)
}
)
Heading.displayName = 'Heading'
export { Heading, headingVariants }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment