* Snap an absolute pixel value to the nearest Tailwind spacing token. * Returns the token name (e.g., "6") or an arbitrary value (e.g., "[32px]").
(absPx: number)
| 969 | * Returns the token name (e.g., "6") or an arbitrary value (e.g., "[32px]"). |
| 970 | */ |
| 971 | function snapPxToSpacingToken(absPx: number): string { |
| 972 | let bestToken: string | null = null; |
| 973 | let bestDist = Infinity; |
| 974 | |
| 975 | for (const [token, tokenPx] of Object.entries(SPACING_TOKEN_PX)) { |
| 976 | const dist = Math.abs(absPx - tokenPx); |
| 977 | if (dist < bestDist) { |
| 978 | bestDist = dist; |
| 979 | bestToken = token; |
| 980 | } |
| 981 | } |
| 982 | |
| 983 | // Accept if within 15% relative threshold (max 8px) |
| 984 | if (bestToken !== null && bestDist <= Math.min(absPx * 0.15, 8)) { |
| 985 | return bestToken; |
| 986 | } |
| 987 | |
| 988 | return `[${Math.round(absPx)}px]`; |
| 989 | } |
| 990 | |
| 991 | // ── Move mechanism detection ───────────────────────────────────────────── |
| 992 |