@number-flow/react 0.3.1 → 0.3.3

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/dist/index.d.mts CHANGED
@@ -1,11 +1,11 @@
1
1
  import * as React from 'react';
2
2
  import { NumberFlowLite, Value, Format, Props } from 'number-flow';
3
- export { Format, Trend, Value } from 'number-flow';
3
+ export { Format, NumberPartType, Trend, Value } from 'number-flow';
4
4
 
5
- declare const OBSERVED_ATTRIBUTES: readonly ["parts"];
5
+ declare const OBSERVED_ATTRIBUTES: readonly ["data"];
6
6
  type ObservedAttribute = (typeof OBSERVED_ATTRIBUTES)[number];
7
7
  declare class NumberFlowElement extends NumberFlowLite {
8
- static observedAttributes: readonly ["parts"] | never[];
8
+ static observedAttributes: readonly ["data"] | never[];
9
9
  attributeChangedCallback(attr: ObservedAttribute, _oldValue: string, newValue: string): void;
10
10
  }
11
11
  type BaseProps = React.HTMLAttributes<NumberFlowElement> & Partial<Props> & {
@@ -18,6 +18,8 @@ type NumberFlowProps = BaseProps & {
18
18
  value: Value;
19
19
  locales?: Intl.LocalesArgument;
20
20
  format?: Format;
21
+ prefix?: string;
22
+ suffix?: string;
21
23
  };
22
24
  declare const NumberFlow: React.ForwardRefExoticComponent<React.HTMLAttributes<NumberFlowElement> & Partial<Props> & {
23
25
  isolate?: boolean;
@@ -28,6 +30,8 @@ declare const NumberFlow: React.ForwardRefExoticComponent<React.HTMLAttributes<N
28
30
  value: Value;
29
31
  locales?: Intl.LocalesArgument;
30
32
  format?: Format;
33
+ prefix?: string;
34
+ suffix?: string;
31
35
  } & React.RefAttributes<NumberFlowElement>>;
32
36
 
33
37
  declare function useCanAnimate({ respectMotionPreference }?: {
package/dist/index.d.ts CHANGED
@@ -1,11 +1,11 @@
1
1
  import * as React from 'react';
2
2
  import { NumberFlowLite, Value, Format, Props } from 'number-flow';
3
- export { Format, Trend, Value } from 'number-flow';
3
+ export { Format, NumberPartType, Trend, Value } from 'number-flow';
4
4
 
5
- declare const OBSERVED_ATTRIBUTES: readonly ["parts"];
5
+ declare const OBSERVED_ATTRIBUTES: readonly ["data"];
6
6
  type ObservedAttribute = (typeof OBSERVED_ATTRIBUTES)[number];
7
7
  declare class NumberFlowElement extends NumberFlowLite {
8
- static observedAttributes: readonly ["parts"] | never[];
8
+ static observedAttributes: readonly ["data"] | never[];
9
9
  attributeChangedCallback(attr: ObservedAttribute, _oldValue: string, newValue: string): void;
10
10
  }
11
11
  type BaseProps = React.HTMLAttributes<NumberFlowElement> & Partial<Props> & {
@@ -18,6 +18,8 @@ type NumberFlowProps = BaseProps & {
18
18
  value: Value;
19
19
  locales?: Intl.LocalesArgument;
20
20
  format?: Format;
21
+ prefix?: string;
22
+ suffix?: string;
21
23
  };
22
24
  declare const NumberFlow: React.ForwardRefExoticComponent<React.HTMLAttributes<NumberFlowElement> & Partial<Props> & {
23
25
  isolate?: boolean;
@@ -28,6 +30,8 @@ declare const NumberFlow: React.ForwardRefExoticComponent<React.HTMLAttributes<N
28
30
  value: Value;
29
31
  locales?: Intl.LocalesArgument;
30
32
  format?: Format;
33
+ prefix?: string;
34
+ suffix?: string;
31
35
  } & React.RefAttributes<NumberFlowElement>>;
32
36
 
33
37
  declare function useCanAnimate({ respectMotionPreference }?: {
package/dist/index.js CHANGED
@@ -47,7 +47,7 @@ var React = __toESM(require("react"));
47
47
  var import_number_flow = require("number-flow");
48
48
  var REACT_MAJOR = parseInt(React.version.match(/^(\d+)\./)?.[1]);
49
49
  var isReact19 = REACT_MAJOR >= 19;
50
- var OBSERVED_ATTRIBUTES = ["parts"];
50
+ var OBSERVED_ATTRIBUTES = ["data"];
51
51
  var NumberFlowElement = class extends import_number_flow.NumberFlowLite {
52
52
  attributeChangedCallback(attr, _oldValue, newValue) {
53
53
  this[attr] = JSON.parse(newValue);
@@ -56,7 +56,7 @@ var NumberFlowElement = class extends import_number_flow.NumberFlowLite {
56
56
  NumberFlowElement.observedAttributes = isReact19 ? [] : OBSERVED_ATTRIBUTES;
57
57
  (0, import_number_flow.define)("number-flow-react", NumberFlowElement);
58
58
  var formatters = {};
59
- var serializeParts = isReact19 ? (p) => p : JSON.stringify;
59
+ var serializeData = isReact19 ? (p) => p : JSON.stringify;
60
60
  function splitProps(props) {
61
61
  const {
62
62
  transformTiming,
@@ -88,15 +88,15 @@ var NumberFlowImpl = class extends React.Component {
88
88
  __privateAdd(this, _el);
89
89
  this.handleRef = this.handleRef.bind(this);
90
90
  }
91
- // Update the non-`parts` props to avoid JSON serialization
92
- // Parts needs to be set in render still:
93
- updateNonPartsProps(prevProps) {
91
+ // Update the non-`data` props to avoid JSON serialization
92
+ // Data needs to be set in render still:
93
+ updateNonDataProps(prevProps) {
94
94
  if (!__privateGet(this, _el)) return;
95
95
  __privateGet(this, _el).manual = !this.props.isolate;
96
- const [nonParts] = splitProps(this.props);
96
+ const [nonData] = splitProps(this.props);
97
97
  Object.assign(
98
98
  __privateGet(this, _el),
99
- Object.fromEntries(Object.entries(nonParts).filter(([_, v]) => v != null))
99
+ Object.fromEntries(Object.entries(nonData).filter(([_, v]) => v != null))
100
100
  );
101
101
  if (prevProps?.onAnimationsStart)
102
102
  __privateGet(this, _el).removeEventListener("animationsstart", prevProps.onAnimationsStart);
@@ -111,14 +111,14 @@ var NumberFlowImpl = class extends React.Component {
111
111
  __privateGet(this, _el).addEventListener("animationsfinish", this.props.onAnimationsFinish);
112
112
  }
113
113
  componentDidMount() {
114
- this.updateNonPartsProps();
114
+ this.updateNonDataProps();
115
115
  if (isReact19 && __privateGet(this, _el)) {
116
- __privateGet(this, _el).parts = this.props.parts;
116
+ __privateGet(this, _el).data = this.props.data;
117
117
  }
118
118
  }
119
119
  getSnapshotBeforeUpdate(prevProps) {
120
- this.updateNonPartsProps(prevProps);
121
- if (this.props.isolate || this.props.animated === false || prevProps.parts === this.props.parts)
120
+ this.updateNonDataProps(prevProps);
121
+ if (this.props.isolate || this.props.animated === false || prevProps.data === this.props.data)
122
122
  return false;
123
123
  __privateGet(this, _el)?.willUpdate();
124
124
  return true;
@@ -131,7 +131,7 @@ var NumberFlowImpl = class extends React.Component {
131
131
  __privateSet(this, _el, el);
132
132
  }
133
133
  render() {
134
- const [_, { innerRef, className, parts, willChange, isolate, ...rest }] = splitProps(this.props);
134
+ const [_, { innerRef, className, data, willChange, isolate, ...rest }] = splitProps(this.props);
135
135
  return (
136
136
  // @ts-expect-error missing types
137
137
  /* @__PURE__ */ React.createElement(
@@ -141,27 +141,29 @@ var NumberFlowImpl = class extends React.Component {
141
141
  "data-will-change": willChange ? "" : void 0,
142
142
  class: className,
143
143
  ...rest,
144
- dangerouslySetInnerHTML: { __html: (0, import_number_flow.render)({ formatted: parts.formatted, willChange }) },
145
- parts: serializeParts(parts)
144
+ dangerouslySetInnerHTML: {
145
+ __html: (0, import_number_flow.render)({ valueAsString: data.valueAsString, willChange })
146
+ },
147
+ data: serializeData(data)
146
148
  }
147
149
  )
148
150
  );
149
151
  }
150
152
  };
151
153
  _el = new WeakMap();
152
- var NumberFlow = React.forwardRef(function NumberFlow2({ value, locales, format, ...props }, _ref) {
154
+ var NumberFlow = React.forwardRef(function NumberFlow2({ value, locales, format, prefix, suffix, ...props }, _ref) {
153
155
  React.useImperativeHandle(_ref, () => ref.current, []);
154
156
  const ref = React.useRef();
155
157
  const localesString = React.useMemo(() => locales ? JSON.stringify(locales) : "", [locales]);
156
158
  const formatString = React.useMemo(() => format ? JSON.stringify(format) : "", [format]);
157
- const parts = React.useMemo(() => {
159
+ const data = React.useMemo(() => {
158
160
  const formatter = formatters[`${localesString}:${formatString}`] ??= new Intl.NumberFormat(
159
161
  locales,
160
162
  format
161
163
  );
162
- return (0, import_number_flow.partitionParts)(value, formatter);
163
- }, [value, localesString, formatString]);
164
- return /* @__PURE__ */ React.createElement(NumberFlowImpl, { ...props, parts, innerRef: ref });
164
+ return (0, import_number_flow.formatToData)(value, formatter, prefix, suffix);
165
+ }, [value, localesString, formatString, prefix, suffix]);
166
+ return /* @__PURE__ */ React.createElement(NumberFlowImpl, { ...props, data, innerRef: ref });
165
167
  });
166
168
  var src_default = NumberFlow;
167
169
  function useCanAnimate({ respectMotionPreference = true } = {}) {
package/dist/index.mjs CHANGED
@@ -11,7 +11,7 @@ var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "
11
11
  import * as React from "react";
12
12
  import {
13
13
  render,
14
- partitionParts,
14
+ formatToData,
15
15
  NumberFlowLite,
16
16
  prefersReducedMotion,
17
17
  canAnimate as _canAnimate,
@@ -19,7 +19,7 @@ import {
19
19
  } from "number-flow";
20
20
  var REACT_MAJOR = parseInt(React.version.match(/^(\d+)\./)?.[1]);
21
21
  var isReact19 = REACT_MAJOR >= 19;
22
- var OBSERVED_ATTRIBUTES = ["parts"];
22
+ var OBSERVED_ATTRIBUTES = ["data"];
23
23
  var NumberFlowElement = class extends NumberFlowLite {
24
24
  attributeChangedCallback(attr, _oldValue, newValue) {
25
25
  this[attr] = JSON.parse(newValue);
@@ -28,7 +28,7 @@ var NumberFlowElement = class extends NumberFlowLite {
28
28
  NumberFlowElement.observedAttributes = isReact19 ? [] : OBSERVED_ATTRIBUTES;
29
29
  define("number-flow-react", NumberFlowElement);
30
30
  var formatters = {};
31
- var serializeParts = isReact19 ? (p) => p : JSON.stringify;
31
+ var serializeData = isReact19 ? (p) => p : JSON.stringify;
32
32
  function splitProps(props) {
33
33
  const {
34
34
  transformTiming,
@@ -60,15 +60,15 @@ var NumberFlowImpl = class extends React.Component {
60
60
  __privateAdd(this, _el);
61
61
  this.handleRef = this.handleRef.bind(this);
62
62
  }
63
- // Update the non-`parts` props to avoid JSON serialization
64
- // Parts needs to be set in render still:
65
- updateNonPartsProps(prevProps) {
63
+ // Update the non-`data` props to avoid JSON serialization
64
+ // Data needs to be set in render still:
65
+ updateNonDataProps(prevProps) {
66
66
  if (!__privateGet(this, _el)) return;
67
67
  __privateGet(this, _el).manual = !this.props.isolate;
68
- const [nonParts] = splitProps(this.props);
68
+ const [nonData] = splitProps(this.props);
69
69
  Object.assign(
70
70
  __privateGet(this, _el),
71
- Object.fromEntries(Object.entries(nonParts).filter(([_, v]) => v != null))
71
+ Object.fromEntries(Object.entries(nonData).filter(([_, v]) => v != null))
72
72
  );
73
73
  if (prevProps?.onAnimationsStart)
74
74
  __privateGet(this, _el).removeEventListener("animationsstart", prevProps.onAnimationsStart);
@@ -83,14 +83,14 @@ var NumberFlowImpl = class extends React.Component {
83
83
  __privateGet(this, _el).addEventListener("animationsfinish", this.props.onAnimationsFinish);
84
84
  }
85
85
  componentDidMount() {
86
- this.updateNonPartsProps();
86
+ this.updateNonDataProps();
87
87
  if (isReact19 && __privateGet(this, _el)) {
88
- __privateGet(this, _el).parts = this.props.parts;
88
+ __privateGet(this, _el).data = this.props.data;
89
89
  }
90
90
  }
91
91
  getSnapshotBeforeUpdate(prevProps) {
92
- this.updateNonPartsProps(prevProps);
93
- if (this.props.isolate || this.props.animated === false || prevProps.parts === this.props.parts)
92
+ this.updateNonDataProps(prevProps);
93
+ if (this.props.isolate || this.props.animated === false || prevProps.data === this.props.data)
94
94
  return false;
95
95
  __privateGet(this, _el)?.willUpdate();
96
96
  return true;
@@ -103,7 +103,7 @@ var NumberFlowImpl = class extends React.Component {
103
103
  __privateSet(this, _el, el);
104
104
  }
105
105
  render() {
106
- const [_, { innerRef, className, parts, willChange, isolate, ...rest }] = splitProps(this.props);
106
+ const [_, { innerRef, className, data, willChange, isolate, ...rest }] = splitProps(this.props);
107
107
  return (
108
108
  // @ts-expect-error missing types
109
109
  /* @__PURE__ */ React.createElement(
@@ -113,27 +113,29 @@ var NumberFlowImpl = class extends React.Component {
113
113
  "data-will-change": willChange ? "" : void 0,
114
114
  class: className,
115
115
  ...rest,
116
- dangerouslySetInnerHTML: { __html: render({ formatted: parts.formatted, willChange }) },
117
- parts: serializeParts(parts)
116
+ dangerouslySetInnerHTML: {
117
+ __html: render({ valueAsString: data.valueAsString, willChange })
118
+ },
119
+ data: serializeData(data)
118
120
  }
119
121
  )
120
122
  );
121
123
  }
122
124
  };
123
125
  _el = new WeakMap();
124
- var NumberFlow = React.forwardRef(function NumberFlow2({ value, locales, format, ...props }, _ref) {
126
+ var NumberFlow = React.forwardRef(function NumberFlow2({ value, locales, format, prefix, suffix, ...props }, _ref) {
125
127
  React.useImperativeHandle(_ref, () => ref.current, []);
126
128
  const ref = React.useRef();
127
129
  const localesString = React.useMemo(() => locales ? JSON.stringify(locales) : "", [locales]);
128
130
  const formatString = React.useMemo(() => format ? JSON.stringify(format) : "", [format]);
129
- const parts = React.useMemo(() => {
131
+ const data = React.useMemo(() => {
130
132
  const formatter = formatters[`${localesString}:${formatString}`] ??= new Intl.NumberFormat(
131
133
  locales,
132
134
  format
133
135
  );
134
- return partitionParts(value, formatter);
135
- }, [value, localesString, formatString]);
136
- return /* @__PURE__ */ React.createElement(NumberFlowImpl, { ...props, parts, innerRef: ref });
136
+ return formatToData(value, formatter, prefix, suffix);
137
+ }, [value, localesString, formatString, prefix, suffix]);
138
+ return /* @__PURE__ */ React.createElement(NumberFlowImpl, { ...props, data, innerRef: ref });
137
139
  });
138
140
  var src_default = NumberFlow;
139
141
  function useCanAnimate({ respectMotionPreference = true } = {}) {
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "0.3.1",
6
+ "version": "0.3.3",
7
7
  "author": {
8
8
  "name": "Maxwell Barvian",
9
9
  "email": "max@barvian.me",
@@ -48,7 +48,7 @@
48
48
  }
49
49
  },
50
50
  "dependencies": {
51
- "number-flow": "0.3.6"
51
+ "number-flow": "0.3.8"
52
52
  },
53
53
  "devDependencies": {
54
54
  "@playwright/test": "^1.48.0",