Typography排版

用于文章与界面的文本:标题、段落、行内文本与链接,支持样式、复制按钮、原地编辑和长文本省略。

引入
import { Typography, TypographyTitle, TypographyParagraph, TypographyText, TypographyLink } from 'svelte-ant-design';
链接
src/lib/Typography 问题反馈 组件 README

何时使用

  • 用于风格统一、易读的文本:文章、笔记或博客。
  • 对需要复制或修改的短内容(如编号、名称)使用 copyable 与 editable。
  • 空间有限时使用 ellipsis,并保留查看全文的方式:提示或 expandable。

代码演示

A design system

Many projects need the same design rules and the same parts. A design system keeps them in one place, so the work does not start again in every application.

How to start

Run npm install svelte-ant-design and press Enter.
Decide first, then build. The API table lists every property.

文章

Typography 是带基础文本样式的 <article>。其中的标题、段落、行内文本和链接用四个组件编写。

Release notes

Read the table of properties before you start.

  • Run npm install and press Enter
  • Decide first, then build
Consistency is a feature that every page gets for free.
npm install svelte-ant-design

现成的 HTML

用于来自 Markdown 或 CMS 的现成 HTML,无法放置组件的场合。Typography 内的普通标题、段落、链接、列表、引用、pre、code、kbd、mark 和 strong 会获得样式;其中本库的组件保持自身外观。只插入你信任或已清理过的 HTML。

level 2 to 5 (level 1 is the default and is the h1 of a page)

h2. Ant Design

h3. Ant Design

h4. Ant Design

h5. Ant Design

标题

level 取 1 到 5,对应 h1 到 h5;字号随级别变化。示例从 2 级开始:一个页面只有一个 h1(默认级别)。

type Ant Design (default) Ant Design (secondary) Ant Design (success) Ant Design (warning) Ant Design (danger) Ant Design (disabled)
decorations Ant Design (mark) Ant Design (code) Ant Design (keyboard) Ant Design (underline) Ant Design (delete) Ant Design (strong) Ant Design (italic)
combined Not for production

文本样式

type、disabled 以及 strong、italic、underline、delete、mark、code 和 keyboard 标志。标志可以组合。

copyable
This is a copyable text.
Nothing copied yet
text: the copy is not the shown text
Copy something else.
text: a function that returns a promise
Ask the server for the text.
Not requested yet
icon, tooltips
Own icon and tips.
tooltips: false
No tip, the button keeps its name.
a Text with only the button

复制

copyable 添加复制按钮。文本、图标、提示和回调用对象形式设置;文本可以是函数或 Promise:等待期间按钮显示加载图标。

bind:content, with the events
This is an editable text.
No events yet
icon, tooltip
Custom icon and tip.
triggerType: ['icon', 'text']
Click the text or the icon.
maxLength, autoSize, no enter mark
No more than 20 chars
bind:editing
reading
Open me from outside.

编辑

editable 在原位打开输入框。Enter、Tab 或点击别处保存,Escape 取消。bind:content 将新文本写回。

one row, with a tooltip
Ant Design, a design language for background applications, is refined by Ant UED Team. It aims to make the interface specifications of internal projects uniform, to lower the cost of design differences and of implementation, and to free the time of designers and front-end developers. Ant Design, a design language for background applications, is refined by Ant UED Team. It aims to make the interface specifications of internal projects uniform, to lower the cost of design differences and of implementation, and to free the time of designers and front-end developers.
rows=3
Ant Design, a design language for background applications, is refined by Ant UED Team. It aims to make the interface specifications of internal projects uniform, to lower the cost of design differences and of implementation, and to free the time of designers and front-end developers. Ant Design, a design language for background applications, is refined by Ant UED Team. It aims to make the interface specifications of internal projects uniform, to lower the cost of design differences and of implementation, and to free the time of designers and front-end developers.
onellipsis
Ant Design, a design language for background applications, is refined by Ant UED Team. It aims to make the interface specifications of internal projects uniform, to lower the cost of design differences and of implementation, and to free the time of designers and front-end developers. Ant Design, a design language for background applications, is refined by Ant UED Team. It aims to make the interface specifications of internal projects uniform, to lower the cost of design differences and of implementation, and to free the time of designers and front-end developers.
The text is shown whole
a Text in a narrow box
Ant Design, a design language for background applications, is refined by Ant UED Team. It aims to make the interface specifications of internal projects uniform, to lower the cost of design differences and of implementation, and to free the time of designers and front-end developers. Ant Design, a design language for background applications, is refined by Ant UED Team. It aims to make the interface specifications of internal projects uniform, to lower the cost of design differences and of implementation, and to free the time of designers and front-end developers.

省略

ellipsis 将文本截断为一行或 rows 行。设置 tooltip 后,仅当文本被截断时,悬停显示完整内容。

expandable
Ant Design, a design language for background applications, is refined by Ant UED Team. It aims to make the interface specifications of internal projects uniform, to lower the cost of design differences and of implementation, and to free the time of designers and front-end developers. Ant Design, a design language for background applications, is refined by Ant UED Team. It aims to make the interface specifications of internal projects uniform, to lower the cost of design differences and of implementation, and to free the time of designers and front-end developers.
expandable: 'collapsible', onexpand
Ant Design, a design language for background applications, is refined by Ant UED Team. It aims to make the interface specifications of internal projects uniform, to lower the cost of design differences and of implementation, and to free the time of designers and front-end developers. Ant Design, a design language for background applications, is refined by Ant UED Team. It aims to make the interface specifications of internal projects uniform, to lower the cost of design differences and of implementation, and to free the time of designers and front-end developers.
Nothing pressed yet
symbol as a snippet
Ant Design, a design language for background applications, is refined by Ant UED Team. It aims to make the interface specifications of internal projects uniform, to lower the cost of design differences and of implementation, and to free the time of designers and front-end developers. Ant Design, a design language for background applications, is refined by Ant UED Team. It aims to make the interface specifications of internal projects uniform, to lower the cost of design differences and of implementation, and to free the time of designers and front-end developers.

展开与收起

expandable 在最后一行末尾放置按钮;'collapsible' 支持收起。symbol 是按钮内容,可以是文本或 snippet。

bind:expanded
cut
Ant Design, a design language for background applications, is refined by Ant UED Team. It aims to make the interface specifications of internal projects uniform, to lower the cost of design differences and of implementation, and to free the time of designers and front-end developers. Ant Design, a design language for background applications, is refined by Ant UED Team. It aims to make the interface specifications of internal projects uniform, to lower the cost of design differences and of implementation, and to free the time of designers and front-end developers.
rows, with a slider
Ant Design, a design language for background applications, is refined by Ant UED Team. It aims to make the interface specifications of internal projects uniform, to lower the cost of design differences and of implementation, and to free the time of designers and front-end developers. Ant Design, a design language for background applications, is refined by Ant UED Team. It aims to make the interface specifications of internal projects uniform, to lower the cost of design differences and of implementation, and to free the time of designers and front-end developers.

受控

bind:expanded 保存状态;行数可以随时修改,文本会重新截断。

suffix, rows and expandable
To be, or not to be, that is the question: whether it is nobler in the mind to suffer the slings and arrows of outrageous fortune, or to take arms against a sea of troubles, and by opposing end them. To die, to sleep; no more. To be, or not to be, that is the question: whether it is nobler in the mind to suffer the slings and arrows of outrageous fortune, or to take arms against a sea of troubles, and by opposing end them. To die, to sleep; no more. To be, or not to be, that is the question: whether it is nobler in the mind to suffer the slings and arrows of outrageous fortune, or to take arms against a sea of troubles, and by opposing end them. To die, to sleep; no more.
suffix with a text that fits
To be, or not to be, that is the question: whether it is nobler in the mind to suffer the slings and arrows of outrageous fortune, or to take arms against a sea of troubles, and by opposing end them. To die, to sleep; no more.

后缀

suffix 跟在截断文本后面,即使文本完整显示也保留。

actions.placement: 'end'
Buttons after the text.
actions.placement: 'start'
Buttons before the text.
expand, edit and copy together
Ant Design, a design language for background applications, is refined by Ant UED Team. It aims to make the interface specifications of internal projects uniform, to lower the cost of design differences and of implementation, and to free the time of designers and front-end developers.

操作栏

actions.placement 将按钮放在文本之后或之前。顺序为展开、编辑、复制。

shimmer
Thinking...
duration: 3 A slow sweep, then one second of rest
duration: [1, 0] A sweep without a pause

微光

shimmer 在准备内容时让一道光带扫过文字。duration 是运动时间,或运动与停顿时间。

classNames: a class for each part (the CSS is in this example)
Styled by part.
styles: inline styles, custom properties included
Purple buttons, further from the text.
class and style
A note with a bar.

各部分样式

classNames 与 styles 对应根元素、操作栏、每个按钮和编辑框。

API

属性说明类型默认值
type文本颜色。'secondary' | 'success' | 'warning' | 'danger'—
disabled禁用颜色与禁止光标。按钮仍可用;链接会失去 href。booleanfalse
codecode 样式。booleanfalse
mark高亮标记。booleanfalse
underline下划线。booleanfalse
delete删除线。booleanfalse
strong加粗。标题不支持。booleanfalse
italic斜体。booleanfalse
keyboardkbd 样式。仅用于 Text 与 Link。booleanfalse
content作为值的文本,用于编辑、复制和截断,优先于子内容。bind:content 写回编辑后的文本。string—
copyable复制按钮(见下表)。boolean | TypographyCopyablefalse
editable编辑按钮与输入框(见下表)。boolean | TypographyEditablefalse
editing输入框是否打开。bind:editing 双向绑定;普通值为初始状态。booleanfalse
ellipsis截断文本(见下表)。Text 与 Link 只有一行:没有 rows、expandable 和 onexpand。boolean | TypographyEllipsisfalse
expanded截断文本是否展开。bind:expanded 双向绑定。Title 与 Paragraph。booleanfalse
actions操作栏的位置。{ placement?: 'start' | 'end' }{ placement: 'end' }
shimmer动态文字。duration 为秒数,或 [运动, 停顿]。boolean | { duration?: number | [number, number] }false
children文本与行内内容。Snippet—
onclick点击组件(含按钮)。(event: MouseEvent) => void—
classNames各部分的附加类名。Partial<Record<'root' | 'actions' | 'action' | 'textarea', string>>—
styles各部分的内联样式,覆盖组件自身样式。Partial<Record<'root' | 'actions' | 'action' | 'textarea', Record<string, string | number>>>—
class根元素类名。string—
style根元素内联样式。string—
level仅 TypographyTitle:1 到 5 对应 h1 到 h5。1 | 2 | 3 | 4 | 51
native attributes其余属性传给元素:id、data-*、aria-*;链接还支持 href、target、rel。HTMLAttributes—

copyable 对象

属性说明类型默认值
text要复制的文本,否则取 content,再否则取渲染文本。等待 Promise 期间按钮显示加载图标,再次点击无效。耗时较长的 Promise 也能复制;但在普通 HTTP 页面和 127 之前的 Firefox 中,超过几秒的 Promise 会复制失败。string | (() => string | Promise<string>)—
oncopy每次复制成功调用一次,失败时不调用。(event: MouseEvent | KeyboardEvent) => void—
icon替换复制与对勾图标;接收 copied。Snippet<[boolean]>—
tooltips复制前后的提示;false 隐藏。字符串同时作为按钮名称。false | [string | Snippet, string | Snippet]['Copy', 'Copied']
tabIndex按钮的 tabindex。number—

editable 对象

属性说明类型默认值
text输入框的初始文本。string—
maxLength输入框的 maxlength。number—
autoSize输入框随文本增高。boolean | { minRows?: number; maxRows?: number }true
icon替换铅笔图标。Snippet—
tooltip编辑按钮的提示;false 隐藏。字符串同时作为名称。string | Snippet | false'Edit'
enterIcon输入框内的标记;null 移除。Snippet | null—
triggerType开始编辑的方式。没有 'icon' 时没有铅笔;以文字触发时无法聚焦。Array<'icon' | 'text'>['icon']
tabIndex编辑按钮的 tabindex。number—
onstart用户打开输入框。() => void—
onchange编辑完成且文本有变化。(text: string) => void—
onend按 Enter 或离开输入框完成编辑。() => void—
oncancel按 Escape 取消。() => void—

ellipsis 对象

属性说明类型默认值
rows显示行数。number1
expandable展开按钮;'collapsible' 支持收起。boolean | 'collapsible'false
suffix截断文本后的内容,始终显示。string—
symbol按钮内容。字符串为展开文字;snippet 接收 expanded。string | Snippet<[boolean]>'Expand', 'Collapse'
tooltip文本被截断时在提示中显示完整内容。boolean | string | Snippet | TooltipProps—
onexpand用户点击展开或收起。(event: MouseEvent, info: { expanded: boolean }) => void—
onellipsis文本开始或不再被截断。(ellipsis: boolean) => void—
与 Ant Design 的差异:用于编辑和省略的文本通过 content(支持 bind:)提供,editing 与 expanded 是可绑定属性而非对象字段。copyable.icon 是接收 copied 的单个 snippet,classNames 与 styles 只接受对象;format: 'text/html' 与表格样式预留到 v1.1。在 ConfigProvider(v1.1)之前,按钮文字为英文。

语义结构

部分说明
root组件自身元素,装饰在其内部。
actions放置按钮的操作栏。
action每个按钮:复制、编辑、展开、收起。
textarea编辑框的 <textarea>。

设计令牌

令牌说明默认值
--ads-typography-font-size段落与文章的字号。var(--ads-font-size)
--ads-typography-line-height段落与文章的行高。22px
--ads-typography-color文本颜色。var(--ads-color-text)
--ads-typography-paragraph-margin-bottom段落、列表与引用的下间距。1em
--ads-typography-title-weight标题字重。600
--ads-typography-title-margin-top标题上间距(首个标题为零)。1.2em
--ads-typography-title-margin-bottom标题下间距。0.5em
--ads-typography-h1-font-size1 级标题字号。38px
--ads-typography-h1-line-height1 级标题行高。46px
--ads-typography-h2-font-size2 级标题字号。30px
--ads-typography-h2-line-height2 级标题行高。38px
--ads-typography-h3-font-size3 级标题字号。24px
--ads-typography-h3-line-height3 级标题行高。32px
--ads-typography-h4-font-size4 级标题字号。20px
--ads-typography-h4-line-height4 级标题行高。28px
--ads-typography-h5-font-size5 级标题字号。16px
--ads-typography-h5-line-height5 级标题行高。24px
--ads-typography-secondary-colorsecondary 类型颜色。tokens-accessible.css 会加深它。var(--ads-color-text-tertiary)
--ads-typography-success-colorsuccess 类型颜色。tokens-accessible.css 会加深它。#52c41a
--ads-typography-warning-colorwarning 类型颜色。tokens-accessible.css 会加深它。#faad14
--ads-typography-danger-colordanger 类型颜色。tokens-accessible.css 会加深它。var(--ads-color-danger)
--ads-typography-disabled-color禁用文本颜色。var(--ads-color-text-disabled)
--ads-typography-link-color链接颜色;tokens-accessible.css 会加深它。var(--ads-color-link)
--ads-typography-link-hover-color链接悬停颜色。var(--ads-color-link-hover)
--ads-typography-link-active-color链接按下颜色。var(--ads-color-link-active)
--ads-typography-link-decoration链接下划线;深色主题中链接颜色与正文过于接近,因此加下划线。none
--ads-typography-action-color按钮颜色。var(--ads-color-link)
--ads-typography-action-hover-color按钮悬停颜色。var(--ads-color-link-hover)
--ads-typography-action-active-color按钮按下颜色。var(--ads-color-link-active)
--ads-typography-actions-gap文本与按钮的间距。4px
--ads-typography-mark-bg高亮背景。#ffe58f
--ads-typography-mark-color高亮文字颜色。#000000
--ads-typography-code-bgcode 与 pre 背景。rgba(150, 150, 150, 0.1)
--ads-typography-code-bordercode、kbd 与 pre 边框。rgba(100, 100, 100, 0.2)
--ads-typography-kbd-bgkbd 背景。rgba(150, 150, 150, 0.06)
--ads-typography-code-font-familycode、kbd 与 pre 字体。SFMono-Regular, Consolas, 'Liberation Mono', Menlo, Courier, monospace
--ads-typography-blockquote-border引用起始边线。rgba(100, 100, 100, 0.2)
--ads-typography-edit-offset-inline编辑框超出行首的距离。12px
--ads-typography-edit-offset-block编辑框超出行上下的距离。5px
--ads-typography-enter-color输入框内回车标记颜色。var(--ads-color-text-tertiary)
--ads-typography-shimmer-motion微光运动时间(秒)。1
--ads-typography-shimmer-wait微光停顿时间(秒)。1
--ads-typography-shimmer-highlight微光光带颜色。var(--ads-color-bg-container)
--ads-typography-transition链接与按钮的颜色过渡时间。0.3s

无障碍

  • 复制、编辑和展开都是可用 Tab 聚焦的真实按钮,名称分别为 Copy、Edit 和展开按钮的可见文字,并用 aria-expanded 表示是否展开。复制成功由礼貌状态区域播报;失败时静默。
  • 由脚本截断的文本,其完整内容仍保留在页面中供辅助技术读取;可见片段与省略号对其隐藏。
  • 编辑框有可访问名称;Enter、Tab 或点击别处保存文本,Escape 放弃;输入法组合输入时按 Enter 无效。
  • 两点限制:不带铅笔的文字触发编辑,以及带提示的文字,都无法获得焦点。请在 triggerType 中保留 'icon',并用 expandable 提供键盘查看全文的方式。
  • type 只改变颜色:当颜色表达含义时,请再加文字或图标。
  • 默认颜色为 Ant Design 原色,部分未达到 4.5:1;使用 tokens-accessible.css 及深色主题时达标。减少动态效果会停止微光,强制颜色模式下高亮使用系统颜色。
  • 已使用 Orca(Firefox)检查:按钮、复制状态、编辑框、展开状态、完整的截断文本、标题级别和普通链接。Orca 会播报 expanded,但按钮再次收起时不播报(任何带 aria-expanded 的按钮都如此)。未检查:耗时数秒的 Promise 文本复制后的 Copied(测试环境不播报这么晚的变化)、NVDA、JAWS、VoiceOver 和手机屏幕阅读器。