758345a412f03695547211ab9727ca759cb7f0d1a3fb28e2df998a26e5555919092b03828cf6af8566ec8c7b4059154ec9074eb95ca28e6a0e4b5cff60ab53 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { isNumber, isStringNumber } from '../types.mjs';
  2. import { isClient } from '@vueuse/core';
  3. import { camelize, isObject, isString } from '@vue/shared';
  4. import { entriesOf, keysOf } from '../objects.mjs';
  5. const classNameToArray = (cls = "") => cls.split(" ").filter((item) => !!item.trim());
  6. const hasClass = (el, cls) => {
  7. if (!el || !cls)
  8. return false;
  9. if (cls.includes(" "))
  10. throw new Error("className should not contain space.");
  11. return el.classList.contains(cls);
  12. };
  13. const addClass = (el, cls) => {
  14. if (!el || !cls.trim())
  15. return;
  16. el.classList.add(...classNameToArray(cls));
  17. };
  18. const removeClass = (el, cls) => {
  19. if (!el || !cls.trim())
  20. return;
  21. el.classList.remove(...classNameToArray(cls));
  22. };
  23. const getStyle = (element, styleName) => {
  24. var _a;
  25. if (!isClient || !element || !styleName)
  26. return "";
  27. let key = camelize(styleName);
  28. if (key === "float")
  29. key = "cssFloat";
  30. try {
  31. const style = element.style[key];
  32. if (style)
  33. return style;
  34. const computed = (_a = document.defaultView) == null ? void 0 : _a.getComputedStyle(element, "");
  35. return computed ? computed[key] : "";
  36. } catch (e) {
  37. return element.style[key];
  38. }
  39. };
  40. const setStyle = (element, styleName, value) => {
  41. if (!element || !styleName)
  42. return;
  43. if (isObject(styleName)) {
  44. entriesOf(styleName).forEach(([prop, value2]) => setStyle(element, prop, value2));
  45. } else {
  46. const key = camelize(styleName);
  47. element.style[key] = value;
  48. }
  49. };
  50. const removeStyle = (element, style) => {
  51. if (!element || !style)
  52. return;
  53. if (isObject(style)) {
  54. keysOf(style).forEach((prop) => removeStyle(element, prop));
  55. } else {
  56. setStyle(element, style, "");
  57. }
  58. };
  59. function addUnit(value, defaultUnit = "px") {
  60. if (!value)
  61. return "";
  62. if (isNumber(value) || isStringNumber(value)) {
  63. return `${value}${defaultUnit}`;
  64. } else if (isString(value)) {
  65. return value;
  66. }
  67. }
  68. export { addClass, addUnit, classNameToArray, getStyle, hasClass, removeClass, removeStyle, setStyle };
  69. //# sourceMappingURL=style.mjs.map