style.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import { isClient } from '@vueuse/core'
  2. import { isNumber, isObject, isString, isStringNumber } from '../types'
  3. import { camelize } from '../strings'
  4. import { entriesOf, keysOf } from '../objects'
  5. import { debugWarn } from '../error'
  6. import type { CSSProperties } from 'vue'
  7. const SCOPE = 'utils/dom/style'
  8. export const classNameToArray = (cls = '') =>
  9. cls.split(' ').filter((item) => !!item.trim())
  10. export const hasClass = (el: Element, cls: string): boolean => {
  11. if (!el || !cls) return false
  12. if (cls.includes(' ')) throw new Error('className should not contain space.')
  13. return el.classList.contains(cls)
  14. }
  15. export const addClass = (el: Element, cls: string) => {
  16. if (!el || !cls.trim()) return
  17. el.classList.add(...classNameToArray(cls))
  18. }
  19. export const removeClass = (el: Element, cls: string) => {
  20. if (!el || !cls.trim()) return
  21. el.classList.remove(...classNameToArray(cls))
  22. }
  23. export const getStyle = (
  24. element: HTMLElement,
  25. styleName: keyof CSSProperties
  26. ): string => {
  27. if (!isClient || !element || !styleName) return ''
  28. let key = camelize(styleName)
  29. if (key === 'float') key = 'cssFloat'
  30. try {
  31. const style = (element.style as any)[key]
  32. if (style) return style
  33. const computed: any = document.defaultView?.getComputedStyle(element, '')
  34. return computed ? computed[key] : ''
  35. } catch {
  36. return (element.style as any)[key]
  37. }
  38. }
  39. export const setStyle = (
  40. element: HTMLElement,
  41. styleName: CSSProperties | keyof CSSProperties,
  42. value?: string | number
  43. ) => {
  44. if (!element || !styleName) return
  45. if (isObject(styleName)) {
  46. entriesOf(styleName).forEach(([prop, value]) =>
  47. setStyle(element, prop, value)
  48. )
  49. } else {
  50. const key: any = camelize(styleName)
  51. element.style[key] = value as any
  52. }
  53. }
  54. export const removeStyle = (
  55. element: HTMLElement,
  56. style: CSSProperties | keyof CSSProperties
  57. ) => {
  58. if (!element || !style) return
  59. if (isObject(style)) {
  60. keysOf(style).forEach((prop) => removeStyle(element, prop))
  61. } else {
  62. setStyle(element, style, '')
  63. }
  64. }
  65. export function addUnit(value?: string | number, defaultUnit = 'px') {
  66. if (!value) return ''
  67. if (isNumber(value) || isStringNumber(value)) {
  68. return `${value}${defaultUnit}`
  69. } else if (isString(value)) {
  70. return value
  71. }
  72. debugWarn(SCOPE, 'binding value must be a string or number')
  73. }