@number-flow/react 0.1.3 → 0.2.1

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/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Maxwell Barvian
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,10 +1,11 @@
1
+ [![NumberFlow for React](https://number-flow.barvian.me/preview.webp)](https://number-flow.barvian.me)
2
+
1
3
  # Number Flow for React
2
4
 
3
5
  A React component to transition & format numbers.
4
6
 
5
- [![NumberFlow for React](https://number-flow.barvian.me/preview.webp)](https://number-flow.barvian.me)
6
-
7
7
  [![NPM Version](https://img.shields.io/npm/v/@number-flow/react.svg)](https://npmjs.com/package/@number-flow/react)
8
+ [![Bundle size](https://badgen.net/bundlephobia/minzip/@number-flow/react@latest)](https://bundlephobia.com/package/@number-flow/react@latest)
8
9
  [![Follow @mbarvian](https://img.shields.io/twitter/follow/mbarvian.svg?style=social&label=Follow)](https://x.com/mbarvian)
9
10
 
10
11
  ---
package/dist/index.d.mts CHANGED
@@ -1,7 +1,6 @@
1
1
  import * as React from 'react';
2
2
  import { NumberFlowLite, Value, Format } from 'number-flow';
3
- export * from 'number-flow';
4
- export { defaultXTiming, defaultYTiming } from 'number-flow';
3
+ export { Format, Trend, Value } from 'number-flow';
5
4
 
6
5
  declare const OBSERVED_ATTRIBUTES: readonly ["parts"];
7
6
  type ObservedAttribute = (typeof OBSERVED_ATTRIBUTES)[number];
@@ -13,27 +12,35 @@ type NumberFlowProps = React.HTMLAttributes<NumberFlowElement> & {
13
12
  value: Value;
14
13
  locales?: Intl.LocalesArgument;
15
14
  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'];
15
+ isolate?: boolean;
16
+ animated?: boolean;
17
+ respectMotionPreference?: boolean;
18
+ willChange?: boolean;
19
+ onAnimationsStart?: () => void;
20
+ onAnimationsFinish?: () => void;
21
+ trend?: (typeof NumberFlowElement)['prototype']['trend'];
22
+ opacityTiming?: (typeof NumberFlowElement)['prototype']['opacityTiming'];
23
+ transformTiming?: (typeof NumberFlowElement)['prototype']['transformTiming'];
24
+ spinTiming?: (typeof NumberFlowElement)['prototype']['spinTiming'];
20
25
  };
21
26
  declare const NumberFlow: React.ForwardRefExoticComponent<React.HTMLAttributes<NumberFlowElement> & {
22
27
  value: Value;
23
28
  locales?: Intl.LocalesArgument;
24
29
  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"];
30
+ isolate?: boolean;
31
+ animated?: boolean;
32
+ respectMotionPreference?: boolean;
33
+ willChange?: boolean;
34
+ onAnimationsStart?: () => void;
35
+ onAnimationsFinish?: () => void;
36
+ trend?: (typeof NumberFlowElement)["prototype"]["trend"];
37
+ opacityTiming?: (typeof NumberFlowElement)["prototype"]["opacityTiming"];
38
+ transformTiming?: (typeof NumberFlowElement)["prototype"]["transformTiming"];
39
+ spinTiming?: (typeof NumberFlowElement)["prototype"]["spinTiming"];
29
40
  } & React.RefAttributes<NumberFlowElement>>;
30
41
 
31
- declare function useSupportsLinear(): boolean;
32
- type LinearEasing = `linear(${string})`;
33
- type LinearTiming = EffectTiming & {
34
- easing: LinearEasing;
35
- };
36
- declare function useLinear(linear: LinearEasing, fallback: string): string;
37
- declare function useLinear(linear: LinearTiming, fallback: EffectTiming): EffectTiming;
42
+ declare function useCanAnimate({ respectMotionPreference }?: {
43
+ respectMotionPreference?: boolean | undefined;
44
+ }): boolean;
38
45
 
39
- export { NumberFlowElement, type NumberFlowProps, NumberFlow as default, useLinear, useSupportsLinear };
46
+ export { NumberFlowElement, type NumberFlowProps, NumberFlow as default, useCanAnimate };
package/dist/index.d.ts CHANGED
@@ -1,7 +1,6 @@
1
1
  import * as React from 'react';
2
2
  import { NumberFlowLite, Value, Format } from 'number-flow';
3
- export * from 'number-flow';
4
- export { defaultXTiming, defaultYTiming } from 'number-flow';
3
+ export { Format, Trend, Value } from 'number-flow';
5
4
 
6
5
  declare const OBSERVED_ATTRIBUTES: readonly ["parts"];
7
6
  type ObservedAttribute = (typeof OBSERVED_ATTRIBUTES)[number];
@@ -13,27 +12,35 @@ type NumberFlowProps = React.HTMLAttributes<NumberFlowElement> & {
13
12
  value: Value;
14
13
  locales?: Intl.LocalesArgument;
15
14
  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'];
15
+ isolate?: boolean;
16
+ animated?: boolean;
17
+ respectMotionPreference?: boolean;
18
+ willChange?: boolean;
19
+ onAnimationsStart?: () => void;
20
+ onAnimationsFinish?: () => void;
21
+ trend?: (typeof NumberFlowElement)['prototype']['trend'];
22
+ opacityTiming?: (typeof NumberFlowElement)['prototype']['opacityTiming'];
23
+ transformTiming?: (typeof NumberFlowElement)['prototype']['transformTiming'];
24
+ spinTiming?: (typeof NumberFlowElement)['prototype']['spinTiming'];
20
25
  };
21
26
  declare const NumberFlow: React.ForwardRefExoticComponent<React.HTMLAttributes<NumberFlowElement> & {
22
27
  value: Value;
23
28
  locales?: Intl.LocalesArgument;
24
29
  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"];
30
+ isolate?: boolean;
31
+ animated?: boolean;
32
+ respectMotionPreference?: boolean;
33
+ willChange?: boolean;
34
+ onAnimationsStart?: () => void;
35
+ onAnimationsFinish?: () => void;
36
+ trend?: (typeof NumberFlowElement)["prototype"]["trend"];
37
+ opacityTiming?: (typeof NumberFlowElement)["prototype"]["opacityTiming"];
38
+ transformTiming?: (typeof NumberFlowElement)["prototype"]["transformTiming"];
39
+ spinTiming?: (typeof NumberFlowElement)["prototype"]["spinTiming"];
29
40
  } & React.RefAttributes<NumberFlowElement>>;
30
41
 
31
- declare function useSupportsLinear(): boolean;
32
- type LinearEasing = `linear(${string})`;
33
- type LinearTiming = EffectTiming & {
34
- easing: LinearEasing;
35
- };
36
- declare function useLinear(linear: LinearEasing, fallback: string): string;
37
- declare function useLinear(linear: LinearTiming, fallback: EffectTiming): EffectTiming;
42
+ declare function useCanAnimate({ respectMotionPreference }?: {
43
+ respectMotionPreference?: boolean | undefined;
44
+ }): boolean;
38
45
 
39
- export { NumberFlowElement, type NumberFlowProps, NumberFlow as default, useLinear, useSupportsLinear };
46
+ export { NumberFlowElement, type NumberFlowProps, NumberFlow as default, useCanAnimate };
package/dist/index.js CHANGED
@@ -6,6 +6,9 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
6
  var __getOwnPropNames = Object.getOwnPropertyNames;
7
7
  var __getProtoOf = Object.getPrototypeOf;
8
8
  var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __typeError = (msg) => {
10
+ throw TypeError(msg);
11
+ };
9
12
  var __export = (target, all) => {
10
13
  for (var name in all)
11
14
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -27,71 +30,94 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
27
30
  mod
28
31
  ));
29
32
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
33
+ var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
34
+ var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
35
+ var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
36
+ var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
30
37
 
31
38
  // src/index.tsx
32
39
  var src_exports = {};
33
40
  __export(src_exports, {
34
41
  NumberFlowElement: () => NumberFlowElement,
35
42
  default: () => src_default,
36
- defaultXTiming: () => import_number_flow2.defaultXTiming,
37
- defaultYTiming: () => import_number_flow2.defaultYTiming,
38
- useLinear: () => useLinear,
39
- useSupportsLinear: () => useSupportsLinear
43
+ useCanAnimate: () => useCanAnimate
40
44
  });
41
45
  module.exports = __toCommonJS(src_exports);
42
46
  var React = __toESM(require("react"));
43
47
  var import_number_flow = require("number-flow");
44
- var import_number_flow2 = require("number-flow");
48
+ var isReact19 = React.version.startsWith("19.");
45
49
  var OBSERVED_ATTRIBUTES = ["parts"];
46
50
  var NumberFlowElement = class extends import_number_flow.NumberFlowLite {
47
- static observedAttributes = OBSERVED_ATTRIBUTES;
48
51
  attributeChangedCallback(attr, _oldValue, newValue) {
49
52
  this[attr] = JSON.parse(newValue);
50
53
  }
51
54
  };
55
+ NumberFlowElement.observedAttributes = OBSERVED_ATTRIBUTES;
52
56
  NumberFlowElement.define();
53
57
  var formatters = {};
54
- var NumberFlowPriv = class extends React.Component {
58
+ var serializeParts = isReact19 ? (p) => p : JSON.stringify;
59
+ var _el;
60
+ var NumberFlowImpl = class extends React.Component {
55
61
  constructor(props) {
56
62
  super(props);
63
+ __privateAdd(this, _el);
57
64
  this.handleRef = this.handleRef.bind(this);
58
65
  }
59
66
  // Update the non-parts props to avoid JSON serialization
60
67
  // Parts needs to be set in render still:
61
- updateNonPartsProps() {
62
- if (this.#el) {
63
- if (this.props.root != null) this.#el.root = this.props.root;
64
- if (this.props.fadeTiming) this.#el.fadeTiming = this.props.fadeTiming;
65
- if (this.props.xTiming) this.#el.xTiming = this.props.xTiming;
66
- if (this.props.yTiming) this.#el.yTiming = this.props.yTiming;
67
- }
68
+ updateNonPartsProps(prevProps) {
69
+ if (!__privateGet(this, _el)) return;
70
+ __privateGet(this, _el).manual = !this.props.isolate;
71
+ if (this.props.animated != null) __privateGet(this, _el).animated = this.props.animated;
72
+ if (this.props.respectMotionPreference != null)
73
+ __privateGet(this, _el).respectMotionPreference = this.props.respectMotionPreference;
74
+ if (this.props.trend != null) __privateGet(this, _el).trend = this.props.trend;
75
+ if (this.props.opacityTiming) __privateGet(this, _el).opacityTiming = this.props.opacityTiming;
76
+ if (this.props.transformTiming) __privateGet(this, _el).transformTiming = this.props.transformTiming;
77
+ if (this.props.spinTiming) __privateGet(this, _el).spinTiming = this.props.spinTiming;
78
+ if (prevProps?.onAnimationsStart)
79
+ __privateGet(this, _el).removeEventListener("animationsstart", prevProps.onAnimationsStart);
80
+ if (this.props.onAnimationsStart)
81
+ __privateGet(this, _el).addEventListener("animationsstart", this.props.onAnimationsStart);
82
+ if (prevProps?.onAnimationsFinish)
83
+ __privateGet(this, _el).removeEventListener("animationsfinish", prevProps.onAnimationsFinish);
84
+ if (this.props.onAnimationsFinish)
85
+ __privateGet(this, _el).addEventListener("animationsfinish", this.props.onAnimationsFinish);
68
86
  }
69
87
  componentDidMount() {
88
+ if (isReact19 && __privateGet(this, _el)) {
89
+ __privateGet(this, _el).parts = this.props.parts;
90
+ }
70
91
  this.updateNonPartsProps();
71
- if (this.#el) this.#el.manual = true;
72
92
  }
73
- getSnapshotBeforeUpdate() {
74
- this.updateNonPartsProps();
75
- this.#el?.willUpdate();
76
- return null;
93
+ getSnapshotBeforeUpdate(prevProps) {
94
+ this.updateNonPartsProps(prevProps);
95
+ if (this.props.isolate) return false;
96
+ if (prevProps.parts === this.props.parts) return false;
97
+ __privateGet(this, _el)?.willUpdate();
98
+ return true;
77
99
  }
78
- componentDidUpdate() {
79
- this.#el?.didUpdate();
100
+ componentDidUpdate(_, __, snapshot) {
101
+ if (snapshot) __privateGet(this, _el)?.didUpdate();
80
102
  }
81
- #el;
82
103
  handleRef(el) {
83
104
  if (this.props.innerRef) this.props.innerRef.current = el;
84
- this.#el = el;
105
+ __privateSet(this, _el, el);
85
106
  }
86
107
  render() {
87
108
  const {
88
109
  innerRef,
89
110
  className,
90
111
  parts,
112
+ willChange,
91
113
  // These are set in updateNonPartsProps, so ignore them here:
92
- fadeTiming,
93
- xTiming,
94
- yTiming,
114
+ animated,
115
+ respectMotionPreference,
116
+ isolate,
117
+ trend,
118
+ opacityTiming,
119
+ transformTiming,
120
+ spinTiming,
95
121
  ...rest
96
122
  } = this.props;
97
123
  return (
@@ -100,15 +126,17 @@ var NumberFlowPriv = class extends React.Component {
100
126
  "number-flow",
101
127
  {
102
128
  ref: this.handleRef,
129
+ "data-will-change": willChange ? "" : void 0,
103
130
  class: className,
104
131
  ...rest,
105
- parts: JSON.stringify(parts)
132
+ parts: serializeParts(parts)
106
133
  },
107
- /* @__PURE__ */ React.createElement(import_number_flow.SlottedTag, { style: import_number_flow.slottedStyles }, parts.formatted)
134
+ /* @__PURE__ */ React.createElement(import_number_flow.SlottedTag, { style: (0, import_number_flow.slottedStyles)({ willChange }) }, parts.formatted)
108
135
  )
109
136
  );
110
137
  }
111
138
  };
139
+ _el = new WeakMap();
112
140
  var NumberFlow = React.forwardRef(function NumberFlow2({ value, locales, format, ...props }, _ref) {
113
141
  React.useImperativeHandle(_ref, () => ref.current, []);
114
142
  const ref = React.useRef();
@@ -121,25 +149,30 @@ var NumberFlow = React.forwardRef(function NumberFlow2({ value, locales, format,
121
149
  );
122
150
  return (0, import_number_flow.partitionParts)(value, formatter);
123
151
  }, [value, localesString, formatString]);
124
- return /* @__PURE__ */ React.createElement(NumberFlowPriv, { ...props, parts, innerRef: ref });
152
+ return /* @__PURE__ */ React.createElement(NumberFlowImpl, { ...props, parts, innerRef: ref });
125
153
  });
126
154
  var src_default = NumberFlow;
127
- function useSupportsLinear() {
128
- const [supported, setSupported] = React.useState(false);
155
+ function useCanAnimate({ respectMotionPreference = true } = {}) {
156
+ const [canAnimate, setCanAnimate] = React.useState(import_number_flow.canAnimate);
157
+ const [reducedMotion, setReducedMotion] = React.useState(false);
129
158
  React.useEffect(() => {
130
- setSupported(import_number_flow.supportsLinear);
159
+ setCanAnimate(import_number_flow.canAnimate);
160
+ setReducedMotion(import_number_flow.prefersReducedMotion?.matches ?? false);
131
161
  }, []);
132
- return supported;
133
- }
134
- function useLinear(linear, fallback) {
135
- const supported = useSupportsLinear();
136
- return supported ? linear : fallback;
162
+ React.useEffect(() => {
163
+ if (!respectMotionPreference) return;
164
+ const onChange = ({ matches }) => {
165
+ setReducedMotion(matches);
166
+ };
167
+ import_number_flow.prefersReducedMotion?.addEventListener("change", onChange);
168
+ return () => {
169
+ import_number_flow.prefersReducedMotion?.removeEventListener("change", onChange);
170
+ };
171
+ }, [respectMotionPreference]);
172
+ return canAnimate && (!respectMotionPreference || !reducedMotion);
137
173
  }
138
174
  // Annotate the CommonJS export names for ESM import in node:
139
175
  0 && (module.exports = {
140
176
  NumberFlowElement,
141
- defaultXTiming,
142
- defaultYTiming,
143
- useLinear,
144
- useSupportsLinear
177
+ useCanAnimate
145
178
  });
package/dist/index.mjs CHANGED
@@ -1,4 +1,11 @@
1
1
  "use client";
2
+ var __typeError = (msg) => {
3
+ throw TypeError(msg);
4
+ };
5
+ var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
6
+ var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
7
+ var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
8
+ var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
2
9
 
3
10
  // src/index.tsx
4
11
  import * as React from "react";
@@ -7,59 +14,82 @@ import {
7
14
  slottedStyles,
8
15
  partitionParts,
9
16
  NumberFlowLite,
10
- supportsLinear
17
+ prefersReducedMotion,
18
+ canAnimate as _canAnimate
11
19
  } from "number-flow";
12
- import { defaultXTiming, defaultYTiming } from "number-flow";
20
+ var isReact19 = React.version.startsWith("19.");
13
21
  var OBSERVED_ATTRIBUTES = ["parts"];
14
22
  var NumberFlowElement = class extends NumberFlowLite {
15
- static observedAttributes = OBSERVED_ATTRIBUTES;
16
23
  attributeChangedCallback(attr, _oldValue, newValue) {
17
24
  this[attr] = JSON.parse(newValue);
18
25
  }
19
26
  };
27
+ NumberFlowElement.observedAttributes = OBSERVED_ATTRIBUTES;
20
28
  NumberFlowElement.define();
21
29
  var formatters = {};
22
- var NumberFlowPriv = class extends React.Component {
30
+ var serializeParts = isReact19 ? (p) => p : JSON.stringify;
31
+ var _el;
32
+ var NumberFlowImpl = class extends React.Component {
23
33
  constructor(props) {
24
34
  super(props);
35
+ __privateAdd(this, _el);
25
36
  this.handleRef = this.handleRef.bind(this);
26
37
  }
27
38
  // Update the non-parts props to avoid JSON serialization
28
39
  // Parts needs to be set in render still:
29
- updateNonPartsProps() {
30
- if (this.#el) {
31
- if (this.props.root != null) this.#el.root = this.props.root;
32
- if (this.props.fadeTiming) this.#el.fadeTiming = this.props.fadeTiming;
33
- if (this.props.xTiming) this.#el.xTiming = this.props.xTiming;
34
- if (this.props.yTiming) this.#el.yTiming = this.props.yTiming;
35
- }
40
+ updateNonPartsProps(prevProps) {
41
+ if (!__privateGet(this, _el)) return;
42
+ __privateGet(this, _el).manual = !this.props.isolate;
43
+ if (this.props.animated != null) __privateGet(this, _el).animated = this.props.animated;
44
+ if (this.props.respectMotionPreference != null)
45
+ __privateGet(this, _el).respectMotionPreference = this.props.respectMotionPreference;
46
+ if (this.props.trend != null) __privateGet(this, _el).trend = this.props.trend;
47
+ if (this.props.opacityTiming) __privateGet(this, _el).opacityTiming = this.props.opacityTiming;
48
+ if (this.props.transformTiming) __privateGet(this, _el).transformTiming = this.props.transformTiming;
49
+ if (this.props.spinTiming) __privateGet(this, _el).spinTiming = this.props.spinTiming;
50
+ if (prevProps?.onAnimationsStart)
51
+ __privateGet(this, _el).removeEventListener("animationsstart", prevProps.onAnimationsStart);
52
+ if (this.props.onAnimationsStart)
53
+ __privateGet(this, _el).addEventListener("animationsstart", this.props.onAnimationsStart);
54
+ if (prevProps?.onAnimationsFinish)
55
+ __privateGet(this, _el).removeEventListener("animationsfinish", prevProps.onAnimationsFinish);
56
+ if (this.props.onAnimationsFinish)
57
+ __privateGet(this, _el).addEventListener("animationsfinish", this.props.onAnimationsFinish);
36
58
  }
37
59
  componentDidMount() {
60
+ if (isReact19 && __privateGet(this, _el)) {
61
+ __privateGet(this, _el).parts = this.props.parts;
62
+ }
38
63
  this.updateNonPartsProps();
39
- if (this.#el) this.#el.manual = true;
40
64
  }
41
- getSnapshotBeforeUpdate() {
42
- this.updateNonPartsProps();
43
- this.#el?.willUpdate();
44
- return null;
65
+ getSnapshotBeforeUpdate(prevProps) {
66
+ this.updateNonPartsProps(prevProps);
67
+ if (this.props.isolate) return false;
68
+ if (prevProps.parts === this.props.parts) return false;
69
+ __privateGet(this, _el)?.willUpdate();
70
+ return true;
45
71
  }
46
- componentDidUpdate() {
47
- this.#el?.didUpdate();
72
+ componentDidUpdate(_, __, snapshot) {
73
+ if (snapshot) __privateGet(this, _el)?.didUpdate();
48
74
  }
49
- #el;
50
75
  handleRef(el) {
51
76
  if (this.props.innerRef) this.props.innerRef.current = el;
52
- this.#el = el;
77
+ __privateSet(this, _el, el);
53
78
  }
54
79
  render() {
55
80
  const {
56
81
  innerRef,
57
82
  className,
58
83
  parts,
84
+ willChange,
59
85
  // These are set in updateNonPartsProps, so ignore them here:
60
- fadeTiming,
61
- xTiming,
62
- yTiming,
86
+ animated,
87
+ respectMotionPreference,
88
+ isolate,
89
+ trend,
90
+ opacityTiming,
91
+ transformTiming,
92
+ spinTiming,
63
93
  ...rest
64
94
  } = this.props;
65
95
  return (
@@ -68,15 +98,17 @@ var NumberFlowPriv = class extends React.Component {
68
98
  "number-flow",
69
99
  {
70
100
  ref: this.handleRef,
101
+ "data-will-change": willChange ? "" : void 0,
71
102
  class: className,
72
103
  ...rest,
73
- parts: JSON.stringify(parts)
104
+ parts: serializeParts(parts)
74
105
  },
75
- /* @__PURE__ */ React.createElement(SlottedTag, { style: slottedStyles }, parts.formatted)
106
+ /* @__PURE__ */ React.createElement(SlottedTag, { style: slottedStyles({ willChange }) }, parts.formatted)
76
107
  )
77
108
  );
78
109
  }
79
110
  };
111
+ _el = new WeakMap();
80
112
  var NumberFlow = React.forwardRef(function NumberFlow2({ value, locales, format, ...props }, _ref) {
81
113
  React.useImperativeHandle(_ref, () => ref.current, []);
82
114
  const ref = React.useRef();
@@ -89,25 +121,30 @@ var NumberFlow = React.forwardRef(function NumberFlow2({ value, locales, format,
89
121
  );
90
122
  return partitionParts(value, formatter);
91
123
  }, [value, localesString, formatString]);
92
- return /* @__PURE__ */ React.createElement(NumberFlowPriv, { ...props, parts, innerRef: ref });
124
+ return /* @__PURE__ */ React.createElement(NumberFlowImpl, { ...props, parts, innerRef: ref });
93
125
  });
94
126
  var src_default = NumberFlow;
95
- function useSupportsLinear() {
96
- const [supported, setSupported] = React.useState(false);
127
+ function useCanAnimate({ respectMotionPreference = true } = {}) {
128
+ const [canAnimate, setCanAnimate] = React.useState(_canAnimate);
129
+ const [reducedMotion, setReducedMotion] = React.useState(false);
97
130
  React.useEffect(() => {
98
- setSupported(supportsLinear);
131
+ setCanAnimate(_canAnimate);
132
+ setReducedMotion(prefersReducedMotion?.matches ?? false);
99
133
  }, []);
100
- return supported;
101
- }
102
- function useLinear(linear, fallback) {
103
- const supported = useSupportsLinear();
104
- return supported ? linear : fallback;
134
+ React.useEffect(() => {
135
+ if (!respectMotionPreference) return;
136
+ const onChange = ({ matches }) => {
137
+ setReducedMotion(matches);
138
+ };
139
+ prefersReducedMotion?.addEventListener("change", onChange);
140
+ return () => {
141
+ prefersReducedMotion?.removeEventListener("change", onChange);
142
+ };
143
+ }, [respectMotionPreference]);
144
+ return canAnimate && (!respectMotionPreference || !reducedMotion);
105
145
  }
106
146
  export {
107
147
  NumberFlowElement,
108
148
  src_default as default,
109
- defaultXTiming,
110
- defaultYTiming,
111
- useLinear,
112
- useSupportsLinear
149
+ useCanAnimate
113
150
  };
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "0.1.3",
6
+ "version": "0.2.1",
7
7
  "author": {
8
8
  "name": "Maxwell Barvian",
9
9
  "email": "max@barvian.me",
@@ -45,44 +45,26 @@
45
45
  "types": "./dist/index.d.ts",
46
46
  "default": "./dist/index.js"
47
47
  }
48
- },
49
- "./framer-motion": {
50
- "import": {
51
- "types": "./dist/framer-motion.d.mts",
52
- "default": "./dist/framer-motion.mjs"
53
- },
54
- "require": {
55
- "types": "./dist/framer-motion.d.ts",
56
- "default": "./dist/framer-motion.js"
57
- }
58
48
  }
59
49
  },
60
50
  "dependencies": {
61
51
  "esm-env": "^1.0.0",
62
- "number-flow": "0.2.3"
52
+ "number-flow": "0.3.0"
63
53
  },
64
54
  "devDependencies": {
65
55
  "@types/react": "^18.3.3",
66
56
  "@types/react-dom": "^18.3.0",
67
- "framer-motion": "^11.9.0",
68
57
  "react": "^18.3.1",
69
58
  "react-dom": "^18.3.1",
70
59
  "tsup": "^8.2.4"
71
60
  },
72
61
  "peerDependencies": {
73
- "@types/react": "^18.3.3",
74
- "@types/react-dom": "^18.3.0",
75
- "framer-motion": "^11.0.0",
76
- "react": "^18.3.1",
77
- "react-dom": "^18.3.1"
78
- },
79
- "peerDependenciesMeta": {
80
- "framer-motion": {
81
- "optional": true
82
- }
62
+ "react": "^18 || ^19.0.0-rc-915b914b3a-20240515",
63
+ "react-dom": "^18"
83
64
  },
84
65
  "scripts": {
85
66
  "build": "tsup --tsconfig tsconfig.build.json",
86
- "dev": "tsup --watch"
67
+ "dev": "tsup --watch",
68
+ "test": "pnpm -r --workspace-concurrency 1 --filter=\"./test/apps/*\" test"
87
69
  }
88
70
  }
@@ -1,7 +0,0 @@
1
- import { Easing, Transition } from 'framer-motion';
2
-
3
- declare function toEase(_easing: string): Easing;
4
- declare function toTransition(timing: EffectTiming): Transition;
5
- declare function useTiming(timing: EffectTiming): Transition;
6
-
7
- export { toEase, toTransition, useTiming };
@@ -1,7 +0,0 @@
1
- import { Easing, Transition } from 'framer-motion';
2
-
3
- declare function toEase(_easing: string): Easing;
4
- declare function toTransition(timing: EffectTiming): Transition;
5
- declare function useTiming(timing: EffectTiming): Transition;
6
-
7
- export { toEase, toTransition, useTiming };
@@ -1,100 +0,0 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
-
30
- // src/framer-motion.ts
31
- var framer_motion_exports = {};
32
- __export(framer_motion_exports, {
33
- toEase: () => toEase,
34
- toTransition: () => toTransition,
35
- useTiming: () => useTiming
36
- });
37
- module.exports = __toCommonJS(framer_motion_exports);
38
- var React = __toESM(require("react"));
39
- var import_framer_motion = require("framer-motion");
40
- var bezierRegExp = /^cubic-bezier\((.*?),(.*?),(.*?),(.*?)\)$/;
41
- var linearRegExp = /^linear\((.*?)\)$/;
42
- function toEase(_easing) {
43
- const easing = _easing.trim();
44
- if (easing === "linear") return easing;
45
- if (easing === "ease-in") return "easeIn";
46
- if (easing === "ease-out") return "easeOut";
47
- if (easing === "ease-in-out") return "easeInOut";
48
- const bezierMatch = easing.match(bezierRegExp);
49
- if (bezierMatch) {
50
- return bezierMatch.slice(1).map(parseFloat);
51
- }
52
- const linearMatch = easing.match(linearRegExp);
53
- const linearExpr = linearMatch?.[1];
54
- if (linearExpr) {
55
- const points = linearExpr.split(",");
56
- const times = [];
57
- const values = [];
58
- try {
59
- points.forEach((point, i) => {
60
- const [p1, p2] = point.trim().split(/\s+/);
61
- if (p1?.includes("%")) {
62
- times.push(parseFloat(p1) / 100);
63
- values.push(parseFloat(p2));
64
- } else if (p2?.includes("%")) {
65
- times.push(parseFloat(p2) / 100);
66
- values.push(parseFloat(p1));
67
- } else {
68
- times.push(i / (points.length - 1));
69
- values.push(parseFloat(p1));
70
- }
71
- });
72
- } catch {
73
- throw new Error(`Cannot parse linear() expression: ${easing}`);
74
- }
75
- return (0, import_framer_motion.transform)(times, values);
76
- }
77
- throw new Error(`Cannot convert ${easing} to a Framer Motion easing`);
78
- }
79
- function toTransition(timing) {
80
- const { duration = 0, delay = 0, easing = "ease" } = timing;
81
- if (typeof duration != "number") throw new Error("Duration must be a number");
82
- return {
83
- duration: duration / 1e3,
84
- delay: delay / 1e3,
85
- ease: toEase(easing)
86
- };
87
- }
88
- function useTiming(timing) {
89
- const [ret, setRet] = React.useState(toTransition(timing));
90
- React.useEffect(() => {
91
- setRet(toTransition(timing));
92
- }, [timing]);
93
- return ret;
94
- }
95
- // Annotate the CommonJS export names for ESM import in node:
96
- 0 && (module.exports = {
97
- toEase,
98
- toTransition,
99
- useTiming
100
- });
@@ -1,63 +0,0 @@
1
- // src/framer-motion.ts
2
- import * as React from "react";
3
- import { transform } from "framer-motion";
4
- var bezierRegExp = /^cubic-bezier\((.*?),(.*?),(.*?),(.*?)\)$/;
5
- var linearRegExp = /^linear\((.*?)\)$/;
6
- function toEase(_easing) {
7
- const easing = _easing.trim();
8
- if (easing === "linear") return easing;
9
- if (easing === "ease-in") return "easeIn";
10
- if (easing === "ease-out") return "easeOut";
11
- if (easing === "ease-in-out") return "easeInOut";
12
- const bezierMatch = easing.match(bezierRegExp);
13
- if (bezierMatch) {
14
- return bezierMatch.slice(1).map(parseFloat);
15
- }
16
- const linearMatch = easing.match(linearRegExp);
17
- const linearExpr = linearMatch?.[1];
18
- if (linearExpr) {
19
- const points = linearExpr.split(",");
20
- const times = [];
21
- const values = [];
22
- try {
23
- points.forEach((point, i) => {
24
- const [p1, p2] = point.trim().split(/\s+/);
25
- if (p1?.includes("%")) {
26
- times.push(parseFloat(p1) / 100);
27
- values.push(parseFloat(p2));
28
- } else if (p2?.includes("%")) {
29
- times.push(parseFloat(p2) / 100);
30
- values.push(parseFloat(p1));
31
- } else {
32
- times.push(i / (points.length - 1));
33
- values.push(parseFloat(p1));
34
- }
35
- });
36
- } catch {
37
- throw new Error(`Cannot parse linear() expression: ${easing}`);
38
- }
39
- return transform(times, values);
40
- }
41
- throw new Error(`Cannot convert ${easing} to a Framer Motion easing`);
42
- }
43
- function toTransition(timing) {
44
- const { duration = 0, delay = 0, easing = "ease" } = timing;
45
- if (typeof duration != "number") throw new Error("Duration must be a number");
46
- return {
47
- duration: duration / 1e3,
48
- delay: delay / 1e3,
49
- ease: toEase(easing)
50
- };
51
- }
52
- function useTiming(timing) {
53
- const [ret, setRet] = React.useState(toTransition(timing));
54
- React.useEffect(() => {
55
- setRet(toTransition(timing));
56
- }, [timing]);
57
- return ret;
58
- }
59
- export {
60
- toEase,
61
- toTransition,
62
- useTiming
63
- };