From 3695eb084d88e66b71d2608589a5e30e6836addb Mon Sep 17 00:00:00 2001 From: Vargha Csongor Date: Mon, 28 Apr 2025 20:28:38 +0200 Subject: [PATCH] tailwind px helper --- apps/app/src/lib/tailwindSizeToPx.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 apps/app/src/lib/tailwindSizeToPx.ts diff --git a/apps/app/src/lib/tailwindSizeToPx.ts b/apps/app/src/lib/tailwindSizeToPx.ts new file mode 100644 index 0000000..1f0b407 --- /dev/null +++ b/apps/app/src/lib/tailwindSizeToPx.ts @@ -0,0 +1,19 @@ +export function tailwindClassToPixels(className: string): number | null { + const remSize = getRemInPixels(); // <-- real rem size at runtime + + const regex = /^(w|h)-(\d+)$/; + const match = className.match(regex); + if (!match) return null; + + const value = parseInt(match[2], 10); + return (value / 4) * remSize; +} + +export function getRemInPixels(): number { + try { + const fontSize = getComputedStyle(document.documentElement).fontSize; + return parseFloat(fontSize); + } catch (e) { + return 16; // Default to 16px if unable to get computed style + } +}