Text
A label in one of the eleven text styles, with the label colour roles and per-style emphasis.
Text styles
large-title
The quick brown fox
jumps over
title-1
The quick brown fox
jumps over
title-2
The quick brown fox
jumps over
title-3
The quick brown fox
jumps over
headline
The quick brown fox
jumps over
body
The quick brown fox
jumps over
callout
The quick brown fox
jumps over
subheadline
The quick brown fox
jumps over
footnote
The quick brown fox
jumps over
caption-1
The quick brown fox
jumps over
caption-2
The quick brown fox
jumps over
import { Text, textStyleNames } from "@applecn/ui/components/text"
export default function TextStyles() {
return (
<div className="flex flex-col gap-3">
{textStyleNames.map((name) => (
<div key={name} className="flex items-baseline gap-4">
<Text
variant="caption-1"
color="label-3"
className="w-24 shrink-0 tabular-nums"
>
{name}
</Text>
<Text variant={name}>The quick brown fox</Text>
<Text variant={name} emphasized>
jumps over
</Text>
</div>
))}
</div>
)
}Install
From the registry this site publishes, with dependencies resolved by the CLI.
npx shadcn@latest add https://applecn.vercel.app/r/text.jsonSource
packages/ui/src/components/text.tsx
import { cva, type VariantProps } from "class-variance-authority"
import { cn } from "cn"
import type { ComponentProps, ElementType } from "react"
type TextStyleName =
| "large-title"
| "title-1"
| "title-2"
| "title-3"
| "headline"
| "body"
| "callout"
| "subheadline"
| "footnote"
| "caption-1"
| "caption-2"
const textStyleNames: readonly TextStyleName[] = [
"large-title",
"title-1",
"title-2",
"title-3",
"headline",
"body",
"callout",
"subheadline",
"footnote",
"caption-1",
"caption-2",
]
/**
* A label. `variant` is one of Apple's eleven text styles (size, leading and weight come from
* the platform's type scale), `color` one of the label roles, and `emphasized` the style's own
* emphasized weight (SwiftUI `.bold()`), which differs per style.
*/
const textVariants = cva("", {
variants: {
variant: {
"large-title": "type-large-title",
"title-1": "type-title-1",
"title-2": "type-title-2",
"title-3": "type-title-3",
headline: "type-headline",
body: "type-body",
callout: "type-callout",
subheadline: "type-subheadline",
footnote: "type-footnote",
"caption-1": "type-caption-1",
"caption-2": "type-caption-2",
} satisfies Record<TextStyleName, string>,
color: {
label: "text-label",
"label-2": "text-label-2",
"label-3": "text-label-3",
"label-4": "text-label-4",
tint: "text-primary",
destructive: "text-destructive",
inherit: "text-inherit",
},
emphasized: {
true: "",
false: "",
},
truncate: {
true: "truncate",
false: "",
},
},
compoundVariants: textStyleNames.map((name) => ({
variant: name,
emphasized: true,
class: `font-(--type-${name}-emphasized)`,
})),
defaultVariants: {
variant: "body",
color: "label",
emphasized: false,
truncate: false,
},
})
type TextProps = Omit<ComponentProps<"p">, "color"> &
VariantProps<typeof textVariants> & {
/** The element to render; a heading level for titles, `span` for inline labels. */
as?: ElementType
}
function Text({
as: Component = "p",
className,
variant = "body",
color = "label",
emphasized = false,
truncate = false,
...props
}: TextProps) {
return (
<Component
data-slot="text"
data-variant={variant}
className={cn(
textVariants({ variant, color, emphasized, truncate }),
className
)}
{...props}
/>
)
}
export { Text, textStyleNames, textVariants }
export type { TextProps, TextStyleName }