@number-flow/react 0.3.2 → 0.3.4

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