ar-test/app/lib/projects/button-size.ts

22 lines
1.1 KiB
TypeScript

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,
};
}