@foxui/time 0.4.0
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.
- package/LICENSE +21 -0
- package/README.md +16 -0
- package/dist/components/countdown/Countdown.svelte +44 -0
- package/dist/components/countdown/Countdown.svelte.d.ts +12 -0
- package/dist/components/countdown/index.d.ts +1 -0
- package/dist/components/countdown/index.js +1 -0
- package/dist/components/index.d.ts +4 -0
- package/dist/components/index.js +4 -0
- package/dist/components/relative-time/RelativeTime.svelte +17 -0
- package/dist/components/relative-time/RelativeTime.svelte.d.ts +23 -0
- package/dist/components/relative-time/action.d.ts +9 -0
- package/dist/components/relative-time/action.js +19 -0
- package/dist/components/relative-time/formatter.d.ts +1 -0
- package/dist/components/relative-time/formatter.js +11 -0
- package/dist/components/relative-time/index.d.ts +5 -0
- package/dist/components/relative-time/index.js +5 -0
- package/dist/components/relative-time/render.d.ts +12 -0
- package/dist/components/relative-time/render.js +53 -0
- package/dist/components/relative-time/state.d.ts +3 -0
- package/dist/components/relative-time/state.js +42 -0
- package/dist/components/stopwatch/Stopwatch.svelte +66 -0
- package/dist/components/stopwatch/Stopwatch.svelte.d.ts +12 -0
- package/dist/components/stopwatch/StopwatchState.svelte.d.ts +23 -0
- package/dist/components/stopwatch/StopwatchState.svelte.js +69 -0
- package/dist/components/stopwatch/index.d.ts +2 -0
- package/dist/components/stopwatch/index.js +2 -0
- package/dist/components/timer/Timer.svelte +66 -0
- package/dist/components/timer/Timer.svelte.d.ts +12 -0
- package/dist/components/timer/TimerState.svelte.d.ts +25 -0
- package/dist/components/timer/TimerState.svelte.js +79 -0
- package/dist/components/timer/index.d.ts +2 -0
- package/dist/components/timer/index.js +2 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/types.d.ts +1 -0
- package/package.json +73 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License Copyright (c) 2025 flo-bit
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of
|
|
4
|
+
charge, to any person obtaining a copy of this software and associated
|
|
5
|
+
documentation files (the "Software"), to deal in the Software without
|
|
6
|
+
restriction, including without limitation the rights to use, copy, modify, merge,
|
|
7
|
+
publish, distribute, sublicense, and/or sell copies of the Software, and to
|
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to the
|
|
9
|
+
following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice
|
|
12
|
+
(including the next paragraph) shall be included in all copies or substantial
|
|
13
|
+
portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
|
|
16
|
+
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
17
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
|
|
18
|
+
EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
|
19
|
+
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
20
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# 🦊 fox ui
|
|
2
|
+
|
|
3
|
+
svelte 5 + tailwind 4 ui kit, time components
|
|
4
|
+
|
|
5
|
+
- [Stopwatch](https://flo-bit.dev/ui-kit/components/time/stopwatch)
|
|
6
|
+
- [Timer](https://flo-bit.dev/ui-kit/components/time/timer)
|
|
7
|
+
|
|
8
|
+
> **This is a public alpha release. Expect bugs and breaking changes.**
|
|
9
|
+
|
|
10
|
+
[See all components here](https://flo-bit.dev/ui-kit)
|
|
11
|
+
|
|
12
|
+
For a guide on how to use this ui kit, see the [Quickstart Guide](https://flo-bit.dev/ui-kit/docs/quick-start).
|
|
13
|
+
|
|
14
|
+
Read more about [the philosophy/aim of this project here](https://flo-bit.dev/ui-kit/docs/philosophy).
|
|
15
|
+
|
|
16
|
+
For more information about development, contributing and the like, see the main [README](https://github.com/flo-bit/ui-kit/blob/main/README.md).
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import NumberFlow, { NumberFlowGroup } from '@number-flow/svelte';
|
|
3
|
+
import { TimerState } from '../timer';
|
|
4
|
+
import type { WithElementRef, WithoutChildrenOrChild } from 'bits-ui';
|
|
5
|
+
import type { HTMLAttributes } from 'svelte/elements';
|
|
6
|
+
import { cn } from '@foxui/core';
|
|
7
|
+
|
|
8
|
+
let {
|
|
9
|
+
timer = $bindable(),
|
|
10
|
+
class: className,
|
|
11
|
+
ref = $bindable(null),
|
|
12
|
+
showHours = false,
|
|
13
|
+
showMinutes = true,
|
|
14
|
+
showSeconds = true,
|
|
15
|
+
...restProps
|
|
16
|
+
}: WithElementRef<WithoutChildrenOrChild<HTMLAttributes<HTMLDivElement>>> & {
|
|
17
|
+
timer?: TimerState;
|
|
18
|
+
showHours?: boolean;
|
|
19
|
+
showMinutes?: boolean;
|
|
20
|
+
showSeconds?: boolean;
|
|
21
|
+
} = $props();
|
|
22
|
+
|
|
23
|
+
if (!timer) {
|
|
24
|
+
timer = new TimerState(1000 * 5);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const ss = $derived(Math.floor(timer.remaining / 1000));
|
|
28
|
+
</script>
|
|
29
|
+
|
|
30
|
+
<NumberFlowGroup>
|
|
31
|
+
<div
|
|
32
|
+
bind:this={ref}
|
|
33
|
+
class={cn(
|
|
34
|
+
'text-base-900 dark:text-base-100 flex w-full justify-center text-5xl font-bold',
|
|
35
|
+
className
|
|
36
|
+
)}
|
|
37
|
+
style="font-variant-numeric: tabular-nums;"
|
|
38
|
+
{...restProps}
|
|
39
|
+
>
|
|
40
|
+
{#if showSeconds}
|
|
41
|
+
<NumberFlow value={ss} trend={-1} digits={{ 1: { max: 5 } }} />
|
|
42
|
+
{/if}
|
|
43
|
+
</div>
|
|
44
|
+
</NumberFlowGroup>
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { TimerState } from '../timer';
|
|
2
|
+
import type { WithElementRef, WithoutChildrenOrChild } from 'bits-ui';
|
|
3
|
+
import type { HTMLAttributes } from 'svelte/elements';
|
|
4
|
+
type $$ComponentProps = WithElementRef<WithoutChildrenOrChild<HTMLAttributes<HTMLDivElement>>> & {
|
|
5
|
+
timer?: TimerState;
|
|
6
|
+
showHours?: boolean;
|
|
7
|
+
showMinutes?: boolean;
|
|
8
|
+
showSeconds?: boolean;
|
|
9
|
+
};
|
|
10
|
+
declare const Countdown: import("svelte").Component<$$ComponentProps, {}, "timer" | "ref">;
|
|
11
|
+
type Countdown = ReturnType<typeof Countdown>;
|
|
12
|
+
export default Countdown;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as Countdown } from './Countdown.svelte';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as Countdown } from './Countdown.svelte';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { onDestroy } from 'svelte';
|
|
3
|
+
import { register, unregister } from './state';
|
|
4
|
+
|
|
5
|
+
export let date: Date | number;
|
|
6
|
+
export let locale: string;
|
|
7
|
+
export let live = true;
|
|
8
|
+
|
|
9
|
+
let instance = new Object();
|
|
10
|
+
let text = '';
|
|
11
|
+
|
|
12
|
+
register(instance, date, locale, live, (value) => ({ text } = value));
|
|
13
|
+
|
|
14
|
+
onDestroy(() => unregister(instance));
|
|
15
|
+
</script>
|
|
16
|
+
|
|
17
|
+
<span class={$$props.class} title={date.toLocaleString()}>{text}</span>
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
interface $$__sveltets_2_IsomorphicComponent<Props extends Record<string, any> = any, Events extends Record<string, any> = any, Slots extends Record<string, any> = any, Exports = {}, Bindings = string> {
|
|
2
|
+
new (options: import('svelte').ComponentConstructorOptions<Props>): import('svelte').SvelteComponent<Props, Events, Slots> & {
|
|
3
|
+
$$bindings?: Bindings;
|
|
4
|
+
} & Exports;
|
|
5
|
+
(internal: unknown, props: Props & {
|
|
6
|
+
$$events?: Events;
|
|
7
|
+
$$slots?: Slots;
|
|
8
|
+
}): Exports & {
|
|
9
|
+
$set?: any;
|
|
10
|
+
$on?: any;
|
|
11
|
+
};
|
|
12
|
+
z_$$bindings?: Bindings;
|
|
13
|
+
}
|
|
14
|
+
declare const RelativeTime: $$__sveltets_2_IsomorphicComponent<{
|
|
15
|
+
[x: string]: any;
|
|
16
|
+
date: Date | number;
|
|
17
|
+
locale: string;
|
|
18
|
+
live?: boolean | undefined;
|
|
19
|
+
}, {
|
|
20
|
+
[evt: string]: CustomEvent<any>;
|
|
21
|
+
}, {}, {}, string>;
|
|
22
|
+
type RelativeTime = InstanceType<typeof RelativeTime>;
|
|
23
|
+
export default RelativeTime;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { register, unregister } from './state';
|
|
2
|
+
export function relativeTime(node, options) {
|
|
3
|
+
const callback = ({ text }) => (node.textContent = text);
|
|
4
|
+
function init(options) {
|
|
5
|
+
const date = options.date;
|
|
6
|
+
const locale = options.locale ?? navigator.language;
|
|
7
|
+
const live = (options.live = true);
|
|
8
|
+
register(node, date, locale, live, callback);
|
|
9
|
+
}
|
|
10
|
+
init(options);
|
|
11
|
+
return {
|
|
12
|
+
update(options) {
|
|
13
|
+
init(options);
|
|
14
|
+
},
|
|
15
|
+
destroy() {
|
|
16
|
+
unregister(node);
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function getFormatter(locale: string): Intl.RelativeTimeFormat;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// keep a cache of formatter per locale, to avoid re-creating them (GC)
|
|
2
|
+
const formatters = new Map();
|
|
3
|
+
// get the Intl.RelativeTimeFormat formatter for the given locale
|
|
4
|
+
export function getFormatter(locale) {
|
|
5
|
+
if (formatters.has(locale)) {
|
|
6
|
+
return formatters.get(locale);
|
|
7
|
+
}
|
|
8
|
+
const formatter = new Intl.RelativeTimeFormat(locale, { numeric: 'always', style: 'narrow' });
|
|
9
|
+
formatters.set(locale, formatter);
|
|
10
|
+
return formatter;
|
|
11
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export type Callback = (result: {
|
|
2
|
+
seconds: number;
|
|
3
|
+
count: number;
|
|
4
|
+
units: Intl.RelativeTimeFormatUnit;
|
|
5
|
+
text: string;
|
|
6
|
+
}) => void;
|
|
7
|
+
export interface RenderState {
|
|
8
|
+
date: Date | number;
|
|
9
|
+
callback: Callback;
|
|
10
|
+
formatter: Intl.RelativeTimeFormat;
|
|
11
|
+
}
|
|
12
|
+
export declare function render(state: RenderState, now: number): number;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
// Array reprsenting one minute, hour, day, week, month, etc in seconds
|
|
2
|
+
const cutoffs = [60, 3600, 86400, 86400 * 7, 86400 * 30, 86400 * 365, Infinity];
|
|
3
|
+
// Array equivalent to the above but in the string representation of the units
|
|
4
|
+
const formatUnits = [
|
|
5
|
+
'seconds',
|
|
6
|
+
'minutes',
|
|
7
|
+
'hours',
|
|
8
|
+
'days',
|
|
9
|
+
'weeks',
|
|
10
|
+
'months',
|
|
11
|
+
'years'
|
|
12
|
+
];
|
|
13
|
+
// function to render relative time into
|
|
14
|
+
export function render(state, now) {
|
|
15
|
+
const { date, callback, formatter } = state;
|
|
16
|
+
// Allow dates or times to be passed
|
|
17
|
+
const timeMs = typeof date === 'number' ? date : date.getTime();
|
|
18
|
+
// Get the amount of seconds between the given date and now
|
|
19
|
+
const delta = timeMs - now;
|
|
20
|
+
const seconds = Math.round(delta / 1000);
|
|
21
|
+
// Grab the ideal cutoff unit
|
|
22
|
+
const unitIndex = cutoffs.findIndex((cutoff) => cutoff > Math.abs(seconds));
|
|
23
|
+
// units
|
|
24
|
+
const units = formatUnits[unitIndex];
|
|
25
|
+
// Get the divisor to divide from the seconds. E.g. if our unit is 'day' our divisor
|
|
26
|
+
// is one day in seconds, so we can divide our seconds by this to get the # of days
|
|
27
|
+
const divisor = unitIndex ? cutoffs[unitIndex - 1] : 1;
|
|
28
|
+
// count of units
|
|
29
|
+
const count = Math.round(seconds / divisor);
|
|
30
|
+
// Intl.RelativeTimeFormat do its magic
|
|
31
|
+
callback({
|
|
32
|
+
seconds: seconds,
|
|
33
|
+
count,
|
|
34
|
+
units,
|
|
35
|
+
text: formatter.format(count, units).replace('ago', '')
|
|
36
|
+
});
|
|
37
|
+
// calculate time to next update, taking account rounding (e.g. it goes from 2 minutes to 1 minute at 90 seconds)
|
|
38
|
+
// and also the changeover from units (59 seconds shouldn't show as 1 minute, so at 61 seconds we schedule the next
|
|
39
|
+
// update for 1 second time)
|
|
40
|
+
const divisorMs = divisor * 1000;
|
|
41
|
+
let updateIn;
|
|
42
|
+
if (unitIndex) {
|
|
43
|
+
updateIn = divisorMs / 2 - (Math.abs(delta) % divisorMs);
|
|
44
|
+
if (updateIn < 0) {
|
|
45
|
+
updateIn += divisorMs;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
updateIn = divisorMs - (Math.abs(delta) % divisorMs);
|
|
50
|
+
}
|
|
51
|
+
const updateAt = now + updateIn;
|
|
52
|
+
return updateAt;
|
|
53
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { getFormatter } from './formatter';
|
|
2
|
+
import { render } from './render';
|
|
3
|
+
// keep track of each instance
|
|
4
|
+
const instances = new Map();
|
|
5
|
+
// we use a single timer for efficiency and to keep updates in sync
|
|
6
|
+
let updateInterval;
|
|
7
|
+
// register or update instance
|
|
8
|
+
export function register(instance, date, locale, live, callback) {
|
|
9
|
+
// get the formatter for the given locale, we do this here so we don't keep having to look it up on each tick
|
|
10
|
+
const formatter = getFormatter(locale);
|
|
11
|
+
// create state to render
|
|
12
|
+
const state = { date, callback, formatter };
|
|
13
|
+
// initial render is immediate, so works for SSR
|
|
14
|
+
const update = render(state, Date.now());
|
|
15
|
+
// if it's to update live, we keep a track and schedule the next update
|
|
16
|
+
if (live) {
|
|
17
|
+
instances.set(instance, { ...state, update });
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
instances.delete(instance);
|
|
21
|
+
}
|
|
22
|
+
// start the clock ticking if there are any live instances
|
|
23
|
+
if (instances.size) {
|
|
24
|
+
updateInterval =
|
|
25
|
+
updateInterval ||
|
|
26
|
+
setInterval(() => {
|
|
27
|
+
const now = Date.now();
|
|
28
|
+
for (const state of instances.values()) {
|
|
29
|
+
if (state.update <= now) {
|
|
30
|
+
state.update = render(state, now);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}, 1000);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
export function unregister(instance) {
|
|
37
|
+
instances.delete(instance);
|
|
38
|
+
if (instances.size === 0) {
|
|
39
|
+
clearInterval(updateInterval);
|
|
40
|
+
updateInterval = 0;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import NumberFlow, { NumberFlowGroup } from '@number-flow/svelte';
|
|
3
|
+
import { StopwatchState } from './StopwatchState.svelte';
|
|
4
|
+
import type { WithElementRef, WithoutChildrenOrChild } from 'bits-ui';
|
|
5
|
+
import type { HTMLAttributes } from 'svelte/elements';
|
|
6
|
+
import { cn } from '@foxui/core';
|
|
7
|
+
|
|
8
|
+
let {
|
|
9
|
+
stopwatch = $bindable(),
|
|
10
|
+
class: className,
|
|
11
|
+
ref = $bindable(null),
|
|
12
|
+
showHours = false,
|
|
13
|
+
showMinutes = true,
|
|
14
|
+
showSeconds = true,
|
|
15
|
+
...restProps
|
|
16
|
+
}: WithElementRef<WithoutChildrenOrChild<HTMLAttributes<HTMLDivElement>>> & {
|
|
17
|
+
stopwatch?: StopwatchState;
|
|
18
|
+
showHours?: boolean;
|
|
19
|
+
showMinutes?: boolean;
|
|
20
|
+
showSeconds?: boolean;
|
|
21
|
+
} = $props();
|
|
22
|
+
|
|
23
|
+
if (!stopwatch) {
|
|
24
|
+
stopwatch = new StopwatchState();
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const hh = $derived(Math.floor(stopwatch.elapsed / 3600000));
|
|
28
|
+
const mm = $derived(Math.floor((stopwatch.elapsed % 3600000) / 60000));
|
|
29
|
+
const ss = $derived(Math.floor((stopwatch.elapsed % 60000) / 1000));
|
|
30
|
+
</script>
|
|
31
|
+
|
|
32
|
+
<NumberFlowGroup>
|
|
33
|
+
<div
|
|
34
|
+
bind:this={ref}
|
|
35
|
+
class={cn(
|
|
36
|
+
'text-base-900 dark:text-base-100 flex w-full justify-center text-5xl font-bold',
|
|
37
|
+
className
|
|
38
|
+
)}
|
|
39
|
+
style="font-variant-numeric: tabular-nums;"
|
|
40
|
+
{...restProps}
|
|
41
|
+
>
|
|
42
|
+
{#if showHours}
|
|
43
|
+
<NumberFlow value={hh} trend={1} format={{ minimumIntegerDigits: 2 }} />
|
|
44
|
+
{/if}
|
|
45
|
+
|
|
46
|
+
{#if showMinutes}
|
|
47
|
+
<NumberFlow
|
|
48
|
+
value={mm}
|
|
49
|
+
trend={1}
|
|
50
|
+
format={{ minimumIntegerDigits: 2 }}
|
|
51
|
+
prefix={showHours ? ':' : ''}
|
|
52
|
+
digits={{ 1: { max: 5 } }}
|
|
53
|
+
/>
|
|
54
|
+
{/if}
|
|
55
|
+
|
|
56
|
+
{#if showSeconds}
|
|
57
|
+
<NumberFlow
|
|
58
|
+
value={ss}
|
|
59
|
+
format={{ minimumIntegerDigits: 2 }}
|
|
60
|
+
trend={1}
|
|
61
|
+
prefix={showHours || showMinutes ? ':' : ''}
|
|
62
|
+
digits={{ 1: { max: 5 } }}
|
|
63
|
+
/>
|
|
64
|
+
{/if}
|
|
65
|
+
</div>
|
|
66
|
+
</NumberFlowGroup>
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { StopwatchState } from './StopwatchState.svelte';
|
|
2
|
+
import type { WithElementRef, WithoutChildrenOrChild } from 'bits-ui';
|
|
3
|
+
import type { HTMLAttributes } from 'svelte/elements';
|
|
4
|
+
type $$ComponentProps = WithElementRef<WithoutChildrenOrChild<HTMLAttributes<HTMLDivElement>>> & {
|
|
5
|
+
stopwatch?: StopwatchState;
|
|
6
|
+
showHours?: boolean;
|
|
7
|
+
showMinutes?: boolean;
|
|
8
|
+
showSeconds?: boolean;
|
|
9
|
+
};
|
|
10
|
+
declare const Stopwatch: import("svelte").Component<$$ComponentProps, {}, "ref" | "stopwatch">;
|
|
11
|
+
type Stopwatch = ReturnType<typeof Stopwatch>;
|
|
12
|
+
export default Stopwatch;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
type Status = 'running' | 'paused' | 'stopped';
|
|
2
|
+
type Options = {
|
|
3
|
+
precision?: number;
|
|
4
|
+
};
|
|
5
|
+
export declare class StopwatchState {
|
|
6
|
+
#private;
|
|
7
|
+
status: Status;
|
|
8
|
+
duration: number;
|
|
9
|
+
elapsed: number;
|
|
10
|
+
startAt: Date | null;
|
|
11
|
+
time: Date | null;
|
|
12
|
+
precision: number;
|
|
13
|
+
constructor(options?: Options);
|
|
14
|
+
get isRunning(): boolean;
|
|
15
|
+
get isStopped(): boolean;
|
|
16
|
+
get isPaused(): boolean;
|
|
17
|
+
start(): void;
|
|
18
|
+
stop(): void;
|
|
19
|
+
pause(): void;
|
|
20
|
+
resume(): void;
|
|
21
|
+
reset(duration?: number): void;
|
|
22
|
+
}
|
|
23
|
+
export {};
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
// adopted from https://github.com/joshnuss/svelte-reactive-timer
|
|
2
|
+
export class StopwatchState {
|
|
3
|
+
status = $state('stopped');
|
|
4
|
+
duration = $state(0);
|
|
5
|
+
elapsed = $state(0);
|
|
6
|
+
startAt = $state(null);
|
|
7
|
+
time = $state(null);
|
|
8
|
+
precision;
|
|
9
|
+
#interval = null;
|
|
10
|
+
constructor(options = {}) {
|
|
11
|
+
this.precision = options.precision ?? 1000 / 60;
|
|
12
|
+
}
|
|
13
|
+
get isRunning() {
|
|
14
|
+
return this.status == 'running';
|
|
15
|
+
}
|
|
16
|
+
get isStopped() {
|
|
17
|
+
return this.status == 'stopped';
|
|
18
|
+
}
|
|
19
|
+
get isPaused() {
|
|
20
|
+
return this.status == 'paused';
|
|
21
|
+
}
|
|
22
|
+
start() {
|
|
23
|
+
if (this.isRunning) {
|
|
24
|
+
this.stop();
|
|
25
|
+
}
|
|
26
|
+
this.time = new Date();
|
|
27
|
+
this.startAt = this.time;
|
|
28
|
+
this.status = 'running';
|
|
29
|
+
this.elapsed = 0;
|
|
30
|
+
this.#schedule();
|
|
31
|
+
}
|
|
32
|
+
stop() {
|
|
33
|
+
this.#dispose();
|
|
34
|
+
this.status = 'stopped';
|
|
35
|
+
}
|
|
36
|
+
pause() {
|
|
37
|
+
this.#dispose();
|
|
38
|
+
this.status = 'paused';
|
|
39
|
+
}
|
|
40
|
+
resume() {
|
|
41
|
+
this.time = new Date();
|
|
42
|
+
this.status = 'running';
|
|
43
|
+
this.#schedule();
|
|
44
|
+
}
|
|
45
|
+
reset(duration) {
|
|
46
|
+
if (duration) {
|
|
47
|
+
this.duration = duration;
|
|
48
|
+
}
|
|
49
|
+
this.#dispose();
|
|
50
|
+
this.status = 'stopped';
|
|
51
|
+
this.time = new Date();
|
|
52
|
+
this.startAt = this.time;
|
|
53
|
+
this.elapsed = 0;
|
|
54
|
+
}
|
|
55
|
+
#schedule() {
|
|
56
|
+
this.time = new Date();
|
|
57
|
+
this.#interval = setInterval(() => this.#onInterval(), this.precision);
|
|
58
|
+
}
|
|
59
|
+
#dispose() {
|
|
60
|
+
if (this.#interval) {
|
|
61
|
+
clearInterval(this.#interval);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
#onInterval() {
|
|
65
|
+
const now = new Date();
|
|
66
|
+
this.elapsed += now.getTime() - this.time.getTime();
|
|
67
|
+
this.time = now;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import NumberFlow, { NumberFlowGroup } from '@number-flow/svelte';
|
|
3
|
+
import { TimerState } from './TimerState.svelte';
|
|
4
|
+
import type { WithElementRef, WithoutChildrenOrChild } from 'bits-ui';
|
|
5
|
+
import type { HTMLAttributes } from 'svelte/elements';
|
|
6
|
+
import { cn } from '@foxui/core';
|
|
7
|
+
|
|
8
|
+
let {
|
|
9
|
+
timer = $bindable(),
|
|
10
|
+
class: className,
|
|
11
|
+
ref = $bindable(null),
|
|
12
|
+
showHours = false,
|
|
13
|
+
showMinutes = true,
|
|
14
|
+
showSeconds = true,
|
|
15
|
+
...restProps
|
|
16
|
+
}: WithElementRef<WithoutChildrenOrChild<HTMLAttributes<HTMLDivElement>>> & {
|
|
17
|
+
timer?: TimerState;
|
|
18
|
+
showHours?: boolean;
|
|
19
|
+
showMinutes?: boolean;
|
|
20
|
+
showSeconds?: boolean;
|
|
21
|
+
} = $props();
|
|
22
|
+
|
|
23
|
+
if (!timer) {
|
|
24
|
+
timer = new TimerState(1000 * 60 * 10);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const hh = $derived(Math.floor(timer.remaining / 3600000));
|
|
28
|
+
const mm = $derived(Math.floor((timer.remaining % 3600000) / 60000));
|
|
29
|
+
const ss = $derived(Math.floor((timer.remaining % 60000) / 1000));
|
|
30
|
+
</script>
|
|
31
|
+
|
|
32
|
+
<NumberFlowGroup>
|
|
33
|
+
<div
|
|
34
|
+
bind:this={ref}
|
|
35
|
+
class={cn(
|
|
36
|
+
'text-base-900 dark:text-base-100 flex w-full justify-center text-5xl font-bold',
|
|
37
|
+
className
|
|
38
|
+
)}
|
|
39
|
+
style="font-variant-numeric: tabular-nums;"
|
|
40
|
+
{...restProps}
|
|
41
|
+
>
|
|
42
|
+
{#if showHours}
|
|
43
|
+
<NumberFlow value={hh} trend={-1} format={{ minimumIntegerDigits: 2 }} />
|
|
44
|
+
{/if}
|
|
45
|
+
|
|
46
|
+
{#if showMinutes}
|
|
47
|
+
<NumberFlow
|
|
48
|
+
value={mm}
|
|
49
|
+
trend={-1}
|
|
50
|
+
format={{ minimumIntegerDigits: 2 }}
|
|
51
|
+
prefix={showHours ? ':' : ''}
|
|
52
|
+
digits={{ 1: { max: 5 } }}
|
|
53
|
+
/>
|
|
54
|
+
{/if}
|
|
55
|
+
|
|
56
|
+
{#if showSeconds}
|
|
57
|
+
<NumberFlow
|
|
58
|
+
value={ss}
|
|
59
|
+
format={{ minimumIntegerDigits: 2 }}
|
|
60
|
+
trend={-1}
|
|
61
|
+
prefix={showHours || showMinutes ? ':' : ''}
|
|
62
|
+
digits={{ 1: { max: 5 } }}
|
|
63
|
+
/>
|
|
64
|
+
{/if}
|
|
65
|
+
</div>
|
|
66
|
+
</NumberFlowGroup>
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { TimerState } from './TimerState.svelte';
|
|
2
|
+
import type { WithElementRef, WithoutChildrenOrChild } from 'bits-ui';
|
|
3
|
+
import type { HTMLAttributes } from 'svelte/elements';
|
|
4
|
+
type $$ComponentProps = WithElementRef<WithoutChildrenOrChild<HTMLAttributes<HTMLDivElement>>> & {
|
|
5
|
+
timer?: TimerState;
|
|
6
|
+
showHours?: boolean;
|
|
7
|
+
showMinutes?: boolean;
|
|
8
|
+
showSeconds?: boolean;
|
|
9
|
+
};
|
|
10
|
+
declare const Timer: import("svelte").Component<$$ComponentProps, {}, "timer" | "ref">;
|
|
11
|
+
type Timer = ReturnType<typeof Timer>;
|
|
12
|
+
export default Timer;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
type Status = 'running' | 'paused' | 'stopped';
|
|
2
|
+
type Options = {
|
|
3
|
+
precision?: number;
|
|
4
|
+
};
|
|
5
|
+
export declare class TimerState {
|
|
6
|
+
#private;
|
|
7
|
+
status: Status;
|
|
8
|
+
duration: number;
|
|
9
|
+
elapsed: number;
|
|
10
|
+
remaining: number;
|
|
11
|
+
startAt: Date | null;
|
|
12
|
+
endAt: Date | null;
|
|
13
|
+
time: Date | null;
|
|
14
|
+
precision: number;
|
|
15
|
+
constructor(duration: number, options?: Options);
|
|
16
|
+
get isRunning(): boolean;
|
|
17
|
+
get isStopped(): boolean;
|
|
18
|
+
get isPaused(): boolean;
|
|
19
|
+
start(): void;
|
|
20
|
+
stop(): void;
|
|
21
|
+
pause(): void;
|
|
22
|
+
resume(): void;
|
|
23
|
+
reset(duration?: number): void;
|
|
24
|
+
}
|
|
25
|
+
export {};
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// adopted from https://github.com/joshnuss/svelte-reactive-timer
|
|
2
|
+
export class TimerState {
|
|
3
|
+
status = $state('stopped');
|
|
4
|
+
duration = $state(0);
|
|
5
|
+
elapsed = $state(0);
|
|
6
|
+
remaining = $derived(this.duration - this.elapsed);
|
|
7
|
+
startAt = $state(null);
|
|
8
|
+
endAt = $state(null);
|
|
9
|
+
time = $state(null);
|
|
10
|
+
precision;
|
|
11
|
+
#interval = null;
|
|
12
|
+
constructor(duration, options = {}) {
|
|
13
|
+
this.duration = duration;
|
|
14
|
+
this.precision = options.precision ?? 300;
|
|
15
|
+
}
|
|
16
|
+
get isRunning() {
|
|
17
|
+
return this.status == 'running';
|
|
18
|
+
}
|
|
19
|
+
get isStopped() {
|
|
20
|
+
return this.status == 'stopped';
|
|
21
|
+
}
|
|
22
|
+
get isPaused() {
|
|
23
|
+
return this.status == 'paused';
|
|
24
|
+
}
|
|
25
|
+
start() {
|
|
26
|
+
if (this.isRunning) {
|
|
27
|
+
this.stop();
|
|
28
|
+
}
|
|
29
|
+
this.time = new Date();
|
|
30
|
+
this.startAt = this.time;
|
|
31
|
+
this.endAt = new Date(this.time.getTime() + this.duration);
|
|
32
|
+
this.status = 'running';
|
|
33
|
+
this.elapsed = 0;
|
|
34
|
+
this.#schedule();
|
|
35
|
+
}
|
|
36
|
+
stop() {
|
|
37
|
+
this.#dispose();
|
|
38
|
+
this.status = 'stopped';
|
|
39
|
+
}
|
|
40
|
+
pause() {
|
|
41
|
+
this.#dispose();
|
|
42
|
+
this.status = 'paused';
|
|
43
|
+
}
|
|
44
|
+
resume() {
|
|
45
|
+
this.time = new Date();
|
|
46
|
+
this.endAt = new Date(this.time.getTime() + this.remaining);
|
|
47
|
+
this.status = 'running';
|
|
48
|
+
this.#schedule();
|
|
49
|
+
}
|
|
50
|
+
reset(duration) {
|
|
51
|
+
if (duration) {
|
|
52
|
+
this.duration = duration;
|
|
53
|
+
}
|
|
54
|
+
this.#dispose();
|
|
55
|
+
this.status = 'stopped';
|
|
56
|
+
this.time = new Date();
|
|
57
|
+
this.startAt = this.time;
|
|
58
|
+
this.endAt = new Date(this.time.getTime() + this.duration);
|
|
59
|
+
this.elapsed = 0;
|
|
60
|
+
}
|
|
61
|
+
#schedule() {
|
|
62
|
+
this.time = new Date();
|
|
63
|
+
this.#interval = setInterval(() => this.#onInterval(), this.precision);
|
|
64
|
+
}
|
|
65
|
+
#dispose() {
|
|
66
|
+
if (this.#interval) {
|
|
67
|
+
clearInterval(this.#interval);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
#onInterval() {
|
|
71
|
+
const now = new Date();
|
|
72
|
+
this.elapsed += now.getTime() - this.time.getTime();
|
|
73
|
+
this.time = now;
|
|
74
|
+
if (this.time >= this.endAt) {
|
|
75
|
+
this.elapsed = this.duration;
|
|
76
|
+
this.stop();
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './components';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './components';
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './index';
|
package/package.json
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@foxui/time",
|
|
3
|
+
"private": false,
|
|
4
|
+
"version": "0.4.0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"files": [
|
|
7
|
+
"dist"
|
|
8
|
+
],
|
|
9
|
+
"sideEffects": [
|
|
10
|
+
"**/*.css"
|
|
11
|
+
],
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./dist/types.d.ts",
|
|
15
|
+
"svelte": "./dist/index.js"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"types": "./dist/types.d.ts",
|
|
19
|
+
"svelte": "./dist/index.js",
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"@eslint/compat": "^1.2.5",
|
|
22
|
+
"@eslint/js": "^9.18.0",
|
|
23
|
+
"@sveltejs/adapter-auto": "^6.0.0",
|
|
24
|
+
"@sveltejs/kit": "^2.16.0",
|
|
25
|
+
"@sveltejs/vite-plugin-svelte": "^5.0.0",
|
|
26
|
+
"@tailwindcss/forms": "^0.5.9",
|
|
27
|
+
"@tailwindcss/typography": "^0.5.15",
|
|
28
|
+
"@tailwindcss/vite": "^4.1.5",
|
|
29
|
+
"eslint": "^9.18.0",
|
|
30
|
+
"eslint-config-prettier": "^10.0.1",
|
|
31
|
+
"eslint-plugin-svelte": "^3.0.0",
|
|
32
|
+
"globals": "^16.0.0",
|
|
33
|
+
"prettier": "^3.4.2",
|
|
34
|
+
"prettier-plugin-svelte": "^3.3.3",
|
|
35
|
+
"prettier-plugin-tailwindcss": "^0.6.11",
|
|
36
|
+
"svelte": "^5.0.0",
|
|
37
|
+
"svelte-check": "^4.0.0",
|
|
38
|
+
"tailwindcss": "^4.1.5",
|
|
39
|
+
"typescript": "^5.0.0",
|
|
40
|
+
"typescript-eslint": "^8.20.0",
|
|
41
|
+
"vite": "^6.2.6",
|
|
42
|
+
"@sveltejs/adapter-static": "^3.0.8",
|
|
43
|
+
"@sveltejs/package": "^2.3.11"
|
|
44
|
+
},
|
|
45
|
+
"dependencies": {
|
|
46
|
+
"@number-flow/svelte": "^0.3.7",
|
|
47
|
+
"bits-ui": "^1.4.3",
|
|
48
|
+
"@foxui/core": "0.4.0"
|
|
49
|
+
},
|
|
50
|
+
"peerDependencies": {
|
|
51
|
+
"svelte": ">=5",
|
|
52
|
+
"tailwindcss": ">=3"
|
|
53
|
+
},
|
|
54
|
+
"description": "ui kit - svelte 5 + tailwind 4 - time components",
|
|
55
|
+
"homepage": "https://flo-bit.dev/ui-kit",
|
|
56
|
+
"repository": {
|
|
57
|
+
"type": "git",
|
|
58
|
+
"url": "git+https://github.com/flo-bit/ui-kit.git"
|
|
59
|
+
},
|
|
60
|
+
"author": "flo-bit (http://flo-bit.dev/)",
|
|
61
|
+
"bugs": "https://github.com/flo-bit/ui-kit/issues",
|
|
62
|
+
"license": "MIT",
|
|
63
|
+
"scripts": {
|
|
64
|
+
"dev": "vite dev",
|
|
65
|
+
"build": "vite build && npm run prepack",
|
|
66
|
+
"build:package": "vite build && npm run prepack",
|
|
67
|
+
"preview": "vite preview",
|
|
68
|
+
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
|
|
69
|
+
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
|
|
70
|
+
"format": "prettier --write .",
|
|
71
|
+
"lint": "prettier --check . && eslint ."
|
|
72
|
+
}
|
|
73
|
+
}
|