UNPKG

@modern-kit/utils

Version:
35 lines (31 loc) 1.15 kB
'use strict'; var validatorIsNumber = require('../../validator/isNumber/index.cjs'); var deviceIsClient = require('../../device/isClient/index.cjs'); require('../../device/isServer/index.cjs'); const cacheMap = /* @__PURE__ */ new Map(); const getFixedRem = (rem2, toFixedDigits) => { if (validatorIsNumber.isNumber(toFixedDigits)) { return Number(rem2.toFixed(toFixedDigits)); } return rem2; }; const getElementFontSize = (element) => { return getComputedStyle(element).fontSize; }; function rem(pixel, options = {}) { if (!deviceIsClient.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; } exports.rem = rem; //# sourceMappingURL=index.cjs.map