@lumen-design/backtop 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,60 @@
1
+ <script lang="ts">
2
+ import type { Snippet } from 'svelte'
3
+ import { onMount, onDestroy } from 'svelte'
4
+ import Icon from '@lumen-design/icon'
5
+ import { ArrowUp } from 'lucide'
6
+
7
+ interface BacktopProps {
8
+ visibilityHeight?: number
9
+ right?: number
10
+ bottom?: number
11
+ target?: string
12
+ onclick?: () => void
13
+ children?: Snippet
14
+ class?: string
15
+ }
16
+
17
+ let { visibilityHeight = 200, right = 40, bottom = 40, target, onclick, children, class: cls = '', ...attrs }: BacktopProps = $props()
18
+
19
+ let visible = $state(false)
20
+ let container: HTMLElement | Window = $state(window)
21
+
22
+ const handleScroll = (): void => {
23
+ const scrollTop = container === window ? document.documentElement.scrollTop || document.body.scrollTop : (container as HTMLElement).scrollTop
24
+ visible = scrollTop >= visibilityHeight
25
+ }
26
+
27
+ const scrollToTop = (): void => {
28
+ const el = container === window ? document.documentElement : (container as HTMLElement)
29
+ el.scrollTo({ top: 0, behavior: 'smooth' })
30
+ onclick?.()
31
+ }
32
+
33
+ onMount(() => {
34
+ if (target) {
35
+ const el = document.querySelector(target)
36
+ if (el) container = el as HTMLElement
37
+ }
38
+ const scrollEl = container === window ? window : container
39
+ scrollEl.addEventListener('scroll', handleScroll, { passive: true })
40
+ handleScroll()
41
+ })
42
+
43
+ onDestroy(() => {
44
+ const scrollEl = container === window ? window : container
45
+ scrollEl.removeEventListener('scroll', handleScroll)
46
+ })
47
+
48
+ const classes = $derived(`lm-backtop${visible ? ' is-visible' : ''}${cls ? ` ${cls}` : ''}`)
49
+ const style = $derived(`right: ${right}px; bottom: ${bottom}px`)
50
+ </script>
51
+
52
+ {#if visible}
53
+ <button type="button" class={classes} {style} onclick={scrollToTop} aria-label="Back to top" {...attrs}>
54
+ {#if children}
55
+ {@render children()}
56
+ {:else}
57
+ <Icon icon={ArrowUp} size={20} />
58
+ {/if}
59
+ </button>
60
+ {/if}
@@ -0,0 +1,13 @@
1
+ import type { Snippet } from 'svelte';
2
+ interface BacktopProps {
3
+ visibilityHeight?: number;
4
+ right?: number;
5
+ bottom?: number;
6
+ target?: string;
7
+ onclick?: () => void;
8
+ children?: Snippet;
9
+ class?: string;
10
+ }
11
+ declare const Backtop: import("svelte").Component<BacktopProps, {}, "">;
12
+ type Backtop = ReturnType<typeof Backtop>;
13
+ export default Backtop;
@@ -0,0 +1,4 @@
1
+ import Backtop from './Backtop.svelte';
2
+ export { Backtop };
3
+ export type { BacktopProps } from './types';
4
+ export default Backtop;
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ import Backtop from './Backtop.svelte';
2
+ export { Backtop };
3
+ export default Backtop;
@@ -0,0 +1,18 @@
1
+ import type { Snippet } from 'svelte';
2
+ /** Backtop 属性 */
3
+ export interface BacktopProps {
4
+ /** 滚动高度达到此值后显示 */
5
+ visibilityHeight?: number;
6
+ /** 距离右边距 */
7
+ right?: number;
8
+ /** 距离底边距 */
9
+ bottom?: number;
10
+ /** 触发滚动的目标元素选择器 */
11
+ target?: string;
12
+ /** 点击事件 */
13
+ onclick?: () => void;
14
+ /** 子内容 */
15
+ children?: Snippet;
16
+ /** 自定义类名 */
17
+ class?: string;
18
+ }
package/dist/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "@lumen-design/backtop",
3
+ "version": "0.0.2",
4
+ "description": "Backtop component for Lumen UI",
5
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "module": "dist/index.js",
8
+ "types": "dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "svelte": "./dist/index.js",
13
+ "import": "./dist/index.js"
14
+ }
15
+ },
16
+ "files": [
17
+ "dist"
18
+ ],
19
+ "scripts": {
20
+ "build": "svelte-package -i src -o dist --types",
21
+ "build:watch": "svelte-package -i src -o dist --types -w"
22
+ },
23
+ "dependencies": {
24
+ "@lumen-design/icon": "0.0.2",
25
+ "lucide": "^0.563.0"
26
+ },
27
+ "devDependencies": {
28
+ "@sveltejs/package": "^2.5.7",
29
+ "svelte": "5.48.2"
30
+ },
31
+ "peerDependencies": {
32
+ "svelte": "^5.0.0"
33
+ }
34
+ }