-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathprogress-bar.svelte
More file actions
84 lines (73 loc) · 1.72 KB
/
Copy pathprogress-bar.svelte
File metadata and controls
84 lines (73 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<script lang="ts">
import { onMount, untrack } from 'svelte';
import { Tween } from 'svelte/motion';
import { ProgressBarLocation, ProgressBarStatus } from './progress-bar.types';
interface Props {
autoplay?: boolean;
status?: ProgressBarStatus;
location?: ProgressBarLocation;
hidden?: boolean;
duration?: number;
onDone: () => void;
onPlaying?: () => void;
onPaused?: () => void;
}
let {
autoplay = false,
status = $bindable(ProgressBarStatus.Paused),
location = ProgressBarLocation.Bottom,
hidden = false,
duration = 5,
onDone,
onPlaying = () => {},
onPaused = () => {}
}: Props = $props();
const progress = new Tween(0, {
duration: (from: number, to: number) => {
if (to === 0) return 0;
return duration * 1000 * (to - from);
}
});
let completed = false;
$effect(() => {
if (progress.current >= 1 && !completed) {
completed = true;
untrack(() => onDone());
} else if (progress.current < 1) {
completed = false;
}
});
onMount(async () => {
if (autoplay) {
await play();
}
});
export const play = async () => {
status = ProgressBarStatus.Playing;
onPlaying();
await progress.set(1);
};
export const pause = async () => {
status = ProgressBarStatus.Paused;
onPaused();
await progress.set(progress.current);
};
export const restart = async (autoplay: boolean) => {
await progress.set(0);
if (autoplay) {
await play();
}
};
export const reset = async () => {
status = ProgressBarStatus.Paused;
await progress.set(0);
};
</script>
{#if !hidden}
<span
id="progressbar"
class="fixed left-0 h-[3px] bg-primary z-[1000]
{location == ProgressBarLocation.Top ? 'top-0' : 'bottom-0'}"
style:width={`${progress.current * 100}%`}
></span>
{/if}