@number-flow/react 0.1.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/README.md ADDED
@@ -0,0 +1,15 @@
1
+ # Number Flow
2
+
3
+ Smooth transitions for `Intl.NumberFormat`.
4
+
5
+ [![MotionNumber for Framer Motion](https://motion-number.barvian.me/preview.webp)](https://motion-number.barvian.me)
6
+
7
+ [![NPM Version](https://img.shields.io/npm/v/motion-number.svg)](https://npmjs.com/package/motion-number)
8
+ [![Bundle size](https://badgen.net/bundlephobia/minzip/motion-number@latest)](https://bundlephobia.com/package/motion-number@latest)
9
+ [![Follow @mbarvian](https://img.shields.io/twitter/follow/mbarvian.svg?style=social&label=Follow)](https://x.com/mbarvian)
10
+
11
+ ---
12
+
13
+ ## Documentation
14
+
15
+ For full documentation, visit [number-flow.barvian.me](https://number-flow.barvian.me).
@@ -0,0 +1,5 @@
1
+ import { Transition } from 'framer-motion';
2
+
3
+ declare const defaultXTiming: Transition;
4
+
5
+ export { defaultXTiming };
@@ -0,0 +1,5 @@
1
+ import { Transition } from 'framer-motion';
2
+
3
+ declare const defaultXTiming: Transition;
4
+
5
+ export { defaultXTiming };
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/framer-motion.ts
21
+ var framer_motion_exports = {};
22
+ __export(framer_motion_exports, {
23
+ defaultXTiming: () => defaultXTiming
24
+ });
25
+ module.exports = __toCommonJS(framer_motion_exports);
26
+ var import_number_flow = require("number-flow");
27
+ var import_framer_motion = require("framer-motion");
28
+ var defaultTransformer = (0, import_framer_motion.transform)(
29
+ import_number_flow.defaultXTimingLinearPoints.map((_, i) => i / (import_number_flow.defaultXTimingLinearPoints.length - 1)),
30
+ import_number_flow.defaultXTimingLinearPoints
31
+ );
32
+ var defaultXTiming = import_number_flow.supportsLinear ? {
33
+ duration: import_number_flow.defaultXTimingLinearDuration / 1e3,
34
+ ease: defaultTransformer
35
+ } : {
36
+ duration: import_number_flow.defaultXTimingFallbackDuration / 1e3,
37
+ easing: import_number_flow.defaultXTimingFallbackPoints
38
+ };
39
+ // Annotate the CommonJS export names for ESM import in node:
40
+ 0 && (module.exports = {
41
+ defaultXTiming
42
+ });
@@ -0,0 +1,23 @@
1
+ // src/framer-motion.ts
2
+ import {
3
+ supportsLinear,
4
+ defaultXTimingFallbackPoints,
5
+ defaultXTimingFallbackDuration,
6
+ defaultXTimingLinearDuration,
7
+ defaultXTimingLinearPoints
8
+ } from "number-flow";
9
+ import { transform } from "framer-motion";
10
+ var defaultTransformer = transform(
11
+ defaultXTimingLinearPoints.map((_, i) => i / (defaultXTimingLinearPoints.length - 1)),
12
+ defaultXTimingLinearPoints
13
+ );
14
+ var defaultXTiming = supportsLinear ? {
15
+ duration: defaultXTimingLinearDuration / 1e3,
16
+ ease: defaultTransformer
17
+ } : {
18
+ duration: defaultXTimingFallbackDuration / 1e3,
19
+ easing: defaultXTimingFallbackPoints
20
+ };
21
+ export {
22
+ defaultXTiming
23
+ };
@@ -0,0 +1,31 @@
1
+ import * as React from 'react';
2
+ import { NumberFlowLite, Value, Format } from 'number-flow';
3
+ export * from 'number-flow';
4
+ export { defaultXTiming, defaultYTiming } from 'number-flow';
5
+
6
+ declare const OBSERVED_ATTRIBUTES: readonly ["parts"];
7
+ type ObservedAttribute = (typeof OBSERVED_ATTRIBUTES)[number];
8
+ declare class NumberFlowElement extends NumberFlowLite {
9
+ static observedAttributes: readonly ["parts"];
10
+ attributeChangedCallback(attr: ObservedAttribute, _oldValue: string, newValue: string): void;
11
+ }
12
+ type NumberFlowProps = React.HTMLAttributes<NumberFlowElement> & {
13
+ value: Value;
14
+ locales?: Intl.LocalesArgument;
15
+ format?: Format;
16
+ root?: (typeof NumberFlowElement)['prototype']['root'];
17
+ fadeTiming?: (typeof NumberFlowElement)['prototype']['fadeTiming'];
18
+ xTiming?: (typeof NumberFlowElement)['prototype']['xTiming'];
19
+ yTiming?: (typeof NumberFlowElement)['prototype']['yTiming'];
20
+ };
21
+ declare const NumberFlow: React.ForwardRefExoticComponent<React.HTMLAttributes<NumberFlowElement> & {
22
+ value: Value;
23
+ locales?: Intl.LocalesArgument;
24
+ format?: Format;
25
+ root?: (typeof NumberFlowElement)["prototype"]["root"];
26
+ fadeTiming?: (typeof NumberFlowElement)["prototype"]["fadeTiming"];
27
+ xTiming?: (typeof NumberFlowElement)["prototype"]["xTiming"];
28
+ yTiming?: (typeof NumberFlowElement)["prototype"]["yTiming"];
29
+ } & React.RefAttributes<NumberFlowElement>>;
30
+
31
+ export { NumberFlowElement, type NumberFlowProps, NumberFlow as default };
@@ -0,0 +1,31 @@
1
+ import * as React from 'react';
2
+ import { NumberFlowLite, Value, Format } from 'number-flow';
3
+ export * from 'number-flow';
4
+ export { defaultXTiming, defaultYTiming } from 'number-flow';
5
+
6
+ declare const OBSERVED_ATTRIBUTES: readonly ["parts"];
7
+ type ObservedAttribute = (typeof OBSERVED_ATTRIBUTES)[number];
8
+ declare class NumberFlowElement extends NumberFlowLite {
9
+ static observedAttributes: readonly ["parts"];
10
+ attributeChangedCallback(attr: ObservedAttribute, _oldValue: string, newValue: string): void;
11
+ }
12
+ type NumberFlowProps = React.HTMLAttributes<NumberFlowElement> & {
13
+ value: Value;
14
+ locales?: Intl.LocalesArgument;
15
+ format?: Format;
16
+ root?: (typeof NumberFlowElement)['prototype']['root'];
17
+ fadeTiming?: (typeof NumberFlowElement)['prototype']['fadeTiming'];
18
+ xTiming?: (typeof NumberFlowElement)['prototype']['xTiming'];
19
+ yTiming?: (typeof NumberFlowElement)['prototype']['yTiming'];
20
+ };
21
+ declare const NumberFlow: React.ForwardRefExoticComponent<React.HTMLAttributes<NumberFlowElement> & {
22
+ value: Value;
23
+ locales?: Intl.LocalesArgument;
24
+ format?: Format;
25
+ root?: (typeof NumberFlowElement)["prototype"]["root"];
26
+ fadeTiming?: (typeof NumberFlowElement)["prototype"]["fadeTiming"];
27
+ xTiming?: (typeof NumberFlowElement)["prototype"]["xTiming"];
28
+ yTiming?: (typeof NumberFlowElement)["prototype"]["yTiming"];
29
+ } & React.RefAttributes<NumberFlowElement>>;
30
+
31
+ export { NumberFlowElement, type NumberFlowProps, NumberFlow as default };
package/dist/index.js ADDED
@@ -0,0 +1,130 @@
1
+ "use strict";
2
+ "use client";
3
+ var __create = Object.create;
4
+ var __defProp = Object.defineProperty;
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getProtoOf = Object.getPrototypeOf;
8
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __export = (target, all) => {
10
+ for (var name in all)
11
+ __defProp(target, name, { get: all[name], enumerable: true });
12
+ };
13
+ var __copyProps = (to, from, except, desc) => {
14
+ if (from && typeof from === "object" || typeof from === "function") {
15
+ for (let key of __getOwnPropNames(from))
16
+ if (!__hasOwnProp.call(to, key) && key !== except)
17
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
18
+ }
19
+ return to;
20
+ };
21
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
22
+ // If the importer is in node compatibility mode or this is not an ESM
23
+ // file that has been converted to a CommonJS file using a Babel-
24
+ // compatible transform (i.e. "__esModule" has not been set), then set
25
+ // "default" to the CommonJS "module.exports" for node compatibility.
26
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
27
+ mod
28
+ ));
29
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
30
+
31
+ // src/index.tsx
32
+ var src_exports = {};
33
+ __export(src_exports, {
34
+ NumberFlowElement: () => NumberFlowElement,
35
+ default: () => src_default,
36
+ defaultXTiming: () => import_number_flow2.defaultXTiming,
37
+ defaultYTiming: () => import_number_flow2.defaultYTiming
38
+ });
39
+ module.exports = __toCommonJS(src_exports);
40
+ var React = __toESM(require("react"));
41
+ var import_number_flow = require("number-flow");
42
+ var import_number_flow2 = require("number-flow");
43
+ var OBSERVED_ATTRIBUTES = ["parts"];
44
+ var NumberFlowElement = class extends import_number_flow.NumberFlowLite {
45
+ static observedAttributes = OBSERVED_ATTRIBUTES;
46
+ attributeChangedCallback(attr, _oldValue, newValue) {
47
+ this[attr] = JSON.parse(newValue);
48
+ }
49
+ };
50
+ NumberFlowElement.define();
51
+ var formatters = {};
52
+ var NumberFlowPriv = class extends React.Component {
53
+ constructor(props) {
54
+ super(props);
55
+ this.handleRef = this.handleRef.bind(this);
56
+ }
57
+ // Update the non-parts props to avoid JSON serialization
58
+ // Parts needs to be set in render still:
59
+ updateNonPartsProps() {
60
+ if (this.#el) {
61
+ if (this.props.root != null) this.#el.root = this.props.root;
62
+ if (this.props.fadeTiming) this.#el.fadeTiming = this.props.fadeTiming;
63
+ if (this.props.xTiming) this.#el.xTiming = this.props.xTiming;
64
+ if (this.props.yTiming) this.#el.yTiming = this.props.yTiming;
65
+ }
66
+ }
67
+ componentDidMount() {
68
+ this.updateNonPartsProps();
69
+ if (this.#el) this.#el.manual = true;
70
+ }
71
+ getSnapshotBeforeUpdate() {
72
+ this.updateNonPartsProps();
73
+ this.#el?.willUpdate();
74
+ return null;
75
+ }
76
+ componentDidUpdate() {
77
+ this.#el?.didUpdate();
78
+ }
79
+ #el;
80
+ handleRef(el) {
81
+ if (this.props.innerRef) this.props.innerRef.current = el;
82
+ this.#el = el;
83
+ }
84
+ render() {
85
+ const {
86
+ innerRef,
87
+ className,
88
+ parts,
89
+ // These are set in updateNonPartsProps, so ignore them here:
90
+ fadeTiming,
91
+ xTiming,
92
+ yTiming,
93
+ ...rest
94
+ } = this.props;
95
+ return (
96
+ // @ts-expect-error missing types
97
+ /* @__PURE__ */ React.createElement(
98
+ "number-flow",
99
+ {
100
+ ref: this.handleRef,
101
+ class: className,
102
+ ...rest,
103
+ parts: JSON.stringify(parts)
104
+ },
105
+ /* @__PURE__ */ React.createElement(import_number_flow.SlottedTag, { style: import_number_flow.slottedStyles }, parts.formatted)
106
+ )
107
+ );
108
+ }
109
+ };
110
+ var NumberFlow = React.forwardRef(function NumberFlow2({ value, locales, format, ...props }, _ref) {
111
+ React.useImperativeHandle(_ref, () => ref.current, []);
112
+ const ref = React.useRef();
113
+ const localesString = React.useMemo(() => locales ? JSON.stringify(locales) : "", [locales]);
114
+ const formatString = React.useMemo(() => format ? JSON.stringify(format) : "", [format]);
115
+ const parts = React.useMemo(() => {
116
+ const formatter = formatters[`${localesString}:${formatString}`] ??= new Intl.NumberFormat(
117
+ locales,
118
+ format
119
+ );
120
+ return (0, import_number_flow.partitionParts)(value, formatter);
121
+ }, [value, localesString, formatString]);
122
+ return /* @__PURE__ */ React.createElement(NumberFlowPriv, { ...props, parts, innerRef: ref });
123
+ });
124
+ var src_default = NumberFlow;
125
+ // Annotate the CommonJS export names for ESM import in node:
126
+ 0 && (module.exports = {
127
+ NumberFlowElement,
128
+ defaultXTiming,
129
+ defaultYTiming
130
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,99 @@
1
+ "use client";
2
+
3
+ // src/index.tsx
4
+ import * as React from "react";
5
+ import {
6
+ SlottedTag,
7
+ slottedStyles,
8
+ partitionParts,
9
+ NumberFlowLite
10
+ } from "number-flow";
11
+ import { defaultXTiming, defaultYTiming } from "number-flow";
12
+ var OBSERVED_ATTRIBUTES = ["parts"];
13
+ var NumberFlowElement = class extends NumberFlowLite {
14
+ static observedAttributes = OBSERVED_ATTRIBUTES;
15
+ attributeChangedCallback(attr, _oldValue, newValue) {
16
+ this[attr] = JSON.parse(newValue);
17
+ }
18
+ };
19
+ NumberFlowElement.define();
20
+ var formatters = {};
21
+ var NumberFlowPriv = class extends React.Component {
22
+ constructor(props) {
23
+ super(props);
24
+ this.handleRef = this.handleRef.bind(this);
25
+ }
26
+ // Update the non-parts props to avoid JSON serialization
27
+ // Parts needs to be set in render still:
28
+ updateNonPartsProps() {
29
+ if (this.#el) {
30
+ if (this.props.root != null) this.#el.root = this.props.root;
31
+ if (this.props.fadeTiming) this.#el.fadeTiming = this.props.fadeTiming;
32
+ if (this.props.xTiming) this.#el.xTiming = this.props.xTiming;
33
+ if (this.props.yTiming) this.#el.yTiming = this.props.yTiming;
34
+ }
35
+ }
36
+ componentDidMount() {
37
+ this.updateNonPartsProps();
38
+ if (this.#el) this.#el.manual = true;
39
+ }
40
+ getSnapshotBeforeUpdate() {
41
+ this.updateNonPartsProps();
42
+ this.#el?.willUpdate();
43
+ return null;
44
+ }
45
+ componentDidUpdate() {
46
+ this.#el?.didUpdate();
47
+ }
48
+ #el;
49
+ handleRef(el) {
50
+ if (this.props.innerRef) this.props.innerRef.current = el;
51
+ this.#el = el;
52
+ }
53
+ render() {
54
+ const {
55
+ innerRef,
56
+ className,
57
+ parts,
58
+ // These are set in updateNonPartsProps, so ignore them here:
59
+ fadeTiming,
60
+ xTiming,
61
+ yTiming,
62
+ ...rest
63
+ } = this.props;
64
+ return (
65
+ // @ts-expect-error missing types
66
+ /* @__PURE__ */ React.createElement(
67
+ "number-flow",
68
+ {
69
+ ref: this.handleRef,
70
+ class: className,
71
+ ...rest,
72
+ parts: JSON.stringify(parts)
73
+ },
74
+ /* @__PURE__ */ React.createElement(SlottedTag, { style: slottedStyles }, parts.formatted)
75
+ )
76
+ );
77
+ }
78
+ };
79
+ var NumberFlow = React.forwardRef(function NumberFlow2({ value, locales, format, ...props }, _ref) {
80
+ React.useImperativeHandle(_ref, () => ref.current, []);
81
+ const ref = React.useRef();
82
+ const localesString = React.useMemo(() => locales ? JSON.stringify(locales) : "", [locales]);
83
+ const formatString = React.useMemo(() => format ? JSON.stringify(format) : "", [format]);
84
+ const parts = React.useMemo(() => {
85
+ const formatter = formatters[`${localesString}:${formatString}`] ??= new Intl.NumberFormat(
86
+ locales,
87
+ format
88
+ );
89
+ return partitionParts(value, formatter);
90
+ }, [value, localesString, formatString]);
91
+ return /* @__PURE__ */ React.createElement(NumberFlowPriv, { ...props, parts, innerRef: ref });
92
+ });
93
+ var src_default = NumberFlow;
94
+ export {
95
+ NumberFlowElement,
96
+ src_default as default,
97
+ defaultXTiming,
98
+ defaultYTiming
99
+ };
package/package.json ADDED
@@ -0,0 +1,85 @@
1
+ {
2
+ "name": "@number-flow/react",
3
+ "publishConfig": {
4
+ "access": "public"
5
+ },
6
+ "version": "0.1.0",
7
+ "author": {
8
+ "name": "Maxwell Barvian",
9
+ "email": "max@barvian.me",
10
+ "url": "https://barvian.me"
11
+ },
12
+ "description": "A lightweight component to transition and format numbers.",
13
+ "license": "MIT",
14
+ "homepage": "https://number-flow.barvian.me/react",
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "https://github.com/barvian/number-flow",
18
+ "directory": "src"
19
+ },
20
+ "bugs": {
21
+ "url": "https://github.com/barvian/number-flow/issues"
22
+ },
23
+ "keywords": [
24
+ "accessible",
25
+ "odometer",
26
+ "animation",
27
+ "number-format",
28
+ "number-animation",
29
+ "animated-number"
30
+ ],
31
+ "files": [
32
+ "dist",
33
+ "README.md"
34
+ ],
35
+ "exports": {
36
+ ".": {
37
+ "import": {
38
+ "types": "./dist/index.d.mts",
39
+ "default": "./dist/index.mjs"
40
+ },
41
+ "require": {
42
+ "types": "./dist/index.d.ts",
43
+ "default": "./dist/index.js"
44
+ }
45
+ },
46
+ "./framer-motion": {
47
+ "import": {
48
+ "types": "./dist/framer-motion.d.mts",
49
+ "default": "./dist/framer-motion.mjs"
50
+ },
51
+ "require": {
52
+ "types": "./dist/framer-motion.d.ts",
53
+ "default": "./dist/framer-motion.js"
54
+ }
55
+ }
56
+ },
57
+ "dependencies": {
58
+ "esm-env": "^1.0.0",
59
+ "number-flow": "0.2.0"
60
+ },
61
+ "devDependencies": {
62
+ "@types/react": "^18.3.3",
63
+ "@types/react-dom": "^18.3.0",
64
+ "framer-motion": "^11.9.0",
65
+ "react": "^18.3.1",
66
+ "react-dom": "^18.3.1",
67
+ "tsup": "^8.2.4"
68
+ },
69
+ "peerDependencies": {
70
+ "@types/react": "^18.3.3",
71
+ "@types/react-dom": "^18.3.0",
72
+ "framer-motion": "^11.0.0",
73
+ "react": "^18.3.1",
74
+ "react-dom": "^18.3.1"
75
+ },
76
+ "peerDependenciesMeta": {
77
+ "framer-motion": {
78
+ "optional": true
79
+ }
80
+ },
81
+ "scripts": {
82
+ "build": "tsup --tsconfig tsconfig.build.json",
83
+ "dev": "tsup --watch"
84
+ }
85
+ }