Created
August 18, 2022 17:08
-
-
Save gustavneustadt/eaeb26772bcd4dbce978aed6a69d1047 to your computer and use it in GitHub Desktop.
Helps doing svelte Tweens directly in the layout; only for numbers.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <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