storytellurx-v1/app/utils/calculate-position.tsx
2025-07-13 22:01:50 +02:00

19 lines
509 B
TypeScript

export function calculatePosition(
value: number | string | undefined,
containerSize: number,
elementSize: number,
): number {
// Handle percentage strings (e.g. "50%")
if (typeof value === "string" && value.endsWith("%")) {
const percentage = parseFloat(value) / 100;
return containerSize * percentage;
}
// Handle direct pixel values
if (typeof value === "number") {
return value;
}
// If no value provided, center the element
return (containerSize - elementSize) / 2;
}