UNPKG

@modern-kit/utils

Version:
33 lines (30 loc) 1.08 kB
import { isNumber } from '../../validator/isNumber/index.mjs'; import { isClient } from '../../device/isClient/index.mjs'; import '../../device/isServer/index.mjs'; const cacheMap = /* @__PURE__ */ new Map(); const getFixedRem = (rem2, toFixedDigits) => { if (isNumber(toFixedDigits)) { return Number(rem2.toFixed(toFixedDigits)); } return rem2; }; const getElementFontSize = (element) => { return getComputedStyle(element).fontSize; }; function rem(pixel, options = {}) { if (!isClient()) { throw new Error("Cannot be executed unless it is a client environment."); } const { suffix = true, toFixedDigits } = options; const cacheKey = `${pixel}-${suffix}-${toFixedDigits}`; if (cacheMap.has(cacheKey)) { return cacheMap.get(cacheKey); } const rootFontSize = getElementFontSize(document.documentElement); const fixedRem = getFixedRem(pixel / parseFloat(rootFontSize), toFixedDigits); const result = suffix ? `${fixedRem}rem` : fixedRem; cacheMap.set(cacheKey, result); return result; } export { rem }; //# sourceMappingURL=index.mjs.map