Typography排版
用于文章与界面的文本:标题、段落、行内文本与链接,支持样式、复制按钮、原地编辑和长文本省略。
何时使用
- 用于风格统一、易读的文本:文章、笔记或博客。
- 对需要复制或修改的短内容(如编号、名称)使用
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.
<script lang="ts">
import { Typography, TypographyLink, TypographyParagraph, TypographyText, TypographyTitle } from 'svelte-ant-design';
</script>
<div class="stack">
<div class="field">
<Typography>
<TypographyTitle level={3}>A design system</TypographyTitle>
<TypographyParagraph>
Many projects need the same design rules and the same parts. A design system keeps them in one place, so the work does not
<TypographyText strong>start again</TypographyText> in every application.
</TypographyParagraph>
<TypographyTitle level={4}>How to start</TypographyTitle>
<TypographyParagraph>
Run <TypographyText code>npm install svelte-ant-design</TypographyText> and press <TypographyText keyboard>Enter</TypographyText>.
</TypographyParagraph>
<TypographyParagraph>
<TypographyText mark>Decide first</TypographyText>, then build. The <TypographyLink href="#api">API table</TypographyLink> lists
every property.
</TypographyParagraph>
</Typography>
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 20px;
width: 100%;
}
.field {
display: grid;
justify-items: start;
gap: 8px;
width: 100%;
}
</style>
Release notes
Read the table of properties before you start.
- Run
npm installand press Enter - Decide first, then build
Consistency is a feature that every page gets for free.
npm install svelte-ant-design
<script lang="ts">
import { Typography } from 'svelte-ant-design';
// HTML that comes ready, here a constant. From Markdown or a CMS, pass only what you trust or have sanitized.
const ready = `
<h4>Release notes</h4>
<p>Read the <a href="#api">table of properties</a> before you start.</p>
<ul>
<li>Run <code>npm install</code> and press <kbd>Enter</kbd></li>
<li><mark>Decide first</mark>, then <strong>build</strong></li>
</ul>
<blockquote>Consistency is a feature that every page gets for free.</blockquote>
<pre>npm install svelte-ant-design</pre>
`;
</script>
<div class="stack">
<div class="field">
<Typography>
<!-- eslint-disable-next-line svelte/no-at-html-tags -->
{@html ready}
</Typography>
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 20px;
width: 100%;
}
/* The article takes the whole width, so a block such as `pre` is as wide as in a real page. */
.field {
display: grid;
justify-items: stretch;
gap: 8px;
width: 100%;
}
</style>
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
<script lang="ts">
import { TypographyTitle } from 'svelte-ant-design';
</script>
<div class="stack">
<div class="field">
<span class="caption">level 2 to 5 (level 1 is the default and is the h1 of a page)</span>
<div class="titles">
<TypographyTitle level={2}>h2. Ant Design</TypographyTitle>
<TypographyTitle level={3}>h3. Ant Design</TypographyTitle>
<TypographyTitle level={4}>h4. Ant Design</TypographyTitle>
<TypographyTitle level={5}>h5. Ant Design</TypographyTitle>
</div>
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 20px;
--ads-typography-paragraph-margin-bottom: 0;
}
.field {
display: grid;
justify-items: start;
gap: 8px;
width: 100%;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
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 (delete) Ant Design (strong) Ant Design (italic)
Ant Design (code) Ant Design (keyboard) Ant Design (underline) combined
Not for production<script lang="ts">
import { TypographyText } from 'svelte-ant-design';
</script>
<div class="stack">
<div class="field">
<span class="caption">type</span>
<TypographyText>Ant Design (default)</TypographyText>
<TypographyText type="secondary">Ant Design (secondary)</TypographyText>
<TypographyText type="success">Ant Design (success)</TypographyText>
<TypographyText type="warning">Ant Design (warning)</TypographyText>
<TypographyText type="danger">Ant Design (danger)</TypographyText>
<TypographyText disabled>Ant Design (disabled)</TypographyText>
</div>
<div class="field">
<span class="caption">decorations</span>
<TypographyText mark>Ant Design (mark)</TypographyText>
<TypographyText code>Ant Design (code)</TypographyText>
<TypographyText keyboard>Ant Design (keyboard)</TypographyText>
<TypographyText underline>Ant Design (underline)</TypographyText>
<TypographyText delete>Ant Design (delete)</TypographyText>
<TypographyText strong>Ant Design (strong)</TypographyText>
<TypographyText italic>Ant Design (italic)</TypographyText>
</div>
<div class="field">
<span class="caption">combined</span>
<TypographyText strong code type="danger">Not for production</TypographyText>
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 20px;
--ads-typography-paragraph-margin-bottom: 0;
}
.field {
display: grid;
justify-items: start;
gap: 8px;
width: 100%;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
href, target svelte.dev
type A dangerous link
disabled (the copy button still works) Not available
a plain <a> inside a part
Read the table of properties first.
<script lang="ts">
import { TypographyLink, TypographyParagraph } from 'svelte-ant-design';
let clicks = $state(0);
</script>
<div class="stack">
<div class="field">
<span class="caption">href, target</span>
<TypographyLink href="https://svelte.dev" target="_blank">svelte.dev</TypographyLink>
</div>
<div class="field">
<span class="caption">type</span>
<TypographyLink href="#api" type="danger">A dangerous link</TypographyLink>
</div>
<div class="field">
<span class="caption">disabled (the copy button still works)</span>
<TypographyLink href="#api" disabled copyable>Not available</TypographyLink>
</div>
<div class="field">
<span class="caption">a plain <a> inside a part</span>
<TypographyParagraph>Read the <a href="#api">table of properties</a> first.</TypographyParagraph>
</div>
<div class="field">
<span class="caption">onclick</span>
<TypographyLink href="#api" onclick={() => (clicks += 1)}>Count the clicks</TypographyLink>
<span role="status">Clicked {clicks} times</span>
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 20px;
--ads-typography-paragraph-margin-bottom: 0;
}
.field {
display: grid;
justify-items: start;
gap: 8px;
width: 100%;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
copyable
This is a copyable text.
Nothing copied yettext: 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 yeticon, tooltips
Own icon and tips.
tooltips: false
No tip, the button keeps its name.
a Text with only the button
<script lang="ts">
import { TypographyParagraph, TypographyText } from 'svelte-ant-design';
import { SmileFilled, SmileOutlined } from 'svelte-ant-design/icons';
let copied = $state('Nothing copied yet');
let requested = $state('Not requested yet');
async function fetchText(): Promise<string> {
await new Promise((resolve) => setTimeout(resolve, 1000));
requested = 'The text was requested';
return 'Text from the server';
}
</script>
<div class="stack">
<div class="field">
<span class="caption">copyable</span>
<TypographyParagraph copyable={{ oncopy: () => (copied = 'The sentence was copied') }}>This is a copyable text.</TypographyParagraph>
<span role="status">{copied}</span>
</div>
<div class="field">
<span class="caption">text: the copy is not the shown text</span>
<TypographyParagraph copyable={{ text: 'Hello, clipboard!' }}>Copy something else.</TypographyParagraph>
</div>
<div class="field">
<span class="caption">text: a function that returns a promise</span>
<TypographyParagraph copyable={{ text: fetchText }}>Ask the server for the text.</TypographyParagraph>
<span role="status">{requested}</span>
</div>
<div class="field">
<span class="caption">icon, tooltips</span>
<TypographyParagraph copyable={{ icon, tooltips: ['Copy the greeting', 'Done'] }}>Own icon and tips.</TypographyParagraph>
</div>
<div class="field">
<span class="caption">tooltips: false</span>
<TypographyParagraph copyable={{ tooltips: false }}>No tip, the button keeps its name.</TypographyParagraph>
</div>
<div class="field">
<span class="caption">a Text with only the button</span>
<TypographyText copyable={{ text: 'icon only' }} content="" />
</div>
{#snippet icon(done: boolean)}
{#if done}<SmileFilled label="" />{:else}<SmileOutlined label="" />{/if}
{/snippet}
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 20px;
--ads-typography-paragraph-margin-bottom: 0;
}
.field {
display: grid;
justify-items: start;
gap: 8px;
width: 100%;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
bind:content, with the events
This is an editable text.
No events yeticon, 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.
<script lang="ts">
import { Button, TypographyParagraph, TypographyText } from 'svelte-ant-design';
import { HighlightOutlined } from 'svelte-ant-design/icons';
let plain = $state('This is an editable text.');
let custom = $state('Custom icon and tip.');
let byText = $state('Click the text or the icon.');
let limited = $state('No more than 20 chars');
let held = $state('Open me from outside.');
let holding = $state(false);
let events = $state('No events yet');
</script>
<div class="stack">
<div class="field">
<span class="caption">bind:content, with the events</span>
<TypographyParagraph
bind:content={plain}
editable={{
onstart: () => (events = 'started'),
onchange: (text) => (events = `changed to "${text}"`),
onend: () => (events += ', ended'),
oncancel: () => (events = 'cancelled')
}}
/>
<span role="status">{events}</span>
</div>
<div class="field">
<span class="caption">icon, tooltip</span>
<TypographyParagraph bind:content={custom} editable={{ icon: pencil, tooltip: 'Click to edit' }} />
</div>
<div class="field">
<span class="caption">triggerType: ['icon', 'text']</span>
<TypographyParagraph bind:content={byText} editable={{ triggerType: ['icon', 'text'] }} />
</div>
<div class="field">
<span class="caption">maxLength, autoSize, no enter mark</span>
<TypographyParagraph
bind:content={limited}
editable={{ maxLength: 20, autoSize: { maxRows: 3 }, enterIcon: null }}
/>
</div>
<div class="field">
<span class="caption">bind:editing</span>
<div class="row">
<Button onclick={() => (holding = true)}>Edit the sentence</Button>
<TypographyText type="secondary">{holding ? 'editing' : 'reading'}</TypographyText>
</div>
<TypographyParagraph bind:content={held} bind:editing={holding} editable={{ triggerType: [] }} />
</div>
{#snippet pencil()}<HighlightOutlined label="" />{/snippet}
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 20px;
--ads-typography-paragraph-margin-bottom: 0;
}
.field {
display: grid;
justify-items: start;
gap: 8px;
width: 100%;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
.row {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
}
</style>
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 wholea 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.
<script lang="ts">
import { TypographyParagraph, TypographyText } from 'svelte-ant-design';
const long =
'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.';
let cut = $state(false);
</script>
<div class="stack">
<div class="field">
<span class="caption">one row, with a tooltip</span>
<TypographyParagraph ellipsis={{ tooltip: true }}>{long}</TypographyParagraph>
</div>
<div class="field">
<span class="caption">rows={3}</span>
<TypographyParagraph ellipsis={{ rows: 3 }}>{long}</TypographyParagraph>
</div>
<div class="field">
<span class="caption">onellipsis</span>
<TypographyParagraph ellipsis={{ rows: 2, onellipsis: (value) => (cut = value) }}>{long}</TypographyParagraph>
<span role="status">The text is {cut ? 'cut' : 'shown whole'}</span>
</div>
<div class="field">
<span class="caption">a Text in a narrow box</span>
<div class="narrow">
<TypographyText ellipsis={{ tooltip: true }}>{long}</TypographyText>
</div>
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 20px;
--ads-typography-paragraph-margin-bottom: 0;
}
.field {
display: grid;
justify-items: start;
gap: 8px;
width: 100%;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
.narrow {
width: 200px;
max-width: 100%;
}
</style>
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. 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. 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 yetsymbol 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. 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.
<script lang="ts">
import { TypographyParagraph } from 'svelte-ant-design';
const long =
'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.';
let note = $state('Nothing pressed yet');
</script>
<div class="stack">
<div class="field">
<span class="caption">expandable</span>
<TypographyParagraph ellipsis={{ rows: 2, expandable: true, symbol: 'more' }}>{long}</TypographyParagraph>
</div>
<div class="field">
<span class="caption">expandable: 'collapsible', onexpand</span>
<TypographyParagraph
ellipsis={{
rows: 2,
expandable: 'collapsible',
onexpand: (_event, { expanded }) => (note = expanded ? 'Expanded' : 'Collapsed')
}}
>
{long}
</TypographyParagraph>
<span role="status">{note}</span>
</div>
<div class="field">
<span class="caption">symbol as a snippet</span>
<TypographyParagraph ellipsis={{ rows: 2, expandable: 'collapsible', symbol }}>{long}</TypographyParagraph>
</div>
{#snippet symbol(expanded: boolean)}{expanded ? 'Show less' : 'Read on'}{/snippet}
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 20px;
--ads-typography-paragraph-margin-bottom: 0;
}
.field {
display: grid;
justify-items: start;
gap: 8px;
width: 100%;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
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. 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. 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.
<script lang="ts">
import { Switch, TypographyParagraph } from 'svelte-ant-design';
const long =
'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.';
let open = $state(false);
let rows = $state(2);
</script>
<div class="stack">
<div class="field">
<span class="caption">bind:expanded</span>
<div class="row">
<Switch bind:checked={open} aria-label="Show the whole text" />
<span>{open ? 'expanded' : 'cut'}</span>
</div>
<TypographyParagraph bind:expanded={open} ellipsis={{ rows: 2, expandable: 'collapsible' }}>{long}</TypographyParagraph>
</div>
<div class="field">
<span class="caption">rows, with a slider</span>
<label class="row">
Rows: {rows}
<input type="range" min="1" max="6" bind:value={rows} />
</label>
<TypographyParagraph ellipsis={{ rows, expandable: true }}>{long}</TypographyParagraph>
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 20px;
--ads-typography-paragraph-margin-bottom: 0;
}
.field {
display: grid;
justify-items: start;
gap: 8px;
width: 100%;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
.row {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
}
</style>
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. 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.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.
<script lang="ts">
import { TypographyParagraph } from 'svelte-ant-design';
const quote =
'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.';
// Long enough to be cut at any width.
const speech = [quote, quote, quote].join(' ');
</script>
<div class="stack">
<div class="field">
<span class="caption">suffix, rows and expandable</span>
<TypographyParagraph ellipsis={{ rows: 2, expandable: 'collapsible', suffix: '--William Shakespeare' }}>{speech}</TypographyParagraph>
</div>
<div class="field">
<span class="caption">suffix with a text that fits</span>
<TypographyParagraph ellipsis={{ rows: 6, suffix: '--William Shakespeare' }}>{quote}</TypographyParagraph>
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 20px;
--ads-typography-paragraph-margin-bottom: 0;
}
.field {
display: grid;
justify-items: start;
gap: 8px;
width: 100%;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
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.
<script lang="ts">
import { TypographyParagraph } from 'svelte-ant-design';
let text = $state('Buttons after the text.');
let other = $state('Buttons before the text.');
const long =
'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.';
</script>
<div class="stack">
<div class="field">
<span class="caption">actions.placement: 'end'</span>
<TypographyParagraph bind:content={text} editable copyable />
</div>
<div class="field">
<span class="caption">actions.placement: 'start'</span>
<TypographyParagraph bind:content={other} editable copyable actions={{ placement: 'start' }} />
</div>
<div class="field">
<span class="caption">expand, edit and copy together</span>
<TypographyParagraph content={long} editable copyable ellipsis={{ rows: 2, expandable: true }} />
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 20px;
--ads-typography-paragraph-margin-bottom: 0;
}
.field {
display: grid;
justify-items: start;
gap: 8px;
width: 100%;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
shimmer
Thinking...
duration: 3 A slow sweep, then one second of rest
duration: [1, 0] A sweep without a pause
<script lang="ts">
import { Switch, TypographyText } from 'svelte-ant-design';
let thinking = $state(true);
</script>
<div class="stack">
<div class="field">
<span class="caption">shimmer</span>
<div class="row">
<Switch bind:checked={thinking} aria-label="Shimmer" />
<TypographyText shimmer={thinking}>{thinking ? 'Thinking...' : 'Done.'}</TypographyText>
</div>
</div>
<div class="field">
<span class="caption">duration: 3</span>
<TypographyText shimmer={{ duration: 3 }}>A slow sweep, then one second of rest</TypographyText>
</div>
<div class="field">
<span class="caption">duration: [1, 0]</span>
<TypographyText shimmer={{ duration: [1, 0] }}>A sweep without a pause</TypographyText>
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 20px;
--ads-typography-paragraph-margin-bottom: 0;
}
.field {
display: grid;
justify-items: start;
gap: 8px;
width: 100%;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
.row {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
}
</style>
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.
<script lang="ts">
import { TypographyParagraph } from 'svelte-ant-design';
let text = $state('Styled by part.');
</script>
<div class="stack">
<div class="field">
<span class="caption">classNames: a class for each part (the CSS is in this example)</span>
<TypographyParagraph
bind:content={text}
copyable
editable
classNames={{ root: 'demo-root', actions: 'demo-actions', action: 'demo-action', textarea: 'demo-textarea' }}
/>
</div>
<div class="field">
<span class="caption">styles: inline styles, custom properties included</span>
<TypographyParagraph
copyable
styles={{
root: { '--ads-typography-action-color': '#722ed1' },
actions: { 'margin-inline-start': '12px' }
}}
>
Purple buttons, further from the text.
</TypographyParagraph>
</div>
<div class="field">
<span class="caption">class and style</span>
<TypographyParagraph class="demo-note" style="padding-inline-start: 8px">A note with a bar.</TypographyParagraph>
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 20px;
--ads-typography-paragraph-margin-bottom: 0;
}
.field {
display: grid;
justify-items: start;
gap: 8px;
width: 100%;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
:global(.demo-root) {
font-weight: 600;
}
:global(.demo-actions) {
border-inline-start: 1px solid var(--ads-color-border);
padding-inline-start: 4px;
}
:global(.demo-action) {
transform: scale(1.15);
}
:global(.demo-textarea) {
font-style: italic;
}
:global(.demo-note) {
border-inline-start: 3px solid var(--ads-color-primary);
}
</style>
API
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| type | 文本颜色。 | 'secondary' | 'success' | 'warning' | 'danger' | — |
| disabled | 禁用颜色与禁止光标。按钮仍可用;链接会失去 href。 | boolean | false |
| code | code 样式。 | boolean | false |
| mark | 高亮标记。 | boolean | false |
| underline | 下划线。 | boolean | false |
| delete | 删除线。 | boolean | false |
| strong | 加粗。标题不支持。 | boolean | false |
| italic | 斜体。 | boolean | false |
| keyboard | kbd 样式。仅用于 Text 与 Link。 | boolean | false |
| content | 作为值的文本,用于编辑、复制和截断,优先于子内容。bind:content 写回编辑后的文本。 | string | — |
| copyable | 复制按钮(见下表)。 | boolean | TypographyCopyable | false |
| editable | 编辑按钮与输入框(见下表)。 | boolean | TypographyEditable | false |
| editing | 输入框是否打开。bind:editing 双向绑定;普通值为初始状态。 | boolean | false |
| ellipsis | 截断文本(见下表)。Text 与 Link 只有一行:没有 rows、expandable 和 onexpand。 | boolean | TypographyEllipsis | false |
| expanded | 截断文本是否展开。bind:expanded 双向绑定。Title 与 Paragraph。 | boolean | false |
| 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 | 5 | 1 |
| 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 | 显示行数。 | number | 1 |
| 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-size | 1 级标题字号。 | 38px |
| --ads-typography-h1-line-height | 1 级标题行高。 | 46px |
| --ads-typography-h2-font-size | 2 级标题字号。 | 30px |
| --ads-typography-h2-line-height | 2 级标题行高。 | 38px |
| --ads-typography-h3-font-size | 3 级标题字号。 | 24px |
| --ads-typography-h3-line-height | 3 级标题行高。 | 32px |
| --ads-typography-h4-font-size | 4 级标题字号。 | 20px |
| --ads-typography-h4-line-height | 4 级标题行高。 | 28px |
| --ads-typography-h5-font-size | 5 级标题字号。 | 16px |
| --ads-typography-h5-line-height | 5 级标题行高。 | 24px |
| --ads-typography-secondary-color | secondary 类型颜色。tokens-accessible.css 会加深它。 | var(--ads-color-text-tertiary) |
| --ads-typography-success-color | success 类型颜色。tokens-accessible.css 会加深它。 | #52c41a |
| --ads-typography-warning-color | warning 类型颜色。tokens-accessible.css 会加深它。 | #faad14 |
| --ads-typography-danger-color | danger 类型颜色。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-bg | code 与 pre 背景。 | rgba(150, 150, 150, 0.1) |
| --ads-typography-code-border | code、kbd 与 pre 边框。 | rgba(100, 100, 100, 0.2) |
| --ads-typography-kbd-bg | kbd 背景。 | rgba(150, 150, 150, 0.06) |
| --ads-typography-code-font-family | code、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 和手机屏幕阅读器。