@number-flow/react 0.1.3 → 0.2.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/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,87 @@ 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");
45
48
  var OBSERVED_ATTRIBUTES = ["parts"];
46
49
  var NumberFlowElement = class extends import_number_flow.NumberFlowLite {
47
- static observedAttributes = OBSERVED_ATTRIBUTES;
48
50
  attributeChangedCallback(attr, _oldValue, newValue) {
49
51
  this[attr] = JSON.parse(newValue);
50
52
  }
51
53
  };
54
+ NumberFlowElement.observedAttributes = OBSERVED_ATTRIBUTES;
52
55
  NumberFlowElement.define();
53
56
  var formatters = {};
54
- var NumberFlowPriv = class extends React.Component {
57
+ var _el;
58
+ var NumberFlowImpl = class extends React.Component {
55
59
  constructor(props) {
56
60
  super(props);
61
+ __privateAdd(this, _el);
57
62
  this.handleRef = this.handleRef.bind(this);
58
63
  }
59
64
  // Update the non-parts props to avoid JSON serialization
60
65
  // 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
- }
66
+ updateNonPartsProps(prevProps) {
67
+ if (!__privateGet(this, _el)) return;
68
+ __privateGet(this, _el).manual = !this.props.isolate;
69
+ if (this.props.animated != null) __privateGet(this, _el).animated = this.props.animated;
70
+ if (this.props.respectMotionPreference != null)
71
+ __privateGet(this, _el).respectMotionPreference = this.props.respectMotionPreference;
72
+ if (this.props.trend != null) __privateGet(this, _el).trend = this.props.trend;
73
+ if (this.props.opacityTiming) __privateGet(this, _el).opacityTiming = this.props.opacityTiming;
74
+ if (this.props.transformTiming) __privateGet(this, _el).transformTiming = this.props.transformTiming;
75
+ if (this.props.spinTiming) __privateGet(this, _el).spinTiming = this.props.spinTiming;
76
+ if (prevProps?.onAnimationsStart)
77
+ __privateGet(this, _el).removeEventListener("animationsstart", prevProps.onAnimationsStart);
78
+ if (this.props.onAnimationsStart)
79
+ __privateGet(this, _el).addEventListener("animationsstart", this.props.onAnimationsStart);
80
+ if (prevProps?.onAnimationsFinish)
81
+ __privateGet(this, _el).removeEventListener("animationsfinish", prevProps.onAnimationsFinish);
82
+ if (this.props.onAnimationsFinish)
83
+ __privateGet(this, _el).addEventListener("animationsfinish", this.props.onAnimationsFinish);
68
84
  }
69
85
  componentDidMount() {
70
86
  this.updateNonPartsProps();
71
- if (this.#el) this.#el.manual = true;
72
87
  }
73
- getSnapshotBeforeUpdate() {
74
- this.updateNonPartsProps();
75
- this.#el?.willUpdate();
88
+ getSnapshotBeforeUpdate(prevProps) {
89
+ this.updateNonPartsProps(prevProps);
90
+ if (!this.props.isolate && prevProps.parts !== this.props.parts) __privateGet(this, _el)?.willUpdate();
76
91
  return null;
77
92
  }
78
- componentDidUpdate() {
79
- this.#el?.didUpdate();
93
+ componentDidUpdate(prevProps) {
94
+ if (!this.props.isolate && prevProps.parts !== this.props.parts) __privateGet(this, _el)?.didUpdate();
80
95
  }
81
- #el;
82
96
  handleRef(el) {
83
97
  if (this.props.innerRef) this.props.innerRef.current = el;
84
- this.#el = el;
98
+ __privateSet(this, _el, el);
85
99
  }
86
100
  render() {
87
101
  const {
88
102
  innerRef,
89
103
  className,
90
104
  parts,
105
+ willChange,
91
106
  // These are set in updateNonPartsProps, so ignore them here:
92
- fadeTiming,
93
- xTiming,
94
- yTiming,
107
+ animated,
108
+ respectMotionPreference,
109
+ isolate,
110
+ trend,
111
+ opacityTiming,
112
+ transformTiming,
113
+ spinTiming,
95
114
  ...rest
96
115
  } = this.props;
97
116
  return (
@@ -100,15 +119,17 @@ var NumberFlowPriv = class extends React.Component {
100
119
  "number-flow",
101
120
  {
102
121
  ref: this.handleRef,
122
+ "data-will-change": willChange ? "" : void 0,
103
123
  class: className,
104
124
  ...rest,
105
125
  parts: JSON.stringify(parts)
106
126
  },
107
- /* @__PURE__ */ React.createElement(import_number_flow.SlottedTag, { style: import_number_flow.slottedStyles }, parts.formatted)
127
+ /* @__PURE__ */ React.createElement(import_number_flow.SlottedTag, { style: (0, import_number_flow.slottedStyles)({ willChange }) }, parts.formatted)
108
128
  )
109
129
  );
110
130
  }
111
131
  };
132
+ _el = new WeakMap();
112
133
  var NumberFlow = React.forwardRef(function NumberFlow2({ value, locales, format, ...props }, _ref) {
113
134
  React.useImperativeHandle(_ref, () => ref.current, []);
114
135
  const ref = React.useRef();
@@ -121,25 +142,30 @@ var NumberFlow = React.forwardRef(function NumberFlow2({ value, locales, format,
121
142
  );
122
143
  return (0, import_number_flow.partitionParts)(value, formatter);
123
144
  }, [value, localesString, formatString]);
124
- return /* @__PURE__ */ React.createElement(NumberFlowPriv, { ...props, parts, innerRef: ref });
145
+ return /* @__PURE__ */ React.createElement(NumberFlowImpl, { ...props, parts, innerRef: ref });
125
146
  });
126
147
  var src_default = NumberFlow;
127
- function useSupportsLinear() {
128
- const [supported, setSupported] = React.useState(false);
148
+ function useCanAnimate({ respectMotionPreference = true } = {}) {
149
+ const [canAnimate, setCanAnimate] = React.useState(import_number_flow.canAnimate);
150
+ const [reducedMotion, setReducedMotion] = React.useState(false);
129
151
  React.useEffect(() => {
130
- setSupported(import_number_flow.supportsLinear);
152
+ setCanAnimate(import_number_flow.canAnimate);
153
+ setReducedMotion(import_number_flow.prefersReducedMotion?.matches ?? false);
131
154
  }, []);
132
- return supported;
133
- }
134
- function useLinear(linear, fallback) {
135
- const supported = useSupportsLinear();
136
- return supported ? linear : fallback;
155
+ React.useEffect(() => {
156
+ if (!respectMotionPreference) return;
157
+ const onChange = ({ matches }) => {
158
+ setReducedMotion(matches);
159
+ };
160
+ import_number_flow.prefersReducedMotion?.addEventListener("change", onChange);
161
+ return () => {
162
+ import_number_flow.prefersReducedMotion?.removeEventListener("change", onChange);
163
+ };
164
+ }, [respectMotionPreference]);
165
+ return canAnimate && (!respectMotionPreference || !reducedMotion);
137
166
  }
138
167
  // Annotate the CommonJS export names for ESM import in node:
139
168
  0 && (module.exports = {
140
169
  NumberFlowElement,
141
- defaultXTiming,
142
- defaultYTiming,
143
- useLinear,
144
- useSupportsLinear
170
+ useCanAnimate
145
171
  });
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,75 @@ 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";
13
20
  var OBSERVED_ATTRIBUTES = ["parts"];
14
21
  var NumberFlowElement = class extends NumberFlowLite {
15
- static observedAttributes = OBSERVED_ATTRIBUTES;
16
22
  attributeChangedCallback(attr, _oldValue, newValue) {
17
23
  this[attr] = JSON.parse(newValue);
18
24
  }
19
25
  };
26
+ NumberFlowElement.observedAttributes = OBSERVED_ATTRIBUTES;
20
27
  NumberFlowElement.define();
21
28
  var formatters = {};
22
- var NumberFlowPriv = class extends React.Component {
29
+ var _el;
30
+ var NumberFlowImpl = class extends React.Component {
23
31
  constructor(props) {
24
32
  super(props);
33
+ __privateAdd(this, _el);
25
34
  this.handleRef = this.handleRef.bind(this);
26
35
  }
27
36
  // Update the non-parts props to avoid JSON serialization
28
37
  // 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
- }
38
+ updateNonPartsProps(prevProps) {
39
+ if (!__privateGet(this, _el)) return;
40
+ __privateGet(this, _el).manual = !this.props.isolate;
41
+ if (this.props.animated != null) __privateGet(this, _el).animated = this.props.animated;
42
+ if (this.props.respectMotionPreference != null)
43
+ __privateGet(this, _el).respectMotionPreference = this.props.respectMotionPreference;
44
+ if (this.props.trend != null) __privateGet(this, _el).trend = this.props.trend;
45
+ if (this.props.opacityTiming) __privateGet(this, _el).opacityTiming = this.props.opacityTiming;
46
+ if (this.props.transformTiming) __privateGet(this, _el).transformTiming = this.props.transformTiming;
47
+ if (this.props.spinTiming) __privateGet(this, _el).spinTiming = this.props.spinTiming;
48
+ if (prevProps?.onAnimationsStart)
49
+ __privateGet(this, _el).removeEventListener("animationsstart", prevProps.onAnimationsStart);
50
+ if (this.props.onAnimationsStart)
51
+ __privateGet(this, _el).addEventListener("animationsstart", this.props.onAnimationsStart);
52
+ if (prevProps?.onAnimationsFinish)
53
+ __privateGet(this, _el).removeEventListener("animationsfinish", prevProps.onAnimationsFinish);
54
+ if (this.props.onAnimationsFinish)
55
+ __privateGet(this, _el).addEventListener("animationsfinish", this.props.onAnimationsFinish);
36
56
  }
37
57
  componentDidMount() {
38
58
  this.updateNonPartsProps();
39
- if (this.#el) this.#el.manual = true;
40
59
  }
41
- getSnapshotBeforeUpdate() {
42
- this.updateNonPartsProps();
43
- this.#el?.willUpdate();
60
+ getSnapshotBeforeUpdate(prevProps) {
61
+ this.updateNonPartsProps(prevProps);
62
+ if (!this.props.isolate && prevProps.parts !== this.props.parts) __privateGet(this, _el)?.willUpdate();
44
63
  return null;
45
64
  }
46
- componentDidUpdate() {
47
- this.#el?.didUpdate();
65
+ componentDidUpdate(prevProps) {
66
+ if (!this.props.isolate && prevProps.parts !== this.props.parts) __privateGet(this, _el)?.didUpdate();
48
67
  }
49
- #el;
50
68
  handleRef(el) {
51
69
  if (this.props.innerRef) this.props.innerRef.current = el;
52
- this.#el = el;
70
+ __privateSet(this, _el, el);
53
71
  }
54
72
  render() {
55
73
  const {
56
74
  innerRef,
57
75
  className,
58
76
  parts,
77
+ willChange,
59
78
  // These are set in updateNonPartsProps, so ignore them here:
60
- fadeTiming,
61
- xTiming,
62
- yTiming,
79
+ animated,
80
+ respectMotionPreference,
81
+ isolate,
82
+ trend,
83
+ opacityTiming,
84
+ transformTiming,
85
+ spinTiming,
63
86
  ...rest
64
87
  } = this.props;
65
88
  return (
@@ -68,15 +91,17 @@ var NumberFlowPriv = class extends React.Component {
68
91
  "number-flow",
69
92
  {
70
93
  ref: this.handleRef,
94
+ "data-will-change": willChange ? "" : void 0,
71
95
  class: className,
72
96
  ...rest,
73
97
  parts: JSON.stringify(parts)
74
98
  },
75
- /* @__PURE__ */ React.createElement(SlottedTag, { style: slottedStyles }, parts.formatted)
99
+ /* @__PURE__ */ React.createElement(SlottedTag, { style: slottedStyles({ willChange }) }, parts.formatted)
76
100
  )
77
101
  );
78
102
  }
79
103
  };
104
+ _el = new WeakMap();
80
105
  var NumberFlow = React.forwardRef(function NumberFlow2({ value, locales, format, ...props }, _ref) {
81
106
  React.useImperativeHandle(_ref, () => ref.current, []);
82
107
  const ref = React.useRef();
@@ -89,25 +114,30 @@ var NumberFlow = React.forwardRef(function NumberFlow2({ value, locales, format,
89
114
  );
90
115
  return partitionParts(value, formatter);
91
116
  }, [value, localesString, formatString]);
92
- return /* @__PURE__ */ React.createElement(NumberFlowPriv, { ...props, parts, innerRef: ref });
117
+ return /* @__PURE__ */ React.createElement(NumberFlowImpl, { ...props, parts, innerRef: ref });
93
118
  });
94
119
  var src_default = NumberFlow;
95
- function useSupportsLinear() {
96
- const [supported, setSupported] = React.useState(false);
120
+ function useCanAnimate({ respectMotionPreference = true } = {}) {
121
+ const [canAnimate, setCanAnimate] = React.useState(_canAnimate);
122
+ const [reducedMotion, setReducedMotion] = React.useState(false);
97
123
  React.useEffect(() => {
98
- setSupported(supportsLinear);
124
+ setCanAnimate(_canAnimate);
125
+ setReducedMotion(prefersReducedMotion?.matches ?? false);
99
126
  }, []);
100
- return supported;
101
- }
102
- function useLinear(linear, fallback) {
103
- const supported = useSupportsLinear();
104
- return supported ? linear : fallback;
127
+ React.useEffect(() => {
128
+ if (!respectMotionPreference) return;
129
+ const onChange = ({ matches }) => {
130
+ setReducedMotion(matches);
131
+ };
132
+ prefersReducedMotion?.addEventListener("change", onChange);
133
+ return () => {
134
+ prefersReducedMotion?.removeEventListener("change", onChange);
135
+ };
136
+ }, [respectMotionPreference]);
137
+ return canAnimate && (!respectMotionPreference || !reducedMotion);
105
138
  }
106
139
  export {
107
140
  NumberFlowElement,
108
141
  src_default as default,
109
- defaultXTiming,
110
- defaultYTiming,
111
- useLinear,
112
- useSupportsLinear
142
+ useCanAnimate
113
143
  };
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.0",
7
7
  "author": {
8
8
  "name": "Maxwell Barvian",
9
9
  "email": "max@barvian.me",
@@ -45,26 +45,15 @@
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"
@@ -72,17 +61,12 @@
72
61
  "peerDependencies": {
73
62
  "@types/react": "^18.3.3",
74
63
  "@types/react-dom": "^18.3.0",
75
- "framer-motion": "^11.0.0",
76
64
  "react": "^18.3.1",
77
65
  "react-dom": "^18.3.1"
78
66
  },
79
- "peerDependenciesMeta": {
80
- "framer-motion": {
81
- "optional": true
82
- }
83
- },
84
67
  "scripts": {
85
68
  "build": "tsup --tsconfig tsconfig.build.json",
86
- "dev": "tsup --watch"
69
+ "dev": "tsup --watch",
70
+ "test": "pnpm -r --workspace-concurrency 1 --filter=\"./test\" test"
87
71
  }
88
72
  }
@@ -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
- };