From c2d6702d64ecb487223abc77f629eb6ed9450837 Mon Sep 17 00:00:00 2001
From: Florian <45694132+flo-bit@users.noreply.github.com>
Date: Thu, 20 Mar 2025 05:34:48 +0100
Subject: [PATCH] add phone, timer, stopwatch
---
src/docs/cards/extras/CardPhone.svelte | 11 +++
src/docs/cards/extras/CardStopwatch.svelte | 16 ++++
src/docs/cards/extras/CardTimer.svelte | 16 ++++
src/docs/site-components/components_extra.ts | 21 +++++
.../base/scroll-area/BodyScrollArea.svelte | 46 +---------
src/lib/components/base/scroll-area/style.css | 41 +++++++++
src/lib/components/extra/phone/Phone.svelte | 12 ++-
.../extra/stopwatch/Stopwatch.svelte | 66 ++++++++++++++
.../extra/stopwatch/StopwatchState.svelte.ts | 90 +++++++++++++++++++
src/lib/components/extra/timer/Timer.svelte | 49 ++++++----
.../extra/timer/TimerState.svelte.ts | 13 ++-
src/lib/index.ts | 6 ++
.../base/number-input/NumberInput.md | 4 +
.../extras/color-picker/ColorPicker.md | 2 +-
.../components/extras/phone/+page.svelte | 9 +-
.../components/extras/phone/Example.svelte | 11 +++
src/routes/components/extras/phone/Phone.md | 23 +++++
.../components/extras/stopwatch/+page.svelte | 8 ++
.../extras/stopwatch/Example.svelte | 27 ++++++
.../components/extras/stopwatch/Stopwatch.md | 28 ++++++
.../components/extras/timer/+page.svelte | 9 +-
.../components/extras/timer/Example.svelte | 27 ++++++
src/routes/components/extras/timer/Timer.md | 28 ++++++
23 files changed, 487 insertions(+), 76 deletions(-)
create mode 100644 src/docs/cards/extras/CardPhone.svelte
create mode 100644 src/docs/cards/extras/CardStopwatch.svelte
create mode 100644 src/docs/cards/extras/CardTimer.svelte
create mode 100644 src/lib/components/base/scroll-area/style.css
create mode 100644 src/lib/components/extra/stopwatch/Stopwatch.svelte
create mode 100644 src/lib/components/extra/stopwatch/StopwatchState.svelte.ts
create mode 100644 src/routes/components/extras/phone/Example.svelte
create mode 100644 src/routes/components/extras/phone/Phone.md
create mode 100644 src/routes/components/extras/stopwatch/+page.svelte
create mode 100644 src/routes/components/extras/stopwatch/Example.svelte
create mode 100644 src/routes/components/extras/stopwatch/Stopwatch.md
create mode 100644 src/routes/components/extras/timer/Example.svelte
create mode 100644 src/routes/components/extras/timer/Timer.md
diff --git a/src/docs/cards/extras/CardPhone.svelte b/src/docs/cards/extras/CardPhone.svelte
new file mode 100644
index 0000000..7dfb124
--- /dev/null
+++ b/src/docs/cards/extras/CardPhone.svelte
@@ -0,0 +1,11 @@
+
+
+
+
diff --git a/src/lib/components/extra/stopwatch/Stopwatch.svelte b/src/lib/components/extra/stopwatch/Stopwatch.svelte
new file mode 100644
index 0000000..54e7d87
--- /dev/null
+++ b/src/lib/components/extra/stopwatch/Stopwatch.svelte
@@ -0,0 +1,66 @@
+
+
+
+
+ {#if showHours}
+
+ {/if}
+
+ {#if showMinutes}
+
+ {/if}
+
+ {#if showSeconds}
+
+ {/if}
+
+
diff --git a/src/lib/components/extra/stopwatch/StopwatchState.svelte.ts b/src/lib/components/extra/stopwatch/StopwatchState.svelte.ts
new file mode 100644
index 0000000..7818748
--- /dev/null
+++ b/src/lib/components/extra/stopwatch/StopwatchState.svelte.ts
@@ -0,0 +1,90 @@
+// adopted from https://github.com/joshnuss/svelte-reactive-timer
+
+type Status = 'running' | 'paused' | 'stopped';
+type Options = {
+ precision?: number;
+};
+
+export class StopwatchState {
+ status = $state
('stopped');
+ duration = $state(0);
+ elapsed = $state(0);
+ startAt = $state(null);
+
+ time = $state(null);
+ precision: number;
+ #interval: number | null = null;
+
+ constructor(options: Options = {}) {
+ this.precision = options.precision ?? 1000 / 60;
+ }
+
+ get isRunning() {
+ return this.status == 'running';
+ }
+
+ get isStopped() {
+ return this.status == 'stopped';
+ }
+
+ get isPaused() {
+ return this.status == 'paused';
+ }
+
+ start() {
+ if (this.isRunning) {
+ this.stop();
+ }
+
+ this.time = new Date();
+ this.startAt = this.time;
+ this.status = 'running';
+ this.elapsed = 0;
+ this.#schedule();
+ }
+
+ stop() {
+ this.#dispose();
+ this.status = 'stopped';
+ }
+
+ pause() {
+ this.#dispose();
+ this.status = 'paused';
+ }
+
+ resume() {
+ this.time = new Date();
+ this.status = 'running';
+ this.#schedule();
+ }
+
+ reset(duration?: number) {
+ if (duration) {
+ this.duration = duration;
+ }
+ this.#dispose();
+
+ this.time = new Date();
+ this.startAt = this.time;
+ this.elapsed = 0;
+ }
+
+ #schedule() {
+ this.time = new Date();
+ this.#interval = setInterval(() => this.#onInterval(), this.precision);
+ }
+
+ #dispose() {
+ if (this.#interval) {
+ clearInterval(this.#interval);
+ }
+ }
+
+ #onInterval() {
+ const now = new Date();
+
+ this.elapsed += now.getTime() - this.time!.getTime();
+ this.time = now;
+ }
+}
diff --git a/src/lib/components/extra/timer/Timer.svelte b/src/lib/components/extra/timer/Timer.svelte
index 9f40cff..b7a9864 100644
--- a/src/lib/components/extra/timer/Timer.svelte
+++ b/src/lib/components/extra/timer/Timer.svelte
@@ -6,14 +6,24 @@
import { cn } from '$lib/utils.js';
let {
- timer = $bindable(new TimerState(1000 * 60 * 60)),
+ timer = $bindable(),
class: className,
ref = $bindable(null),
+ showHours = false,
+ showMinutes = true,
+ showSeconds = true,
...restProps
}: WithElementRef>> & {
timer?: TimerState;
+ showHours?: boolean;
+ showMinutes?: boolean;
+ showSeconds?: boolean;
} = $props();
+ if (!timer) {
+ timer = new TimerState(1000 * 60 * 10);
+ }
+
const hh = $derived(Math.floor(timer.remaining / 3600000));
const mm = $derived(Math.floor((timer.remaining % 3600000) / 60000));
const ss = $derived(Math.floor((timer.remaining % 60000) / 1000));
@@ -29,21 +39,28 @@
style="font-variant-numeric: tabular-nums;"
{...restProps}
>
-
+ {#if showHours}
+
+ {/if}
+
+ {#if showMinutes}
+
+ {/if}
-
-
+ {#if showSeconds}
+
+ {/if}
diff --git a/src/lib/components/extra/timer/TimerState.svelte.ts b/src/lib/components/extra/timer/TimerState.svelte.ts
index dee3395..3b172ba 100644
--- a/src/lib/components/extra/timer/TimerState.svelte.ts
+++ b/src/lib/components/extra/timer/TimerState.svelte.ts
@@ -69,14 +69,11 @@ export class TimerState {
}
this.#dispose();
- if (!this.isStopped) {
- this.start();
- } else {
- this.time = new Date();
- this.startAt = this.time;
- this.endAt = new Date(this.time.getTime() + this.duration);
- this.elapsed = 0;
- }
+ this.status = 'stopped';
+ this.time = new Date();
+ this.startAt = this.time;
+ this.endAt = new Date(this.time.getTime() + this.duration);
+ this.elapsed = 0;
}
#schedule() {
diff --git a/src/lib/index.ts b/src/lib/index.ts
index 61221a8..eb574cb 100644
--- a/src/lib/index.ts
+++ b/src/lib/index.ts
@@ -6,6 +6,7 @@ export { Alert, AlertDescription, AlertTitle } from '$lib/components/base/alert'
export { Avatar, AvatarGroup } from '$lib/components/base/avatar';
export { Badge } from '$lib/components/base/badge';
export { Button } from '$lib/components/base/button';
+export { default as Box } from '$lib/components/base/box/Box.svelte';
export { default as ImageCard } from '$lib/components/base/card/ImageCard.svelte';
export { default as ChatBubble } from '$lib/components/base/chat-bubble/ChatBubble.svelte';
export { Checkbox } from '$lib/components/base/checkbox';
@@ -34,6 +35,11 @@ export { ColorPicker, PopoverColorPicker } from '$lib/components/extra/color-pic
export { default as Excalidraw } from '$lib/components/extra/excalidraw/Excalidraw.svelte';
export { default as Undraw } from '$lib/components/extra/undraw/Undraw.svelte';
export { ColorSelect } from '$lib/components/extra/color-select';
+export { default as Stopwatch } from '$lib/components/extra/stopwatch/Stopwatch.svelte';
+export { default as Timer } from '$lib/components/extra/timer/Timer.svelte';
+export { StopwatchState } from '$lib/components/extra/stopwatch/StopwatchState.svelte';
+export { TimerState } from '$lib/components/extra/timer/TimerState.svelte';
+export { default as Phone } from '$lib/components/extra/phone/Phone.svelte';
// 3d components
export { default as ModelPicker } from '$lib/components/3d/model-picker/ModelPicker.svelte';
diff --git a/src/routes/components/base/number-input/NumberInput.md b/src/routes/components/base/number-input/NumberInput.md
index c049858..815bedc 100644
--- a/src/routes/components/base/number-input/NumberInput.md
+++ b/src/routes/components/base/number-input/NumberInput.md
@@ -20,3 +20,7 @@
```
+
+## Credits
+
+- Adapted from an example of [number-flow](https://number-flow.barvian.me/)
\ No newline at end of file
diff --git a/src/routes/components/extras/color-picker/ColorPicker.md b/src/routes/components/extras/color-picker/ColorPicker.md
index c10d68f..4517eb2 100644
--- a/src/routes/components/extras/color-picker/ColorPicker.md
+++ b/src/routes/components/extras/color-picker/ColorPicker.md
@@ -30,4 +30,4 @@
### Credits
-Adapted from [svelte-color-select](https://github.com/CaptainCodeman/svelte-color-select).
\ No newline at end of file
+- Adapted from [svelte-color-select](https://github.com/CaptainCodeman/svelte-color-select).
\ No newline at end of file
diff --git a/src/routes/components/extras/phone/+page.svelte b/src/routes/components/extras/phone/+page.svelte
index ae7d1db..f2a2026 100644
--- a/src/routes/components/extras/phone/+page.svelte
+++ b/src/routes/components/extras/phone/+page.svelte
@@ -1,5 +1,8 @@
-
-
+
+
+
diff --git a/src/routes/components/extras/phone/Example.svelte b/src/routes/components/extras/phone/Example.svelte
new file mode 100644
index 0000000..fcf9aa4
--- /dev/null
+++ b/src/routes/components/extras/phone/Example.svelte
@@ -0,0 +1,11 @@
+
+
+
+
+
diff --git a/src/routes/components/extras/phone/Phone.md b/src/routes/components/extras/phone/Phone.md
new file mode 100644
index 0000000..971560b
--- /dev/null
+++ b/src/routes/components/extras/phone/Phone.md
@@ -0,0 +1,23 @@
+
+
+# Phone
+
+## Example
+
+
+
+## Usage
+
+```svelte
+
+
+
+
+
+```
\ No newline at end of file
diff --git a/src/routes/components/extras/stopwatch/+page.svelte b/src/routes/components/extras/stopwatch/+page.svelte
new file mode 100644
index 0000000..a7e65ff
--- /dev/null
+++ b/src/routes/components/extras/stopwatch/+page.svelte
@@ -0,0 +1,8 @@
+
+
+
+
+
diff --git a/src/routes/components/extras/stopwatch/Example.svelte b/src/routes/components/extras/stopwatch/Example.svelte
new file mode 100644
index 0000000..d0fb16b
--- /dev/null
+++ b/src/routes/components/extras/stopwatch/Example.svelte
@@ -0,0 +1,27 @@
+
+
+
+
+
+
+
+
+
+
diff --git a/src/routes/components/extras/stopwatch/Stopwatch.md b/src/routes/components/extras/stopwatch/Stopwatch.md
new file mode 100644
index 0000000..e2c597a
--- /dev/null
+++ b/src/routes/components/extras/stopwatch/Stopwatch.md
@@ -0,0 +1,28 @@
+
+
+# Stopwatch
+
+## Example
+
+
+
+## Usage
+
+```svelte
+
+
+
+
+
+```
+
+## Credits
+
+- Stopwatch state based on [svelte-reactive-timer](https://github.com/joshnuss/svelte-reactive-timer)
+- Moving numbers component from [number-flow](https://number-flow.barvian.me/)
\ No newline at end of file
diff --git a/src/routes/components/extras/timer/+page.svelte b/src/routes/components/extras/timer/+page.svelte
index f7acb62..c2b8263 100644
--- a/src/routes/components/extras/timer/+page.svelte
+++ b/src/routes/components/extras/timer/+page.svelte
@@ -1,5 +1,8 @@
-
-
+
+
+
diff --git a/src/routes/components/extras/timer/Example.svelte b/src/routes/components/extras/timer/Example.svelte
new file mode 100644
index 0000000..b542d63
--- /dev/null
+++ b/src/routes/components/extras/timer/Example.svelte
@@ -0,0 +1,27 @@
+
+
+
+
+
+
+
+
+
+
diff --git a/src/routes/components/extras/timer/Timer.md b/src/routes/components/extras/timer/Timer.md
new file mode 100644
index 0000000..b071910
--- /dev/null
+++ b/src/routes/components/extras/timer/Timer.md
@@ -0,0 +1,28 @@
+
+
+# Timer
+
+## Example
+
+
+
+## Usage
+
+```svelte
+
+
+
+
+
+```
+
+## Credits
+
+- Timer state based on [svelte-reactive-timer](https://github.com/joshnuss/svelte-reactive-timer)
+- Moving numbers component from [number-flow](https://number-flow.barvian.me/)
\ No newline at end of file
--
knotmirror 0.0.1