Spin加载中
Spin 用于表示内容正在加载:四个旋转的圆点,可以单独显示,也可以覆盖在等待中的内容或整个页面上。它不可交互,也从不获得焦点。
何时使用
- 当区块或页面在等待数据时使用 Spin。卡片或列表加载时,用它包裹内容;小范围的位置则单独使用。
- 对通常很快的加载,设置
delay以免指示器闪烁。如果知道进度,请使用percent。 fullscreen只用于页面加载,并用description说明正在发生什么,因为屏幕阅读器不会播报其他内容。
代码演示
spinning
A standalone Spin: only the indicator spinning is true
<script lang="ts">
import { Spin, Switch } from 'svelte-ant-design';
let spinning = $state(true);
</script>
<div class="stack">
<div class="field">
<span class="caption">spinning</span>
<Switch bind:checked={spinning} aria-label="Spinning" />
</div>
<div class="field">
<span class="caption">A standalone Spin: only the indicator</span>
<!-- A Spin with no children is just the indicator. Not spinning, it renders nothing; the slot below keeps the height. -->
<div class="slot">
<Spin {spinning} aria-label="Loading the data" />
</div>
<span>spinning is {spinning}</span>
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 20px;
}
.field {
display: grid;
justify-items: start;
gap: 8px;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
.slot {
display: flex;
align-items: center;
min-height: 32px;
}
</style>
size="small" (14 px)
size="medium" (20 px, the default)
size="large" (32 px)
size="default" (an alias of medium)
<script lang="ts">
import { Spin } from 'svelte-ant-design';
</script>
<div class="stack">
<div class="field">
<span class="caption">size="small" (14 px)</span>
<Spin size="small" aria-label="Loading, small" />
</div>
<div class="field">
<span class="caption">size="medium" (20 px, the default)</span>
<Spin size="medium" aria-label="Loading, medium" />
</div>
<div class="field">
<span class="caption">size="large" (32 px)</span>
<Spin size="large" aria-label="Loading, large" />
</div>
<div class="field">
<span class="caption">size="default" (an alias of medium)</span>
<Spin size="default" aria-label="Loading, default" />
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 8px;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
spinning
Spin with children: the content is dimmed and ignores the mouse The button was pressed 0 times
<script lang="ts">
import { Button, Spin, Switch } from 'svelte-ant-design';
let loading = $state(true);
let presses = $state(0);
</script>
<div class="stack">
<div class="field">
<span class="caption">spinning</span>
<Switch bind:checked={loading} aria-label="Spinning" />
</div>
<div class="field">
<span class="caption">Spin with children: the content is dimmed and ignores the mouse</span>
<!--
The content stays reachable with the keyboard: Tab to the button and press Enter while it is dimmed. Something
that must not run twice (a form being sent) has to guard itself.
-->
<Spin spinning={loading} description="Loading the report">
<div class="panel">
<p>The report has three rows.</p>
<Button onclick={() => (presses += 1)}>Press me</Button>
</div>
</Spin>
<span>The button was pressed {presses} times</span>
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 20px;
}
.field {
display: grid;
justify-items: start;
gap: 8px;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
.panel {
box-sizing: border-box;
width: 280px;
height: 190px;
padding: 16px;
border: 1px solid var(--ads-color-border);
border-radius: 8px;
}
p {
margin: 0 0 12px;
}
</style>
description as a string, standalone
Loading
description as a snippet, standalone
Saving (this takes a moment)
description over embedded content
<script lang="ts">
import { Spin } from 'svelte-ant-design';
</script>
<!-- The text of the description is what a screen reader announces, so a Spin with one needs no aria-label. -->
{#snippet detail()}
<strong>Saving</strong> <small>(this takes a moment)</small>
{/snippet}
<div class="stack">
<div class="field">
<span class="caption">description as a string, standalone</span>
<Spin description="Loading" />
</div>
<div class="field">
<span class="caption">description as a snippet, standalone</span>
<Spin description={detail} />
</div>
<div class="field">
<span class="caption">description over embedded content</span>
<Spin description="Loading the table">
<div class="panel">Rows that are being loaded.</div>
</Spin>
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 20px;
}
.field {
display: grid;
justify-items: start;
gap: 8px;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
.panel {
box-sizing: border-box;
width: 280px;
height: 120px;
padding: 16px;
border: 1px solid var(--ads-color-border);
border-radius: 8px;
}
</style>
delay={500}: the indicator waits half a second
A load shorter than the delay never shows the indicator Nothing has been loaded yet
<script lang="ts">
import { Button, Spin } from 'svelte-ant-design';
let loading = $state(false);
let last = $state('Nothing has been loaded yet');
function load(ms: number) {
loading = true;
last = `Loading, it will take ${ms} ms`;
setTimeout(() => {
loading = false;
last = `Loaded in ${ms} ms`;
}, ms);
}
</script>
<div class="stack">
<div class="field">
<span class="caption">delay={500}: the indicator waits half a second</span>
<div class="row">
<Button disabled={loading} onclick={() => load(200)}>Load fast (200 ms)</Button>
<Button disabled={loading} onclick={() => load(2000)}>Load slow (2 s)</Button>
</div>
</div>
<div class="field">
<span class="caption">A load shorter than the delay never shows the indicator</span>
<Spin spinning={loading} delay={500} description="Loading">
<div class="panel">The content of the panel.</div>
</Spin>
<span role="status">{last}</span>
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 20px;
}
.field {
display: grid;
justify-items: start;
gap: 8px;
}
.row {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
.panel {
box-sizing: border-box;
width: 280px;
height: 120px;
padding: 16px;
border: 1px solid var(--ads-color-border);
border-radius: 8px;
}
</style>
indicator: an icon the example turns
Loading
indicator: a large icon
indicator: text
<script lang="ts">
import { Spin } from 'svelte-ant-design';
</script>
<!--
An indicator is drawn in a box of the dot size, and its font size is the dot size, so an icon in `em` scales with
`size`. The Spin does not turn it: this example animates its own icon and keeps still when asked to reduce motion.
-->
{#snippet arc()}
<svg class="arc" viewBox="0 0 24 24" width="1em" height="1em" fill="none" stroke="currentColor" stroke-width="3">
<path d="M12 3a9 9 0 1 0 9 9" stroke-linecap="round" />
</svg>
{/snippet}
{#snippet dots()}
<span class="ellipsis">…</span>
{/snippet}
<div class="stack">
<div class="field">
<span class="caption">indicator: an icon the example turns</span>
<Spin indicator={arc} description="Loading" />
</div>
<div class="field">
<span class="caption">indicator: a large icon</span>
<Spin indicator={arc} size="large" aria-label="Loading, large icon" />
</div>
<div class="field">
<span class="caption">indicator: text</span>
<Spin indicator={dots} aria-label="Loading, text indicator" />
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 20px;
}
.field {
display: grid;
justify-items: start;
gap: 8px;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
.arc {
animation: turn 1s linear infinite;
}
.ellipsis {
font-weight: 700;
}
@keyframes turn {
to {
transform: rotate(360deg);
}
}
@media (prefers-reduced-motion: reduce) {
.arc {
animation-duration: 3s;
}
}
</style>
percent: a number from 0 to 100
Uploading
percent="auto": an indeterminate ring, no value
Preparing
percent with size="large"
<script lang="ts">
import { Spin } from 'svelte-ant-design';
let percent = $state(35);
</script>
<div class="stack">
<div class="field">
<span class="caption">percent: a number from 0 to 100</span>
<label class="row">
<input type="range" min="0" max="100" bind:value={percent} />
<span>{percent}%</span>
</label>
<!-- The ring is a progressbar; the description is its name. -->
<Spin {percent} description="Uploading" />
</div>
<div class="field">
<span class="caption">percent="auto": an indeterminate ring, no value</span>
<Spin percent="auto" description="Preparing" />
</div>
<div class="field">
<span class="caption">percent with size="large"</span>
<Spin percent={70} size="large" aria-label="Upload progress" />
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 20px;
}
.field {
display: grid;
justify-items: start;
gap: 8px;
}
.row {
display: flex;
align-items: center;
gap: 8px;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
fullscreen: a dimmed backdrop over the viewport, for three seconds spinning is false
<script lang="ts">
import { Button, Spin } from 'svelte-ant-design';
let loading = $state(false);
function show() {
loading = true;
setTimeout(() => (loading = false), 3000);
}
</script>
<div class="stack">
<div class="field">
<span class="caption">fullscreen: a dimmed backdrop over the viewport, for three seconds</span>
<!--
The backdrop is drawn where the component is (position: fixed), so put it near the root of the application, not
inside an element with a transform. It has no focus trap: Tab still reaches the page behind it.
-->
<Button disabled={loading} onclick={show}>Show fullscreen</Button>
<span>spinning is {loading}</span>
</div>
</div>
<Spin fullscreen spinning={loading} description="Loading the page" />
<style>
.stack {
display: grid;
justify-items: start;
gap: 20px;
}
.field {
display: grid;
justify-items: start;
gap: 8px;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
classNames: a class for each part (the CSS is in this example)
Loading
styles: inline styles, custom properties included
Loading
class and style go to the root; container is the covered content
<script lang="ts">
import { Spin } from 'svelte-ant-design';
</script>
<div class="stack">
<div class="field">
<span class="caption">classNames: a class for each part (the CSS is in this example)</span>
<Spin
description="Loading"
classNames={{ indicator: 'demo-indicator', description: 'demo-description' }}
/>
</div>
<div class="field">
<span class="caption">styles: inline styles, custom properties included</span>
<Spin
description="Loading"
styles={{
root: { '--ads-spin-color': '#52c41a' },
description: { 'font-weight': 700, 'letter-spacing': '1px' }
}}
/>
</div>
<div class="field">
<span class="caption">class and style go to the root; container is the covered content</span>
<Spin
description="Loading"
class="demo-root"
style="border-radius: 8px"
classNames={{ container: 'demo-container' }}
styles={{ section: { 'gap': '4px' } }}
>
<div class="panel">Covered content</div>
</Spin>
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 20px;
}
.field {
display: grid;
justify-items: start;
gap: 8px;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
.panel {
box-sizing: border-box;
width: 280px;
height: 120px;
padding: 16px;
border: 1px solid var(--ads-color-border);
border-radius: 8px;
}
:global(.demo-indicator) {
color: #fa541c;
}
:global(.demo-description) {
color: #fa541c;
font-style: italic;
}
:global(.demo-root) {
background: var(--ads-color-bg-container);
}
:global(.demo-container) {
border-radius: 8px;
}
</style>
API
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| spinning | Spin 是否处于激活状态。spinning={undefined} 视为默认值 true。独立使用时,不旋转(或仍在延迟中)不渲染任何内容;内嵌时按原样显示内容。 | boolean | true |
| delay | 每次 spinning 变为 true(包括挂载时)后,等待多少毫秒再显示指示器。关闭 spinning 会取消等待并立即隐藏;重复设为 true 不会启动第二个计时器。负数或非有限数在开发环境抛出 RangeError,生产环境视为无延迟。 | number | — |
| size | 圆点方框的尺寸:14、20 或 32 px。default 为 medium 的兼容别名。其他值在开发环境抛出 RangeError,生产环境回退为 medium。 | 'small' | 'medium' | 'large' | 'default' | 'medium' |
| description | 指示器下方的文字或内容。字符串按文本处理,从不作为 HTML;空字符串视为未提供。它是屏幕阅读器播报的状态信息,也是进度环的名称。 | string | Snippet | — |
| indicator | 替换默认的圆点。绘制在与圆点同尺寸的方框中,字体大小相同,颜色为 Spin 的颜色。组件不会让它旋转。对辅助技术隐藏。 | Snippet | — |
| percent | 用进度环替换圆点。数字会被限制在 0 到 100;'auto' 是不确定进度的旋转环,没有 aria-valuenow,也不使用计时器。不是有效数字的值或其他类型在开发环境抛出 RangeError,生产环境被忽略。自定义 indicator 优先。 | number | 'auto' | — |
| fullscreen | 覆盖整个视口的半透明遮罩,指示器居中。就地绘制,使用 position: fixed,所以只有在祖先元素没有 transform、filter 或 contain 时才相对于视口。它会阻止指针操作,没有焦点陷阱,也不锁定滚动。淡入出现,消失时立即移除。 | boolean | false |
| children | 内嵌模式:Spin 所覆盖的内容。旋转时内容半透明,不响应指针与文本选择,并带有 aria-busy。它仍可聚焦、可阅读。与 fullscreen 同时使用时按原样绘制,不会变暗。 | Snippet | — |
| classNames | 为 root、section(指示器与描述合在一起)、indicator、description 和 container(被覆盖的内容,仅内嵌模式)追加的类名,附加在组件自带类名之后。独立使用的 Spin 中,root 与 section 是同一个元素。 | Partial<Record<'root' | 'section' | 'indicator' | 'description' | 'container', string>> | — |
| styles | 这些部分的内联样式。属性名使用 kebab-case 或自定义属性,数字原样输出(不会自动添加 px),优先于组件自身样式。 | Partial<Record<'root' | 'section' | 'indicator' | 'description' | 'container', Record<string, string | number>>> | — |
| class | 根元素附加的 CSS 类名。 | string | — |
| style | 根元素的内联样式;styles.root 写在其后。 | string | — |
| native attributes | 根元素 <div> 的原生属性(id、title、data-*、aria-*、事件处理器)会透传到元素上。aria-label 与 aria-labelledby 会加到状态元素上(内嵌模式下是 section),为没有描述的 Spin 命名。role 由组件管理。 | HTMLAttributes<HTMLDivElement> | — |
与 Ant Design 的差异:
description 取代已弃用的 tip,classNames.root 取代 wrapperClassName,size="default" 只是 medium 的别名。percent="auto" 是旋转的不确定进度环,而不是模拟出来的进度。indicator 是 snippet,classNames 与 styles 接受对象而不是函数。没有 Spin.setDefaultIndicator:默认指示器与默认尺寸预留给 v1.1 的 ConfigProvider。语义结构
| 部分 | 说明 |
|---|---|
| root | 最外层元素。独立使用与全屏时,它本身就是状态元素。 |
| section | 指示器与描述合在一起。独立使用时与 root 是同一元素;内嵌或全屏时是单独的元素,居中显示在内容上方。 |
| indicator | 圆点、进度环或自定义指示器所在的方框。 |
| description | 指示器下方的文字。 |
| container | 被覆盖的内容。仅内嵌模式(以及 fullscreen 与 children 同时使用时)。 |
设计令牌
| 令牌 | 说明 | 默认值 |
|---|---|---|
| --ads-spin-dot-size | 中号 Spin 的圆点方框。 | 20px |
| --ads-spin-dot-size-sm | 小号 Spin 的圆点方框。 | 14px |
| --ads-spin-dot-size-lg | 大号 Spin 的圆点方框。 | 32px |
| --ads-spin-gap | 指示器与描述之间的间距。 | 12px |
| --ads-spin-color | 圆点、进度环与自定义指示器的颜色。 | #1677ff |
| --ads-spin-description-color | 描述文字的颜色。tokens-accessible.css 将其加深为 #1668dc,以保证作为文字 4.5:1 的对比度。 | #1677ff |
| --ads-spin-container-opacity | 旋转时被覆盖内容的不透明度。 | 0.5 |
| --ads-spin-duration | 圆点或不确定进度环旋转一圈的时间。启用 prefers-reduced-motion 时延长为 2.5 倍。 | 1.2s |
| --ads-spin-pulse-duration | 单个圆点一次脉动的时间。启用 prefers-reduced-motion 时延长为 2.5 倍。 | 1s |
| --ads-spin-mask-bg | 全屏 Spin 的遮罩背景。tokens-accessible.css 设为 60% 黑色,使白色文字在白色页面上保持 4.5:1 的对比度。 | rgba(0, 0, 0, 0.45) |
| --ads-spin-fullscreen-color | 遮罩上指示器与描述的颜色。 | #ffffff |
| --ads-spin-fullscreen-z-index | 遮罩所在的层级(Ant Design 弹出层的基础层级)。 | 1000 |
| --ads-spin-ring-track | 进度环的轨道颜色。 | rgba(0, 0, 0, 0.06) |
无障碍
- 圆点、进度环的图形以及自定义指示器都对辅助技术隐藏。屏幕阅读器播报的是状态元素:带有
description文字的role="status"(礼貌的实时区域)。 - 没有描述的 Spin 不会发出任何声音。请用
aria-label或aria-labelledby为它命名;开发环境中,正在旋转却两者都没有的 Spin 会输出一次警告。 - 内嵌模式下,旋转期间被覆盖的内容带有
aria-busy="true"。它不会变成inert,也不会被隐藏:鼠标与文本选择被阻止,键盘和屏幕阅读器则不会,所以不能执行两次的操作需要自行防护。 - 进度环是带有
aria-valuenow的role="progressbar",名称取自描述或aria-label。不确定进度的环('auto')没有数值。 - 全屏遮罩会阻止指针操作,但没有焦点陷阱,也不锁定滚动,所以按 Tab 仍然可以到达它背后的页面。
- 默认颜色是 Ant Design 自己的颜色。使用
tokens-accessible.css时以及在深色主题中,圆点和进度环与页面背景保持 3:1,描述文字保持 4.5:1,全屏遮罩上的白色文字也保持 4.5:1。prefers-reduced-motion会减慢旋转而不是停止它;在 Windows 高对比度等强制颜色模式下,圆点与进度环使用系统颜色绘制。 - 已用真实读屏软件(Orca 搭配 Firefox)验证:它能把描述读作状态信息,读出由
aria-label命名的 Spin、嵌入式 Spin 所覆盖的内容、带名称和百分比数值的进度环(不确定时不带数值),以及不会隐藏背后页面的全屏 Spin。未验证:读屏软件在状态出现时是否会播报(我们的 Orca 环境不会朗读被插入页面的 live 区域)、NVDA、JAWS 和 VoiceOver。