Skip to content

Commit a7876a6

Browse files
Avoid normalizing QR hex input
As the user was typing a hex code, e.g., `abc`, the normalisation would automatically update the input field to `#aabbcc`, preventing you from writing a hex value from start to finish.
1 parent 762318b commit a7876a6

1 file changed

Lines changed: 22 additions & 12 deletions

File tree

qr.html

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -419,19 +419,29 @@ <h2>Preview</h2>
419419
});
420420
}
421421

422-
function syncForeground(hex) {
423-
const normalized = toFullHex(hex);
424-
fgHexInput.value = normalized;
422+
function syncForeground(hex, { updateHexInput = true, displayValue } = {}) {
423+
const normalized = normalizeHex(hex);
424+
if (!normalized) {
425+
return;
426+
}
427+
if (updateHexInput) {
428+
fgHexInput.value = displayValue ?? normalized;
429+
}
425430
fgPicker.value = normalized;
426431
updateRgbField(fgRgbInput, normalized);
427432
applyColorPreview(normalized, [fgPicker]);
428433
state.foreground = normalized;
429434
refreshQr();
430435
}
431436

432-
function syncBackground(hex) {
433-
const normalized = toFullHex(hex);
434-
bgHexInput.value = normalized;
437+
function syncBackground(hex, { updateHexInput = true, displayValue } = {}) {
438+
const normalized = normalizeHex(hex);
439+
if (!normalized) {
440+
return;
441+
}
442+
if (updateHexInput) {
443+
bgHexInput.value = displayValue ?? normalized;
444+
}
435445
bgPicker.value = normalized;
436446
updateRgbField(bgRgbInput, normalized);
437447
applyColorPreview(normalized, [bgPicker]);
@@ -448,7 +458,7 @@ <h2>Preview</h2>
448458
fgHexInput.addEventListener('input', () => {
449459
const value = fgHexInput.value.trim();
450460
if (isValidHex(value)) {
451-
syncForeground(value);
461+
syncForeground(value, { updateHexInput: false });
452462
setError();
453463
}
454464
});
@@ -457,22 +467,22 @@ <h2>Preview</h2>
457467
const rgb = parseRgb(fgRgbInput.value);
458468
if (rgb) {
459469
const hex = rgbToHex(rgb);
460-
syncForeground(hex);
470+
syncForeground(hex, { displayValue: hex });
461471
setError();
462472
} else {
463473
setError('Foreground RGB must be in the format "r, g, b" with values between 0 and 255.');
464474
}
465475
});
466476

467477
fgPicker.addEventListener('change', () => {
468-
syncForeground(fgPicker.value);
478+
syncForeground(fgPicker.value, { displayValue: fgPicker.value });
469479
setError();
470480
});
471481

472482
bgHexInput.addEventListener('input', () => {
473483
const value = bgHexInput.value.trim();
474484
if (isValidHex(value)) {
475-
syncBackground(value);
485+
syncBackground(value, { updateHexInput: false });
476486
setError();
477487
}
478488
});
@@ -481,15 +491,15 @@ <h2>Preview</h2>
481491
const rgb = parseRgb(bgRgbInput.value);
482492
if (rgb) {
483493
const hex = rgbToHex(rgb);
484-
syncBackground(hex);
494+
syncBackground(hex, { displayValue: hex });
485495
setError();
486496
} else {
487497
setError('Background RGB must be in the format "r, g, b" with values between 0 and 255.');
488498
}
489499
});
490500

491501
bgPicker.addEventListener('change', () => {
492-
syncBackground(bgPicker.value);
502+
syncBackground(bgPicker.value, { displayValue: bgPicker.value });
493503
setError();
494504
});
495505

0 commit comments

Comments
 (0)