UNPKG

@react-md/utils

Version:
30 lines 1.07 kB
/** * A simple util to convert a unit that is using `px`, `em`, or `rem` to a * number so that calculations can be made on that unit. * * @param unit - The unit to convert to a number * @param element - The element to use to use for calculating `em` * @returns the unit as a number */ export function unitToNumber(unit, options) { if (options === void 0) { options = {}; } var _a = options.fontSizeFallback, fontSizeFallback = _a === void 0 ? 16 : _a, element = options.element; if (typeof unit === "number") { return unit; } var parsed = parseFloat(unit); if (/px$/.test(unit)) { return parsed; } if (typeof document === "undefined") { return parsed * fontSizeFallback; } var rem = /rem$/.test(unit); var el = document.documentElement; if (!rem && element) { el = element.parentElement || element; } var fontSize = parseFloat(window.getComputedStyle(el).fontSize || "".concat(fontSizeFallback, "px")); return parsed * fontSize; } //# sourceMappingURL=unitToNumber.js.map