Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support svelte 5 #4

Open
J4gQBqqR opened this issue Jun 11, 2024 · 2 comments
Open

Support svelte 5 #4

J4gQBqqR opened this issue Jun 11, 2024 · 2 comments

Comments

@J4gQBqqR
Copy link

Svelte introduces runes and changes to event.
I rewrote your component using svelte 5 and typescript.
I do not know your opinion on typescript and therefore did not create pull request.

Here is the code for your reference:

<script lang="ts">
	import { inview } from "svelte-inview";

	let isInView: boolean = $state(false);

	let {
		value,
		initial = 0,
		duration = 3000,
		step = 1,
		roundto = 1,
		format = true,
	}: {
		value: number;
		initial?: number;
		duration?: number;
		step?: number;
		roundto?: number;
		format?: boolean;
	} = $props();

	function formatNumber(input: number) {
		if (format) {
			return Math.round(input).toLocaleString();
		}
		return input;
	}

	let counterResult: number = $state(initial);
	let timers: number;

	while (duration / ((value - initial) / step) < 2) {
		step++;
	}

	timers = setInterval(
		() => {
			if (isInView) {
				if (counterResult < value) {
					counterResult += step;
				} else {
					clearInterval(timers);
					counterResult = Math.round(value / roundto) * roundto;
				}
			}
		},
		duration / ((value - initial) / step),
	);
</script>

<span
	use:inview
	oninview_change={(event: CustomEvent<ObserverEventDetails>) => {
		const { inView } = event.detail;
		isInView = inView;
	}}
>
	{formatNumber(counterResult)}
</span>
@J4gQBqqR
Copy link
Author

BTW, the update interval is 2 milliseconds (while (duration / ((value - initial) / step) < 2) {) this is way too aggressive. I would suggest set something as 50ms.

  • Persistence of Vision: ~50 milliseconds.
    
  • Average Visual Reaction Time: 200-250 milliseconds.
    
  • High-Speed Perception: Can be as fast as 100 milliseconds in trained individuals.
    

@saadeghi
Copy link
Owner

Thank you.
Using runes is not backward compatible so the component will be incompatible with all Svelte 4 and Svelte 3 apps if we use runes.
I think it's still too soon. Especially when stable version of Svelte 5 is not released yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants