@tremolo-ui/react 0.0.2 → 0.0.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.
Files changed (120) hide show
  1. package/README.md +11 -0
  2. package/dist/cjs/index.d.ts +72 -7
  3. package/dist/cjs/index.js +320 -106
  4. package/dist/cjs/index.js.map +1 -1
  5. package/dist/cjs/packages/react/tsconfig.tsbuildinfo +1 -0
  6. package/dist/cjs/types/packages/functions/components/NumberInput/type.d.ts +14 -0
  7. package/dist/cjs/types/packages/functions/components/NumberInput/type.d.ts.map +1 -0
  8. package/dist/cjs/types/packages/functions/components/Slider/type.d.ts +32 -0
  9. package/dist/cjs/types/packages/functions/components/Slider/type.d.ts.map +1 -0
  10. package/dist/cjs/types/{src → packages/functions}/math.d.ts +4 -0
  11. package/dist/cjs/types/packages/functions/math.d.ts.map +1 -0
  12. package/dist/cjs/types/packages/functions/types.d.ts.map +1 -0
  13. package/dist/cjs/types/packages/functions/util.d.ts +6 -0
  14. package/dist/cjs/types/packages/functions/util.d.ts.map +1 -0
  15. package/dist/cjs/types/packages/react/src/components/AnimationCanvas/canvas.d.ts.map +1 -0
  16. package/dist/cjs/types/packages/react/src/components/AnimationCanvas/index.d.ts.map +1 -0
  17. package/dist/cjs/types/{src → packages/react/src}/components/Knob/index.d.ts +1 -1
  18. package/dist/cjs/types/packages/react/src/components/Knob/index.d.ts.map +1 -0
  19. package/dist/{esm/types → cjs/types/packages/react}/src/components/NumberInput/index.d.ts +1 -1
  20. package/dist/cjs/types/packages/react/src/components/NumberInput/index.d.ts.map +1 -0
  21. package/dist/cjs/types/packages/react/src/components/Slider/Thumb.d.ts.map +1 -0
  22. package/dist/cjs/types/packages/react/src/components/Slider/Track.d.ts.map +1 -0
  23. package/dist/{esm/types → cjs/types/packages/react}/src/components/Slider/index.d.ts +3 -3
  24. package/dist/cjs/types/packages/react/src/components/Slider/index.d.ts.map +1 -0
  25. package/dist/cjs/types/packages/react/src/components/Slider/type.d.ts +6 -0
  26. package/dist/cjs/types/packages/react/src/components/Slider/type.d.ts.map +1 -0
  27. package/dist/cjs/types/packages/react/src/hooks/useCallbackRef.d.ts.map +1 -0
  28. package/dist/cjs/types/packages/react/src/hooks/useEventListener.d.ts.map +1 -0
  29. package/dist/cjs/types/packages/react/src/hooks/useRefCallbackEvent.d.ts.map +1 -0
  30. package/dist/cjs/types/packages/react/src/index.d.ts +7 -0
  31. package/dist/cjs/types/packages/react/src/index.d.ts.map +1 -0
  32. package/dist/cjs/types/packages/react/src/system/pseudo.d.ts.map +1 -0
  33. package/dist/esm/index.d.ts +72 -7
  34. package/dist/esm/index.js +259 -53
  35. package/dist/esm/index.js.map +1 -1
  36. package/dist/esm/packages/react/tsconfig.tsbuildinfo +1 -0
  37. package/dist/esm/types/packages/functions/components/NumberInput/type.d.ts +14 -0
  38. package/dist/esm/types/packages/functions/components/NumberInput/type.d.ts.map +1 -0
  39. package/dist/esm/types/packages/functions/components/Slider/type.d.ts +32 -0
  40. package/dist/esm/types/packages/functions/components/Slider/type.d.ts.map +1 -0
  41. package/dist/esm/types/{src → packages/functions}/math.d.ts +4 -0
  42. package/dist/esm/types/packages/functions/math.d.ts.map +1 -0
  43. package/dist/esm/types/packages/functions/types.d.ts.map +1 -0
  44. package/dist/esm/types/packages/functions/util.d.ts +6 -0
  45. package/dist/esm/types/packages/functions/util.d.ts.map +1 -0
  46. package/dist/esm/types/packages/react/src/components/AnimationCanvas/canvas.d.ts.map +1 -0
  47. package/dist/esm/types/packages/react/src/components/AnimationCanvas/index.d.ts.map +1 -0
  48. package/dist/esm/types/{src → packages/react/src}/components/Knob/index.d.ts +1 -1
  49. package/dist/esm/types/packages/react/src/components/Knob/index.d.ts.map +1 -0
  50. package/dist/{cjs/types → esm/types/packages/react}/src/components/NumberInput/index.d.ts +1 -1
  51. package/dist/esm/types/packages/react/src/components/NumberInput/index.d.ts.map +1 -0
  52. package/dist/esm/types/packages/react/src/components/Slider/Thumb.d.ts.map +1 -0
  53. package/dist/esm/types/packages/react/src/components/Slider/Track.d.ts.map +1 -0
  54. package/dist/{cjs/types → esm/types/packages/react}/src/components/Slider/index.d.ts +3 -3
  55. package/dist/esm/types/packages/react/src/components/Slider/index.d.ts.map +1 -0
  56. package/dist/esm/types/packages/react/src/components/Slider/type.d.ts +6 -0
  57. package/dist/esm/types/packages/react/src/components/Slider/type.d.ts.map +1 -0
  58. package/dist/esm/types/packages/react/src/hooks/useCallbackRef.d.ts.map +1 -0
  59. package/dist/esm/types/packages/react/src/hooks/useEventListener.d.ts.map +1 -0
  60. package/dist/esm/types/packages/react/src/hooks/useRefCallbackEvent.d.ts.map +1 -0
  61. package/dist/esm/types/packages/react/src/index.d.ts +7 -0
  62. package/dist/esm/types/packages/react/src/index.d.ts.map +1 -0
  63. package/dist/esm/types/packages/react/src/system/pseudo.d.ts.map +1 -0
  64. package/package.json +9 -7
  65. package/dist/cjs/types/src/components/AnimationCanvas/canvas.d.ts.map +0 -1
  66. package/dist/cjs/types/src/components/AnimationCanvas/index.d.ts.map +0 -1
  67. package/dist/cjs/types/src/components/Knob/index.d.ts.map +0 -1
  68. package/dist/cjs/types/src/components/NumberInput/index.d.ts.map +0 -1
  69. package/dist/cjs/types/src/components/Slider/Thumb.d.ts.map +0 -1
  70. package/dist/cjs/types/src/components/Slider/Track.d.ts.map +0 -1
  71. package/dist/cjs/types/src/components/Slider/index.d.ts.map +0 -1
  72. package/dist/cjs/types/src/components/Slider/type.d.ts +0 -6
  73. package/dist/cjs/types/src/components/Slider/type.d.ts.map +0 -1
  74. package/dist/cjs/types/src/hooks/useCallbackRef.d.ts.map +0 -1
  75. package/dist/cjs/types/src/hooks/useEventListener.d.ts.map +0 -1
  76. package/dist/cjs/types/src/hooks/useRefCallbackEvent.d.ts.map +0 -1
  77. package/dist/cjs/types/src/index.d.ts +0 -5
  78. package/dist/cjs/types/src/index.d.ts.map +0 -1
  79. package/dist/cjs/types/src/math.d.ts.map +0 -1
  80. package/dist/cjs/types/src/system/pseudo.d.ts.map +0 -1
  81. package/dist/cjs/types/src/types.d.ts.map +0 -1
  82. package/dist/cjs/types/src/util.d.ts +0 -2
  83. package/dist/cjs/types/src/util.d.ts.map +0 -1
  84. package/dist/esm/types/src/components/AnimationCanvas/canvas.d.ts.map +0 -1
  85. package/dist/esm/types/src/components/AnimationCanvas/index.d.ts.map +0 -1
  86. package/dist/esm/types/src/components/Knob/index.d.ts.map +0 -1
  87. package/dist/esm/types/src/components/NumberInput/index.d.ts.map +0 -1
  88. package/dist/esm/types/src/components/Slider/Thumb.d.ts.map +0 -1
  89. package/dist/esm/types/src/components/Slider/Track.d.ts.map +0 -1
  90. package/dist/esm/types/src/components/Slider/index.d.ts.map +0 -1
  91. package/dist/esm/types/src/components/Slider/type.d.ts +0 -6
  92. package/dist/esm/types/src/components/Slider/type.d.ts.map +0 -1
  93. package/dist/esm/types/src/hooks/useCallbackRef.d.ts.map +0 -1
  94. package/dist/esm/types/src/hooks/useEventListener.d.ts.map +0 -1
  95. package/dist/esm/types/src/hooks/useRefCallbackEvent.d.ts.map +0 -1
  96. package/dist/esm/types/src/index.d.ts +0 -5
  97. package/dist/esm/types/src/index.d.ts.map +0 -1
  98. package/dist/esm/types/src/math.d.ts.map +0 -1
  99. package/dist/esm/types/src/system/pseudo.d.ts.map +0 -1
  100. package/dist/esm/types/src/types.d.ts.map +0 -1
  101. package/dist/esm/types/src/util.d.ts +0 -2
  102. package/dist/esm/types/src/util.d.ts.map +0 -1
  103. /package/dist/cjs/types/{src → packages/functions}/types.d.ts +0 -0
  104. /package/dist/cjs/types/{src → packages/react/src}/components/AnimationCanvas/canvas.d.ts +0 -0
  105. /package/dist/cjs/types/{src → packages/react/src}/components/AnimationCanvas/index.d.ts +0 -0
  106. /package/dist/cjs/types/{src → packages/react/src}/components/Slider/Thumb.d.ts +0 -0
  107. /package/dist/cjs/types/{src → packages/react/src}/components/Slider/Track.d.ts +0 -0
  108. /package/dist/cjs/types/{src → packages/react/src}/hooks/useCallbackRef.d.ts +0 -0
  109. /package/dist/cjs/types/{src → packages/react/src}/hooks/useEventListener.d.ts +0 -0
  110. /package/dist/cjs/types/{src → packages/react/src}/hooks/useRefCallbackEvent.d.ts +0 -0
  111. /package/dist/cjs/types/{src → packages/react/src}/system/pseudo.d.ts +0 -0
  112. /package/dist/esm/types/{src → packages/functions}/types.d.ts +0 -0
  113. /package/dist/esm/types/{src → packages/react/src}/components/AnimationCanvas/canvas.d.ts +0 -0
  114. /package/dist/esm/types/{src → packages/react/src}/components/AnimationCanvas/index.d.ts +0 -0
  115. /package/dist/esm/types/{src → packages/react/src}/components/Slider/Thumb.d.ts +0 -0
  116. /package/dist/esm/types/{src → packages/react/src}/components/Slider/Track.d.ts +0 -0
  117. /package/dist/esm/types/{src → packages/react/src}/hooks/useCallbackRef.d.ts +0 -0
  118. /package/dist/esm/types/{src → packages/react/src}/hooks/useEventListener.d.ts +0 -0
  119. /package/dist/esm/types/{src → packages/react/src}/hooks/useRefCallbackEvent.d.ts +0 -0
  120. /package/dist/esm/types/{src → packages/react/src}/system/pseudo.d.ts +0 -0
package/dist/cjs/index.js CHANGED
@@ -1,11 +1,59 @@
1
1
  'use strict';
2
2
 
3
- var math = require('common/math');
4
3
  var jsxRuntime = require('@emotion/react/jsx-runtime');
5
4
  var react = require('@emotion/react');
6
5
  var React = require('react');
7
- var type = require('common/components/Slider/type');
8
- var util = require('common/util');
6
+
7
+ function clamp(value, min, max) {
8
+ return Math.max(min, Math.min(value, max));
9
+ }
10
+ function normalizeValue(rawValue, min, max, skew = 1) {
11
+ if (min >= max)
12
+ throw new RangeError('requirements: min < max');
13
+ const v = clamp((rawValue - min) / (max - min), 0, 1);
14
+ return Math.pow(v, skew);
15
+ }
16
+ function rawValue(normalizedValue, min, max, skew = 1) {
17
+ if (min >= max)
18
+ throw new RangeError('requirements: min < max');
19
+ const v = skew == 1
20
+ ? clamp(normalizedValue, 0, 1)
21
+ : Math.exp(Math.log(clamp(normalizedValue, 0, 1)) / skew);
22
+ return min + v * (max - min);
23
+ }
24
+ function skewWithCenterValue(centerValue, min, max) {
25
+ if (!(min <= centerValue && centerValue <= max))
26
+ throw new RangeError('requirements: min <= centerValue <= max');
27
+ return Math.log(0.5) / Math.log((centerValue - min) / (max - min));
28
+ }
29
+ function stepValue(value, step) {
30
+ var _a;
31
+ if (step <= 0)
32
+ throw new RangeError('requirements: step > 0');
33
+ const quotient = Math.floor(value / step);
34
+ const decimalDigits = (_a = decimalPart(step)) === null || _a === void 0 ? void 0 : _a.length;
35
+ const v = toFixed(quotient * step, decimalDigits);
36
+ const next = toFixed((quotient + 1) * step, decimalDigits);
37
+ return Math.abs(value - v) < Math.abs(value - next) ? v : next;
38
+ }
39
+ function toFixed(x, fractionDigits) {
40
+ return Number(x.toFixed(fractionDigits));
41
+ }
42
+ function decimalPart(x) {
43
+ return String(x).split('.')[1];
44
+ }
45
+ function radian(degree) {
46
+ return (Math.PI * degree) / 180;
47
+ }
48
+ function degree(radian) {
49
+ return (180 * radian) / Math.PI;
50
+ }
51
+ function dbToGain(db) {
52
+ return Math.pow(10, db / 20);
53
+ }
54
+ function gainToDb(gain) {
55
+ return 20 * (Math.log(gain) / Math.LN10);
56
+ }
9
57
 
10
58
  /******************************************************************************
11
59
  Copyright (c) Microsoft Corporation.
@@ -41,53 +89,6 @@ typeof SuppressedError === "function" ? SuppressedError : function (error, suppr
41
89
  return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
42
90
  };
43
91
 
44
- /**
45
- * This hook is user-land implementation of the experimental `useEffectEvent` hook.
46
- * React docs: https://react.dev/learn/separating-events-from-effects#declaring-an-effect-event
47
- */
48
- function useCallbackRef(callback, deps = []) {
49
- const callbackRef = React.useRef(() => {
50
- throw new Error('Cannot call an event handler while rendering.');
51
- });
52
- React.useInsertionEffect(() => {
53
- callbackRef.current = callback;
54
- });
55
- return React.useCallback((...args) => { var _a; return (_a = callbackRef.current) === null || _a === void 0 ? void 0 : _a.call(callbackRef, ...args); }, deps);
56
- }
57
-
58
- function useEventListener(target, event, handler, options) {
59
- const listener = useCallbackRef(handler);
60
- React.useEffect(() => {
61
- const node = typeof target === 'function' ? target() : (target !== null && target !== void 0 ? target : document);
62
- if (!handler || !node)
63
- return;
64
- node.addEventListener(event, listener, options);
65
- return () => {
66
- node.removeEventListener(event, listener, options);
67
- };
68
- }, [event, target, options, listener, handler]);
69
- return () => {
70
- const node = typeof target === 'function' ? target() : (target !== null && target !== void 0 ? target : document);
71
- node === null || node === void 0 ? void 0 : node.removeEventListener(event, listener, options);
72
- };
73
- }
74
-
75
- function useRefCallbackEvent(event, handler, options, deps = []) {
76
- return React.useMemo(() => {
77
- let cleanup;
78
- return (node) => {
79
- if (!node) {
80
- cleanup === null || cleanup === void 0 ? void 0 : cleanup();
81
- return;
82
- }
83
- node.addEventListener(event, handler, options);
84
- cleanup = () => {
85
- node.removeEventListener(event, handler, options);
86
- };
87
- };
88
- }, deps);
89
- }
90
-
91
92
  // https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/save
92
93
  const drawingState = [
93
94
  'strokeStyle',
@@ -200,6 +201,53 @@ function AnimationCanvas(_a) {
200
201
  return (jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [jsxRuntime.jsx("canvas", Object.assign({ width: relativeSize ? 0 : width, height: relativeSize ? 0 : height, css: react.css(style), ref: canvasRef }, props)), relativeSize && reduceFlickering && (jsxRuntime.jsx("canvas", { style: { display: 'none' }, width: relativeSize ? 0 : width, height: relativeSize ? 0 : height, ref: memoCanvasRef }))] }));
201
202
  }
202
203
 
204
+ /**
205
+ * This hook is user-land implementation of the experimental `useEffectEvent` hook.
206
+ * React docs: https://react.dev/learn/separating-events-from-effects#declaring-an-effect-event
207
+ */
208
+ function useCallbackRef(callback, deps = []) {
209
+ const callbackRef = React.useRef(() => {
210
+ throw new Error('Cannot call an event handler while rendering.');
211
+ });
212
+ React.useInsertionEffect(() => {
213
+ callbackRef.current = callback;
214
+ });
215
+ return React.useCallback((...args) => { var _a; return (_a = callbackRef.current) === null || _a === void 0 ? void 0 : _a.call(callbackRef, ...args); }, deps);
216
+ }
217
+
218
+ function useEventListener(target, event, handler, options) {
219
+ const listener = useCallbackRef(handler);
220
+ React.useEffect(() => {
221
+ const node = typeof target === 'function' ? target() : (target !== null && target !== void 0 ? target : document);
222
+ if (!handler || !node)
223
+ return;
224
+ node.addEventListener(event, listener, options);
225
+ return () => {
226
+ node.removeEventListener(event, listener, options);
227
+ };
228
+ }, [event, target, options, listener, handler]);
229
+ return () => {
230
+ const node = typeof target === 'function' ? target() : (target !== null && target !== void 0 ? target : document);
231
+ node === null || node === void 0 ? void 0 : node.removeEventListener(event, listener, options);
232
+ };
233
+ }
234
+
235
+ function useRefCallbackEvent(event, handler, options, deps = []) {
236
+ return React.useMemo(() => {
237
+ let cleanup;
238
+ return (node) => {
239
+ if (!node) {
240
+ cleanup === null || cleanup === void 0 ? void 0 : cleanup();
241
+ return;
242
+ }
243
+ node.addEventListener(event, handler, options);
244
+ cleanup = () => {
245
+ node.removeEventListener(event, handler, options);
246
+ };
247
+ };
248
+ }, deps);
249
+ }
250
+
203
251
  const defaultOptions = {
204
252
  active: '#7998ec',
205
253
  inactive: '#eee',
@@ -240,9 +288,9 @@ function Knob(_a) {
240
288
  const screenY = isTouch ? event.touches[0].screenY : event.screenY;
241
289
  const delta = dragOffsetY.current - screenY;
242
290
  dragOffsetY.current = screenY;
243
- const n = math.normalizeValue(value, min, max, skew);
244
- const v = math.rawValue(n + delta / 100, min, max, skew);
245
- const v2 = math.clamp(math.stepValue(v, step), min, max);
291
+ const n = normalizeValue(value, min, max, skew);
292
+ const v = rawValue(n + delta / 100, min, max, skew);
293
+ const v2 = clamp(stepValue(v, step), min, max);
246
294
  if (onChange)
247
295
  onChange(v2);
248
296
  }
@@ -255,14 +303,14 @@ function Knob(_a) {
255
303
  const x = event.deltaY > 0 ? -enableWheel[1] : enableWheel[1];
256
304
  let v;
257
305
  if (enableWheel[0] == 'normalized') {
258
- const n = math.normalizeValue(value, min, max, skew);
259
- v = math.rawValue(n + x, min, max, skew);
306
+ const n = normalizeValue(value, min, max, skew);
307
+ v = rawValue(n + x, min, max, skew);
260
308
  }
261
309
  else {
262
310
  v = value + x;
263
311
  }
264
312
  if (onChange)
265
- onChange(math.clamp(math.stepValue(v, step), min, max));
313
+ onChange(clamp(stepValue(v, step), min, max));
266
314
  }, { passive: false }, [enableWheel, value, min, max, skew, step, onChange]);
267
315
  const touchMoveRefCallback = useRefCallbackEvent('touchmove', handleEvent, { passive: false }, [value, min, max, skew, step, onChange, bodyNoSelect]);
268
316
  useEventListener(window, 'mousemove', handleEvent);
@@ -289,11 +337,13 @@ function Knob(_a) {
289
337
  // TODO
290
338
  // reduceFlickering={false}
291
339
  draw: (ctx, _w, _h) => {
292
- const p = math.normalizeValue(value, min, max, skew);
293
- const startP = math.normalizeValue(startValue, min, max, skew);
340
+ const p = normalizeValue(value, min, max, skew);
341
+ const startP = normalizeValue(startValue, min, max, skew);
294
342
  const w = _w.current, cx = w / 2;
295
343
  const h = _h.current, cy = h / 2;
296
344
  const r = Math.min(cx, cy);
345
+ if (r <= 0)
346
+ return;
297
347
  // reset
298
348
  ctx.clearRect(0, 0, w, h);
299
349
  ctx.lineCap = 'butt';
@@ -307,20 +357,20 @@ function Knob(_a) {
307
357
  // inactive rotary
308
358
  ctx.beginPath();
309
359
  ctx.strokeStyle = opts.inactive;
310
- ctx.arc(cx, cy, r - 0.5 - opts.lineWeight / 2, math.radian(135), math.radian(45));
360
+ ctx.arc(cx, cy, r - 0.5 - opts.lineWeight / 2, radian(135), radian(45));
311
361
  ctx.stroke();
312
362
  // active rotary
313
363
  ctx.beginPath();
314
364
  ctx.strokeStyle = opts.active;
315
- ctx.arc(cx, cy, r - 0.5 - opts.lineWeight / 2, math.radian(135 + 270 * startP), math.radian(135 + 270 * p), p < startP);
365
+ ctx.arc(cx, cy, r - 0.5 - opts.lineWeight / 2, radian(135 + 270 * startP), radian(135 + 270 * p), p < startP);
316
366
  ctx.stroke();
317
367
  // thumb
318
368
  ctx.beginPath();
319
369
  ctx.moveTo(cx, cy);
320
370
  ctx.lineTo(cx +
321
- Math.cos(math.radian(135 + 270 * p)) *
371
+ Math.cos(radian(135 + 270 * p)) *
322
372
  (r - 0.5 - opts.thumbWeight / 2), cy +
323
- Math.sin(math.radian(135 + 270 * p)) *
373
+ Math.sin(radian(135 + 270 * p)) *
324
374
  (r - 0.5 - opts.thumbWeight / 2));
325
375
  ctx.strokeStyle = opts.thumb;
326
376
  ctx.lineWidth = opts.thumbWeight;
@@ -328,6 +378,163 @@ function Knob(_a) {
328
378
  } })] })));
329
379
  }
330
380
 
381
+ function selectUnit(units, value) {
382
+ if (typeof units == 'string')
383
+ return [units, 1];
384
+ let i = 0;
385
+ for (; i < units.length; i++) {
386
+ if (Math.abs(units[i][1]) > Math.abs(value))
387
+ break;
388
+ }
389
+ return units[Math.max(0, i - 1)];
390
+ }
391
+ function parseValue(inputString, units, digit) {
392
+ if (!units) {
393
+ return {
394
+ rawValue: Number(inputString.trim()) || 0,
395
+ formatValue: inputString.trim(),
396
+ unit: '',
397
+ };
398
+ }
399
+ else if (typeof units == 'string') {
400
+ return {
401
+ rawValue: Number(inputString.trim()) || 0,
402
+ formatValue: `${inputString.trim()}${units}`,
403
+ unit: units,
404
+ };
405
+ }
406
+ const unitList = units.map(([u, _s]) => u);
407
+ const scaleList = units.map(([_u, s]) => s);
408
+ let rawValue = 0;
409
+ let formatValue = `${scaleList[0]}`;
410
+ let unit = unitList[0];
411
+ const m = inputString.trim().match(/^(-?\d+(\.\d+)?)\s*(\w*)$/);
412
+ if (m) {
413
+ rawValue = Number(m[1]) || 0;
414
+ const inputUnit = m[3];
415
+ if (inputUnit != '' && unitList.indexOf(inputUnit) != -1) {
416
+ rawValue *= scaleList[unitList.indexOf(inputUnit)];
417
+ const s = selectUnit(units, rawValue);
418
+ const v = rawValue / s[1];
419
+ formatValue = `${digit != undefined ? toFixed(v, digit) : v}${s[0]}`;
420
+ unit = s[0];
421
+ }
422
+ else {
423
+ const [u, scale] = selectUnit(units, rawValue);
424
+ const v = rawValue / scale;
425
+ formatValue = `${digit != undefined ? toFixed(v, digit) : v}${u}`;
426
+ unit = u;
427
+ }
428
+ }
429
+ return {
430
+ rawValue: rawValue,
431
+ formatValue: formatValue,
432
+ unit: unit,
433
+ };
434
+ }
435
+
436
+ /**
437
+ * Input with some useful functions for entering numerical values.
438
+ */
439
+ function NumberInput(_a) {
440
+ var { value, min, // TODO
441
+ max, // TODO
442
+ step, // TODO
443
+ units, disabled = false, placeholder, readOnly, tabIndex, strict = false, digit, selectWithFocus = 'none', blurWithEnter = true, className, style, onChange, onFocus, onBlur } = _a, pseudo = __rest(_a, ["value", "min", "max", "step", "units", "disabled", "placeholder", "readOnly", "tabIndex", "strict", "digit", "selectWithFocus", "blurWithEnter", "className", "style", "onChange", "onFocus", "onBlur"]);
444
+ const [showValue, setShowValue] = React.useState(parseValue(String(value), units, digit).formatValue);
445
+ const calculatedStrict = units ? false : strict;
446
+ const { _active, _focus, _hover } = pseudo;
447
+ return (jsxRuntime.jsx("input", { className: 'tremolo-slider' + (className ? ` ${className}` : ''), value: showValue, type: calculatedStrict ? 'number' : 'text', spellCheck: 'false', disabled: disabled, placeholder: placeholder, readOnly: readOnly, tabIndex: tabIndex, css: react.css(Object.assign(Object.assign({ display: 'inline-block', height: '2rem', font: 'inherit', margin: 0, paddingLeft: 10, paddingRight: 10, outlineColor: 'transparent', outlineStyle: 'solid', outlineWidth: 1, background: 'inherit', appearance: 'none', borderStyle: 'solid', borderWidth: 1, borderColor: '#bbb', borderRadius: 4, '&:hover': Object.assign({ outlineColor: '#bbb' }, _hover), '&:focus': Object.assign({ outlineColor: '#4e76e6', outlineWidth: 2 }, _focus) }, (_active && { '&:active': Object.assign({}, _active) })), style)), onChange: (event) => {
448
+ const v = event.currentTarget.value;
449
+ setShowValue(v);
450
+ if (onChange)
451
+ onChange(parseValue(v, units, digit).rawValue, event);
452
+ }, onFocus: (event) => {
453
+ const v = event.currentTarget.value;
454
+ const parsed = parseValue(v, units, digit);
455
+ if (selectWithFocus == 'all') {
456
+ event.currentTarget.setSelectionRange(0, v.length);
457
+ }
458
+ else if (selectWithFocus == 'number') {
459
+ event.currentTarget.setSelectionRange(0, parsed.formatValue.length - parsed.unit.length);
460
+ }
461
+ if (onFocus)
462
+ onFocus(parsed.rawValue, event);
463
+ }, onBlur: (event) => {
464
+ // update value
465
+ const v = event.currentTarget.value;
466
+ const parsed = parseValue(v, units, digit);
467
+ setShowValue(parsed.formatValue);
468
+ if (onBlur)
469
+ onBlur(parsed.rawValue, event);
470
+ }, onKeyDown: (event) => {
471
+ if (blurWithEnter && event.key == 'Enter') {
472
+ event.currentTarget.blur();
473
+ }
474
+ } }));
475
+ }
476
+
477
+ function isHorizontal(d) {
478
+ return d == 'right' || d == 'left';
479
+ }
480
+ function isVertical(d) {
481
+ return d == 'up' || d == 'down';
482
+ }
483
+ function isReversed(d) {
484
+ return d == 'left' || d == 'up';
485
+ }
486
+ function gradientDirection(d) {
487
+ if (d == 'left' || d == 'right')
488
+ return d;
489
+ if (d == 'up')
490
+ return 'top';
491
+ if (d == 'down')
492
+ return 'bottom';
493
+ }
494
+ function parseScaleOrderList(scale, min, max, step) {
495
+ var _a;
496
+ let scalesList = [];
497
+ if (!scale) ;
498
+ else if (typeof scale[0] == 'object') {
499
+ scalesList = scale;
500
+ }
501
+ else {
502
+ const per = scale[0] == 'step' ? step : scale[0];
503
+ const count = Math.floor(max / per) - Math.ceil(min / per) + 1;
504
+ for (let i = 0; i < count; i++) {
505
+ const at = toFixed(per * (Math.ceil(min / per) + i), (_a = decimalPart(per)) === null || _a === void 0 ? void 0 : _a.length);
506
+ scalesList.push({ at: at, type: scale[1] });
507
+ }
508
+ }
509
+ return scalesList;
510
+ }
511
+
512
+ function styleHelper(value, op, influencer) {
513
+ if (op && influencer) {
514
+ if (typeof value == 'number') {
515
+ if (op == '+')
516
+ return `${value + influencer}px`;
517
+ if (op == '-')
518
+ return `${value - influencer}px`;
519
+ if (op == '*')
520
+ return `${value * influencer}px`;
521
+ if (op == '/')
522
+ return `${value / influencer}px`;
523
+ }
524
+ else {
525
+ return `calc(${value}px ${op} ${influencer})`;
526
+ }
527
+ }
528
+ else {
529
+ if (typeof value == 'number') {
530
+ return `${value}px`;
531
+ }
532
+ else {
533
+ return value;
534
+ }
535
+ }
536
+ }
537
+
331
538
  function SliderThumb({ color, children, style, __css, }) {
332
539
  return (jsxRuntime.jsx("div", { className: "tremolo-slider-thumb-wrapper", css: react.css(Object.assign({ width: 'fit-content', height: 'fit-content', position: 'absolute', translate: '-50% -50%', zIndex: 100 }, __css)), children: children ? (children) : (
333
540
  // default slider thumb
@@ -348,10 +555,10 @@ function Slider(_a) {
348
555
  var { value, min, max, step = 1, skew = 1, length = 140, thickness = 10, direction = 'right', scale, scaleOption, color = '#4e76e6', bg = '#eee', className, style, bodyNoSelect = true, enableWheel, onChange, children } = _a; __rest(_a, ["value", "min", "max", "step", "skew", "length", "thickness", "direction", "scale", "scaleOption", "color", "bg", "className", "style", "bodyNoSelect", "enableWheel", "onChange", "children"]);
349
556
  // -- state and ref ---
350
557
  const trackElementRef = React.useRef(null);
351
- const percent = math.normalizeValue(value, min, max, skew) * 100;
352
- const percentRev = type.isReversed(direction) ? 100 - percent : percent;
353
- const scalesList = type.parseScaleOrderList(scale, min, max, step);
354
- if (type.isReversed(direction))
558
+ const percent = normalizeValue(value, min, max, skew) * 100;
559
+ const percentRev = isReversed(direction) ? 100 - percent : percent;
560
+ const scalesList = parseScaleOrderList(scale, min, max, step);
561
+ if (isReversed(direction))
355
562
  scalesList.reverse();
356
563
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
357
564
  let trackProps;
@@ -387,11 +594,11 @@ function Slider(_a) {
387
594
  const { left: x1, top: y1, right: x2, bottom: y2, } = trackElementRef.current.getBoundingClientRect();
388
595
  const mouseX = isTouch ? event.touches[0].clientX : event.clientX;
389
596
  const mouseY = isTouch ? event.touches[0].clientY : event.clientY;
390
- const n = type.isHorizontal(direction)
391
- ? math.normalizeValue(mouseX, x1, x2)
392
- : math.normalizeValue(mouseY, y1, y2);
393
- const v = math.rawValue(type.isReversed(direction) ? 1 - n : n, min, max, skew);
394
- const v2 = math.clamp(math.stepValue(v, step), min, max);
597
+ const n = isHorizontal(direction)
598
+ ? normalizeValue(mouseX, x1, x2)
599
+ : normalizeValue(mouseY, y1, y2);
600
+ const v = rawValue(isReversed(direction) ? 1 - n : n, min, max, skew);
601
+ const v2 = clamp(stepValue(v, step), min, max);
395
602
  if (onChange)
396
603
  onChange(v2);
397
604
  };
@@ -402,18 +609,18 @@ function Slider(_a) {
402
609
  return;
403
610
  event.preventDefault();
404
611
  let x = event.deltaY > 0 ? enableWheel[1] : -enableWheel[1];
405
- if (type.isReversed(direction) || type.isHorizontal(direction))
612
+ if (isReversed(direction) || isHorizontal(direction))
406
613
  x *= -1;
407
614
  let v;
408
615
  if (enableWheel[0] == 'normalized') {
409
- const n = math.normalizeValue(value, min, max, skew);
410
- v = math.rawValue(n + x, min, max, skew);
616
+ const n = normalizeValue(value, min, max, skew);
617
+ v = rawValue(n + x, min, max, skew);
411
618
  }
412
619
  else {
413
620
  v = value + x;
414
621
  }
415
622
  if (onChange)
416
- onChange(math.clamp(math.stepValue(v, step), min, max));
623
+ onChange(clamp(stepValue(v, step), min, max));
417
624
  }, {
418
625
  passive: false,
419
626
  }, [enableWheel, value, min, max, skew, step]);
@@ -428,7 +635,7 @@ function Slider(_a) {
428
635
  return (jsxRuntime.jsxs("div", { className: 'tremolo-slider' + (className ? ` ${className}` : ''), ref: (div) => {
429
636
  wheelRefCallback(div);
430
637
  touchMoveRefCallback(div);
431
- }, tabIndex: -1, role: "slider", "aria-valuenow": value, "aria-valuemax": max, "aria-valuemin": min, "aria-orientation": type.isHorizontal(direction) ? 'horizontal' : 'vertical', css: react.css(Object.assign({ display: 'inline-block', boxSizing: 'border-box', margin: '0.7rem', padding: 0, cursor: 'pointer' }, style)), onPointerDown: (event) => {
638
+ }, tabIndex: -1, role: "slider", "aria-valuenow": value, "aria-valuemax": max, "aria-valuemin": min, "aria-orientation": isHorizontal(direction) ? 'horizontal' : 'vertical', css: react.css(Object.assign({ display: 'inline-block', boxSizing: 'border-box', margin: '0.7rem', padding: 0, cursor: 'pointer' }, style)), onPointerDown: (event) => {
432
639
  thumbDragged.current = true;
433
640
  handleValue(event);
434
641
  }, children: [jsxRuntime.jsx(react.Global, { styles: {
@@ -437,54 +644,54 @@ function Slider(_a) {
437
644
  },
438
645
  } }), jsxRuntime.jsxs("div", { css: react.css({
439
646
  display: 'flex',
440
- flexDirection: type.isHorizontal(direction) ? 'column' : 'row',
647
+ flexDirection: isHorizontal(direction) ? 'column' : 'row',
441
648
  }), children: [jsxRuntime.jsxs("div", { className: "tremolo-slider-track-wrapper", ref: trackElementRef, css: react.css({ position: 'relative' }), children: [jsxRuntime.jsx(SliderTrack, Object.assign({ __css: {
442
- background: `linear-gradient(to ${type.gradientDirection(direction)}, ${color} ${percent}%, ${bg} ${percent}%)`,
443
- borderRadius: util.styleHelper(thickness, '/', 2),
649
+ background: `linear-gradient(to ${gradientDirection(direction)}, ${color} ${percent}%, ${bg} ${percent}%)`,
650
+ borderRadius: styleHelper(thickness, '/', 2),
444
651
  borderStyle: 'solid',
445
652
  borderColor: '#ccc',
446
653
  borderWidth: 2,
447
- width: type.isHorizontal(direction) ? length : thickness,
448
- height: type.isHorizontal(direction) ? thickness : length,
654
+ width: isHorizontal(direction) ? length : thickness,
655
+ height: isHorizontal(direction) ? thickness : length,
449
656
  zIndex: 1,
450
657
  } }, trackProps)), jsxRuntime.jsx(SliderThumb, Object.assign({ color: color, __css: {
451
- top: type.isHorizontal(direction) ? '50%' : `${percentRev}%`,
452
- left: type.isHorizontal(direction) ? `${percentRev}%` : '50%',
453
- } }, thumbProps))] }), scale && (jsxRuntime.jsx("div", { className: "tremolo-slider-scale-wrapper", css: react.css(Object.assign({ position: 'relative', width: type.isHorizontal(direction) ? length : undefined, height: type.isVertical(direction) ? length : undefined, marginLeft: type.isHorizontal(direction)
658
+ top: isHorizontal(direction) ? '50%' : `${percentRev}%`,
659
+ left: isHorizontal(direction) ? `${percentRev}%` : '50%',
660
+ } }, thumbProps))] }), scale && (jsxRuntime.jsx("div", { className: "tremolo-slider-scale-wrapper", css: react.css(Object.assign({ position: 'relative', width: isHorizontal(direction) ? length : undefined, height: isVertical(direction) ? length : undefined, marginLeft: isHorizontal(direction)
454
661
  ? 2
455
- : ((_b = scaleOption === null || scaleOption === void 0 ? void 0 : scaleOption.gap) !== null && _b !== void 0 ? _b : `calc((1.4rem - ${util.styleHelper(thickness)}) / 2)`), marginTop: type.isVertical(direction)
662
+ : ((_b = scaleOption === null || scaleOption === void 0 ? void 0 : scaleOption.gap) !== null && _b !== void 0 ? _b : `calc((1.4rem - ${styleHelper(thickness)}) / 2)`), marginTop: isVertical(direction)
456
663
  ? 2
457
- : ((_c = scaleOption === null || scaleOption === void 0 ? void 0 : scaleOption.gap) !== null && _c !== void 0 ? _c : `calc((1.4rem - ${util.styleHelper(thickness)}) / 2)`) }, scaleOption === null || scaleOption === void 0 ? void 0 : scaleOption.style)), children: scalesList.map((item, index) => {
664
+ : ((_c = scaleOption === null || scaleOption === void 0 ? void 0 : scaleOption.gap) !== null && _c !== void 0 ? _c : `calc((1.4rem - ${styleHelper(thickness)}) / 2)`) }, scaleOption === null || scaleOption === void 0 ? void 0 : scaleOption.style)), children: scalesList.map((item, index) => {
458
665
  var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t;
459
666
  return (jsxRuntime.jsxs("div", { css: react.css({
460
667
  display: 'flex',
461
- flexDirection: type.isHorizontal(direction) ? 'column' : 'row',
668
+ flexDirection: isHorizontal(direction) ? 'column' : 'row',
462
669
  justifyContent: 'center',
463
670
  alignItems: 'center',
464
671
  position: 'absolute',
465
- left: type.isHorizontal(direction)
466
- ? `${type.isReversed(direction)
467
- ? 100 - math.normalizeValue(item.at, min, max, skew) * 100
468
- : math.normalizeValue(item.at, min, max, skew) * 100}%`
672
+ left: isHorizontal(direction)
673
+ ? `${isReversed(direction)
674
+ ? 100 - normalizeValue(item.at, min, max, skew) * 100
675
+ : normalizeValue(item.at, min, max, skew) * 100}%`
469
676
  : undefined,
470
- top: type.isVertical(direction)
471
- ? `${type.isReversed(direction)
472
- ? 100 - math.normalizeValue(item.at, min, max, skew) * 100
473
- : math.normalizeValue(item.at, min, max, skew) * 100}%`
677
+ top: isVertical(direction)
678
+ ? `${isReversed(direction)
679
+ ? 100 - normalizeValue(item.at, min, max, skew) * 100
680
+ : normalizeValue(item.at, min, max, skew) * 100}%`
474
681
  : undefined,
475
- translate: type.isHorizontal(direction) ? '-50% 0' : '0 -50%',
682
+ translate: isHorizontal(direction) ? '-50% 0' : '0 -50%',
476
683
  zIndex: 10,
477
684
  }), children: [((_b = (_a = item.type) !== null && _a !== void 0 ? _a : scaleOption === null || scaleOption === void 0 ? void 0 : scaleOption.defaultType) !== null && _b !== void 0 ? _b : 'mark-number') !=
478
685
  'number' && (jsxRuntime.jsx("div", { className: "tremolo-slider-scale-mark", css: react.css({
479
- width: type.isHorizontal(direction)
686
+ width: isHorizontal(direction)
480
687
  ? ((_d = (_c = item.style) === null || _c === void 0 ? void 0 : _c.thickness) !== null && _d !== void 0 ? _d : 1)
481
688
  : ((_f = (_e = item.style) === null || _e === void 0 ? void 0 : _e.length) !== null && _f !== void 0 ? _f : '0.5rem'),
482
- height: type.isVertical(direction)
689
+ height: isVertical(direction)
483
690
  ? ((_h = (_g = item.style) === null || _g === void 0 ? void 0 : _g.thickness) !== null && _h !== void 0 ? _h : 1)
484
691
  : ((_k = (_j = item.style) === null || _j === void 0 ? void 0 : _j.length) !== null && _k !== void 0 ? _k : '0.5rem'),
485
692
  backgroundColor: (_o = (_m = (_l = item.style) === null || _l === void 0 ? void 0 : _l.markColor) !== null && _m !== void 0 ? _m : scaleOption === null || scaleOption === void 0 ? void 0 : scaleOption.markColor) !== null && _o !== void 0 ? _o : '#222',
486
- marginBottom: type.isHorizontal(direction) ? 2 : undefined,
487
- marginRight: type.isVertical(direction) ? 2 : undefined,
693
+ marginBottom: isHorizontal(direction) ? 2 : undefined,
694
+ marginRight: isVertical(direction) ? 2 : undefined,
488
695
  }) })), ((_q = (_p = item.type) !== null && _p !== void 0 ? _p : scaleOption === null || scaleOption === void 0 ? void 0 : scaleOption.defaultType) !== null && _q !== void 0 ? _q : 'mark-number') !=
489
696
  'mark' && (jsxRuntime.jsx("div", { className: "tremolo-slider-scale-number", css: react.css({
490
697
  color: (_s = (_r = item.style) === null || _r === void 0 ? void 0 : _r.labelColor) !== null && _s !== void 0 ? _s : scaleOption === null || scaleOption === void 0 ? void 0 : scaleOption.labelColor,
@@ -494,12 +701,19 @@ function Slider(_a) {
494
701
  }) }))] })] }));
495
702
  }
496
703
 
704
+ exports.AnimationCanvas = AnimationCanvas;
497
705
  exports.Knob = Knob;
706
+ exports.NumberInput = NumberInput;
498
707
  exports.Slider = Slider;
499
- Object.keys(math).forEach(function (k) {
500
- if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
501
- enumerable: true,
502
- get: function () { return math[k]; }
503
- });
504
- });
708
+ exports.clamp = clamp;
709
+ exports.dbToGain = dbToGain;
710
+ exports.decimalPart = decimalPart;
711
+ exports.degree = degree;
712
+ exports.gainToDb = gainToDb;
713
+ exports.normalizeValue = normalizeValue;
714
+ exports.radian = radian;
715
+ exports.rawValue = rawValue;
716
+ exports.skewWithCenterValue = skewWithCenterValue;
717
+ exports.stepValue = stepValue;
718
+ exports.toFixed = toFixed;
505
719
  //# sourceMappingURL=index.js.map