Demos
How to import
<script lang="ts">
import { BackTop } from '@fibrae/semi-ui-svelte';
</script>
Basic Usage
BackTop is a FloatButton that appears once target scrolls past visibilityHeight and scrolls it back to the top when clicked. It shows a back-to-top icon by default, and its accessible name comes from the locale by default ('Back to top' in English), which aria-label can override.
Code
<script lang="ts">
import { BackTop } from '@fibrae/semi-ui-svelte';
</script>
<div>
<span>Scroll down to see the bottom-right gray button.</span>
<BackTop aria-label="Back to top" />
</div>Customized Style
BackTop sits 50px from the bottom and 100px from the right (the left in RTL) by default, which style can override.
Its look is customized with the icon snippet and the FloatButton props shape, size, colorful and badge; style only accepts a string.
Code
<script lang="ts">
import { IconArrowUp } from '@fibrae/semi-icons-svelte';
import { BackTop } from '@fibrae/semi-ui-svelte';
</script>
<div>
<span>向下滚动,右下角会出现一个方形的多彩按钮。</span>
<BackTop aria-label="回到顶部" colorful shape="square" size="large" style="bottom: 100px">
{#snippet icon()}
<IconArrowUp />
{/snippet}
</BackTop>
</div>API Reference
Props
| Property | Description | Type | Default |
|---|---|---|---|
| badge | Badge props, same as FloatButton. | FloatButtonBadge | - |
| badgeCount | Snippet for custom badge content, in place of badge.count. | Snippet | - |
| children | Static content of the button, shown in place of the icon. Must not contain interactive elements. | Snippet | - |
| colorful | Colorful button, same as FloatButton. Falls back to the FloatButton default when unset. | boolean | - |
| duration | Milliseconds the scroll to the top takes. onclick is throttled by this interval. | number | 450 |
| icon | Snippet for the button icon. Defaults to IconBackTop. | Snippet | - |
| onclick | Called on click with the native click event. Throttled by duration, and called once the scroll has started. | TypeMouseEventHandler<T> | null | - |
| shape | Shape: round or square. Falls back to the FloatButton default when unset. | FloatButtonShape | - |
| size | Size: default, small or large. Falls back to the FloatButton default when unset. | FloatButtonSize | - |
| target | Function returning the scrolled element, the window when unset. Returning null means the node is not available yet. Called on mount and again when the function itself changes. | Type() => Window | HTMLElement | null | Type() => window |
| visibilityHeight | Scroll distance after which the BackTop appears. | number | 400 |
class, style (a string) and native attributes (such as data-* and aria-*) land on the button. The click callback is the native onclick, throttled by duration, and overrideDefaultProps.BackTop cannot set it. children, icon and badgeCount are snippets.
target is called once after mount, when the element reference of a parent container is already readable; a stable target function that returns the element only after mount needs BackTop remounted.
Accessibility
ARIA
- BackTop renders a single native
<button type="button">, which screen readers announce as a button - The accessible name comes from
aria-label; without it, it comes from the button content when content is passed, and otherwise from the locale's default name ('Back to top' in English) children(the default slot in Vue, a snippet in Svelte) is static content of the button and becomes part of its name; the button must not contain interactive elements
Keyboard and focus
- Once visible, the button is reached with Tab and Shift + Tab, and Enter or Space scrolls to the top
- A focus outline is shown on keyboard focus
Design Tokens
| Sass variable | Value | Usage |
|---|---|---|
| $spacing-backtop-right | 100px | 返回顶部距离右侧距离 |
| $spacing-backtop-bottom | 50px | 返回顶部距离底侧距离 |