NumberFlow

repository·main·Indexed 27 days ago

https://github.com/barvian/number-flow

A library of animated number components providing smooth transitions for changing numeric values. It is available for React, Vue, Svelte, and vanilla TypeScript/JavaScript. The library includes a custom element <number-flow>, SSR support via renderInnerHTML, and a core engine (NumberFlowLite) that allows for custom animation timing, digit spinning trends, and plugin extensions.

Tokens
5.3K
Snippets
10
Records
52
Agent score
92%

What's inside NumberFlow

  1. Basic usage of NumberFlow

    main

    NumberFlow provides framework-specific packages for React, Vue, Svelte, and Vanilla JavaScript. It automatically transitions when the value prop changes.

    // React
    import NumberFlow from '@number-flow/react'
    function Example() {
    	return <NumberFlow value={123} />
    }
    <!-- Vue -->
    <script setup>
    import NumberFlow from '@number-flow/vue'
    </script>
    <template>
    	<NumberFlow :value="123" />
    </template>
    <!-- Svelte -->
    <script>
    import NumberFlow from '@number-flow/svelte'
    </script>
    <NumberFlow value={123} />
    <!-- Vanilla JS -->
    <number-flow></number-flow>
    
    <script type="module">
    	import 'number-flow'
    	const flow = document.querySelector('number-flow')
    	flow.update(123)
    </script>
  2. Server-side rendering with renderInnerHTML

    main

    For SSR (like Astro), use renderInnerHTML to generate the initial HTML. Ensure you pass the same locales, format, and nonce to both the renderer and the client-side hydration.

    ---
    import { renderInnerHTML } from 'number-flow'
    ---
    <number-flow
    	nonce="..."
    	set:html={renderInnerHTML(123, {
    		locales: 'en-US',
    		format: { notation: 'compact' },
    		nonce: '...'
    	})}
    />
    
    <script>
    	import 'number-flow'
    	const flow = document.querySelector('number-flow')
    	flow.locales = 'en-US'
    	flow.format = { notation: 'compact' }
    	flow.update(123)
    </script>