@web3r/flowerkit
Version:
Tree-shakable JavaScript and TypeScript utility library for frontend/browser apps: DOM, events, arrays, objects, strings, date, JSON, and network helpers (ESM/CJS, SSR-friendly).
1 lines • 1.78 kB
Source Map (JSON)
{"version":3,"file":"index.mjs","sources":[""],"sourcesContent":["import { getWindow } from \"ssr-window\";\r\n\r\nexport type TGetCSSValueArgs = Parameters<typeof getCSSValue>;\r\n\r\nexport type TGetCSSValueReturn = ReturnType<typeof getCSSValue>;\r\n\r\n/**\r\n * Gets a calculated CSS property of an DOM-element\r\n * @param el{HTMLElement} DOM element\r\n * @param prop{String} CSS property\r\n * @see https://developer.mozilla.org/en-US/docs/Web/API/Window/getComputedStyle\r\n * @returns {string}\r\n * @throws {TypeError} getCSSValue: el must be an HTMLElement\r\n * @throws {TypeError} getCSSValue: prop must be a string\r\n * @example\r\n * // How to get \"height\" prop of div from JS?\r\n * const block = document.querySelector(\"#myBlock\");\r\n * getCSSValue(block, \"height\");\r\n */\r\nexport const getCSSValue = (el: HTMLElement, prop: string): string => {\r\n if (!el || typeof (el as any).style !== \"object\") {\r\n throw new TypeError(\"getCSSValue: el must be an HTMLElement\");\r\n }\r\n if (typeof prop !== \"string\") {\r\n throw new TypeError(\"getCSSValue: prop must be a string\");\r\n }\r\n switch (prop) {\r\n case \"width\":\r\n return `${el.offsetWidth}px`;\r\n case \"height\":\r\n return `${el.offsetHeight}px`;\r\n default:\r\n return getWindow().getComputedStyle(el).getPropertyValue(prop);\r\n }\r\n};\r\n"],"names":["getCSSValue","el","prop","style","TypeError","offsetWidth","offsetHeight","getWindow","getComputedStyle","getPropertyValue"],"mappings":";;;;;;;;;;;;;SAmBaA,YAAcA,CAACC,GAAiBC,QAC3C,IAAKD,WAAcA,GAAWE,QAAU,SACtC,MAAM,IAAIC,UAAU,0CAEtB,UAAWF,OAAS,SAClB,MAAM,IAAIE,UAAU,sCAEtB,OAAQF,MACN,IAAK,QACH,MAAO,GAAGD,GAAGI,gBACf,IAAK,SACH,MAAO,GAAGJ,GAAGK,iBACf,QACE,OAAOC,YAAYC,iBAAiBP,IAAIQ,iBAAiBP"}