Translation in progress
This page hasn't been translated into English yet, so the content below is in Chinese. The code examples work the same way.
Typography 排版
标题、正文和段落。包含 NsTitle、NsText、NsParagraph 三个组件,以及只设置字体的容器 NsTypography。
引入
import { NsTypography, NsTitle, NsText, NsParagraph } from '@ns/ui'
基础用法
NsTypography 只统一设置里面的字体、颜色和行高;标题、段落、行内文字分别用 NsTitle、NsParagraph、NsText。
<template>
<NsTypography>
<NsTitle :level="2">标题</NsTitle>
<NsParagraph>一段正文。<NsText type="primary" bold>强调的文字</NsText>接着写。</NsParagraph>
</NsTypography>
</template>
标题级别
level 是标题级别,1~5 分别渲染成 <h1>~<h5>,字号依次是 48、38、30、24、20px。1、2 级是粗体(700),3、4 级是半粗(600),5 级是中等(500)。标题本身没有外边距。
<template>
<NsSpace direction="vertical" align="start">
<NsTitle :level="1">一级标题(level 1)</NsTitle>
<NsTitle :level="2">二级标题(level 2)</NsTitle>
<NsTitle :level="3">三级标题(level 3)</NsTitle>
<NsTitle :level="4">四级标题(level 4)</NsTitle>
<NsTitle :level="5">五级标题(level 5)</NsTitle>
</NsSpace>
</template>
颜色类型
type 设置文字颜色,共 7 种:default(默认文字色)、primary、success、warning、danger、info、secondary(次要文字色)。Title、Text、Paragraph 都有这个属性。默认主题里 info 和 primary 是同一个蓝色。
<template>
<NsSpace direction="vertical" align="start">
<NsSpace wrap>
<span class="row-label">Text</span>
<NsText>默认(default)</NsText>
<NsText type="primary">主要(primary)</NsText>
<NsText type="success">成功(success)</NsText>
<NsText type="warning">警告(warning)</NsText>
<NsText type="danger">危险(danger)</NsText>
<NsText type="info">信息(info)</NsText>
<NsText type="secondary">次要(secondary)</NsText>
</NsSpace>
<NsSpace wrap>
<span class="row-label">Title</span>
<NsTitle :level="5">默认</NsTitle>
<NsTitle :level="5" type="primary">主要</NsTitle>
<NsTitle :level="5" type="success">成功</NsTitle>
<NsTitle :level="5" type="warning">警告</NsTitle>
<NsTitle :level="5" type="danger">危险</NsTitle>
<NsTitle :level="5" type="info">信息</NsTitle>
<NsTitle :level="5" type="secondary">次要</NsTitle>
</NsSpace>
<NsSpace wrap>
<span class="row-label">Paragraph</span>
<NsParagraph>默认段落</NsParagraph>
<NsParagraph type="primary">主要段落</NsParagraph>
<NsParagraph type="success">成功段落</NsParagraph>
<NsParagraph type="warning">警告段落</NsParagraph>
<NsParagraph type="danger">危险段落</NsParagraph>
<NsParagraph type="info">信息段落</NsParagraph>
<NsParagraph type="secondary">次要段落</NsParagraph>
</NsSpace>
</NsSpace>
</template>
<style scoped>
.row-label {
display: inline-block;
width: 72px;
color: var(--ns-text-color-secondary);
font-size: var(--ns-font-size-sm);
}
</style>
文字尺寸
size 设置 Text、Paragraph 的字号:xs 12px、sm 13px、base 14px(默认)、lg 16px、xl 20px。Title 的字号由 level 决定,没有 size。
<template>
<NsSpace direction="vertical" align="start">
<NsSpace wrap align="baseline">
<NsText size="xs">特小(xs)</NsText>
<NsText size="sm">小(sm)</NsText>
<NsText>默认(base)</NsText>
<NsText size="lg">大(lg)</NsText>
<NsText size="xl">特大(xl)</NsText>
</NsSpace>
<NsParagraph size="xs">xs:段落也可以设置字号,行高是字号的 1.75 倍。</NsParagraph>
<NsParagraph size="sm">sm:段落也可以设置字号,行高是字号的 1.75 倍。</NsParagraph>
<NsParagraph>base:段落也可以设置字号,行高是字号的 1.75 倍。</NsParagraph>
<NsParagraph size="lg">lg:段落也可以设置字号,行高是字号的 1.75 倍。</NsParagraph>
<NsParagraph size="xl">xl:段落也可以设置字号,行高是字号的 1.75 倍。</NsParagraph>
</NsSpace>
</template>
文字修饰
bold 加粗、italic 斜体、underline 下划线、deleted 删除线、mark 标记(浅黄底)、code 代码样式(灰底、等宽字体,字号是周围文字的 0.9 倍),可以任意组合。Title、Text、Paragraph 都有这几个属性。
删除线、标记、代码的文字颜色跟随 type:
<template>
<NsSpace direction="vertical" align="start">
<NsSpace wrap>
<span class="row-label">单个</span>
<NsText bold>加粗(bold)</NsText>
<NsText italic>斜体(italic)</NsText>
<NsText underline>下划线(underline)</NsText>
<NsText deleted>删除线(deleted)</NsText>
<NsText mark>标记(mark)</NsText>
<NsText code>代码(code)</NsText>
</NsSpace>
<NsSpace wrap>
<span class="row-label">组合</span>
<NsText bold italic>加粗斜体</NsText>
<NsText bold underline>加粗下划线</NsText>
<NsText deleted mark>删除线加标记</NsText>
<NsText mark code>标记的代码</NsText>
<NsText deleted code>删除的代码</NsText>
<NsText bold italic underline>三种一起</NsText>
</NsSpace>
<NsSpace wrap>
<span class="row-label">带颜色</span>
<NsText type="danger" deleted>已作废</NsText>
<NsText type="success" mark>已通过</NsText>
<NsText type="primary" code>v-model</NsText>
<NsText type="warning" underline>待确认</NsText>
<NsText type="secondary" italic>备注</NsText>
<NsText size="lg" bold>大号加粗</NsText>
</NsSpace>
<NsSpace wrap>
<span class="row-label">标题</span>
<NsTitle :level="5" italic>斜体标题</NsTitle>
<NsTitle :level="5" underline>下划线标题</NsTitle>
<NsTitle :level="5" deleted>删除线标题</NsTitle>
<NsTitle :level="5" mark>标记标题</NsTitle>
<NsTitle :level="5" code>NsTitle</NsTitle>
<NsTitle :level="5" bold>加粗的五级标题</NsTitle>
</NsSpace>
</NsSpace>
</template>
<style scoped>
.row-label {
display: inline-block;
width: 56px;
color: var(--ns-text-color-secondary);
font-size: var(--ns-font-size-sm);
}
</style>
段落里同样可以用这些修饰,也可以在段落里嵌套 NsText 只修饰其中几个字:
<template>
<NsTypography>
<NsParagraph>
订单 <NsText code>20260001</NsText> 的状态从
<NsText type="danger" deleted>待付款</NsText> 变成了
<NsText type="success" bold>已付款</NsText>,<NsText mark>预计明天发货</NsText>。
</NsParagraph>
<NsParagraph type="secondary" italic>整段斜体的次要文字,适合放补充说明。</NsParagraph>
<NsParagraph mark>整段标记,适合放需要特别留意的提示。</NsParagraph>
</NsTypography>
</template>
复制
copyable 在文字后面显示“复制”按钮,复制成功后显示“已复制”,2 秒后复原,并触发 copy 事件,参数是复制的文字。复制的是渲染出来的文字,嵌套元素里的文字也包括在内。Title、Text、Paragraph 都可以复制。
<script setup lang="ts">
import { ref } from 'vue'
import { NsParagraph, NsSpace, NsText, NsTitle } from '@ns/ui'
const copied = ref('')
</script>
<template>
<NsSpace direction="vertical" align="start">
<NsText copyable @copy="copied = $event">
<span>外层<b>内层</b></span>
</NsText>
<NsText code copyable @copy="copied = $event">npm i @ns/ui</NsText>
<NsTitle :level="4" copyable @copy="copied = $event">可以复制的标题</NsTitle>
<NsParagraph copyable @copy="copied = $event">段落的复制按钮跟在正文后面。</NsParagraph>
</NsSpace>
<p style="margin-top: 12px">{{ copied ? `已复制:${copied}` : '点“复制”试试' }}</p>
</template>
单行省略(Title、Text)
ellipsis 是布尔值,超出一行的部分显示省略号。Text 默认最多占满父元素的一行,要更窄时给组件设 max-width 或 width;Title 按父元素宽度省略。和 copyable 同时用时,复制按钮紧跟在省略号后面,复制的仍是完整的文字。
<template>
<NsSpace direction="vertical" fill>
<NsText ellipsis style="max-width: 200px">这是一段很长很长的文字内容,超出部分会被省略</NsText>
<div style="max-width: 240px">
<NsText ellipsis>
没有设宽度时按父元素的宽度省略,这里父元素最宽 240px,放不下的部分显示省略号
</NsText>
</div>
<NsText ellipsis copyable style="max-width: 260px">
和复制一起用时,复制按钮紧跟在省略号后面,复制的是完整的文字
</NsText>
<div style="max-width: 360px">
<NsTitle :level="3" ellipsis copyable>很长很长的标题,超出父元素宽度的部分显示省略号</NsTitle>
</div>
</NsSpace>
</template>
多行省略和展开(Paragraph)
ellipsis 传 true 时按 1 行截断,传对象时可以设置行数、展开按钮和后缀(EllipsisConfig):
rows:最多显示几行。expandable:文字被截断时显示“展开”按钮,点了以后显示全文,不能再收起。没有溢出时不显示。suffix:截断时显示在文字后面的后缀,展开后不显示。
后缀、展开、复制排在截断文字的下一行,顺序是“后缀 → 展开 → 复制”。下面的段落限宽 420px,点“展开”后可以点“重新折叠”再看一次:
<script setup lang="ts">
import { ref } from 'vue'
import { NsButton, NsParagraph, NsSpace } from '@ns/ui'
const text =
'NsUI 是一个基于 Vue 3 的组件库,提供按钮、表单、表格、弹窗等常用组件,颜色、尺寸和间距都由主题变量控制,切换暗色主题时所有组件跟着变化。组件带有键盘操作和读屏软件需要的属性,可以直接用在后台管理系统、数据看板和各种内部工具里。文档里的每个示例都能直接运行,把代码复制到自己的项目里就能看到同样的效果。'
// 改 key 让段落重新渲染,回到截断的状态
const key = ref(0)
</script>
<template>
<div style="max-width: 420px">
<NsSpace :key="key" direction="vertical" fill>
<div>
<div class="label">ellipsis(按 1 行截断)</div>
<NsParagraph ellipsis>{{ text }}</NsParagraph>
</div>
<div>
<div class="label">{ rows: 2 }</div>
<NsParagraph :ellipsis="{ rows: 2 }">{{ text }}</NsParagraph>
</div>
<div>
<div class="label">{ rows: 3, expandable: true }</div>
<NsParagraph :ellipsis="{ rows: 3, expandable: true }">{{ text }}</NsParagraph>
</div>
<div>
<div class="label">{ rows: 2, expandable: true, suffix: '—— 摘自说明' },加 copyable</div>
<NsParagraph :ellipsis="{ rows: 2, expandable: true, suffix: '—— 摘自说明' }" copyable>
{{ text }}
</NsParagraph>
</div>
<div>
<div class="label">文字没超出 2 行,不显示“展开”</div>
<NsParagraph :ellipsis="{ rows: 2, expandable: true }">一段很短的文字。</NsParagraph>
</div>
</NsSpace>
<NsButton size="small" style="margin-top: 12px" @click="key++">重新折叠</NsButton>
</div>
</template>
<style scoped>
.label {
margin-bottom: 4px;
color: var(--ns-text-color-secondary);
font-size: var(--ns-font-size-sm);
}
</style>
Typography Props
没有属性,只统一设置里面的字体、颜色和行高。
Title Props
| 属性名 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| level | 1 | 2 | 3 | 4 | 5 | 1 | 标题级别 |
| type | 'default' | 'primary' | 'success' | 'warning' | 'danger' | 'info' | 'secondary' | 'default' | 标题颜色类型 |
| bold | boolean | false | 是否加粗 |
| italic | boolean | false | 是否斜体 |
| underline | boolean | false | 是否下划线 |
| deleted | boolean | false | 是否删除线 |
| mark | boolean | false | 是否标记高亮 |
| code | boolean | false | 是否代码样式 |
| copyable | boolean | false | 是否可复制 |
| ellipsis | boolean | false | 是否省略溢出文本 |
Text Props
| 属性名 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| type | 'default' | 'primary' | 'success' | 'warning' | 'danger' | 'info' | 'secondary' | 'default' | 文本颜色类型 |
| size | 'xs' | 'sm' | 'base' | 'lg' | 'xl' | 'base' | 文本尺寸:xs 12px、sm 13px、base 14px、lg 16px、xl 20px |
| bold | boolean | false | 是否加粗 |
| italic | boolean | false | 是否斜体 |
| underline | boolean | false | 是否下划线 |
| deleted | boolean | false | 是否删除线 |
| mark | boolean | false | 是否标记高亮 |
| code | boolean | false | 是否代码样式 |
| copyable | boolean | false | 是否可复制 |
| ellipsis | boolean | false | 是否省略溢出文本 |
Paragraph Props
| 属性名 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| type | 'default' | 'primary' | 'success' | 'warning' | 'danger' | 'info' | 'secondary' | 'default' | 段落颜色类型 |
| size | 'xs' | 'sm' | 'base' | 'lg' | 'xl' | 'base' | 段落尺寸:xs 12px、sm 13px、base 14px、lg 16px、xl 20px |
| bold | boolean | false | 是否加粗 |
| italic | boolean | false | 是否斜体 |
| underline | boolean | false | 是否下划线 |
| deleted | boolean | false | 是否删除线 |
| mark | boolean | false | 是否标记高亮 |
| code | boolean | false | 是否代码样式 |
| copyable | boolean | false | 是否可复制 |
| ellipsis | boolean | { rows: number; expandable?: boolean; suffix?: string } | false | 是否省略溢出文本,可配置行数 |
Events(Title、Text、Paragraph)
| 事件名 | 参数 | 说明 |
|---|---|---|
| copy | (text: string) | 复制成功时触发 |
Slots
| 插槽 | 说明 |
|---|---|
| default | 标题、文字或段落的内容 |
类型
import type {
TitleProps,
TextProps,
ParagraphProps,
EllipsisConfig,
TitleLevel,
TypographyType,
TextSize
} from '@ns/ui'
type EllipsisConfig = { rows: number; expandable?: boolean; suffix?: string }
注意事项
- Text、Title 的
ellipsis只做单行省略;Text 要设了max-width或width才会在更窄的位置省略。 - Paragraph 的多行省略用的是
-webkit-line-clamp,行数通过根元素上的--ns-paragraph-rows变量传给样式。 - “复制”“已复制”“展开”目前固定是中文。
- 复制用的是
navigator.clipboard,只在 HTTPS 或 localhost 下可用;复制失败时控制台有警告,不触发copy事件。 - 复制、展开按钮是
<button>,可以用 Tab 选中、回车或空格触发。