From a72f76f814cd3148f9479587fb589a3a732ad97c Mon Sep 17 00:00:00 2001 From: rajun Date: Tue, 27 Oct 2026 16:28:53 +0530 Subject: [PATCH] feat: clamp custom button sizes for the live preview --- app/lib/projects/button-size.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 app/lib/projects/button-size.ts diff --git a/app/lib/projects/button-size.ts b/app/lib/projects/button-size.ts new file mode 100644 index 0000000..8c27855 --- /dev/null +++ b/app/lib/projects/button-size.ts @@ -0,0 +1,21 @@ +import { BUTTON_CUSTOM_DEFAULT, BUTTON_CUSTOM_LIMITS } from '~/data/projects/constants'; +import type { ButtonCustomSize } from '~/types/project'; + +function fit(key: keyof typeof BUTTON_CUSTOM_LIMITS, value: number | null): number | null { + if (typeof value !== 'number' || !Number.isFinite(value)) return null; + const { min, max } = BUTTON_CUSTOM_LIMITS[key]; + return Math.min(max, Math.max(min, value)); +} + +// Custom measurements as they are being typed, made safe to draw: each held within its range, and an empty box +// falls back to the default (an empty width fits the label). The preview follows every keystroke this way. +export function fitButtonCustom(custom: ButtonCustomSize): ButtonCustomSize { + return { + height: fit('height', custom.height) ?? BUTTON_CUSTOM_DEFAULT.height, + width: fit('width', custom.width), + paddingX: fit('paddingX', custom.paddingX) ?? BUTTON_CUSTOM_DEFAULT.paddingX, + radius: fit('radius', custom.radius) ?? BUTTON_CUSTOM_DEFAULT.radius, + borderWidth: fit('borderWidth', custom.borderWidth) ?? BUTTON_CUSTOM_DEFAULT.borderWidth, + borderColor: custom.borderColor, + }; +}