Skip to content
Lenso UI

Typography 排版

面向标题、正文与行内代码的语义化排版原语,基于 React Aria Components 的 Text 构建。

用法

import {Typography} from "@lenso/ui";

此预览复用英文版适配,不代表中文源示例已完成本地实现。

"use client";
import { Typography } from "@lenso/ui";import * as stylex from "@stylexjs/stylex";
const styles = stylex.create({  column: { display: "flex", flexDirection: "column", gap: 16, maxWidth: 576 },});
export function Default() {  return (    <div {...stylex.props(styles.column)}>      <Typography type="h1">Build better interfaces</Typography>      <Typography type="h2">Typography that stays semantic</Typography>      <Typography type="h3">Composable by default</Typography>      <Typography type="h4">Small heading</Typography>      <Typography>        Lenso UI Typography provides semantic typography types and native element composition.      </Typography>      <Typography color="muted" type="body-sm">        Smaller muted body copy for secondary descriptions.      </Typography>      <Typography type="code">pnpm add @lenso/ui</Typography>    </div>  );}

Local adaptation source above. Derived from HeroUI v3.2.6 source.

组件结构

默认情况下,Typography 会将视觉上的 type 映射到对应的语义化元素。

import {Typography} from "@lenso/ui";
export default () => (  <Typography type="h1">Heading</Typography>);

示例

层级

此预览复用英文版适配,不代表中文源示例已完成本地实现。

"use client";// Adapted from HeroUI v3.2.6 (Apache-2.0).import { Typography } from "@lenso/ui";import * as stylex from "@stylexjs/stylex";const scale = [  { label: "h1", meta: "36px / 600 / 1.11 / tight", sample: "Build better interfaces", type: "h1" },  {    label: "h2",    meta: "30px / 600 / 1.17 / tight",    sample: "Built for the intelligence age",    type: "h2",  },  { label: "h3", meta: "24px / 600 / 1.25 / tight", sample: "Pricing on your terms", type: "h3" },  {    label: "h4",    meta: "20px / 600 / 1.33 / tight",    sample: "Apply to the startup program",    type: "h4",  },  { label: "h5", meta: "18px / 600 / 1.39 / tight", sample: "Card titles", type: "h5" },  { label: "h6", meta: "16px / 600 / 1.50 / tight", sample: "Smaller feature headers", type: "h6" },  {    label: "body",    meta: "16px / 400 / 1.75",    sample: "Primary body text used across documentation, marketing copy, and descriptions.",    type: "body",  },  {    label: "body-sm",    meta: "14px / 400 / 1.50",    sample: "Secondary body, table cells, navigation, and sidebar items.",    type: "body-sm",  },  {    label: "body-xs",    meta: "12px / 400 / 1.25",    sample: "Captions, badges, helper text, and fine print.",    type: "body-xs",  },  { label: "code", meta: "14px / mono", sample: "pnpm add @lenso/ui", type: "code" },] as const;const styles = stylex.create({  column: { display: "flex", width: "100%", flexDirection: "column" },  row: {    display: "grid",    gridTemplateColumns: "160px 1fr",    alignItems: "center",    gap: 32,    paddingBlock: 20,    borderBlockEndWidth: { default: 1, ":last-child": 0 },    borderBlockEndStyle: "solid",    borderBlockEndColor: "var(--border)",  },  caption: { display: "flex", flexShrink: 0, flexDirection: "column", gap: 2 },  label: { fontSize: 14, lineHeight: "20px", fontWeight: 600, color: "var(--foreground)" },  meta: { fontSize: 12, lineHeight: "16px", whiteSpace: "nowrap", color: "var(--muted)" },});export function TypographyScale() {  return (    <div {...stylex.props(styles.column)}>      {scale.map((row) => (        <div key={row.label} {...stylex.props(styles.row)}>          <div {...stylex.props(styles.caption)}>            <span {...stylex.props(styles.label)}>{row.label}</span>            <span {...stylex.props(styles.meta)}>{row.meta}</span>          </div>          <Typography type={row.type}>{row.sample}</Typography>        </div>      ))}    </div>  );}export default TypographyScale;

Local adaptation source above. Derived from HeroUI v3.2.6 source.

原语

此预览复用英文版适配,不代表中文源示例已完成本地实现。

"use client";// Adapted from HeroUI v3.2.6 (Apache-2.0).import { Typography } from "@lenso/ui";import * as stylex from "@stylexjs/stylex";const styles = stylex.create({  column: { display: "flex", maxWidth: 576, flexDirection: "column", gap: 16 },});export function Primitives() {  return (    <div {...stylex.props(styles.column)}>      <Typography.Heading level={1}>Dashboard</Typography.Heading>      <Typography.Paragraph>        Convenience primitives are thin wrappers over Typography, so you can choose explicit        composition without learning a second styling system.      </Typography.Paragraph>      <Typography.Paragraph color="muted" size="sm">        Paragraph supports base, sm, and xs sizes.      </Typography.Paragraph>      <Typography.Code>Typography.Code</Typography.Code>    </div>  );}export default Primitives;

Local adaptation source above. Derived from HeroUI v3.2.6 source.

  • Typography.Heading 将 level={1..6} 映射为 type="h1" 至 type="h6"。
  • Typography.Paragraph 将 size="base" | "sm" | "xs" 映射为正文样式。
  • Typography.Code 映射为行内代码样式。
  • Typography.Prose 为以常规 HTML 子节点传入的富文本内容提供排版样式。

富文本排版

此预览复用英文版适配,不代表中文源示例已完成本地实现。

"use client";// Adapted from HeroUI v3.2.6 (Apache-2.0).import { Typography } from "@lenso/ui";import * as stylex from "@stylexjs/stylex";const styles = stylex.create({  column: { display: "flex", maxWidth: 576, flexDirection: "column", gap: 12 },});export function Prose() {  return (    <Typography.Prose xstyle={styles.column}>      <h1>Prose title</h1>      <p>        Prose is for authored content where the markup is already semantic and Lenso UI applies the        default typography rhythm.      </p>      <h2>Section title</h2>      <p>        Inline code like <code>render</code> receives the same code treatment as the Typography        primitive.      </p>    </Typography.Prose>  );}export default Prose;

Local adaptation source above. Derived from HeroUI v3.2.6 source.

渲染属性

此预览复用英文版适配,不代表中文源示例已完成本地实现。

"use client";// Adapted from HeroUI v3.2.6 (Apache-2.0); uses native Base UI render props.import { Typography } from "@lenso/ui";import * as stylex from "@stylexjs/stylex";const styles = stylex.create({  column: { display: "flex", maxWidth: 576, flexDirection: "column", gap: 16 },});export function RenderProps() {  return (    <div {...stylex.props(styles.column)}>      <Typography        render={({ children, ...domProps }) => <h2 {...domProps}>{children}</h2>}        type="h1"      >        H1 visual style, h2 semantic element      </Typography>      <Typography render={({ children, ...domProps }) => <span {...domProps}>{children}</span>}>        The render prop can swap the underlying element while preserving Lenso UI props and styles.      </Typography>    </div>  );}export default RenderProps;

Local adaptation source above. Derived from HeroUI v3.2.6 source.

需要自定义实际渲染的元素时,可使用 React Aria Components 风格的 render prop。

自定义样式

Tailwind CSS

此预览复用英文版适配,不代表中文源示例已完成本地实现。

"use client";// Adapted from HeroUI v3.2.6 (Apache-2.0).import { Typography } from "@lenso/ui";import * as stylex from "@stylexjs/stylex";const styles = stylex.create({  panel: {    display: "flex",    maxWidth: 448,    flexDirection: "column",    gap: 8,    borderRadius: "var(--radius-xl)",    border: "1px solid color-mix(in oklab, var(--border) 80%, transparent)",    backgroundColor: "var(--surface-secondary)",    padding: 16,  },  category: {    fontSize: 12,    fontWeight: 500,    letterSpacing: ".025em",    color: "var(--accent)",    textTransform: "uppercase",  },  title: { fontWeight: 600, letterSpacing: "-.025em", color: "var(--foreground)" },  text: { fontSize: 14, lineHeight: 1.625, color: "var(--muted)" },});export function CustomStyles() {  return (    <div {...stylex.props(styles.panel)}>      <Typography xstyle={styles.category} type="body-xs">        Changelog      </Typography>      <Typography xstyle={styles.title} type="h4">        Faster search results      </Typography>      <Typography xstyle={styles.text} type="body-sm">        Queries now return in under 200ms thanks to an improved index.      </Typography>    </div>  );}export default CustomStyles;

Local adaptation source above. Derived from HeroUI v3.2.6 source.

全局 CSS

若要自定义 Typography 组件类,可使用 @layer components 指令。了解更多。

@layer components {  .typography--h1 {    @apply font-extrabold tracking-tight;  }
  .typography--code {    @apply rounded-md bg-default-100 font-mono dark:bg-default-50/20;  }}

样式参考

HeroUI 遵循 BEM 方法论,确保组件变体与状态可复用且易于自定义。

CSS 类

Typography 组件使用以下 CSS 类(查看源码样式):

基础类 [!toc]

  • .typography - 排版基础原语
  • .typography-prose - 富文本文章体容器

类型类 [!toc]

  • .typography--h1 至 .typography--h6
  • .typography--body、.typography--body-sm、.typography--body-xs
  • .typography--code

修饰类 [!toc]

  • .typography--align-start、.typography--align-center、.typography--align-end、.typography--align-justify
  • .typography--color-default、.typography--color-muted
  • .typography--truncate
  • .typography--weight-normal、.typography--weight-medium、.typography--weight-semibold、.typography--weight-bold

API 参考

Typography

属性类型默认值说明
type'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6' | 'body' | 'body-sm' | 'body-xs' | 'code''body'语义化排版样式。
align'start' | 'center' | 'end' | 'justify''start'文本对齐。
color'default' | 'muted''default'文本颜色。
weight'normal' | 'medium' | 'semibold' | 'bold'-字重覆盖。
truncateboolean-将文本截断为单行并显示省略号。
renderDOMRenderFunction-来自 React Aria 的自定义渲染函数。
childrenReactNode-文本内容。

相关组件