* Parse a Tailwind translate class (e.g., "translate-x-6", "-translate-y-4", "translate-x-[32px]") * back to a signed pixel value.
(cls: string, basePrefix: string)
| 944 | * back to a signed pixel value. |
| 945 | */ |
| 946 | function parseTranslateClassPx(cls: string, basePrefix: string): number { |
| 947 | const isNeg = cls.startsWith("-"); |
| 948 | // Strip leading "-" and the prefix + "-" to get the token |
| 949 | const stripped = isNeg ? cls.slice(1) : cls; |
| 950 | const token = stripped.slice(basePrefix.length + 1); // +1 for the "-" separator |
| 951 | |
| 952 | // Arbitrary value: [Npx] |
| 953 | const arbMatch = token.match(/^\[(-?\d+(?:\.\d+)?)px\]$/); |
| 954 | if (arbMatch) { |
| 955 | return (isNeg ? -1 : 1) * parseFloat(arbMatch[1]); |
| 956 | } |
| 957 | |
| 958 | // Standard token |
| 959 | const px = SPACING_TOKEN_PX[token]; |
| 960 | if (px != null) { |
| 961 | return (isNeg ? -1 : 1) * px; |
| 962 | } |
| 963 | |
| 964 | return 0; // unknown token, treat as 0 |
| 965 | } |
| 966 | |
| 967 | /** |
| 968 | * Snap an absolute pixel value to the nearest Tailwind spacing token. |