fix: integer width

This commit is contained in:
Felix Hungenberg
2026-01-21 16:35:13 +01:00
parent 2a90d5de3f
commit f54cde734e

View File

@@ -1,210 +1,210 @@
<script lang="ts"> <script lang="ts">
interface Props { interface Props {
value?: number; value?: number;
step?: number; step?: number;
min?: number | undefined; min?: number | undefined;
max?: number | undefined; max?: number | undefined;
id?: string; id?: string;
change?: (arg: number) => void; change?: (arg: number) => void;
} }
let { let {
value = $bindable(0), value = $bindable(0),
step = 1, step = 1,
min = $bindable(0), min = $bindable(0),
max = $bindable(1), max = $bindable(1),
id, id,
change change
}: Props = $props(); }: Props = $props();
if (min > max) { if (min > max) {
[min, max] = [max, min]; [min, max] = [max, min];
} }
if (value > max) { if (value > max) {
max = value; max = value;
} }
function strip(input: number) { function strip(input: number) {
return +parseFloat(input + '').toPrecision(2); return +parseFloat(input + '').toPrecision(2);
} }
let inputEl = $state() as HTMLInputElement; let inputEl = $state() as HTMLInputElement;
let wrapper = $state() as HTMLDivElement; let wrapper = $state() as HTMLDivElement;
let prev = -1; let prev = -1;
function update() { function update() {
if (prev === value) return; if (prev === value) return;
prev = value; prev = value;
change?.(value); change?.(value);
} }
function handleChange(change: number) { function handleChange(change: number) {
value = Math.max(min ?? -Infinity, Math.min(+value + change, max ?? Infinity)); value = Math.max(min ?? -Infinity, Math.min(+value + change, max ?? Infinity));
} }
let isMouseDown = $state(false); let isMouseDown = $state(false);
let downX = 0; let downX = 0;
let downV = 0; let downV = 0;
let rect: DOMRect; let rect: DOMRect;
function handleMouseDown(ev: MouseEvent) { function handleMouseDown(ev: MouseEvent) {
ev.preventDefault(); ev.preventDefault();
downV = value; downV = value;
downX = ev.clientX; downX = ev.clientX;
rect = wrapper.getBoundingClientRect(); rect = wrapper.getBoundingClientRect();
window.removeEventListener('mousemove', handleMouseMove); window.removeEventListener('mousemove', handleMouseMove);
window.addEventListener('mousemove', handleMouseMove); window.addEventListener('mousemove', handleMouseMove);
window.addEventListener('mouseup', handleMouseUp); window.addEventListener('mouseup', handleMouseUp);
document.body.style.cursor = 'ew-resize'; document.body.style.cursor = 'ew-resize';
} }
function handleMouseUp() { function handleMouseUp() {
if (downV === value) { if (downV === value) {
inputEl.focus(); inputEl.focus();
} else { } else {
inputEl.blur(); inputEl.blur();
} }
document.body.style.cursor = 'unset'; document.body.style.cursor = 'unset';
window.removeEventListener('mouseup', handleMouseUp); window.removeEventListener('mouseup', handleMouseUp);
window.removeEventListener('mousemove', handleMouseMove); window.removeEventListener('mousemove', handleMouseMove);
} }
function handleKeyDown(ev: KeyboardEvent) { function handleKeyDown(ev: KeyboardEvent) {
if (ev.key === 'Escape' || ev.key === 'Enter') { if (ev.key === 'Escape' || ev.key === 'Enter') {
handleMouseUp(); handleMouseUp();
inputEl?.blur(); inputEl?.blur();
} }
} }
function handleMouseMove(ev: MouseEvent) { function handleMouseMove(ev: MouseEvent) {
if (!ev.ctrlKey && typeof min === 'number' && typeof max === 'number') { if (!ev.ctrlKey && typeof min === 'number' && typeof max === 'number') {
const vx = (ev.clientX - rect.left) / rect.width; const vx = (ev.clientX - rect.left) / rect.width;
value = Math.max(Math.min(Math.round(min + (max - min) * vx), max), min); value = Math.max(Math.min(Math.round(min + (max - min) * vx), max), min);
} else { } else {
const vx = ev.clientX - downX; const vx = ev.clientX - downX;
value = downV + Math.round(vx / 10); value = downV + Math.round(vx / 10);
} }
} }
$effect(() => { $effect(() => {
if ((value || 0).toString().length > 5) { if ((value || 0).toString().length > 5) {
value = strip(value || 0); value = strip(value || 0);
} }
value !== undefined && update(); value !== undefined && update();
}); });
let width = $derived( let width = $derived(
Number.isFinite(value) ? Math.max((value?.toString().length ?? 1) * 8, 30) + 'px' : '20px' Number.isFinite(value) ? Math.max((value?.toString().length ?? 1) * 8, 30) + 'px' : '20px'
); );
</script> </script>
<div <div
class="component-wrapper" class="component-wrapper"
bind:this={wrapper} bind:this={wrapper}
class:is-down={isMouseDown} class:is-down={isMouseDown}
role="slider" role="slider"
tabindex="0" tabindex="0"
aria-valuenow={value} aria-valuenow={value}
onkeydown={handleKeyDown} onkeydown={handleKeyDown}
onmousedown={handleMouseDown} onmousedown={handleMouseDown}
onmouseup={handleMouseUp} onmouseup={handleMouseUp}
> >
<div class=""> <button onclick={() => handleChange(-step)}>-</button>
<button onclick={() => handleChange(-step)}>-</button> <input
<input bind:value
bind:value bind:this={inputEl}
bind:this={inputEl} {id}
{id} {step}
{step} {max}
{max} {min}
{min} type="number"
type="number" style={`width:${width};`}
style={`width:${width};`} />
/>
<button onclick={() => handleChange(+step)}>+</button> <button onclick={() => handleChange(+step)}>+</button>
</div> {#if typeof min !== 'undefined' && typeof max !== 'undefined'}
{#if typeof min !== 'undefined' && typeof max !== 'undefined'} <span
<span class="overlay" style={`width: ${Math.min((value - min) / (max - min), 1) * 100}%`} class="overlay"
></span> style={`width: ${Math.min((value - min) / (max - min), 1) * 100}%`}
{/if} ></span>
{/if}
</div> </div>
<style> <style>
.component-wrapper { .component-wrapper {
position: relative; position: relative;
display: flex; display: flex;
background-color: var(--layer-2, #4b4b4b); background-color: var(--layer-2, #4b4b4b);
border-radius: 2px; border-radius: 2px;
user-select: none; user-select: none;
transition: box-shadow 0.3s ease; transition: box-shadow 0.3s ease;
outline: solid 1px var(--outline); outline: solid 1px var(--outline);
overflow: hidden; overflow: hidden;
border-radius: var(--border-radius, 2px); border-radius: var(--border-radius, 2px);
} }
input[type='number']::-webkit-inner-spin-button, input[type="number"]::-webkit-inner-spin-button,
input[type='number']::-webkit-outer-spin-button { input[type="number"]::-webkit-outer-spin-button {
-webkit-appearance: none; -webkit-appearance: none;
} }
input[type='number'] { input[type="number"] {
-webkit-appearance: textfield; -webkit-appearance: textfield;
-moz-appearance: textfield; -moz-appearance: textfield;
appearance: textfield; appearance: textfield;
cursor: pointer; cursor: pointer;
font-family: var(--font-family); font-family: var(--font-family);
font-variant-numeric: tabular-nums; font-variant-numeric: tabular-nums;
color: var(--text-color); color: var(--text-color);
background-color: transparent; background-color: transparent;
padding: var(--padding, 6px); padding: var(--padding, 6px);
font-size: 1em; font-size: 1em;
padding-inline: 10px; padding-inline: 10px;
text-align: center; text-align: center;
border: none; border: none;
border-style: none; border-style: none;
flex: 1; flex: 1;
width: 72%; width: 72%;
} }
.is-down > input { .is-down > input {
cursor: ew-resize !important; cursor: ew-resize !important;
} }
.overlay { .overlay {
position: absolute; position: absolute;
top: 0px; top: 0px;
left: 0px; left: 0px;
height: 100%; height: 100%;
background-color: var(--layer-3); background-color: var(--layer-3);
opacity: 0.3; opacity: 0.3;
pointer-events: none; pointer-events: none;
} }
.is-down > .overlay { .is-down > .overlay {
transition: none !important; transition: none !important;
} }
button { button {
background-color: transparent; background-color: transparent;
border: none; border: none;
cursor: pointer; cursor: pointer;
line-height: 0px; line-height: 0px;
margin: 0; margin: 0;
color: var(--text-color); color: var(--text-color);
margin-inline: 6px; margin-inline: 6px;
} }
div input[type='number'] { div input[type="number"] {
color: var(--text-color); color: var(--text-color);
background-color: transparent; background-color: transparent;
padding: var(--padding, 6px); padding: var(--padding, 6px);
padding-inline: 0px; padding-inline: 0px;
text-align: center; text-align: center;
border: none; border: none;
border-style: none; border-style: none;
} }
</style> </style>