# 文本

## 使用方法

`Text` 用于渲染正文、辅助说明、表单标签等文字内容，`type` 提供了一套预设的字号、字重和行高组合，让整站文字风格保持一致。从独立入口导入即可，结构样式会随组件自动加载：

```vue
<script setup lang="ts">
import { Text } from '@astryx-vue/core/Text'
</script>

<template>
  <Text type="body">
    正文内容
  </Text>
  <Text type="supporting">
    辅助说明
  </Text>
</template>
```

### 结构

- **文本**: 多态文本元素，用主题里的字体渲染放进来的内容。
- **标题**: Text 上的 Heading。把放进来的内容渲染成语义上的 h1 到 h6。
- **截断提示**: 只有 Text 或 Heading 被测出已经截断，并且打开了截断提示时，才出现的 Tooltip 表面。

## 最佳实践

| 指引 | 实践 |
| --- | --- |
| 推荐 | 通过 `type` 表达文字的语义层级：`large` 用于强调性内容，`supporting` 用于辅助说明，`label` 用于表单标签。 |
| 推荐 | 需要块级布局时，把 `as` 设为 `p` 或将 `display` 设为 `block`，默认的行内元素不利于控制间距。 |
| 推荐 | 对可能超出容器的长文本设置 `maxLines`，超出部分会自动截断并显示省略号，避免撑破布局。 |
| 避免 | 用 `Text` 充当标题：标题请使用 `Heading` 组件，才能保证文档大纲对辅助技术可见。 |
| 避免 | 在同一小段文字中混用过多 `type` 和 `size`：样式变化过多会打断阅读节奏。 |

## 示例

### 颜色

primary、secondary、accent、disabled、placeholder 五种颜色，各自对应正文、辅助信息、强调、不可用内容和占位提示。

```vue
<script setup lang="ts">
import type { TextColor } from '@astryx-vue/core/Text'
import { VStack } from '@astryx-vue/core/Stack'
import { Text } from '@astryx-vue/core/Text'

const colors: { color: TextColor, description: string }[] = [
  { color: 'primary', description: 'Primary — Default for headings and body text' },
  { color: 'secondary', description: 'Secondary — Supporting details and metadata' },
  { color: 'accent', description: 'Accent — Links, emphasis, and accent-colored text' },
  { color: 'disabled', description: 'Disabled — Unavailable or inactive content' },
  { color: 'placeholder', description: 'Placeholder — Empty field hints' },
]
</script>

<template>
  <VStack :gap="3">
    <Text v-for="item in colors" :key="item.color" type="body" :color="item.color">
      {{ item.description }}
    </Text>
  </VStack>
</template>
```

### 标题层级

Heading 从 h1 排到 h6，字号随层级下降。

```vue
<script setup lang="ts">
import type { HeadingLevel } from '@astryx-vue/core/Heading'
import { Heading } from '@astryx-vue/core/Heading'
import { VStack } from '@astryx-vue/core/Stack'

const levels: HeadingLevel[] = [1, 2, 3, 4, 5, 6]
</script>

<template>
  <VStack :gap="3">
    <Heading v-for="level in levels" :key="level" :level="level">
      Heading {{ level }}
    </Heading>
  </VStack>
</template>
```

### 行内混排

默认是行内元素，所以正文和等宽代码可以排在同一行里。

```vue
<script setup lang="ts">
import { Text } from '@astryx-vue/core/Text'
</script>

<template>
  <Text type="body" display="block">
    Design tokens are <Text type="code">
      themeable
    </Text> and shared across every surface.
  </Text>
</template>
```

### 截断

在限宽容器里用 maxLines 把文字收成一行、两行或三行，超出的部分以省略号结尾。

```vue
<script setup lang="ts">
import { Text } from '@astryx-vue/core/Text'

const longText = 'The design system provides a consistent set of typography tokens, spacing scales, and color palettes that ensure every surface in the product feels cohesive regardless of which team built it.'
const lines = [
  { maxLines: 1, label: '1 line' },
  { maxLines: 2, label: '2 lines' },
  { maxLines: 3, label: '3 lines' },
]
</script>

<template>
  <div class="text-preview__clamp">
    <div v-for="item in lines" :key="item.maxLines">
      <Text type="supporting" color="secondary" display="block">
        {{ item.label }}
      </Text>
      <div class="text-preview__box">
        <Text type="body" :max-lines="item.maxLines">
          {{ longText }}
        </Text>
      </div>
    </div>
  </div>
</template>
```

### 文本类型

body、large、label、supporting、code 五种语义类型，以及删除线和等宽数字。

```vue
<script setup lang="ts">
import type { TextType } from '@astryx-vue/core/Text'
import { VStack } from '@astryx-vue/core/Stack'
import { Text } from '@astryx-vue/core/Text'

const types: {
  type: TextType
  label: string
  sample: string
  hasStrikethrough?: boolean
  hasTabularNumbers?: boolean
}[] = [
  { type: 'body', label: 'Body text', sample: 'Body text for paragraphs and general content' },
  { type: 'large', label: 'Large text', sample: 'Large text for introductions and callouts' },
  { type: 'label', label: 'Label text', sample: 'Label text for form fields and section titles' },
  { type: 'supporting', label: 'Supporting text', sample: 'Supporting text for captions and metadata' },
  { type: 'code', label: 'Code text', sample: 'const theme = defineTheme({})' },
  { type: 'body', label: 'Strikethrough', sample: 'Body text with strikethrough decoration', hasStrikethrough: true },
  { type: 'body', label: 'Tabular numbers', sample: '1,234.56  78.90  100,000.00', hasTabularNumbers: true },
]
</script>

<template>
  <VStack :gap="3">
    <VStack v-for="item in types" :key="item.label" :gap="0">
      <Text type="supporting" color="secondary">
        {{ item.label }}
      </Text>
      <Text
        :type="item.type"
        display="block"
        :has-strikethrough="item.hasStrikethrough"
        :has-tabular-numbers="item.hasTabularNumbers"
      >
        {{ item.sample }}
      </Text>
    </VStack>
  </VStack>
</template>
```

### 字重

normal、medium、semibold、bold 四种字重。normal 与 regular 是同一档。

```vue
<script setup lang="ts">
import type { TextWeight } from '@astryx-vue/core/Text'
import { VStack } from '@astryx-vue/core/Stack'
import { Text } from '@astryx-vue/core/Text'

const weights: { weight: TextWeight, label: string }[] = [
  { weight: 'normal', label: 'Normal' },
  { weight: 'medium', label: 'Medium' },
  { weight: 'semibold', label: 'Semibold' },
  { weight: 'bold', label: 'Bold' },
]
</script>

<template>
  <VStack :gap="3">
    <Text
      v-for="item in weights"
      :key="item.weight"
      type="body"
      :weight="item.weight"
      display="block"
    >
      {{ item.label }}
    </Text>
  </VStack>
</template>
```

### 断词

一长串没有空格的文字，break-word 尽量在词的边界断开，break-all 则可以在任意字符处断开。

```vue
<script setup lang="ts">
import { Text } from '@astryx-vue/core/Text'
</script>

<template>
  <div class="text-preview__clamp">
    <div>
      <Text type="label" display="block">
        Break-word (default for multi-line)
      </Text>
      <div class="text-preview__box">
        <Text type="body" :max-lines="2" word-break="break-word">
          This is a verylongunbreakableword for a break-word example
        </Text>
      </div>
    </div>
    <div>
      <Text type="label" display="block">
        Break-all (default for single-line)
      </Text>
      <div class="text-preview__box">
        <Text type="body" :max-lines="2" word-break="break-all">
          Breaks anywhere: abcdefghijklmnopqrstuvwxyz0123456789
        </Text>
      </div>
    </div>
  </div>
</template>
```

### 换行

wrap、nowrap、balance、pretty 四种换行方式，都放在同样宽度的盒子里。

```vue
<script setup lang="ts">
import { Text } from '@astryx-vue/core/Text'
</script>

<template>
  <div class="text-preview__clamp">
    <div>
      <Text type="label" display="block">
        Wrap (default)
      </Text>
      <div class="text-preview__box">
        <Text type="body" text-wrap="wrap">
          This text wraps normally at word boundaries when it reaches the edge.
        </Text>
      </div>
    </div>
    <div>
      <Text type="label" display="block">
        Nowrap
      </Text>
      <div class="text-preview__box text-preview__box--clip">
        <Text type="body" text-wrap="nowrap">
          This text does not wrap and will overflow its container.
        </Text>
      </div>
    </div>
    <div>
      <Text type="label" display="block">
        Balance
      </Text>
      <div class="text-preview__box">
        <Text type="body" text-wrap="balance">
          This text is balanced for better visual appearance across lines.
        </Text>
      </div>
    </div>
    <div>
      <Text type="label" display="block">
        Pretty
      </Text>
      <div class="text-preview__box">
        <Text type="body" text-wrap="pretty">
          This text uses pretty wrap to avoid orphans at the end of paragraphs.
        </Text>
      </div>
    </div>
  </div>
</template>
```

## Props

| 名称 | 类型 | 默认值 | 说明 |
| --- | --- | --- | --- |
| `type` | `TextType` | `'body'` | 语义文本类型，可选 body、large、label、supporting、code、display-1、display-2、display-3。 |
| `size` | `TextSize` | - | 显式字号覆盖，可选 sm、md、lg、xl。 |
| `color` | `TextColor` | `'primary'` | 文字颜色，可选 primary、secondary、accent、disabled、placeholder、inherit。 |
| `weight` | `TextWeight` | - | 字重覆盖，可选 regular、normal、medium、semibold、bold。normal 与 regular 相同。 |
| `textWrap` | `TextWrap` | - | 换行方式，可选 wrap、nowrap、balance、pretty。 |
| `wordBreak` | `TextWordBreak` | - | 截断时长词如何断开，可选 normal、break-word、break-all。没有截断时，比一行还长的串（URL、ID）会换行（`overflow-wrap: break-word`），普通文档流里不会撑出横向滚动。放在 flex 行里时，记得给 flex 子项设 `min-width: 0` 让它能收缩。你自己设了 `overflow-wrap: anywhere` 的容器管不到这里的文字；Astryx 自带的容器会把它们的换行方式传进来。 |
| `hasCapsize` | `boolean` | `false` | 用 `text-box-trim` 裁掉大写字母高度以上的空白和基线以下的空白，让字形贴住盒子边缘。打开后强制块级布局。 |
| `hasStrikethrough` | `boolean` | `false` | 给文字加删除线。 |
| `hasTabularNumbers` | `boolean` | `false` | 使用等宽数字，方便多行数字对齐。 |
| `display` | `TextDisplay` | `'inline'` | 显示类型：`inline` 或 `block`。 |
| `as` | `TextAs` | `'span'` | 实际渲染的 HTML 元素，可选 span、p、div、label、h1、h2、h3、h4、h5、h6。标题标签渲染真正的标题元素，但保留 `Text` 的字号体系；要标题字号请用 `Heading`。 |
| `justify` | `'start' \| 'center' \| 'end'` | `'start'` | 对齐方式。`start` 和 `end` 跟随书写方向。 |
| `hasTruncateTooltip` | `boolean \| 'above' \| 'below' \| 'start' \| 'end'` | `true` | 截断之后要不要把全文放进提示。`false` 关掉，同时也不再测量。没被截断时不会弹出。提示等到第一次悬停、轻点或键盘聚焦才挂载，在那之前元素上没有提示层，也没有 `aria-describedby`。 |
| `maxLines` | `number` | `0` | 最大显示行数，超出部分截断并显示省略号，0 表示不截断。大于 0 时会改成块级，省略号才有宽度可用。 |

## 原生属性

根元素由 `as` 决定，默认为 `<span>`。

以下内容传入根元素：

- HTML 属性。
- `aria-*` 属性。
- `data-*` 属性。
- `class`。
- `style`。
