145a208687
HD-04 scope, against heroui ce90ea3 cn sources: - stale/fictional content: input.md no longer claims TextField is unmigrated; color-field.md no longer claims ColorPicker is private; header.md drops the fictional Header.Root subcomponent and the stale 'menus not migrated' wording (MenuSection label registration is now documented from HeaderRoot source); radio-group.md drops three copied CheckboxGroup wordings (variant override is Radio's, scalar emits, native single-value submit); input-otp.md drops the fictional data-complete state and lists the real missing upstream props - toggle-button disabled row now matches the real CSS hooks (:disabled / [aria-disabled], with data-disabled as a marker only) - part API / CSS classes / interactive states restored per pinned sources: button (full class lists + real variable-based global CSS), accordion (per-part tables + variant/state lists), card (per-part tables + variant list), switch (SwitchGroup snippet + Switch/ SwitchGroup class lists + states), color-field Group (class list + states), input-otp (variant notes, global CSS, class/state lists); input-group gains the two upstream usage chapters - RelatedComponents restored from the pinned componentRelationships (first-3, filtered against componentsMap): 64 pages rewritten, list-box/slider sections added, 7 empty-mapping pages' fabricated entries removed; RelatedShowcases left unmigrated with its real data inventory recorded (2 cases, showcase-site cards) - defaults unified on implementation evidence: granularity (inferred day/minute) and visibleYears (full min-max span, no "or 20" fallback); HD-03 hand-offs resolved (trigger focus-visible noted for HD-06, width guards kept) - new demos: header-collection-grouping (MenuSection/ListBoxSection label wiring), empty-state-collection (collection empty slot); empty-state page deduplicated with a boundary compatibility note - ComponentPreview minHeight/isBgSolid restored per the 30 upstream marks; tabs-overflow max-w-full kept after measuring internal scroll - tests: +3 header registration/demo cases, +1 empty-state collection case; full suite 110 files / 1630 tests, typecheck and build:docs pass
190 lines
9.4 KiB
Markdown
190 lines
9.4 KiB
Markdown
---
|
||
title: Typography 排版
|
||
description: 面向标题、正文与行内代码的语义化排版原语。
|
||
component: typography
|
||
group: 组件
|
||
---
|
||
|
||
## 用法
|
||
|
||
```vue
|
||
<script setup lang="ts">
|
||
import { Typography } from "@heroui-vue/components";
|
||
</script>
|
||
|
||
<template>
|
||
<Typography type="h1">Heading</Typography>
|
||
</template>
|
||
```
|
||
|
||
<ComponentPreview name="typography-default" />
|
||
|
||
> 示例中的 `pnpm add @heroui/react` 沿用固定基线文案;本 Vue 项目的实际包名与安装命令以[安装指南](../guide)为准。
|
||
|
||
## 组件结构
|
||
|
||
默认情况下,`Typography` 会将视觉上的 `type` 映射到对应的语义化元素。
|
||
|
||
```vue
|
||
<script setup lang="ts">
|
||
import { Typography } from "@heroui-vue/components";
|
||
</script>
|
||
|
||
<template>
|
||
<Typography type="h1">Heading</Typography>
|
||
</template>
|
||
```
|
||
|
||
| `type` | 默认元素 |
|
||
| ---------------------------- | ----------- |
|
||
| `h1` – `h6` | `h1` – `h6` |
|
||
| `body`、`body-sm`、`body-xs` | `p` |
|
||
| `code` | `code` |
|
||
|
||
需要保留视觉类型但更换语义元素时,使用 `as`。它对应上游的 `render` 自定义渲染能力。
|
||
|
||
```vue
|
||
<Typography type="h1" as="h2">H1 视觉样式,h2 语义元素</Typography>
|
||
```
|
||
|
||
## 示例
|
||
|
||
### 层级
|
||
|
||
<ComponentPreview name="typography-typography-scale" />
|
||
|
||
### 原语
|
||
|
||
<ComponentPreview name="typography-primitives" />
|
||
|
||
- `TypographyHeading` 将 `level`(1–6)映射为 `type="h1"` 至 `type="h6"`。
|
||
- `TypographyParagraph` 将 `size="base" | "sm" | "xs"` 映射为正文样式。
|
||
- `TypographyCode` 映射为行内代码样式。
|
||
- `TypographyProse` 为以常规 HTML 子节点传入的富文本内容提供排版样式。
|
||
|
||
上游以 `Typography.Heading`、`Typography.Paragraph`、`Typography.Code` 和
|
||
`Typography.Prose` 对象属性提供这些原语;Vue 版按本库约定改为独立具名导出。
|
||
|
||
### 富文本排版
|
||
|
||
<ComponentPreview name="typography-prose" />
|
||
|
||
### 渲染属性
|
||
|
||
<ComponentPreview name="typography-render-props" />
|
||
|
||
React 的 `render` 由 Vue 的 `as` 属性替代,只更换实际渲染元素,不改变
|
||
`type` 对应的视觉样式与 `data-type`。
|
||
|
||
## 自定义样式
|
||
|
||
### Tailwind CSS
|
||
|
||
<ComponentPreview name="typography-custom-styles" />
|
||
|
||
### 全局 CSS
|
||
|
||
若要自定义 Typography 组件类,可使用 `@layer components` 指令。
|
||
|
||
```css
|
||
@layer components {
|
||
.hv-typography--h1 {
|
||
@apply font-extrabold tracking-tight;
|
||
}
|
||
|
||
.hv-typography--code {
|
||
@apply rounded-md bg-default-100 font-mono dark:bg-default-50/20;
|
||
}
|
||
}
|
||
```
|
||
|
||
## 样式参考
|
||
|
||
HeroUI 遵循 [BEM](https://getbem.com/) 方法论,确保组件变体与状态可复用且易于自定义。类名前缀为 `hv-`。
|
||
|
||
### CSS 类
|
||
|
||
Typography 组件使用以下 CSS 类([查看源码样式](https://github.com/heroui-inc/heroui/blob/v3/packages/styles/components/typography.css)):
|
||
|
||
#### 基础类 [!toc]
|
||
|
||
- `.hv-typography` - 排版基础原语
|
||
- `.hv-typography-prose` - 富文本文章体容器
|
||
|
||
#### 类型类 [!toc]
|
||
|
||
- `.hv-typography--h1` 至 `.hv-typography--h6`
|
||
- `.hv-typography--body`、`.hv-typography--body-sm`、`.hv-typography--body-xs`
|
||
- `.hv-typography--code`
|
||
|
||
#### 修饰类 [!toc]
|
||
|
||
- `.hv-typography--align-start`、`.hv-typography--align-center`、`.hv-typography--align-end`、`.hv-typography--align-justify`
|
||
- `.hv-typography--color-default`、`.hv-typography--color-muted`
|
||
- `.hv-typography--truncate`
|
||
- `.hv-typography--weight-normal`、`.hv-typography--weight-medium`、`.hv-typography--weight-semibold`、`.hv-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'` | - | 字重覆盖。 |
|
||
| `truncate` | `boolean` | `false` | 将文本截断为单行并显示省略号。 |
|
||
| `as` | `string` | 由 `type` 决定 | 自定义实际渲染的元素;替代上游 `render`。 |
|
||
| `class` | `string \| Array \| Object` | - | 附加 CSS 类(Vue class 绑定)。 |
|
||
| 默认插槽 | - | - | 文本内容。 |
|
||
|
||
`$attrs`(`aria-*`、`data-*`、`id`、`style`、事件等)透传至实际根元素。组件不修改传入内容,也没有内部值或事件。
|
||
|
||
### TypographyHeading
|
||
|
||
| 属性 | 类型 | 默认值 | 说明 |
|
||
| ---------- | ---------------------------------------------- | --------------- | ----------------------------------- |
|
||
| `level` | `1 \| 2 \| 3 \| 4 \| 5 \| 6` | `1` | 映射到 `h1` – `h6` 与对应视觉样式。 |
|
||
| `align` | `'start' \| 'center' \| 'end' \| 'justify'` | `'start'` | 文本对齐。 |
|
||
| `color` | `'default' \| 'muted'` | `'default'` | 文本颜色。 |
|
||
| `weight` | `'normal' \| 'medium' \| 'semibold' \| 'bold'` | - | 字重覆盖。 |
|
||
| `truncate` | `boolean` | `false` | 单行截断。 |
|
||
| `as` | `string` | 由 `level` 决定 | 自定义实际渲染的元素。 |
|
||
| `class` | `string \| Array \| Object` | - | 附加 CSS 类。 |
|
||
| 默认插槽 | - | - | 标题内容。 |
|
||
|
||
### TypographyParagraph
|
||
|
||
| 属性 | 类型 | 默认值 | 说明 |
|
||
| ---------- | ---------------------------------------------- | ----------- | ------------------------------------- |
|
||
| `size` | `'base' \| 'sm' \| 'xs'` | `'base'` | 映射到 `body`、`body-sm`、`body-xs`。 |
|
||
| `align` | `'start' \| 'center' \| 'end' \| 'justify'` | `'start'` | 文本对齐。 |
|
||
| `color` | `'default' \| 'muted'` | `'default'` | 文本颜色。 |
|
||
| `weight` | `'normal' \| 'medium' \| 'semibold' \| 'bold'` | - | 字重覆盖。 |
|
||
| `truncate` | `boolean` | `false` | 单行截断。 |
|
||
| `as` | `string` | `'p'` | 自定义实际渲染的元素。 |
|
||
| `class` | `string \| Array \| Object` | - | 附加 CSS 类。 |
|
||
| 默认插槽 | - | - | 正文内容。 |
|
||
|
||
### TypographyCode
|
||
|
||
| 属性 | 类型 | 默认值 | 说明 |
|
||
| ---------- | ---------------------------------------------- | ----------- | ---------------------- |
|
||
| `align` | `'start' \| 'center' \| 'end' \| 'justify'` | `'start'` | 文本对齐。 |
|
||
| `color` | `'default' \| 'muted'` | `'default'` | 文本颜色。 |
|
||
| `weight` | `'normal' \| 'medium' \| 'semibold' \| 'bold'` | - | 字重覆盖。 |
|
||
| `truncate` | `boolean` | `false` | 单行截断。 |
|
||
| `as` | `string` | `'code'` | 自定义实际渲染的元素。 |
|
||
| `class` | `string \| Array \| Object` | - | 附加 CSS 类。 |
|
||
| 默认插槽 | - | - | 行内代码内容。 |
|
||
|
||
### TypographyProse
|
||
|
||
| 属性 | 类型 | 默认值 | 说明 |
|
||
| -------- | --------------------------- | ------ | ---------------------- |
|
||
| `class` | `string \| Array \| Object` | - | 附加 CSS 类。 |
|
||
| 默认插槽 | - | - | 常规 HTML 富文本内容。 |
|
||
|
||
`TypographyProse` 固定渲染 `div`,内部标题、段落、列表、引用、代码块、链接、图片等使用基线排版规则。`$attrs` 透传至该 `div`。
|