Typography
A simple typography component
Typography is the shared text primitive for the web registry. It centralizes semantic text variants while still allowing the rendered element to be overridden with the as prop.
The variant controls both styling and, by default, the element rendered to the DOM.
Installation
CLI
npx shadcn add https://ui.tiesen.id.vn/r/typography.jsonManual
Install the required dependencies:
npm install @base-ui/reactCopy and paste the following code into your project.
import type { VariantProps } from 'class-variance-authority'import { cva } from 'class-variance-authority'import { cn } from '@/lib/utils'const typographyVariants = cva('text-base font-normal', { variants: { variant: { h1: 'scroll-m-20 text-4xl font-extrabold tracking-tight text-balance', h2: 'scroll-m-20 text-2xl font-bold tracking-tight text-balance', h3: 'scroll-m-20 text-xl font-semibold tracking-tight text-balance', h4: 'scroll-m-20 text-lg font-semibold tracking-tight text-balance', p: 'leading-7 text-pretty', small: 'block text-sm leading-none font-medium tracking-wide', ul: 'ml-6 list-disc text-base [&>li]:mt-2 [&>li]:first:mt-0', ol: 'ml-6 list-decimal text-base [&>li]:mt-2 [&>li]:first:mt-0', blockquote: 'inline-block border-l-2 pl-6 italic before:content-["“"] after:content-["”"]', code: 'relative w-fit rounded-sm border border-accent bg-accent/40 px-[0.3rem] py-[0.2rem] font-mono text-sm font-medium text-accent-foreground', caption: 'mt-1 block text-center text-sm tracking-wide text-muted-foreground', }, }, defaultVariants: { variant: 'p', },})interface TypographyProps extends React.ComponentProps<'p'>, VariantProps<typeof typographyVariants> { as?: React.ElementType}function Typography({ className, variant = 'p', as, ...props}: TypographyProps) { const Comp = as ?? variant ?? 'p' return ( <Comp data-slot='typography' className={cn(typographyVariants({ variant }), className)} {...props} /> )}export { Typography, typographyVariants }Usage
import { Typography } from '@/components/ui/typography'
export function TypographyExample() {
return (
<div className='space-y-2'>
<Typography variant='h1'>Account settings</Typography>
<Typography>Update your profile and notification preferences.</Typography>
</div>
)
}Variants
The shared variant map contains headings, paragraph text, list styles, quotes, code, and captions:
<Typography variant='h1'>Heading 1</Typography>
<Typography variant='h2'>Heading 2</Typography>
<Typography variant='h3'>Heading 3</Typography>
<Typography variant='h4'>Heading 4</Typography>
<Typography variant='p'>Paragraph</Typography>
<Typography variant='small'>Small</Typography>
<Typography variant='ul'>List</Typography>
<Typography variant='ol'>Ordered list</Typography>
<Typography variant='blockquote'>Quote</Typography>
<Typography variant='code'>Code</Typography>
<Typography variant='caption'>Caption</Typography>Changing the rendered element
The component normally renders the element implied by variant. Use as when you need the styling of one variant with another semantic element:
<Typography variant='h2' as='div'>
Non-heading layout text
</Typography>Class merging
className is merged after the variant classes, so local styles can override the base typography classes.
API Reference
Typography
| Prop | Type | Default |
|---|---|---|
variant | 'h1' | 'h2' | 'h3' | 'h4' | 'p' | 'small' | 'ul' | 'ol' | 'blockquote' | 'code' | 'caption' | 'p' |
as | React.ElementType | variant |
className | string | — |
...props | React.ComponentProps<'p'> | — |
typographyVariants
The exported CVA helper used by Typography:
typographyVariants(props?: VariantProps<typeof typographyVariants>)