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.

css.js 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { camelCase } from '../../utils/utils.js'
  2. import { isBlank } from '../core/regex.js'
  3. import { registerMethods } from '../../utils/methods.js'
  4. const camelCaseWithVars = (str) => (str.startsWith('--') ? str : camelCase(str))
  5. // Dynamic style generator
  6. export function css(style, val) {
  7. const ret = {}
  8. if (arguments.length === 0) {
  9. // get full style as object
  10. this.node.style.cssText
  11. .split(/\s*;\s*/)
  12. .filter(function (el) {
  13. return !!el.length
  14. })
  15. .forEach(function (el) {
  16. const t = el.split(/\s*:\s*/)
  17. ret[t[0]] = t[1]
  18. })
  19. return ret
  20. }
  21. if (arguments.length < 2) {
  22. // get style properties as array
  23. if (Array.isArray(style)) {
  24. for (const name of style) {
  25. const cased = camelCaseWithVars(name)
  26. ret[name] = this.node.style.getPropertyValue(cased)
  27. }
  28. return ret
  29. }
  30. // get style for property
  31. if (typeof style === 'string') {
  32. return this.node.style.getPropertyValue(camelCaseWithVars(style))
  33. }
  34. // set styles in object
  35. if (typeof style === 'object') {
  36. for (const name in style) {
  37. // set empty string if null/undefined/'' was given
  38. this.node.style.setProperty(
  39. camelCaseWithVars(name),
  40. style[name] == null || isBlank.test(style[name]) ? '' : style[name]
  41. )
  42. }
  43. }
  44. }
  45. // set style for property
  46. if (arguments.length === 2) {
  47. this.node.style.setProperty(
  48. camelCaseWithVars(style),
  49. val == null || isBlank.test(val) ? '' : val
  50. )
  51. }
  52. return this
  53. }
  54. // Show element
  55. export function show() {
  56. return this.css('display', '')
  57. }
  58. // Hide element
  59. export function hide() {
  60. return this.css('display', 'none')
  61. }
  62. // Is element visible?
  63. export function visible() {
  64. return this.css('display') !== 'none'
  65. }
  66. registerMethods('Dom', {
  67. css,
  68. show,
  69. hide,
  70. visible
  71. })