Skip to content

Instantly share code, notes, and snippets.

@gustavneustadt
Created August 18, 2022 17:08
Show Gist options
  • Select an option

  • Save gustavneustadt/eaeb26772bcd4dbce978aed6a69d1047 to your computer and use it in GitHub Desktop.

Select an option

Save gustavneustadt/eaeb26772bcd4dbce978aed6a69d1047 to your computer and use it in GitHub Desktop.
Helps doing svelte Tweens directly in the layout; only for numbers.
<script lang="ts">
import { Tweened, tweened } from "svelte/motion"
import { cubicInOut } from "svelte/easing"
export let tween: Tweened<number> = null
export let options: Intl.NumberFormatOptions = null
export let locale: string | string[] = null
export let formatter: Intl.NumberFormat = null
export let startValue: number = null
interface TweenOptions<T> {
delay?: number;
duration?: number | ((from: T, to: T) => number);
easing?: (t: number) => number;
interpolate?: (a: T, b: T) => (t: number) => T;
}
export let tweenOptions: TweenOptions<number> = null
export let value: number = 0
$: if(tween === null) {
tween = tween ?? tweened(startValue ?? value, tweenOptions ?? {
duration: 200,
easing: cubicInOut
})
}
$: tween.set(valueNumber)
$: if(!formatter) {
formatter = new Intl.NumberFormat(locale ?? "fr", options ?? {
maximumFractionDigits: 0
})
}
$: valueNumber = value ?? 0
</script>
{formatter.format($tween)}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment