@tremolo-ui/react 0.0.2 → 0.0.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.
Files changed (120) hide show
  1. package/README.md +11 -0
  2. package/dist/cjs/index.js +326 -118
  3. package/dist/cjs/index.js.map +1 -1
  4. package/dist/cjs/types/components/AnimationCanvas/canvas.d.ts.map +1 -0
  5. package/dist/cjs/types/components/AnimationCanvas/index.d.ts +20 -0
  6. package/dist/cjs/types/components/AnimationCanvas/index.d.ts.map +1 -0
  7. package/dist/cjs/types/{src/components → components}/Knob/index.d.ts +16 -4
  8. package/dist/cjs/types/components/Knob/index.d.ts.map +1 -0
  9. package/dist/cjs/types/{src/components → components}/NumberInput/index.d.ts +3 -5
  10. package/dist/cjs/types/components/NumberInput/index.d.ts.map +1 -0
  11. package/dist/cjs/types/{src/components → components}/Slider/Thumb.d.ts +2 -2
  12. package/dist/cjs/types/components/Slider/Thumb.d.ts.map +1 -0
  13. package/dist/cjs/types/{src/components → components}/Slider/Track.d.ts +1 -1
  14. package/dist/cjs/types/components/Slider/Track.d.ts.map +1 -0
  15. package/dist/cjs/types/{src/components → components}/Slider/index.d.ts +5 -5
  16. package/dist/cjs/types/components/Slider/index.d.ts.map +1 -0
  17. package/dist/cjs/types/components/Slider/type.d.ts +7 -0
  18. package/dist/cjs/types/components/Slider/type.d.ts.map +1 -0
  19. package/dist/cjs/types/hooks/useCallbackRef.d.ts.map +1 -0
  20. package/dist/cjs/types/hooks/useEventListener.d.ts.map +1 -0
  21. package/dist/cjs/types/hooks/useRefCallbackEvent.d.ts.map +1 -0
  22. package/dist/cjs/types/index.d.ts +5 -0
  23. package/dist/cjs/types/index.d.ts.map +1 -0
  24. package/dist/cjs/types/system/pseudo.d.ts.map +1 -0
  25. package/dist/esm/index.js +273 -65
  26. package/dist/esm/index.js.map +1 -1
  27. package/dist/esm/types/components/AnimationCanvas/canvas.d.ts.map +1 -0
  28. package/dist/esm/types/components/AnimationCanvas/index.d.ts +20 -0
  29. package/dist/esm/types/components/AnimationCanvas/index.d.ts.map +1 -0
  30. package/dist/esm/types/{src/components → components}/Knob/index.d.ts +16 -4
  31. package/dist/esm/types/components/Knob/index.d.ts.map +1 -0
  32. package/dist/esm/types/{src/components → components}/NumberInput/index.d.ts +3 -5
  33. package/dist/esm/types/components/NumberInput/index.d.ts.map +1 -0
  34. package/dist/esm/types/{src/components → components}/Slider/Thumb.d.ts +2 -2
  35. package/dist/esm/types/components/Slider/Thumb.d.ts.map +1 -0
  36. package/dist/esm/types/{src/components → components}/Slider/Track.d.ts +1 -1
  37. package/dist/esm/types/components/Slider/Track.d.ts.map +1 -0
  38. package/dist/esm/types/{src/components → components}/Slider/index.d.ts +5 -5
  39. package/dist/esm/types/components/Slider/index.d.ts.map +1 -0
  40. package/dist/esm/types/components/Slider/type.d.ts +7 -0
  41. package/dist/esm/types/components/Slider/type.d.ts.map +1 -0
  42. package/dist/esm/types/hooks/useCallbackRef.d.ts.map +1 -0
  43. package/dist/esm/types/hooks/useEventListener.d.ts.map +1 -0
  44. package/dist/esm/types/hooks/useRefCallbackEvent.d.ts.map +1 -0
  45. package/dist/esm/types/index.d.ts +5 -0
  46. package/dist/esm/types/index.d.ts.map +1 -0
  47. package/dist/esm/types/system/pseudo.d.ts.map +1 -0
  48. package/dist/styles/variables.css +4 -0
  49. package/package.json +26 -9
  50. package/src/Globals.d.ts +2 -0
  51. package/src/components/AnimationCanvas/canvas.ts +30 -0
  52. package/src/components/AnimationCanvas/index.tsx +169 -0
  53. package/src/components/Knob/index.tsx +292 -0
  54. package/src/components/NumberInput/index.tsx +165 -0
  55. package/src/components/Slider/Thumb.tsx +54 -0
  56. package/src/components/Slider/Track.tsx +20 -0
  57. package/src/components/Slider/index.tsx +326 -0
  58. package/src/components/Slider/type.ts +16 -0
  59. package/src/hooks/useCallbackRef.ts +20 -0
  60. package/src/hooks/useEventListener.ts +49 -0
  61. package/src/hooks/useRefCallbackEvent.ts +32 -0
  62. package/src/index.ts +4 -0
  63. package/src/styles/variables.css +4 -0
  64. package/src/system/pseudo.ts +55 -0
  65. package/dist/cjs/index.d.ts +0 -75
  66. package/dist/cjs/types/src/components/AnimationCanvas/canvas.d.ts.map +0 -1
  67. package/dist/cjs/types/src/components/AnimationCanvas/index.d.ts +0 -20
  68. package/dist/cjs/types/src/components/AnimationCanvas/index.d.ts.map +0 -1
  69. package/dist/cjs/types/src/components/Knob/index.d.ts.map +0 -1
  70. package/dist/cjs/types/src/components/NumberInput/index.d.ts.map +0 -1
  71. package/dist/cjs/types/src/components/Slider/Thumb.d.ts.map +0 -1
  72. package/dist/cjs/types/src/components/Slider/Track.d.ts.map +0 -1
  73. package/dist/cjs/types/src/components/Slider/index.d.ts.map +0 -1
  74. package/dist/cjs/types/src/components/Slider/type.d.ts +0 -6
  75. package/dist/cjs/types/src/components/Slider/type.d.ts.map +0 -1
  76. package/dist/cjs/types/src/hooks/useCallbackRef.d.ts.map +0 -1
  77. package/dist/cjs/types/src/hooks/useEventListener.d.ts.map +0 -1
  78. package/dist/cjs/types/src/hooks/useRefCallbackEvent.d.ts.map +0 -1
  79. package/dist/cjs/types/src/index.d.ts +0 -5
  80. package/dist/cjs/types/src/index.d.ts.map +0 -1
  81. package/dist/cjs/types/src/math.d.ts +0 -8
  82. package/dist/cjs/types/src/math.d.ts.map +0 -1
  83. package/dist/cjs/types/src/system/pseudo.d.ts.map +0 -1
  84. package/dist/cjs/types/src/types.d.ts +0 -2
  85. package/dist/cjs/types/src/types.d.ts.map +0 -1
  86. package/dist/cjs/types/src/util.d.ts +0 -2
  87. package/dist/cjs/types/src/util.d.ts.map +0 -1
  88. package/dist/esm/index.d.ts +0 -75
  89. package/dist/esm/types/src/components/AnimationCanvas/canvas.d.ts.map +0 -1
  90. package/dist/esm/types/src/components/AnimationCanvas/index.d.ts +0 -20
  91. package/dist/esm/types/src/components/AnimationCanvas/index.d.ts.map +0 -1
  92. package/dist/esm/types/src/components/Knob/index.d.ts.map +0 -1
  93. package/dist/esm/types/src/components/NumberInput/index.d.ts.map +0 -1
  94. package/dist/esm/types/src/components/Slider/Thumb.d.ts.map +0 -1
  95. package/dist/esm/types/src/components/Slider/Track.d.ts.map +0 -1
  96. package/dist/esm/types/src/components/Slider/index.d.ts.map +0 -1
  97. package/dist/esm/types/src/components/Slider/type.d.ts +0 -6
  98. package/dist/esm/types/src/components/Slider/type.d.ts.map +0 -1
  99. package/dist/esm/types/src/hooks/useCallbackRef.d.ts.map +0 -1
  100. package/dist/esm/types/src/hooks/useEventListener.d.ts.map +0 -1
  101. package/dist/esm/types/src/hooks/useRefCallbackEvent.d.ts.map +0 -1
  102. package/dist/esm/types/src/index.d.ts +0 -5
  103. package/dist/esm/types/src/index.d.ts.map +0 -1
  104. package/dist/esm/types/src/math.d.ts +0 -8
  105. package/dist/esm/types/src/math.d.ts.map +0 -1
  106. package/dist/esm/types/src/system/pseudo.d.ts.map +0 -1
  107. package/dist/esm/types/src/types.d.ts +0 -2
  108. package/dist/esm/types/src/types.d.ts.map +0 -1
  109. package/dist/esm/types/src/util.d.ts +0 -2
  110. package/dist/esm/types/src/util.d.ts.map +0 -1
  111. /package/dist/cjs/types/{src/components → components}/AnimationCanvas/canvas.d.ts +0 -0
  112. /package/dist/cjs/types/{src/hooks → hooks}/useCallbackRef.d.ts +0 -0
  113. /package/dist/cjs/types/{src/hooks → hooks}/useEventListener.d.ts +0 -0
  114. /package/dist/cjs/types/{src/hooks → hooks}/useRefCallbackEvent.d.ts +0 -0
  115. /package/dist/cjs/types/{src/system → system}/pseudo.d.ts +0 -0
  116. /package/dist/esm/types/{src/components → components}/AnimationCanvas/canvas.d.ts +0 -0
  117. /package/dist/esm/types/{src/hooks → hooks}/useCallbackRef.d.ts +0 -0
  118. /package/dist/esm/types/{src/hooks → hooks}/useEventListener.d.ts +0 -0
  119. /package/dist/esm/types/{src/hooks → hooks}/useRefCallbackEvent.d.ts +0 -0
  120. /package/dist/esm/types/{src/system → system}/pseudo.d.ts +0 -0
package/dist/esm/index.js CHANGED
@@ -1,10 +1,6 @@
1
- import { normalizeValue, radian, rawValue, clamp, stepValue } from 'common/math';
2
- export * from 'common/math';
3
1
  import { jsxs, Fragment, jsx } from '@emotion/react/jsx-runtime';
4
2
  import { css, Global } from '@emotion/react';
5
- import React, { useRef, useInsertionEffect, useCallback, useEffect, useMemo } from 'react';
6
- import { isReversed, parseScaleOrderList, isHorizontal, gradientDirection, isVertical } from 'common/components/Slider/type';
7
- import { styleHelper } from 'common/util';
3
+ import React, { useRef, useEffect, useInsertionEffect, useCallback, useMemo, useState } from 'react';
8
4
 
9
5
  /******************************************************************************
10
6
  Copyright (c) Microsoft Corporation.
@@ -40,52 +36,7 @@ typeof SuppressedError === "function" ? SuppressedError : function (error, suppr
40
36
  return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
41
37
  };
42
38
 
43
- /**
44
- * This hook is user-land implementation of the experimental `useEffectEvent` hook.
45
- * React docs: https://react.dev/learn/separating-events-from-effects#declaring-an-effect-event
46
- */
47
- function useCallbackRef(callback, deps = []) {
48
- const callbackRef = useRef(() => {
49
- throw new Error('Cannot call an event handler while rendering.');
50
- });
51
- useInsertionEffect(() => {
52
- callbackRef.current = callback;
53
- });
54
- return useCallback((...args) => { var _a; return (_a = callbackRef.current) === null || _a === void 0 ? void 0 : _a.call(callbackRef, ...args); }, deps);
55
- }
56
-
57
- function useEventListener(target, event, handler, options) {
58
- const listener = useCallbackRef(handler);
59
- useEffect(() => {
60
- const node = typeof target === 'function' ? target() : (target !== null && target !== void 0 ? target : document);
61
- if (!handler || !node)
62
- return;
63
- node.addEventListener(event, listener, options);
64
- return () => {
65
- node.removeEventListener(event, listener, options);
66
- };
67
- }, [event, target, options, listener, handler]);
68
- return () => {
69
- const node = typeof target === 'function' ? target() : (target !== null && target !== void 0 ? target : document);
70
- node === null || node === void 0 ? void 0 : node.removeEventListener(event, listener, options);
71
- };
72
- }
73
-
74
- function useRefCallbackEvent(event, handler, options, deps = []) {
75
- return useMemo(() => {
76
- let cleanup;
77
- return (node) => {
78
- if (!node) {
79
- cleanup === null || cleanup === void 0 ? void 0 : cleanup();
80
- return;
81
- }
82
- node.addEventListener(event, handler, options);
83
- cleanup = () => {
84
- node.removeEventListener(event, handler, options);
85
- };
86
- };
87
- }, deps);
88
- }
39
+ function r(e){var t,f,n="";if("string"==typeof e||"number"==typeof e)n+=e;else if("object"==typeof e)if(Array.isArray(e)){var o=e.length;for(t=0;t<o;t++)e[t]&&(f=r(e[t]))&&(n&&(n+=" "),n+=f);}else for(f in e)e[f]&&(n&&(n+=" "),n+=f);return n}function clsx(){for(var e,t,f=0,n="",o=arguments.length;f<o;f++)(e=arguments[f])&&(t=r(e))&&(n&&(n+=" "),n+=t);return n}
89
40
 
90
41
  // https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/save
91
42
  const drawingState = [
@@ -120,7 +71,7 @@ function setDprConfig(canvas, context, width, height, dpr) {
120
71
  * A simple animatable canvas with requestAnimationFrame()
121
72
  */
122
73
  function AnimationCanvas(_a) {
123
- var { width = 100, height = 100, style, relativeSize = false, reduceFlickering = true, options, init, draw } = _a, props = __rest(_a, ["width", "height", "style", "relativeSize", "reduceFlickering", "options", "init", "draw"]);
74
+ var { width = 100, height = 100, style, relativeSize = false, reduceFlickering = true, options, init, draw, className } = _a, props = __rest(_a, ["width", "height", "style", "relativeSize", "reduceFlickering", "options", "init", "draw", "className"]);
124
75
  const canvasRef = useRef(null);
125
76
  const memoCanvasRef = useRef(null);
126
77
  const reqIdRef = useRef();
@@ -145,7 +96,7 @@ function AnimationCanvas(_a) {
145
96
  const memoCanvas = memoCanvasRef.current;
146
97
  const memoContext = memoCanvas === null || memoCanvas === void 0 ? void 0 : memoCanvas.getContext('2d', options);
147
98
  const resizeObserver = new ResizeObserver(() => {
148
- console.log('resize');
99
+ // console.log('resize')
149
100
  // Prevents loss of some context when the canvas is resized
150
101
  const contextMemo = {};
151
102
  for (const prop of drawingState) {
@@ -196,7 +147,117 @@ function AnimationCanvas(_a) {
196
147
  };
197
148
  }
198
149
  }, [loop]);
199
- return (jsxs(Fragment, { children: [jsx("canvas", Object.assign({ width: relativeSize ? 0 : width, height: relativeSize ? 0 : height, css: css(style), ref: canvasRef }, props)), relativeSize && reduceFlickering && (jsx("canvas", { style: { display: 'none' }, width: relativeSize ? 0 : width, height: relativeSize ? 0 : height, ref: memoCanvasRef }))] }));
150
+ return (jsxs(Fragment, { children: [jsx("canvas", Object.assign({ className: clsx('tremolo-animation-canvas', className), width: relativeSize ? 0 : width, height: relativeSize ? 0 : height, css: css(style), ref: canvasRef }, props)), relativeSize && reduceFlickering && (jsx("canvas", { style: { display: 'none' }, width: relativeSize ? 0 : width, height: relativeSize ? 0 : height, ref: memoCanvasRef }))] }));
151
+ }
152
+
153
+ function clamp(value, min, max) {
154
+ return Math.max(min, Math.min(value, max));
155
+ }
156
+ function normalizeValue(rawValue, min, max, skew = 1) {
157
+ if (min >= max)
158
+ throw new RangeError('requirements: min < max');
159
+ const v = clamp((rawValue - min) / (max - min), 0, 1);
160
+ return Math.pow(v, skew);
161
+ }
162
+ function rawValue(normalizedValue, min, max, skew = 1) {
163
+ if (min >= max)
164
+ throw new RangeError('requirements: min < max');
165
+ const v = skew == 1
166
+ ? clamp(normalizedValue, 0, 1)
167
+ : Math.exp(Math.log(clamp(normalizedValue, 0, 1)) / skew);
168
+ return min + v * (max - min);
169
+ }
170
+ function stepValue(value, step) {
171
+ var _a;
172
+ if (step <= 0)
173
+ throw new RangeError('requirements: step > 0');
174
+ const quotient = Math.floor(value / step);
175
+ const decimalDigits = (_a = decimalPart$1(step)) === null || _a === void 0 ? void 0 : _a.length;
176
+ const v = toFixed$2(quotient * step, decimalDigits);
177
+ const next = toFixed$2((quotient + 1) * step, decimalDigits);
178
+ return Math.abs(value - v) < Math.abs(value - next) ? v : next;
179
+ }
180
+ function toFixed$2(x, fractionDigits) {
181
+ return Number(x.toFixed(fractionDigits));
182
+ }
183
+ function decimalPart$1(x) {
184
+ return String(x).split('.')[1];
185
+ }
186
+ function radian(degree) {
187
+ return (Math.PI * degree) / 180;
188
+ }
189
+
190
+ function styleHelper(value, op, influencer) {
191
+ if (op && influencer) {
192
+ if (typeof value == 'number') {
193
+ if (op == '+')
194
+ return `${value + influencer}px`;
195
+ if (op == '-')
196
+ return `${value - influencer}px`;
197
+ if (op == '*')
198
+ return `${value * influencer}px`;
199
+ if (op == '/')
200
+ return `${value / influencer}px`;
201
+ }
202
+ else {
203
+ return `calc(${value}px ${op} ${influencer})`;
204
+ }
205
+ }
206
+ else {
207
+ if (typeof value == 'number') {
208
+ return `${value}px`;
209
+ }
210
+ else {
211
+ return value;
212
+ }
213
+ }
214
+ }
215
+
216
+ /**
217
+ * This hook is user-land implementation of the experimental `useEffectEvent` hook.
218
+ * React docs: https://react.dev/learn/separating-events-from-effects#declaring-an-effect-event
219
+ */
220
+ function useCallbackRef(callback, deps = []) {
221
+ const callbackRef = useRef(() => {
222
+ throw new Error('Cannot call an event handler while rendering.');
223
+ });
224
+ useInsertionEffect(() => {
225
+ callbackRef.current = callback;
226
+ });
227
+ return useCallback((...args) => { var _a; return (_a = callbackRef.current) === null || _a === void 0 ? void 0 : _a.call(callbackRef, ...args); }, deps);
228
+ }
229
+
230
+ function useEventListener(target, event, handler, options) {
231
+ const listener = useCallbackRef(handler);
232
+ useEffect(() => {
233
+ const node = typeof target === 'function' ? target() : (target !== null && target !== void 0 ? target : document);
234
+ if (!handler || !node)
235
+ return;
236
+ node.addEventListener(event, listener, options);
237
+ return () => {
238
+ node.removeEventListener(event, listener, options);
239
+ };
240
+ }, [event, target, options, listener, handler]);
241
+ return () => {
242
+ const node = typeof target === 'function' ? target() : (target !== null && target !== void 0 ? target : document);
243
+ node === null || node === void 0 ? void 0 : node.removeEventListener(event, listener, options);
244
+ };
245
+ }
246
+
247
+ function useRefCallbackEvent(event, handler, options, deps = []) {
248
+ return useMemo(() => {
249
+ let cleanup;
250
+ return (node) => {
251
+ if (!node) {
252
+ cleanup === null || cleanup === void 0 ? void 0 : cleanup();
253
+ return;
254
+ }
255
+ node.addEventListener(event, handler, options);
256
+ cleanup = () => {
257
+ node.removeEventListener(event, handler, options);
258
+ };
259
+ };
260
+ }, deps);
200
261
  }
201
262
 
202
263
  const defaultOptions = {
@@ -211,9 +272,7 @@ const defaultOptions = {
211
272
  * simple rotary Knob.
212
273
  */
213
274
  function Knob(_a) {
214
- var { value, min, max, defaultValue = min, startValue = min, step = 1, skew = 1, size, width = 50, height = 50, options = defaultOptions,
215
- // draw,
216
- cursor = 'pointer', style, bodyNoSelect = true, enableWheel, enableDoubleClickDefault = true, onChange, _active, _focus, _hover } = _a, props = __rest(_a, ["value", "min", "max", "defaultValue", "startValue", "step", "skew", "size", "width", "height", "options", "cursor", "style", "bodyNoSelect", "enableWheel", "enableDoubleClickDefault", "onChange", "_active", "_focus", "_hover"]);
275
+ var { value, min, max, defaultValue = min, startValue = min, step = 1, skew = 1, size, width = 50, height = 50, options = defaultOptions, draw, bodyNoSelect = true, enableWheel, enableDoubleClickDefault = true, style, className, onChange, _active, _focus, _hover } = _a, props = __rest(_a, ["value", "min", "max", "defaultValue", "startValue", "step", "skew", "size", "width", "height", "options", "draw", "bodyNoSelect", "enableWheel", "enableDoubleClickDefault", "style", "className", "onChange", "_active", "_focus", "_hover"]);
217
276
  // -- state and ref ---
218
277
  // const [privateValue, setPrivateValue] = useState(value);
219
278
  const dragOffsetY = useRef(undefined);
@@ -270,10 +329,10 @@ function Knob(_a) {
270
329
  if (bodyNoSelect)
271
330
  document.body.classList.remove('no-select');
272
331
  });
273
- return (jsxs("div", Object.assign({ className: "tremolo-knob", ref: (div) => {
332
+ return (jsxs("div", Object.assign({ className: clsx('tremolo-knob', className), ref: (div) => {
274
333
  wheelRefCallback(div);
275
334
  touchMoveRefCallback(div);
276
- }, role: "slider", tabIndex: 0, "aria-valuenow": value, "aria-valuemax": max, "aria-valuemin": min, css: css(Object.assign(Object.assign({ display: 'inline-block', width: size !== null && size !== void 0 ? size : width, height: size !== null && size !== void 0 ? size : height, cursor: cursor }, style), { ':active': Object.assign({}, _active), ':focus': Object.assign({}, _focus), ':hover': Object.assign({}, _hover) })), onPointerDown: (event) => {
335
+ }, role: "slider", tabIndex: 0, "aria-valuenow": value, "aria-valuemax": max, "aria-valuemin": min, css: css(Object.assign(Object.assign({ display: 'block', width: size !== null && size !== void 0 ? size : width, height: size !== null && size !== void 0 ? size : height, cursor: 'pointer' }, style), { ':active': Object.assign({}, _active), ':focus': Object.assign({}, _focus), ':hover': Object.assign({}, _hover) })), onPointerDown: (event) => {
277
336
  dragOffsetY.current = event.screenY;
278
337
  handleEvent(event);
279
338
  }, onDoubleClick: () => {
@@ -287,12 +346,14 @@ function Knob(_a) {
287
346
  } }), jsx(AnimationCanvas, { width: adaptSize(size !== null && size !== void 0 ? size : width), height: adaptSize(size !== null && size !== void 0 ? size : height), relativeSize: isRelativeSize,
288
347
  // TODO
289
348
  // reduceFlickering={false}
290
- draw: (ctx, _w, _h) => {
349
+ draw: draw ? draw : (ctx, width, height) => {
291
350
  const p = normalizeValue(value, min, max, skew);
292
351
  const startP = normalizeValue(startValue, min, max, skew);
293
- const w = _w.current, cx = w / 2;
294
- const h = _h.current, cy = h / 2;
352
+ const w = width.current, cx = w / 2;
353
+ const h = height.current, cy = h / 2;
295
354
  const r = Math.min(cx, cy);
355
+ if (r <= 0)
356
+ return;
296
357
  // reset
297
358
  ctx.clearRect(0, 0, w, h);
298
359
  ctx.lineCap = 'butt';
@@ -327,12 +388,159 @@ function Knob(_a) {
327
388
  } })] })));
328
389
  }
329
390
 
391
+ function toFixed$1(x, fractionDigits) {
392
+ return Number(x.toFixed(fractionDigits));
393
+ }
394
+
395
+ function selectUnit(units, value) {
396
+ if (typeof units == 'string')
397
+ return [units, 1];
398
+ let i = 0;
399
+ for (; i < units.length; i++) {
400
+ if (Math.abs(units[i][1]) > Math.abs(value))
401
+ break;
402
+ }
403
+ return units[Math.max(0, i - 1)];
404
+ }
405
+ function parseValue(inputString, units, digit, defaultValue = 0) {
406
+ let str = inputString.trim();
407
+ if (str == '')
408
+ str = String(defaultValue);
409
+ if (!units) {
410
+ return {
411
+ rawValue: Number(str) || defaultValue,
412
+ formatValue: str,
413
+ unit: '',
414
+ };
415
+ }
416
+ else if (typeof units == 'string') {
417
+ return {
418
+ rawValue: Number(str) || defaultValue,
419
+ formatValue: `${str}${units}`,
420
+ unit: units,
421
+ };
422
+ }
423
+ const unitList = units.map(([u, _s]) => u);
424
+ const scaleList = units.map(([_u, s]) => s);
425
+ let rawValue = defaultValue;
426
+ let formatValue = `${scaleList[0]}`;
427
+ let unit = unitList[0];
428
+ const m = str.match(/^(-?\d+(\.\d+)?)\s*(\w*)$/);
429
+ if (m) {
430
+ rawValue = Number(m[1]) || 0;
431
+ const inputUnit = m[3];
432
+ if (inputUnit != '' && unitList.indexOf(inputUnit) != -1) {
433
+ rawValue *= scaleList[unitList.indexOf(inputUnit)];
434
+ const s = selectUnit(units, rawValue);
435
+ const v = rawValue / s[1];
436
+ formatValue = `${digit != undefined ? toFixed$1(v, digit) : v}${s[0]}`;
437
+ unit = s[0];
438
+ }
439
+ else {
440
+ const [u, scale] = selectUnit(units, rawValue);
441
+ const v = rawValue / scale;
442
+ formatValue = `${digit != undefined ? toFixed$1(v, digit) : v}${u}`;
443
+ unit = u;
444
+ }
445
+ }
446
+ return {
447
+ rawValue: rawValue,
448
+ formatValue: formatValue,
449
+ unit: unit,
450
+ };
451
+ }
452
+
453
+ /**
454
+ * Input with some useful functions for entering numerical values.
455
+ */
456
+ function NumberInput(_a) {
457
+ var { value, min, max, step, defaultValue = min, 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", "defaultValue", "units", "disabled", "placeholder", "readOnly", "tabIndex", "strict", "digit", "selectWithFocus", "blurWithEnter", "className", "style", "onChange", "onFocus", "onBlur"]);
458
+ const [showValue, setShowValue] = useState(parseValue(String(value), units, digit, defaultValue).formatValue);
459
+ const calculatedStrict = units ? false : strict;
460
+ const { _active, _focus, _hover } = pseudo;
461
+ return (jsx("input", { className: clsx('tremolo-number-input', className), value: showValue, min: min, max: max, step: step, type: calculatedStrict ? 'number' : 'text', spellCheck: 'false', disabled: disabled, placeholder: placeholder, readOnly: readOnly, tabIndex: tabIndex, css: css(Object.assign(Object.assign({ display: 'block', font: 'inherit', margin: 0, paddingTop: 6, paddingBottom: 6, paddingLeft: 10, paddingRight: 10, borderStyle: 'solid', borderWidth: 1, borderColor: '#bbb', borderRadius: 4, outline: 0, transition: 'all 0.1s linear', appearance: 'textfield', '&:hover': Object.assign({ borderColor: 'var(--tremolo-theme-color)' }, _hover), '&:focus': Object.assign({ borderColor: 'var(--tremolo-theme-color)', boxShadow: '0px 0px 0px 2px rgba(var(--tremolo-theme-color-rgb), 0.1)' }, _focus) }, (_active && { '&:active': Object.assign({}, _active) })), style)), onChange: (event) => {
462
+ const v = event.currentTarget.value;
463
+ setShowValue(v);
464
+ const newValue = clamp(parseValue(v, units, digit, defaultValue).rawValue, min !== null && min !== void 0 ? min : -Infinity, max !== null && max !== void 0 ? max : Infinity);
465
+ if (onChange)
466
+ onChange(newValue, event);
467
+ }, onFocus: (event) => {
468
+ const v = event.currentTarget.value;
469
+ const parsed = parseValue(v, units, digit, defaultValue);
470
+ if (selectWithFocus == 'all') {
471
+ event.currentTarget.setSelectionRange(0, v.length);
472
+ }
473
+ else if (selectWithFocus == 'number') {
474
+ event.currentTarget.setSelectionRange(0, parsed.formatValue.length - parsed.unit.length);
475
+ }
476
+ if (onFocus)
477
+ onFocus(parsed.rawValue, event);
478
+ }, onBlur: (event) => {
479
+ // update value
480
+ const v = event.currentTarget.value;
481
+ const parsed = parseValue(v, units, digit, defaultValue);
482
+ setShowValue(parsed.formatValue);
483
+ if (onBlur)
484
+ onBlur(parsed.rawValue, event);
485
+ }, onKeyDown: (event) => {
486
+ if (blurWithEnter && event.key == 'Enter') {
487
+ event.currentTarget.blur();
488
+ }
489
+ } }));
490
+ }
491
+
492
+ function toFixed(x, fractionDigits) {
493
+ return Number(x.toFixed(fractionDigits));
494
+ }
495
+ function decimalPart(x) {
496
+ return String(x).split('.')[1];
497
+ }
498
+
499
+ function isHorizontal(d) {
500
+ return d == 'right' || d == 'left';
501
+ }
502
+ function isVertical(d) {
503
+ return d == 'up' || d == 'down';
504
+ }
505
+ function isReversed(d) {
506
+ return d == 'left' || d == 'up';
507
+ }
508
+ function gradientDirection(d) {
509
+ if (d == 'left' || d == 'right')
510
+ return d;
511
+ if (d == 'up')
512
+ return 'top';
513
+ if (d == 'down')
514
+ return 'bottom';
515
+ }
516
+ function parseScaleOrderList(scale, min, max, step) {
517
+ var _a;
518
+ let scalesList = [];
519
+ if (!scale) ;
520
+ else if (typeof scale[0] == 'object') {
521
+ scalesList = scale;
522
+ }
523
+ else {
524
+ const per = scale[0] == 'step' ? step : scale[0];
525
+ const count = Math.floor(max / per) - Math.ceil(min / per) + 1;
526
+ for (let i = 0; i < count; i++) {
527
+ const at = toFixed(per * (Math.ceil(min / per) + i), (_a = decimalPart(per)) === null || _a === void 0 ? void 0 : _a.length);
528
+ scalesList.push({ at: at, type: scale[1] });
529
+ }
530
+ }
531
+ return scalesList;
532
+ }
533
+
330
534
  function SliderThumb({ color, children, style, __css, }) {
331
535
  return (jsx("div", { className: "tremolo-slider-thumb-wrapper", css: css(Object.assign({ width: 'fit-content', height: 'fit-content', position: 'absolute', translate: '-50% -50%', zIndex: 100 }, __css)), children: children ? (children) : (
332
536
  // default slider thumb
333
537
  jsx("div", { className: "tremolo-slider-thumb",
334
538
  // eslint-disable-next-line jsx-a11y/no-noninteractive-tabindex
335
- tabIndex: 0, css: css(Object.assign({ background: color, width: '1.4rem', height: '1.4rem', borderRadius: '50%' }, style)) })) }));
539
+ tabIndex: 0, css: css(Object.assign({ background: color, width: '1.4rem', height: '1.4rem', borderRadius: '50%', '&:hover': {
540
+ background: `color-mix(in srgb, ${color} 95%, black)`
541
+ }, '&:active': {
542
+ boxShadow: '0px 0px 0px 3px rgba(var(--tremolo-theme-color-rgb), 0.2)',
543
+ } }, style)) })) }));
336
544
  }
337
545
 
338
546
  function SliderTrack({ children, style, __css, }) {
@@ -424,7 +632,7 @@ function Slider(_a) {
424
632
  if (bodyNoSelect)
425
633
  document.body.classList.remove('no-select');
426
634
  });
427
- return (jsxs("div", { className: 'tremolo-slider' + (className ? ` ${className}` : ''), ref: (div) => {
635
+ return (jsxs("div", { className: clsx('tremolo-slider', className), ref: (div) => {
428
636
  wheelRefCallback(div);
429
637
  touchMoveRefCallback(div);
430
638
  }, tabIndex: -1, role: "slider", "aria-valuenow": value, "aria-valuemax": max, "aria-valuemin": min, "aria-orientation": isHorizontal(direction) ? 'horizontal' : 'vertical', css: css(Object.assign({ display: 'inline-block', boxSizing: 'border-box', margin: '0.7rem', padding: 0, cursor: 'pointer' }, style)), onPointerDown: (event) => {
@@ -493,5 +701,5 @@ function Slider(_a) {
493
701
  }) }))] })] }));
494
702
  }
495
703
 
496
- export { Knob, Slider };
704
+ export { AnimationCanvas, Knob, NumberInput, Slider, SliderThumb, SliderTrack, gradientDirection };
497
705
  //# sourceMappingURL=index.js.map