velvetkit 0.4.0 → 0.4.1
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +31 -2
- data/lib/velvetkit/version.rb +1 -1
- data/registry/app/components/ui/money_input.html.erb +8 -0
- data/registry/app/components/ui/money_input.rb +153 -0
- data/registry/app/components/ui/scan_input.html.erb +6 -0
- data/registry/app/components/ui/scan_input.rb +94 -0
- data/registry/app/components/ui/slider.html.erb +23 -0
- data/registry/app/components/ui/slider.rb +254 -0
- data/registry/app/javascript/controllers/ui/money_input_controller.ts +228 -0
- data/registry/app/javascript/controllers/ui/scan_input_controller.ts +143 -0
- data/registry/app/javascript/controllers/ui/slider_controller.ts +216 -0
- data/registry/components.yml +6 -0
- data/registry/config/locales/velvetkit.en.yml +6 -0
- data/registry/icons/lucide/scan-barcode.svg +21 -0
- data/registry/icons/lucide/volume-2.svg +17 -0
- data/registry/icons/lucide/volume-x.svg +17 -0
- data/registry/icons/phosphor/barcode.svg +2 -0
- data/registry/icons/phosphor/speaker-high.svg +2 -0
- data/registry/icons/phosphor/speaker-x.svg +2 -0
- data/registry/icons/tabler/barcode.svg +23 -0
- data/registry/icons/tabler/volume-3.svg +17 -0
- data/registry/icons/tabler/volume.svg +18 -0
- data/registry/icons.yml +3 -0
- data/registry/test/components/previews/ui/money_input_preview/cash_tendered.html.erb +24 -0
- data/registry/test/components/previews/ui/money_input_preview/currencies.html.erb +7 -0
- data/registry/test/components/previews/ui/money_input_preview/invoice.html.erb +24 -0
- data/registry/test/components/previews/ui/money_input_preview/sizes_and_states.html.erb +9 -0
- data/registry/test/components/previews/ui/money_input_preview.rb +34 -0
- data/registry/test/components/previews/ui/scan_input_preview/_simulate.html.erb +20 -0
- data/registry/test/components/previews/ui/scan_input_preview/library_checkout.html.erb +30 -0
- data/registry/test/components/previews/ui/scan_input_preview/playground.html.erb +13 -0
- data/registry/test/components/previews/ui/scan_input_preview/stock_count.html.erb +24 -0
- data/registry/test/components/previews/ui/scan_input_preview/ticket_check_in.html.erb +34 -0
- data/registry/test/components/previews/ui/scan_input_preview/till.html.erb +9 -0
- data/registry/test/components/previews/ui/scan_input_preview.rb +33 -0
- data/registry/test/components/previews/ui/slider_preview/named_steps.html.erb +7 -0
- data/registry/test/components/previews/ui/slider_preview/opening_hours.html.erb +5 -0
- data/registry/test/components/previews/ui/slider_preview/price_range.html.erb +6 -0
- data/registry/test/components/previews/ui/slider_preview/sizes_and_states.html.erb +9 -0
- data/registry/test/components/previews/ui/slider_preview/text_size.html.erb +16 -0
- data/registry/test/components/previews/ui/slider_preview/tip.html.erb +22 -0
- data/registry/test/components/previews/ui/slider_preview/volume.html.erb +5 -0
- data/registry/test/components/previews/ui/slider_preview.rb +45 -0
- data/registry/test/components/ui/money_input_test.rb +149 -0
- data/registry/test/components/ui/scan_input_test.rb +110 -0
- data/registry/test/components/ui/slider_test.rb +190 -0
- metadata +42 -1
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import { Controller } from '@hotwired/stimulus';
|
|
2
|
+
|
|
3
|
+
// Connects to data-controller="ui--money-input"
|
|
4
|
+
//
|
|
5
|
+
// Regroups the amount as it's typed and keeps the caret where it was among
|
|
6
|
+
// the digits, so typing in the middle of "1,23,456" doesn't throw it to the
|
|
7
|
+
// end. The hidden field gets the plain number, built from the digits as a
|
|
8
|
+
// string: no floats, so nothing is rounded on the way to the server.
|
|
9
|
+
const MAX_DIGITS = 15;
|
|
10
|
+
|
|
11
|
+
type Amount = { negative: boolean; whole: string; fraction: string; point: boolean };
|
|
12
|
+
|
|
13
|
+
export default class extends Controller<HTMLElement> {
|
|
14
|
+
static targets = ['field', 'value'];
|
|
15
|
+
static values = {
|
|
16
|
+
separator: { type: String, default: '.' },
|
|
17
|
+
delimiter: { type: String, default: ',' },
|
|
18
|
+
precision: { type: Number, default: 2 },
|
|
19
|
+
grouping: { type: String, default: 'thousands' },
|
|
20
|
+
negative: Boolean,
|
|
21
|
+
step: { type: String, default: '1' },
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
declare readonly fieldTarget: HTMLInputElement;
|
|
25
|
+
declare readonly valueTarget: HTMLInputElement;
|
|
26
|
+
declare readonly separatorValue: string;
|
|
27
|
+
declare readonly delimiterValue: string;
|
|
28
|
+
declare readonly precisionValue: number;
|
|
29
|
+
declare readonly groupingValue: string;
|
|
30
|
+
declare readonly negativeValue: boolean;
|
|
31
|
+
declare readonly stepValue: string;
|
|
32
|
+
|
|
33
|
+
connect() {
|
|
34
|
+
this.element.closest('form')?.addEventListener('reset', this.afterReset);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
disconnect() {
|
|
38
|
+
this.element.closest('form')?.removeEventListener('reset', this.afterReset);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
type() {
|
|
42
|
+
const field = this.fieldTarget;
|
|
43
|
+
const caret = field.selectionStart ?? field.value.length;
|
|
44
|
+
const { amount, kept } = this.read(field.value, caret);
|
|
45
|
+
const text = this.write(amount);
|
|
46
|
+
|
|
47
|
+
field.value = text;
|
|
48
|
+
const at = this.caretAt(text, kept);
|
|
49
|
+
if (document.activeElement === field) field.setSelectionRange(at, at);
|
|
50
|
+
this.valueTarget.value = this.plain(amount);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Before a keystroke lands. Backspace just after a delimiter, or Delete
|
|
54
|
+
// just before one, would remove only the comma, which regrouping puts
|
|
55
|
+
// straight back: step over it so the digit beside it goes instead.
|
|
56
|
+
guard(event: InputEvent) {
|
|
57
|
+
const field = this.fieldTarget;
|
|
58
|
+
// No decimals: a typed point is refused, or the digits after it would
|
|
59
|
+
// join the whole amount once it's dropped.
|
|
60
|
+
if (event.inputType === 'insertText' && this.precisionValue === 0 && event.data && this.isPoint(event.data)) {
|
|
61
|
+
event.preventDefault();
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const { selectionStart: start, selectionEnd: end } = field;
|
|
66
|
+
if (start === null || start !== end) return;
|
|
67
|
+
|
|
68
|
+
if (event.inputType === 'deleteContentBackward' && field.value[start - 1] === this.delimiterValue) {
|
|
69
|
+
field.setSelectionRange(start - 1, start - 1);
|
|
70
|
+
} else if (event.inputType === 'deleteContentForward' && field.value[start] === this.delimiterValue) {
|
|
71
|
+
field.setSelectionRange(start + 1, start + 1);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// ↑ and ↓ add or take away a step (one whole unit unless `step:` says),
|
|
76
|
+
// Shift ten. Counted in paise or cents as whole numbers, so nothing drifts.
|
|
77
|
+
nudge(event: KeyboardEvent) {
|
|
78
|
+
if ((event.key !== 'ArrowUp' && event.key !== 'ArrowDown') || event.altKey || event.metaKey || event.ctrlKey) return;
|
|
79
|
+
if (this.fieldTarget.readOnly) return;
|
|
80
|
+
|
|
81
|
+
event.preventDefault();
|
|
82
|
+
const { amount } = this.read(this.fieldTarget.value, 0);
|
|
83
|
+
const scale = 10n ** BigInt(this.precisionValue);
|
|
84
|
+
let minor = this.minor(amount, scale);
|
|
85
|
+
const step = this.minor(this.read(this.stepValue.replace('.', this.separatorValue), 0).amount, scale) || scale;
|
|
86
|
+
minor += (event.key === 'ArrowUp' ? 1n : -1n) * step * (event.shiftKey ? 10n : 1n);
|
|
87
|
+
if (minor < 0n && !this.negativeValue) minor = 0n;
|
|
88
|
+
|
|
89
|
+
const digits = (minor < 0n ? -minor : minor).toString().padStart(this.precisionValue + 1, '0');
|
|
90
|
+
const cut = digits.length - this.precisionValue;
|
|
91
|
+
const next: Amount = {
|
|
92
|
+
negative: minor < 0n,
|
|
93
|
+
whole: digits.slice(0, cut),
|
|
94
|
+
fraction: digits.slice(cut),
|
|
95
|
+
point: this.precisionValue > 0,
|
|
96
|
+
};
|
|
97
|
+
const text = this.write(next);
|
|
98
|
+
if (text === this.fieldTarget.value) return;
|
|
99
|
+
|
|
100
|
+
this.fieldTarget.value = text;
|
|
101
|
+
this.fieldTarget.setSelectionRange(text.length, text.length);
|
|
102
|
+
this.valueTarget.value = this.plain(next);
|
|
103
|
+
this.fieldTarget.dispatchEvent(new Event('input', { bubbles: true }));
|
|
104
|
+
this.fieldTarget.dispatchEvent(new Event('change', { bubbles: true }));
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// Leaving the field fills in the decimals: "1,200" becomes "1,200.00".
|
|
108
|
+
finish() {
|
|
109
|
+
const { amount } = this.read(this.fieldTarget.value, 0);
|
|
110
|
+
if (!amount.whole && !amount.fraction) {
|
|
111
|
+
this.fieldTarget.value = '';
|
|
112
|
+
this.valueTarget.value = '';
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const full = { ...amount, whole: amount.whole || '0', point: this.precisionValue > 0 };
|
|
117
|
+
full.fraction = full.fraction.padEnd(this.precisionValue, '0');
|
|
118
|
+
this.fieldTarget.value = this.write(full);
|
|
119
|
+
this.valueTarget.value = this.plain(full);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// What was typed or pasted, as an amount, and how many of its characters
|
|
123
|
+
// (digits, the point, a minus) came before the caret.
|
|
124
|
+
private read(text: string, caret: number): { amount: Amount; kept: number } {
|
|
125
|
+
const amount: Amount = { negative: false, whole: '', fraction: '', point: false };
|
|
126
|
+
let kept = 0;
|
|
127
|
+
let zeros = 0;
|
|
128
|
+
let zeroBefore = false;
|
|
129
|
+
// A point typed where there are no decimals ends the amount: "15800.5" is 15800.
|
|
130
|
+
let ended = false;
|
|
131
|
+
|
|
132
|
+
[...text].forEach((char, index) => {
|
|
133
|
+
const before = index < caret;
|
|
134
|
+
if (ended) return;
|
|
135
|
+
if (char === '-' && this.negativeValue && !amount.negative && !amount.whole && !amount.point) {
|
|
136
|
+
amount.negative = true;
|
|
137
|
+
} else if (this.isPoint(char) && !amount.point && this.precisionValue === 0) {
|
|
138
|
+
ended = true;
|
|
139
|
+
return;
|
|
140
|
+
} else if (this.isPoint(char) && !amount.point) {
|
|
141
|
+
amount.point = true;
|
|
142
|
+
// ".5" reads as "0.5"; the 0 is one more character before the caret.
|
|
143
|
+
if (!amount.whole) {
|
|
144
|
+
amount.whole = '0';
|
|
145
|
+
if (before) kept += 1;
|
|
146
|
+
}
|
|
147
|
+
} else if (/\d/.test(char)) {
|
|
148
|
+
if (amount.point) {
|
|
149
|
+
if (amount.fraction.length >= this.precisionValue) return;
|
|
150
|
+
amount.fraction += char;
|
|
151
|
+
} else {
|
|
152
|
+
if (amount.whole.length >= MAX_DIGITS) return;
|
|
153
|
+
// Leading zeros go, unless the 0 is all there is.
|
|
154
|
+
if (amount.whole === '0') {
|
|
155
|
+
amount.whole = char;
|
|
156
|
+
if (zeroBefore) zeros += 1;
|
|
157
|
+
} else {
|
|
158
|
+
amount.whole += char;
|
|
159
|
+
}
|
|
160
|
+
if (amount.whole === '0') zeroBefore = before;
|
|
161
|
+
}
|
|
162
|
+
} else {
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
if (before) kept += 1;
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
return { amount, kept: kept - zeros };
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// An amount in paise or cents, as a whole number.
|
|
172
|
+
private minor(amount: Amount, scale: bigint): bigint {
|
|
173
|
+
const whole = BigInt(amount.whole || '0') * scale;
|
|
174
|
+
const fraction = BigInt(amount.fraction.padEnd(this.precisionValue, '0') || '0');
|
|
175
|
+
const total = whole + fraction;
|
|
176
|
+
return amount.negative ? -total : total;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
private write(amount: Amount): string {
|
|
180
|
+
const whole = this.group(amount.whole);
|
|
181
|
+
const point = amount.point ? this.separatorValue + amount.fraction : '';
|
|
182
|
+
return `${amount.negative ? '-' : ''}${whole}${point}`;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
private plain(amount: Amount): string {
|
|
186
|
+
if (!amount.whole && !amount.fraction) return '';
|
|
187
|
+
|
|
188
|
+
const fraction = amount.fraction ? `.${amount.fraction}` : '';
|
|
189
|
+
return `${amount.negative ? '-' : ''}${amount.whole || '0'}${fraction}`;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// 1234567 as 1,234,567, or the Indian way, 12,34,567.
|
|
193
|
+
private group(digits: string): string {
|
|
194
|
+
const d = this.delimiterValue;
|
|
195
|
+
if (this.groupingValue !== 'indian') return digits.replace(/\B(?=(\d{3})+(?!\d))/g, d);
|
|
196
|
+
if (digits.length <= 3) return digits;
|
|
197
|
+
|
|
198
|
+
return `${digits.slice(0, -3).replace(/\B(?=(\d{2})+(?!\d))/g, d)}${d}${digits.slice(-3)}`;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// The place in the new text after `kept` characters that aren't delimiters.
|
|
202
|
+
private caretAt(text: string, kept: number): number {
|
|
203
|
+
if (kept <= 0) return 0;
|
|
204
|
+
|
|
205
|
+
let seen = 0;
|
|
206
|
+
for (let index = 0; index < text.length; index += 1) {
|
|
207
|
+
if (text[index] !== this.delimiterValue || index === this.pointIndex(text)) seen += 1;
|
|
208
|
+
if (seen === kept) return index + 1;
|
|
209
|
+
}
|
|
210
|
+
return text.length;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
private pointIndex(text: string): number {
|
|
214
|
+
return this.separatorValue === this.delimiterValue ? -1 : text.indexOf(this.separatorValue);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// The locale's decimal mark, or a full stop where that isn't the delimiter:
|
|
218
|
+
// the key on a phone's decimal pad.
|
|
219
|
+
private isPoint(char: string): boolean {
|
|
220
|
+
return char === this.separatorValue || (char === '.' && this.delimiterValue !== '.');
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
private afterReset = () =>
|
|
224
|
+
requestAnimationFrame(() => {
|
|
225
|
+
this.type();
|
|
226
|
+
this.finish();
|
|
227
|
+
});
|
|
228
|
+
}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { Controller } from '@hotwired/stimulus';
|
|
2
|
+
|
|
3
|
+
// Connects to data-controller="ui--scan-input"
|
|
4
|
+
//
|
|
5
|
+
// A scanner types a code and presses Enter (or Tab). On that key this fires
|
|
6
|
+
// `ui--scan-input:scan`, then submits the form with the code and empties the
|
|
7
|
+
// field. While a Turbo submission is on its way, later codes queue and go
|
|
8
|
+
// one after another, so none is lost to a busy form. Focus is kept on the
|
|
9
|
+
// field when it would otherwise fall to the page.
|
|
10
|
+
const SCANNED_FOR = 700;
|
|
11
|
+
|
|
12
|
+
export default class extends Controller<HTMLElement> {
|
|
13
|
+
static targets = ['field'];
|
|
14
|
+
static values = {
|
|
15
|
+
submit: { type: Boolean, default: true },
|
|
16
|
+
clear: { type: Boolean, default: true },
|
|
17
|
+
keepFocus: { type: Boolean, default: true },
|
|
18
|
+
capture: Boolean,
|
|
19
|
+
suffix: { type: String, default: 'enter' },
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
declare readonly fieldTarget: HTMLInputElement;
|
|
23
|
+
declare readonly submitValue: boolean;
|
|
24
|
+
declare readonly clearValue: boolean;
|
|
25
|
+
declare readonly keepFocusValue: boolean;
|
|
26
|
+
declare readonly captureValue: boolean;
|
|
27
|
+
declare readonly suffixValue: string;
|
|
28
|
+
|
|
29
|
+
private queue: string[] = [];
|
|
30
|
+
private sending = false;
|
|
31
|
+
private form: HTMLFormElement | null = null;
|
|
32
|
+
private scannedTimer?: number;
|
|
33
|
+
|
|
34
|
+
connect() {
|
|
35
|
+
this.form = this.fieldTarget.form;
|
|
36
|
+
this.form?.addEventListener('turbo:submit-start', this.started);
|
|
37
|
+
this.form?.addEventListener('turbo:submit-end', this.ended);
|
|
38
|
+
this.fieldTarget.addEventListener('blur', this.blurred);
|
|
39
|
+
if (this.keepFocusValue) document.addEventListener('visibilitychange', this.refocus);
|
|
40
|
+
if (this.captureValue) document.addEventListener('keydown', this.captured);
|
|
41
|
+
// autofocus only works on page load; back in a Turbo update, focus here.
|
|
42
|
+
if (this.fieldTarget.autofocus) this.refocus();
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
disconnect() {
|
|
46
|
+
this.form?.removeEventListener('turbo:submit-start', this.started);
|
|
47
|
+
this.form?.removeEventListener('turbo:submit-end', this.ended);
|
|
48
|
+
this.fieldTarget.removeEventListener('blur', this.blurred);
|
|
49
|
+
document.removeEventListener('visibilitychange', this.refocus);
|
|
50
|
+
document.removeEventListener('keydown', this.captured);
|
|
51
|
+
window.clearTimeout(this.scannedTimer);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
key(event: KeyboardEvent) {
|
|
55
|
+
if (event.isComposing || !this.isSuffix(event)) return;
|
|
56
|
+
|
|
57
|
+
event.preventDefault();
|
|
58
|
+
const code = this.fieldTarget.value.trim();
|
|
59
|
+
if (!code) return;
|
|
60
|
+
|
|
61
|
+
this.flash();
|
|
62
|
+
const scan = new CustomEvent('ui--scan-input:scan', { detail: { code }, bubbles: true, cancelable: true });
|
|
63
|
+
this.element.dispatchEvent(scan);
|
|
64
|
+
if (scan.defaultPrevented || !this.submitValue || !this.form) {
|
|
65
|
+
if (this.clearValue) this.fieldTarget.value = '';
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (this.sending) {
|
|
70
|
+
this.queue.push(code);
|
|
71
|
+
if (this.clearValue) this.fieldTarget.value = '';
|
|
72
|
+
} else {
|
|
73
|
+
this.send(code);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// The form data is read as the submit event runs, so the field can be
|
|
78
|
+
// emptied straight after.
|
|
79
|
+
private send(code: string) {
|
|
80
|
+
this.fieldTarget.value = code;
|
|
81
|
+
this.form?.requestSubmit();
|
|
82
|
+
if (this.clearValue) this.fieldTarget.value = '';
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
private started = () => {
|
|
86
|
+
this.sending = true;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
private ended = () => {
|
|
90
|
+
this.sending = false;
|
|
91
|
+
const next = this.queue.shift();
|
|
92
|
+
if (next !== undefined) this.send(next);
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
private isSuffix(event: KeyboardEvent): boolean {
|
|
96
|
+
if (event.altKey || event.ctrlKey || event.metaKey) return false;
|
|
97
|
+
if (this.suffixValue === 'tab') return event.key === 'Tab' && !event.shiftKey;
|
|
98
|
+
|
|
99
|
+
return event.key === 'Enter';
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
private flash() {
|
|
103
|
+
this.element.dataset.scanned = '';
|
|
104
|
+
window.clearTimeout(this.scannedTimer);
|
|
105
|
+
this.scannedTimer = window.setTimeout(() => delete this.element.dataset.scanned, SCANNED_FOR);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Only when focus has fallen to the page: a person moving to another field,
|
|
109
|
+
// a button or a dialog keeps it.
|
|
110
|
+
private blurred = () => {
|
|
111
|
+
if (!this.keepFocusValue) return;
|
|
112
|
+
|
|
113
|
+
requestAnimationFrame(() => this.refocus());
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
private refocus = () => {
|
|
117
|
+
const field = this.fieldTarget;
|
|
118
|
+
if (document.visibilityState !== 'visible' || field.disabled || field.readOnly) return;
|
|
119
|
+
if (!this.keepFocusValue && !field.autofocus) return;
|
|
120
|
+
|
|
121
|
+
const active = document.activeElement;
|
|
122
|
+
if (active && active !== document.body && active !== document.documentElement) return;
|
|
123
|
+
|
|
124
|
+
field.focus({ preventScroll: true });
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
// Typed anywhere but another field, a character goes to this one: focusing it
|
|
128
|
+
// during keydown lets the browser put the key there.
|
|
129
|
+
private captured = (event: KeyboardEvent) => {
|
|
130
|
+
if (event.defaultPrevented || event.altKey || event.ctrlKey || event.metaKey || event.key.length !== 1) return;
|
|
131
|
+
if (this.fieldTarget.disabled || this.isEditable(event.target)) return;
|
|
132
|
+
// Space presses a focused button; leave that alone.
|
|
133
|
+
if (event.key === ' ' && event.target instanceof HTMLElement && event.target.closest('button, a, [role=button]')) return;
|
|
134
|
+
|
|
135
|
+
this.fieldTarget.focus({ preventScroll: true });
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
private isEditable(target: EventTarget | null): boolean {
|
|
139
|
+
if (!(target instanceof HTMLElement)) return false;
|
|
140
|
+
|
|
141
|
+
return target.isContentEditable || target.closest('input, textarea, select, [role=textbox], [role=combobox]') !== null;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
import { Controller } from '@hotwired/stimulus';
|
|
2
|
+
|
|
3
|
+
// Connects to data-controller="ui--slider"
|
|
4
|
+
//
|
|
5
|
+
// The handles are native range inputs, which do the keyboard, the form and
|
|
6
|
+
// the screen reader, and can't be seen. This places the knobs and the filled
|
|
7
|
+
// part of the track, keeps the shown value and aria-valuetext current, and
|
|
8
|
+
// takes the pointer: a press anywhere on the track grabs the nearest handle,
|
|
9
|
+
// so a range's two handles can be dragged even when they sit on top of each
|
|
10
|
+
// other, and they never pass. While dragged, a knob follows the pointer
|
|
11
|
+
// freely and the value keeps to the step; let go, it glides onto the value.
|
|
12
|
+
type PickEvent = MouseEvent & { params: { value: number } };
|
|
13
|
+
|
|
14
|
+
export default class extends Controller<HTMLElement> {
|
|
15
|
+
static targets = ['input', 'track', 'output'];
|
|
16
|
+
static values = { format: String, rangeFormat: String, labels: Object };
|
|
17
|
+
|
|
18
|
+
declare readonly inputTargets: HTMLInputElement[];
|
|
19
|
+
declare readonly trackTarget: HTMLElement;
|
|
20
|
+
declare readonly outputTarget: HTMLOutputElement;
|
|
21
|
+
declare readonly hasOutputTarget: boolean;
|
|
22
|
+
declare readonly formatValue: string;
|
|
23
|
+
declare readonly rangeFormatValue: string;
|
|
24
|
+
declare readonly labelsValue: Record<string, string>;
|
|
25
|
+
|
|
26
|
+
private dragging?: HTMLInputElement;
|
|
27
|
+
private pointerId?: number;
|
|
28
|
+
private startX = 0;
|
|
29
|
+
private before = '';
|
|
30
|
+
// Where the pointer is along the track, 0 to 1, while it holds a handle:
|
|
31
|
+
// the knob follows this rather than the value, which keeps to the step.
|
|
32
|
+
private held?: number;
|
|
33
|
+
|
|
34
|
+
connect() {
|
|
35
|
+
this.element.closest('form')?.addEventListener('reset', this.afterReset);
|
|
36
|
+
this.sync();
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
disconnect() {
|
|
40
|
+
this.element.closest('form')?.removeEventListener('reset', this.afterReset);
|
|
41
|
+
this.release();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// On every input: a range's handles can meet but not cross.
|
|
45
|
+
sync(event?: Event) {
|
|
46
|
+
const [low, high] = this.inputTargets;
|
|
47
|
+
if (high) {
|
|
48
|
+
if (event?.target === low && low.valueAsNumber > high.valueAsNumber) low.value = high.value;
|
|
49
|
+
if (event?.target === high && high.valueAsNumber < low.valueAsNumber) high.value = low.value;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
let from = high ? this.fraction(low) : 0;
|
|
53
|
+
let to = this.fraction(high ?? low);
|
|
54
|
+
if (this.dragging && this.held !== undefined) {
|
|
55
|
+
if (high && this.dragging === low) from = Math.min(this.held, to);
|
|
56
|
+
else to = Math.max(this.held, from);
|
|
57
|
+
}
|
|
58
|
+
this.element.style.setProperty('--from', String(from));
|
|
59
|
+
this.element.style.setProperty('--to', String(to));
|
|
60
|
+
for (const input of this.inputTargets) input.setAttribute('aria-valuetext', this.text(input.value));
|
|
61
|
+
if (this.hasOutputTarget) {
|
|
62
|
+
this.outputTarget.textContent = high
|
|
63
|
+
? this.rangeFormatValue.replace('%{from}', this.text(low.value)).replace('%{to}', this.text(high.value))
|
|
64
|
+
: this.text(low.value);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
press(event: PointerEvent) {
|
|
69
|
+
if (event.button !== 0 || this.inputTargets[0].disabled) return;
|
|
70
|
+
|
|
71
|
+
event.preventDefault();
|
|
72
|
+
const at = this.fractionAt(event.clientX);
|
|
73
|
+
const value = this.valueOf(at);
|
|
74
|
+
this.dragging = this.nearest(value);
|
|
75
|
+
this.startX = event.clientX;
|
|
76
|
+
this.pointerId = event.pointerId;
|
|
77
|
+
this.before = this.inputTargets.map((input) => input.value).join();
|
|
78
|
+
this.trackTarget.setPointerCapture(event.pointerId);
|
|
79
|
+
this.trackTarget.addEventListener('pointermove', this.move);
|
|
80
|
+
this.trackTarget.addEventListener('pointerup', this.release);
|
|
81
|
+
this.trackTarget.addEventListener('pointercancel', this.release);
|
|
82
|
+
// Two handles on one spot: which one is decided by the way the pointer moves.
|
|
83
|
+
if (this.dragging) this.moveTo(this.dragging, value, at);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// A mark: the nearest handle goes there, as if the track were pressed.
|
|
87
|
+
pick(event: PickEvent) {
|
|
88
|
+
const value = event.params.value;
|
|
89
|
+
const input = this.nearest(value) ?? this.inputTargets[0];
|
|
90
|
+
const before = input.value;
|
|
91
|
+
this.moveTo(input, value);
|
|
92
|
+
input.focus({ focusVisible: false } as FocusOptions);
|
|
93
|
+
if (input.value !== before) input.dispatchEvent(new Event('change', { bubbles: true }));
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// Once it moves, the knob keeps up with the pointer: no gliding.
|
|
97
|
+
private move = (event: PointerEvent) => {
|
|
98
|
+
const at = this.fractionAt(event.clientX);
|
|
99
|
+
if (!this.dragging) {
|
|
100
|
+
if (Math.abs(event.clientX - this.startX) < 2) return;
|
|
101
|
+
|
|
102
|
+
const [low, high] = this.inputTargets;
|
|
103
|
+
this.dragging = (event.clientX < this.startX) !== this.rtl ? low : high;
|
|
104
|
+
}
|
|
105
|
+
this.element.dataset.dragging = '';
|
|
106
|
+
this.moveTo(this.dragging, this.valueOf(at), at);
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
private release = () => {
|
|
110
|
+
const input = this.dragging;
|
|
111
|
+
if (this.pointerId !== undefined && this.trackTarget.hasPointerCapture(this.pointerId)) {
|
|
112
|
+
this.trackTarget.releasePointerCapture(this.pointerId);
|
|
113
|
+
}
|
|
114
|
+
this.trackTarget.removeEventListener('pointermove', this.move);
|
|
115
|
+
this.trackTarget.removeEventListener('pointerup', this.release);
|
|
116
|
+
this.trackTarget.removeEventListener('pointercancel', this.release);
|
|
117
|
+
for (const each of this.inputTargets) delete each.dataset.active;
|
|
118
|
+
delete this.element.dataset.dragging;
|
|
119
|
+
this.dragging = undefined;
|
|
120
|
+
this.held = undefined;
|
|
121
|
+
this.pointerId = undefined;
|
|
122
|
+
// The knob glides from the pointer onto the value.
|
|
123
|
+
this.sync();
|
|
124
|
+
if (input && this.inputTargets.map((each) => each.value).join() !== this.before) {
|
|
125
|
+
input.dispatchEvent(new Event('change', { bubbles: true }));
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
private afterReset = () => requestAnimationFrame(() => this.sync());
|
|
130
|
+
|
|
131
|
+
// Sets the value the way the keyboard would, so step and the other handle
|
|
132
|
+
// are respected, and fires input as the browser does. `at` is the pointer's
|
|
133
|
+
// place, for the knob to follow.
|
|
134
|
+
private moveTo(input: HTMLInputElement, value: number, at?: number) {
|
|
135
|
+
const [low, high] = this.inputTargets;
|
|
136
|
+
let next = value;
|
|
137
|
+
if (high && input === low) next = Math.min(next, high.valueAsNumber);
|
|
138
|
+
if (high && input === high) next = Math.max(next, low.valueAsNumber);
|
|
139
|
+
|
|
140
|
+
input.dataset.active = '';
|
|
141
|
+
// Focused for the keyboard to carry on from, without the keyboard's ring.
|
|
142
|
+
input.focus({ preventScroll: true, focusVisible: false } as FocusOptions);
|
|
143
|
+
this.held = at;
|
|
144
|
+
if (String(next) === input.value) {
|
|
145
|
+
this.sync();
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
input.value = String(next);
|
|
150
|
+
input.dispatchEvent(new Event('input', { bubbles: true }));
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// The handle closest to a value; none when a range's two are equally close
|
|
154
|
+
// and on one spot, until the pointer moves.
|
|
155
|
+
private nearest(value: number): HTMLInputElement | undefined {
|
|
156
|
+
const [low, high] = this.inputTargets;
|
|
157
|
+
if (!high) return low;
|
|
158
|
+
if (low.value === high.value) {
|
|
159
|
+
if (value === low.valueAsNumber) return undefined;
|
|
160
|
+
return value < low.valueAsNumber ? low : high;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
return Math.abs(value - low.valueAsNumber) <= Math.abs(value - high.valueAsNumber) ? low : high;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// How far along the track a point is, 0 to 1. The knob's centre can't
|
|
167
|
+
// reach the very ends, so neither does the scale.
|
|
168
|
+
private fractionAt(clientX: number): number {
|
|
169
|
+
const rect = this.trackTarget.getBoundingClientRect();
|
|
170
|
+
const thumb = this.thumbWidth;
|
|
171
|
+
const fraction = Math.min(1, Math.max(0, (clientX - rect.left - thumb / 2) / Math.max(1, rect.width - thumb)));
|
|
172
|
+
return this.rtl ? 1 - fraction : fraction;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// The value at a fraction of the track, snapped to step.
|
|
176
|
+
private valueOf(fraction: number): number {
|
|
177
|
+
const { min, max, step } = this.bounds(this.inputTargets[0]);
|
|
178
|
+
const steps = Math.round((fraction * (max - min)) / step);
|
|
179
|
+
return Number(Math.min(max, min + steps * step).toFixed(this.decimals(step)));
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
private fraction(input: HTMLInputElement): number {
|
|
183
|
+
const { min, max } = this.bounds(input);
|
|
184
|
+
return max === min ? 0 : (input.valueAsNumber - min) / (max - min);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
private bounds(input: HTMLInputElement) {
|
|
188
|
+
const min = parseFloat(input.min) || 0;
|
|
189
|
+
const max = Number.isNaN(parseFloat(input.max)) ? 100 : parseFloat(input.max);
|
|
190
|
+
const step = parseFloat(input.step) || 1;
|
|
191
|
+
return { min, max, step };
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// Matches the server's: the value as the step writes it, "3" not "3.0".
|
|
195
|
+
private text(value: string): string {
|
|
196
|
+
const number = String(Number(Number(value).toFixed(this.decimals(this.bounds(this.inputTargets[0]).step))));
|
|
197
|
+
return this.labelsValue[number] ?? this.formatValue.replace('%{value}', number);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
private decimals(step: number): number {
|
|
201
|
+
return (String(step).split('.')[1] ?? '').length;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// --thumb, in pixels: a custom property comes back as written, "0.5rem".
|
|
205
|
+
private get thumbWidth(): number {
|
|
206
|
+
const written = getComputedStyle(this.element).getPropertyValue('--thumb').trim();
|
|
207
|
+
const size = parseFloat(written) || 0;
|
|
208
|
+
if (!written.endsWith('rem')) return size;
|
|
209
|
+
|
|
210
|
+
return size * parseFloat(getComputedStyle(document.documentElement).fontSize);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
private get rtl(): boolean {
|
|
214
|
+
return getComputedStyle(this.element).direction === 'rtl';
|
|
215
|
+
}
|
|
216
|
+
}
|
data/registry/components.yml
CHANGED
|
@@ -84,6 +84,8 @@ components:
|
|
|
84
84
|
description: A link styled as text or as a button.
|
|
85
85
|
menu:
|
|
86
86
|
description: A dropdown menu with keyboard navigation.
|
|
87
|
+
money_input:
|
|
88
|
+
description: An amount of money, grouped as it's typed with its currency sign, posting the plain number.
|
|
87
89
|
navigation_menu:
|
|
88
90
|
description: Site navigation with links and hover or click panels of rich links.
|
|
89
91
|
number_input:
|
|
@@ -101,6 +103,8 @@ components:
|
|
|
101
103
|
description: A set of radio buttons.
|
|
102
104
|
native_select:
|
|
103
105
|
description: The browser's own select, styled closed; best for phones' system pickers.
|
|
106
|
+
scan_input:
|
|
107
|
+
description: A field for a barcode or QR scanner, submitting each code as it's scanned and keeping focus.
|
|
104
108
|
select:
|
|
105
109
|
description: A styled dropdown with keyboard navigation and type-ahead, posting like a native select.
|
|
106
110
|
session_tabs:
|
|
@@ -111,6 +115,8 @@ components:
|
|
|
111
115
|
description: A collapsible application sidebar with groups and menus.
|
|
112
116
|
skeleton:
|
|
113
117
|
description: A pulsing placeholder shown in a piece of content's shape while it loads.
|
|
118
|
+
slider:
|
|
119
|
+
description: A handle to drag along a track for a value, or two for a range, on the browser's own range input.
|
|
114
120
|
spinner:
|
|
115
121
|
description: A loading indicator.
|
|
116
122
|
stepper:
|
|
@@ -76,6 +76,10 @@ en:
|
|
|
76
76
|
progress:
|
|
77
77
|
label: 'Progress'
|
|
78
78
|
value_text: '%{percent}%'
|
|
79
|
+
slider:
|
|
80
|
+
from: 'Minimum'
|
|
81
|
+
to: 'Maximum'
|
|
82
|
+
range_text: '%{from} – %{to}'
|
|
79
83
|
sidebar:
|
|
80
84
|
toggle: 'Toggle sidebar'
|
|
81
85
|
table_of_contents:
|
|
@@ -98,6 +102,8 @@ en:
|
|
|
98
102
|
error: 'Needs attention'
|
|
99
103
|
loading: 'Checking'
|
|
100
104
|
summary: 'Step %{current} of %{total}'
|
|
105
|
+
scan_input:
|
|
106
|
+
placeholder: 'Scan or type a code'
|
|
101
107
|
select:
|
|
102
108
|
placeholder: 'Choose…'
|
|
103
109
|
search: 'Search'
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
<!-- @license lucide-static v0.545.0 - ISC -->
|
|
2
|
+
<svg
|
|
3
|
+
class="lucide lucide-scan-barcode"
|
|
4
|
+
xmlns="http://www.w3.org/2000/svg"
|
|
5
|
+
width="24"
|
|
6
|
+
height="24"
|
|
7
|
+
viewBox="0 0 24 24"
|
|
8
|
+
fill="none"
|
|
9
|
+
stroke="currentColor"
|
|
10
|
+
stroke-width="2"
|
|
11
|
+
stroke-linecap="round"
|
|
12
|
+
stroke-linejoin="round"
|
|
13
|
+
>
|
|
14
|
+
<path d="M3 7V5a2 2 0 0 1 2-2h2" />
|
|
15
|
+
<path d="M17 3h2a2 2 0 0 1 2 2v2" />
|
|
16
|
+
<path d="M21 17v2a2 2 0 0 1-2 2h-2" />
|
|
17
|
+
<path d="M7 21H5a2 2 0 0 1-2-2v-2" />
|
|
18
|
+
<path d="M8 7v10" />
|
|
19
|
+
<path d="M12 7v10" />
|
|
20
|
+
<path d="M17 7v10" />
|
|
21
|
+
</svg>
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
<!-- @license lucide-static v0.545.0 - ISC -->
|
|
2
|
+
<svg
|
|
3
|
+
class="lucide lucide-volume-2"
|
|
4
|
+
xmlns="http://www.w3.org/2000/svg"
|
|
5
|
+
width="24"
|
|
6
|
+
height="24"
|
|
7
|
+
viewBox="0 0 24 24"
|
|
8
|
+
fill="none"
|
|
9
|
+
stroke="currentColor"
|
|
10
|
+
stroke-width="2"
|
|
11
|
+
stroke-linecap="round"
|
|
12
|
+
stroke-linejoin="round"
|
|
13
|
+
>
|
|
14
|
+
<path d="M11 4.702a.705.705 0 0 0-1.203-.498L6.413 7.587A1.4 1.4 0 0 1 5.416 8H3a1 1 0 0 0-1 1v6a1 1 0 0 0 1 1h2.416a1.4 1.4 0 0 1 .997.413l3.383 3.384A.705.705 0 0 0 11 19.298z" />
|
|
15
|
+
<path d="M16 9a5 5 0 0 1 0 6" />
|
|
16
|
+
<path d="M19.364 18.364a9 9 0 0 0 0-12.728" />
|
|
17
|
+
</svg>
|