@widgetic/design-system 0.5.40 → 0.5.42

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 (166) hide show
  1. package/README.md +73 -0
  2. package/dist/components/ContentRefreshOverlay.svelte +103 -0
  3. package/dist/components/ContentRefreshOverlay.svelte.d.ts +18 -0
  4. package/dist/components/EmptyState.svelte +6 -2
  5. package/dist/components/chat/Composer.svelte +380 -19
  6. package/dist/components/chat/Composer.svelte.d.ts +88 -1
  7. package/dist/components/chat/PlanCard.svelte +218 -88
  8. package/dist/components/chat/PlanCard.svelte.d.ts +11 -0
  9. package/dist/components/chat/QuestionCard.svelte +180 -70
  10. package/dist/components/chat/QuestionCard.svelte.d.ts +28 -2
  11. package/dist/components/chat/QuestionCardSummary.svelte +96 -0
  12. package/dist/components/chat/QuestionCardSummary.svelte.d.ts +20 -0
  13. package/dist/components/chat/QueueCard.svelte +185 -0
  14. package/dist/components/chat/QueueCard.svelte.d.ts +40 -0
  15. package/dist/components/chat/composerLogic.d.ts +31 -0
  16. package/dist/components/chat/composerLogic.js +37 -0
  17. package/dist/components/chat/composerReferences.d.ts +28 -0
  18. package/dist/components/chat/composerReferences.js +23 -0
  19. package/dist/components/chat/index.d.ts +8 -3
  20. package/dist/components/chat/index.js +10 -2
  21. package/dist/components/chat/questionCardLogic.d.ts +40 -3
  22. package/dist/components/chat/questionCardLogic.js +54 -3
  23. package/dist/components/chat/queueCardLogic.d.ts +7 -0
  24. package/dist/components/chat/queueCardLogic.js +5 -0
  25. package/dist/components/creator/CreatorBar.svelte +54 -1
  26. package/dist/components/creator/CreatorBar.svelte.d.ts +5 -0
  27. package/dist/components/creator/index.d.ts +1 -2
  28. package/dist/components/creator/index.js +2 -0
  29. package/dist/components/index.d.ts +8 -7
  30. package/dist/components/index.js +8 -6
  31. package/dist/components/modals/BaseModal.svelte +23 -4
  32. package/dist/components/modals/BaseModal.svelte.d.ts +5 -0
  33. package/dist/components/navigation/breadcrumbSlideIn.d.ts +6 -2
  34. package/dist/components/navigation/breadcrumbSlideIn.js +6 -6
  35. package/dist/components/search-input/SearchInput.svelte +317 -14
  36. package/dist/components/search-input/SearchInput.svelte.d.ts +3 -0
  37. package/dist/components/search-input/index.d.ts +1 -0
  38. package/dist/components/search-input/index.js +1 -0
  39. package/dist/components/search-input/index2.d.ts +4 -0
  40. package/dist/components/search-input/recentSearches.d.ts +16 -0
  41. package/dist/components/search-input/recentSearches.js +141 -0
  42. package/dist/components/shell/NavRow.svelte +53 -10
  43. package/dist/components/shell/NavRow.svelte.d.ts +23 -2
  44. package/dist/components/shell/NotificationRow.svelte +3 -2
  45. package/dist/components/shell/ProfileRow.svelte +6 -4
  46. package/dist/components/shell/ProfileRow.svelte.d.ts +2 -2
  47. package/dist/components/shell/SettingsDialog.svelte +23 -1
  48. package/dist/components/shell/WidgetRow.svelte +4 -3
  49. package/dist/components/shell/WidgetRow.svelte.d.ts +2 -2
  50. package/dist/components/toast/ToasterModal.svelte +8 -3
  51. package/dist/components/toast/ToasterModal.svelte.d.ts +2 -0
  52. package/dist/components/ui/accordion/accordion-trigger.svelte +19 -0
  53. package/dist/components/ui/button/button.svelte +4 -2
  54. package/dist/components/ui/chip/chip.svelte +39 -1
  55. package/dist/components/ui/chip/chip.svelte.d.ts +8 -0
  56. package/dist/components/ui/chip/chipProgress.d.ts +14 -0
  57. package/dist/components/ui/chip/chipProgress.js +21 -0
  58. package/dist/components/ui/dialog/dialog-content.svelte +24 -2
  59. package/dist/components/ui/dropdown-menu/dropdown-menu-content.svelte +2 -1
  60. package/dist/components/ui/dropdown-menu/dropdown-menu-sub-content.svelte +2 -1
  61. package/dist/components/ui/hover-card/hover-card-content.svelte +2 -1
  62. package/dist/components/ui/icon-button/icon-button.svelte +2 -0
  63. package/dist/components/ui/icon-button/icon-button.svelte.d.ts +5 -0
  64. package/dist/components/ui/number-controls/index.d.ts +2 -0
  65. package/dist/components/ui/number-controls/index.js +2 -0
  66. package/dist/components/ui/number-controls/number-controls.svelte +530 -0
  67. package/dist/components/ui/number-controls/number-controls.svelte.d.ts +46 -0
  68. package/dist/components/ui/number-controls/numberControlsSteps.d.ts +17 -0
  69. package/dist/components/ui/number-controls/numberControlsSteps.js +23 -0
  70. package/dist/components/ui/number-controls/numberControlsValueArea.d.ts +29 -0
  71. package/dist/components/ui/number-controls/numberControlsValueArea.js +51 -0
  72. package/dist/components/ui/popover/popover-content.svelte +2 -1
  73. package/dist/components/ui/select/select-content.svelte +2 -1
  74. package/dist/components/ui/text-flip/index.d.ts +1 -0
  75. package/dist/components/ui/text-flip/index.js +1 -0
  76. package/dist/components/ui/text-flip/text-flip.svelte +103 -0
  77. package/dist/components/ui/text-flip/text-flip.svelte.d.ts +15 -0
  78. package/dist/components/ui/text-flip/textFlipLogic.d.ts +49 -0
  79. package/dist/components/ui/text-flip/textFlipLogic.js +151 -0
  80. package/dist/components/ui/tooltip/tooltip-content.svelte +1 -1
  81. package/dist/inline-completion/completionAnswer.d.ts +11 -0
  82. package/dist/inline-completion/completionAnswer.js +49 -0
  83. package/dist/inline-completion/completionCache.d.ts +24 -0
  84. package/dist/inline-completion/completionCache.js +47 -0
  85. package/dist/inline-completion/completionController.d.ts +61 -0
  86. package/dist/inline-completion/completionController.js +280 -0
  87. package/dist/inline-completion/completionKeys.d.ts +21 -0
  88. package/dist/inline-completion/completionKeys.js +19 -0
  89. package/dist/inline-completion/completionText.d.ts +53 -0
  90. package/dist/inline-completion/completionText.js +79 -0
  91. package/dist/inline-completion/completionTypes.d.ts +71 -0
  92. package/dist/inline-completion/completionTypes.js +5 -0
  93. package/dist/inline-completion/gatewayCompletion.d.ts +37 -0
  94. package/dist/inline-completion/gatewayCompletion.js +77 -0
  95. package/dist/inline-completion/ghostTap.d.ts +19 -0
  96. package/dist/inline-completion/ghostTap.js +20 -0
  97. package/dist/inline-completion/hintMemory.d.ts +43 -0
  98. package/dist/inline-completion/hintMemory.js +90 -0
  99. package/dist/inline-completion/httpSource.d.ts +40 -0
  100. package/dist/inline-completion/httpSource.js +149 -0
  101. package/dist/inline-completion/index.d.ts +30 -0
  102. package/dist/inline-completion/index.js +20 -0
  103. package/dist/inline-completion/inlineCompletionAction.d.ts +54 -0
  104. package/dist/inline-completion/inlineCompletionAction.js +310 -0
  105. package/dist/inline-completion/outcomeReporter.d.ts +49 -0
  106. package/dist/inline-completion/outcomeReporter.js +139 -0
  107. package/dist/inline-completion/promiseLike.d.ts +9 -0
  108. package/dist/inline-completion/promiseLike.js +9 -0
  109. package/dist/prompt-references/ReferenceMenu.svelte +344 -0
  110. package/dist/prompt-references/ReferenceMenu.svelte.d.ts +26 -0
  111. package/dist/prompt-references/index.d.ts +27 -0
  112. package/dist/prompt-references/index.js +20 -0
  113. package/dist/prompt-references/neutralizingSource.d.ts +21 -0
  114. package/dist/prompt-references/neutralizingSource.js +29 -0
  115. package/dist/prompt-references/promptReferences.d.ts +28 -0
  116. package/dist/prompt-references/promptReferences.js +766 -0
  117. package/dist/prompt-references/referenceBadge.d.ts +61 -0
  118. package/dist/prompt-references/referenceBadge.js +82 -0
  119. package/dist/prompt-references/referenceBadgePainter.d.ts +31 -0
  120. package/dist/prompt-references/referenceBadgePainter.js +106 -0
  121. package/dist/prompt-references/referenceEditing.d.ts +60 -0
  122. package/dist/prompt-references/referenceEditing.js +135 -0
  123. package/dist/prompt-references/referenceLabels.d.ts +56 -0
  124. package/dist/prompt-references/referenceLabels.js +215 -0
  125. package/dist/prompt-references/referenceMeasure.d.ts +12 -0
  126. package/dist/prompt-references/referenceMeasure.js +63 -0
  127. package/dist/prompt-references/referenceMenuHost.svelte.d.ts +29 -0
  128. package/dist/prompt-references/referenceMenuHost.svelte.js +43 -0
  129. package/dist/prompt-references/referenceMenuKeys.d.ts +26 -0
  130. package/dist/prompt-references/referenceMenuKeys.js +49 -0
  131. package/dist/prompt-references/referenceMenuPlacement.d.ts +63 -0
  132. package/dist/prompt-references/referenceMenuPlacement.js +66 -0
  133. package/dist/prompt-references/referenceTokens.d.ts +175 -0
  134. package/dist/prompt-references/referenceTokens.js +332 -0
  135. package/dist/prompt-references/referenceTrigger.d.ts +52 -0
  136. package/dist/prompt-references/referenceTrigger.js +100 -0
  137. package/dist/prompt-references/referenceTypes.d.ts +38 -0
  138. package/dist/prompt-references/referenceTypes.js +4 -0
  139. package/dist/prompt-references/sentReferences.d.ts +50 -0
  140. package/dist/prompt-references/sentReferences.js +122 -0
  141. package/dist/styles/tokens.css +126 -1
  142. package/dist/textarea-overlay/index.d.ts +9 -0
  143. package/dist/textarea-overlay/index.js +7 -0
  144. package/dist/textarea-overlay/mirrorSegments.d.ts +31 -0
  145. package/dist/textarea-overlay/mirrorSegments.js +47 -0
  146. package/dist/textarea-overlay/mirrorSizing.d.ts +39 -0
  147. package/dist/textarea-overlay/mirrorSizing.js +40 -0
  148. package/dist/textarea-overlay/textareaOverlay.d.ts +25 -0
  149. package/dist/textarea-overlay/textareaOverlay.js +277 -0
  150. package/dist/utils/modalEnterKey.d.ts +139 -0
  151. package/dist/utils/modalEnterKey.js +276 -0
  152. package/dist/utils/modalFocusTrap.d.ts +7 -0
  153. package/dist/utils/modalFocusTrap.js +22 -0
  154. package/dist/utils/overlayMotion.d.ts +10 -0
  155. package/dist/utils/overlayMotion.js +12 -3
  156. package/dist/utils/reducedMotion.d.ts +9 -0
  157. package/dist/utils/reducedMotion.js +13 -0
  158. package/dist/utils/revealOpeningSection.d.ts +51 -0
  159. package/dist/utils/revealOpeningSection.js +109 -0
  160. package/package.json +16 -1
  161. package/dist/components/ui/number-stepper/index.d.ts +0 -3
  162. package/dist/components/ui/number-stepper/index.js +0 -3
  163. package/dist/components/ui/number-stepper/number-stepper.svelte +0 -212
  164. package/dist/components/ui/number-stepper/number-stepper.svelte.d.ts +0 -24
  165. package/dist/components/ui/number-stepper/numberStepperSteps.d.ts +0 -26
  166. package/dist/components/ui/number-stepper/numberStepperSteps.js +0 -45
@@ -1,212 +0,0 @@
1
- <!-- Figma: IC Number (Editor / Input controls): an accent surface, the value at the start (12 px in), the minus and the plus at the end.
2
- The editor's `NumberControls` is the same control with more behaviour (angle wrap, drag in the value area); this is the part
3
- that other packages need (the canvas panel), so that they stop drawing a plain number beside two loose stepper buttons.
4
-
5
- The value can be typed (it is kept inside min and max when the field loses the focus or Enter is pressed, Escape puts the
6
- old number back), stepped with the arrow keys, or stepped with the two buttons, which repeat while they are held.
7
- `value` is `null` when the things selected disagree: the field then shows `mixedText` and the first step starts from `fallbackValue`. -->
8
- <script lang="ts">
9
- import { onDestroy } from "svelte";
10
- import { cn } from "../../../utils.js";
11
- import { StepperButton } from "../stepper-button";
12
- import { clampToLimits, decimalsOf, parseTypedNumber, steppedNumber } from "./numberStepperSteps.js";
13
-
14
- /** The pause before a held button starts to repeat, and the time between two repeats (ms): the editor's numbers. */
15
- const HOLD_DELAY_MS = 400;
16
- const REPEAT_INTERVAL_MS = 80;
17
-
18
- interface Props {
19
- /** The current number, or `null` when the selection has no single value (the field shows `mixedText`). */
20
- value: number | null;
21
- /** Called with the new number each time it changes: a step, a typed number committed, an arrow key. */
22
- onChange: (nextValue: number) => void;
23
- min?: number;
24
- max?: number;
25
- /** How far one press moves the value. Its decimals are the decimals the value is kept to. */
26
- step?: number;
27
- /** The name of the field, for screen readers ("Stroke width"). */
28
- label: string;
29
- /** The names of the two buttons ("Decrease", "Increase"); they are announced with the name of the field. */
30
- decreaseLabel: string;
31
- increaseLabel: string;
32
- /** What the field shows when `value` is `null`. */
33
- mixedText?: string;
34
- /** Where the first step starts from when `value` is `null`. */
35
- fallbackValue?: number;
36
- disabled?: boolean;
37
- class?: string;
38
- }
39
-
40
- let {
41
- value,
42
- onChange,
43
- min,
44
- max,
45
- step = 1,
46
- label,
47
- decreaseLabel,
48
- increaseLabel,
49
- mixedText = "–",
50
- fallbackValue = 0,
51
- disabled = false,
52
- class: className
53
- }: Props = $props();
54
-
55
- /** The text in the field while the person types; `null` when the field shows the value itself. */
56
- let typedText = $state<string | null>(null);
57
- let pressedStepper = $state<"increment" | "decrement" | null>(null);
58
-
59
- let autoRepeatTimer: ReturnType<typeof setTimeout> | null = null;
60
- let autoRepeatInterval: ReturnType<typeof setInterval> | null = null;
61
-
62
- const decimals = $derived(decimalsOf(step));
63
- const shownText = $derived(typedText ?? (value === null ? mixedText : String(value)));
64
- const isAtMin = $derived(value !== null && min !== undefined && value <= min);
65
- const isAtMax = $derived(value !== null && max !== undefined && value >= max);
66
-
67
- /**
68
- * One step. Returns false when nothing was applied because the value is at a limit, so that a held button stops there.
69
- */
70
- function applyStep(direction: 1 | -1): boolean {
71
- try {
72
- const current = value ?? fallbackValue;
73
- const next = steppedNumber(current, direction * step, { min, max }, step);
74
- typedText = null;
75
- if (next === value) {
76
- stopAutoRepeat();
77
- return false;
78
- }
79
- onChange(next);
80
- return true;
81
- } catch (error) {
82
- console.error("[NumberStepper] a step could not be applied", error);
83
- return false;
84
- }
85
- }
86
-
87
- function startAutoRepeat(direction: 1 | -1, event: MouseEvent): void {
88
- // Only the main button steps: a right or middle click must not
89
- if (event.button !== 0 || disabled) return;
90
- stopAutoRepeat();
91
- pressedStepper = direction === 1 ? "increment" : "decrement";
92
-
93
- // A button that turns disabled at a limit gets no mouseup: the release is watched on the window too
94
- window.addEventListener("mouseup", stopAutoRepeat);
95
- window.addEventListener("blur", stopAutoRepeat);
96
-
97
- // At a limit there is nothing to hold
98
- if (!applyStep(direction)) return;
99
-
100
- autoRepeatTimer = setTimeout(() => {
101
- if (pressedStepper === null) return;
102
- autoRepeatInterval = setInterval(() => {
103
- if (pressedStepper !== null) applyStep(direction);
104
- }, REPEAT_INTERVAL_MS);
105
- }, HOLD_DELAY_MS);
106
- }
107
-
108
- /** A button pressed with the keyboard (Enter or Space) clicks without a mouse down: `detail` is 0 then. */
109
- function handleStepperClick(direction: 1 | -1, event: MouseEvent): void {
110
- if (event.detail !== 0) return;
111
- applyStep(direction);
112
- }
113
-
114
- function stopAutoRepeat(): void {
115
- if (typeof window !== "undefined") {
116
- window.removeEventListener("mouseup", stopAutoRepeat);
117
- window.removeEventListener("blur", stopAutoRepeat);
118
- }
119
- if (autoRepeatTimer !== null) clearTimeout(autoRepeatTimer);
120
- if (autoRepeatInterval !== null) clearInterval(autoRepeatInterval);
121
- autoRepeatTimer = null;
122
- autoRepeatInterval = null;
123
- pressedStepper = null;
124
- }
125
-
126
- /** Keeps what was typed to the characters of a number; the number itself is read when the field is committed. */
127
- function handleInput(event: Event): void {
128
- const input = event.currentTarget as HTMLInputElement;
129
- const allowed = min !== undefined && min >= 0 ? /[^\d.,]/g : /[^\d.,-]/g;
130
- const cleaned = input.value.replace(allowed, "");
131
- if (cleaned !== input.value) input.value = cleaned;
132
- typedText = cleaned;
133
- }
134
-
135
- /** Reads what was typed: a number inside the limits becomes the value, anything else puts the old value back. */
136
- function commitTypedText(): void {
137
- if (typedText === null) return;
138
- try {
139
- const parsed = parseTypedNumber(typedText, { min, max }, decimals);
140
- typedText = null;
141
- if (parsed !== null && parsed !== value) onChange(clampToLimits(parsed, { min, max }));
142
- } catch (error) {
143
- typedText = null;
144
- console.error("[NumberStepper] the typed number could not be read", error);
145
- }
146
- }
147
-
148
- function handleKeyDown(event: KeyboardEvent): void {
149
- if (event.metaKey || event.ctrlKey || event.altKey) return;
150
- // The page around the field has shortcuts of its own (delete, the arrow keys move the selection): the field keeps its keys
151
- event.stopPropagation();
152
- if (event.key === "ArrowUp") {
153
- event.preventDefault();
154
- applyStep(1);
155
- } else if (event.key === "ArrowDown") {
156
- event.preventDefault();
157
- applyStep(-1);
158
- } else if (event.key === "Enter") {
159
- event.preventDefault();
160
- commitTypedText();
161
- (event.currentTarget as HTMLInputElement).blur();
162
- } else if (event.key === "Escape") {
163
- typedText = null;
164
- (event.currentTarget as HTMLInputElement).blur();
165
- }
166
- }
167
-
168
- onDestroy(stopAutoRepeat);
169
- </script>
170
-
171
- <!-- The group gives the two buttons the name of their row ("Stroke width: Decrease"). -->
172
- <div
173
- role="group"
174
- aria-label={label}
175
- data-disabled={disabled ? "true" : undefined}
176
- class={cn(
177
- "flex h-9 min-w-0 items-center gap-1 rounded-lg bg-accent pe-1 transition-colors duration-fast hover:bg-accent-hover focus-within:ring-2 focus-within:ring-ring focus-within:ring-offset-2 focus-within:ring-offset-background data-[disabled=true]:pointer-events-none data-[disabled=true]:opacity-50",
178
- className
179
- )}
180
- >
181
- <input
182
- type="text"
183
- inputmode={decimals > 0 ? "decimal" : "numeric"}
184
- autocomplete="off"
185
- aria-label={label}
186
- {disabled}
187
- value={shownText}
188
- oninput={handleInput}
189
- onblur={commitTypedText}
190
- onfocus={(event) => event.currentTarget.select()}
191
- onkeydown={handleKeyDown}
192
- class="h-full min-w-0 flex-1 bg-transparent ps-3 text-body tabular-nums outline-hidden"
193
- />
194
- <StepperButton
195
- kind="minus"
196
- label={decreaseLabel}
197
- disabled={disabled || isAtMin}
198
- data-pressed={pressedStepper === "decrement" ? "true" : undefined}
199
- onmousedown={(event) => startAutoRepeat(-1, event)}
200
- onmouseup={stopAutoRepeat}
201
- onclick={(event) => handleStepperClick(-1, event)}
202
- />
203
- <StepperButton
204
- kind="plus"
205
- label={increaseLabel}
206
- disabled={disabled || isAtMax}
207
- data-pressed={pressedStepper === "increment" ? "true" : undefined}
208
- onmousedown={(event) => startAutoRepeat(1, event)}
209
- onmouseup={stopAutoRepeat}
210
- onclick={(event) => handleStepperClick(1, event)}
211
- />
212
- </div>
@@ -1,24 +0,0 @@
1
- interface Props {
2
- /** The current number, or `null` when the selection has no single value (the field shows `mixedText`). */
3
- value: number | null;
4
- /** Called with the new number each time it changes: a step, a typed number committed, an arrow key. */
5
- onChange: (nextValue: number) => void;
6
- min?: number;
7
- max?: number;
8
- /** How far one press moves the value. Its decimals are the decimals the value is kept to. */
9
- step?: number;
10
- /** The name of the field, for screen readers ("Stroke width"). */
11
- label: string;
12
- /** The names of the two buttons ("Decrease", "Increase"); they are announced with the name of the field. */
13
- decreaseLabel: string;
14
- increaseLabel: string;
15
- /** What the field shows when `value` is `null`. */
16
- mixedText?: string;
17
- /** Where the first step starts from when `value` is `null`. */
18
- fallbackValue?: number;
19
- disabled?: boolean;
20
- class?: string;
21
- }
22
- declare const NumberStepper: import("svelte").Component<Props, {}, "">;
23
- type NumberStepper = ReturnType<typeof NumberStepper>;
24
- export default NumberStepper;
@@ -1,26 +0,0 @@
1
- /**
2
- * The arithmetic of the number stepper (`number-stepper.svelte`), kept apart from the component so Node can test it.
3
- *
4
- * Same rules as the editor's `NumberControls`: one press moves the value by one step and keeps it inside the limits, and at a
5
- * limit the result is the value itself, which tells the caller that there is nothing to apply (and nothing to repeat).
6
- */
7
- export interface NumberStepLimits {
8
- /** The lowest value; no limit when left out. */
9
- min?: number;
10
- /** The highest value; no limit when left out. */
11
- max?: number;
12
- }
13
- /** How many decimals a step has (0.1 → 1, 5 → 0), so that 0.1 + 0.2 shows as 0.3 and not 0.30000000000000004. */
14
- export declare function decimalsOf(step: number): number;
15
- /** `value` kept between `min` and `max` (either may be left out). */
16
- export declare function clampToLimits(value: number, { min, max }?: NumberStepLimits): number;
17
- /**
18
- * The value after one press of a stepper button (or one arrow key): `currentValue` moved by `adjustment`, rounded to the
19
- * decimals of `precisionStep`, then kept inside the limits.
20
- */
21
- export declare function steppedNumber(currentValue: number, adjustment: number, limits?: NumberStepLimits, precisionStep?: number): number;
22
- /**
23
- * What a person typed in the field, as a number inside the limits, or `null` when it is not a number (an empty field, a lone
24
- * minus sign, letters): the field then goes back to the value it had.
25
- */
26
- export declare function parseTypedNumber(text: string, limits?: NumberStepLimits, decimals?: number): number | null;
@@ -1,45 +0,0 @@
1
- /**
2
- * The arithmetic of the number stepper (`number-stepper.svelte`), kept apart from the component so Node can test it.
3
- *
4
- * Same rules as the editor's `NumberControls`: one press moves the value by one step and keeps it inside the limits, and at a
5
- * limit the result is the value itself, which tells the caller that there is nothing to apply (and nothing to repeat).
6
- */
7
- /** How many decimals a step has (0.1 → 1, 5 → 0), so that 0.1 + 0.2 shows as 0.3 and not 0.30000000000000004. */
8
- export function decimalsOf(step) {
9
- if (!Number.isFinite(step))
10
- return 0;
11
- const text = String(step);
12
- const dot = text.indexOf('.');
13
- return dot === -1 ? 0 : text.length - dot - 1;
14
- }
15
- /** `value` kept between `min` and `max` (either may be left out). */
16
- export function clampToLimits(value, { min, max } = {}) {
17
- let clamped = value;
18
- if (min !== undefined && clamped < min)
19
- clamped = min;
20
- if (max !== undefined && clamped > max)
21
- clamped = max;
22
- return clamped;
23
- }
24
- /**
25
- * The value after one press of a stepper button (or one arrow key): `currentValue` moved by `adjustment`, rounded to the
26
- * decimals of `precisionStep`, then kept inside the limits.
27
- */
28
- export function steppedNumber(currentValue, adjustment, limits = {}, precisionStep = Math.abs(adjustment)) {
29
- const decimals = decimalsOf(precisionStep);
30
- const moved = Number((currentValue + adjustment).toFixed(decimals));
31
- return clampToLimits(moved, limits);
32
- }
33
- /**
34
- * What a person typed in the field, as a number inside the limits, or `null` when it is not a number (an empty field, a lone
35
- * minus sign, letters): the field then goes back to the value it had.
36
- */
37
- export function parseTypedNumber(text, limits = {}, decimals = 0) {
38
- const trimmed = text.trim().replace(',', '.');
39
- if (trimmed === '' || trimmed === '-' || trimmed === '.')
40
- return null;
41
- const parsed = Number(trimmed);
42
- if (!Number.isFinite(parsed))
43
- return null;
44
- return clampToLimits(Number(parsed.toFixed(decimals)), limits);
45
- }