You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

0 lines
7.6 KiB

1 month ago
  1. {"ast":null,"code":"import { isNumber, isStringNumber } from '../types.mjs';\nimport { isClient } from '@vueuse/core';\nimport { entriesOf, keysOf } from '../objects.mjs';\nimport { debugWarn } from '../error.mjs';\nimport { camelize, isObject, isString } from '@vue/shared';\nconst SCOPE = \"utils/dom/style\";\nconst classNameToArray = (cls = \"\") => cls.split(\" \").filter(item => !!item.trim());\nconst hasClass = (el, cls) => {\n if (!el || !cls) return false;\n if (cls.includes(\" \")) throw new Error(\"className should not contain space.\");\n return el.classList.contains(cls);\n};\nconst addClass = (el, cls) => {\n if (!el || !cls.trim()) return;\n el.classList.add(...classNameToArray(cls));\n};\nconst removeClass = (el, cls) => {\n if (!el || !cls.trim()) return;\n el.classList.remove(...classNameToArray(cls));\n};\nconst getStyle = (element, styleName) => {\n var _a;\n if (!isClient || !element || !styleName) return \"\";\n let key = camelize(styleName);\n if (key === \"float\") key = \"cssFloat\";\n try {\n const style = element.style[key];\n if (style) return style;\n const computed = (_a = document.defaultView) == null ? void 0 : _a.getComputedStyle(element, \"\");\n return computed ? computed[key] : \"\";\n } catch (e) {\n return element.style[key];\n }\n};\nconst setStyle = (element, styleName, value) => {\n if (!element || !styleName) return;\n if (isObject(styleName)) {\n entriesOf(styleName).forEach(([prop, value2]) => setStyle(element, prop, value2));\n } else {\n const key = camelize(styleName);\n element.style[key] = value;\n }\n};\nconst removeStyle = (element, style) => {\n if (!element || !style) return;\n if (isObject(style)) {\n keysOf(style).forEach(prop => removeStyle(element, prop));\n } else {\n setStyle(element, style, \"\");\n }\n};\nfunction addUnit(value, defaultUnit = \"px\") {\n if (!value) return \"\";\n if (isNumber(value) || isStringNumber(value)) {\n return `${value}${defaultUnit}`;\n } else if (isString(value)) {\n return value;\n }\n debugWarn(SCOPE, \"binding value must be a string or number\");\n}\nexport { addClass, addUnit, classNameToArray, getStyle, hasClass, removeClass, removeStyle, setStyle };","map":{"version":3,"names":["SCOPE","classNameToArray","cls","split","filter","item","trim","hasClass","el","includes","Error","classList","contains","addClass","add","removeClass","remove","getStyle","element","styleName","_a","isClient","key","camelize","style","computed","document","defaultView","getComputedStyle","e","setStyle","value","isObject","entriesOf","forEach","prop","value2","removeStyle","keysOf","addUnit","defaultUnit","isNumber","isStringNumber","isString","debugWarn"],"sources":["../../../../../packages/utils/dom/style.ts"],"sourcesContent":["import { isNumber, isObject, isString, isStringNumber } from '../types'\nimport { isClient } from '../browser'\nimport { camelize } from '../strings'\nimport { entriesOf, keysOf } from '../objects'\nimport { debugWarn } from '../error'\nimport type { CSSProperties } from 'vue'\n\nconst SCOPE = 'utils/dom/style'\n\nexport const classNameToArray = (cls = '') =>\n cls.split(' ').filter((item) => !!item.trim())\n\nexport const hasClass = (el: Element, cls: string): boolean => {\n if (!el || !cls) return false\n if (cls.includes(' ')) throw new Error('className should not contain space.')\n return el.classList.contains(cls)\n}\n\nexport const addClass = (el: Element, cls: string) => {\n if (!el || !cls.trim()) return\n el.classList.add(...classNameToArray(cls))\n}\n\nexport const removeClass = (el: Element, cls: string) => {\n if (!el || !cls.trim()) return\n el.classList.remove(...classNameToArray(cls))\n}\n\nexport const getStyle = (\n element: HTMLElement,\n styleName: keyof CSSProperties\n): string => {\n if (!isClient || !element || !styleName) return ''\n\n let key = camelize(styleName)\n if (key === 'float') key = 'cssFloat'\n try {\n const style = (element.style as any)[key]\n if (style) return style\n const computed: any = document.defaul