xeact.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /**
  2. * Creates a DOM element, assigns the properties of `data` to it, and appends all `children`.
  3. *
  4. * @type{function(string|Function, Object=, Node|Array.<Node|string>=)}
  5. */
  6. const h = (name, data = {}, children = []) => {
  7. const result =
  8. typeof name == "function"
  9. ? name(data)
  10. : Object.assign(document.createElement(name), data);
  11. if (!Array.isArray(children)) {
  12. children = [children];
  13. }
  14. result.append(...children);
  15. return result;
  16. };
  17. /**
  18. * Create a text node.
  19. *
  20. * Equivalent to `document.createTextNode(text)`
  21. *
  22. * @type{function(string): Text}
  23. */
  24. const t = (text) => document.createTextNode(text);
  25. /**
  26. * Remove all child nodes from a DOM element.
  27. *
  28. * @type{function(Node)}
  29. */
  30. const x = (elem) => {
  31. while (elem.lastChild) {
  32. elem.removeChild(elem.lastChild);
  33. }
  34. };
  35. /**
  36. * Get all elements with the given ID.
  37. *
  38. * Equivalent to `document.getElementById(name)`
  39. *
  40. * @type{function(string): HTMLElement}
  41. */
  42. const g = (name) => document.getElementById(name);
  43. /**
  44. * Get all elements with the given class name.
  45. *
  46. * Equivalent to `document.getElementsByClassName(name)`
  47. *
  48. * @type{function(string): HTMLCollectionOf.<Element>}
  49. */
  50. const c = (name) => document.getElementsByClassName(name);
  51. /** @type{function(string): HTMLCollectionOf.<Element>} */
  52. const n = (name) => document.getElementsByName(name);
  53. /**
  54. * Get all elements matching the given HTML selector.
  55. *
  56. * Matches selectors with `document.querySelectorAll(selector)`
  57. *
  58. * @type{function(string): Array.<HTMLElement>}
  59. */
  60. const s = (selector) => Array.from(document.querySelectorAll(selector));
  61. /**
  62. * Generate a relative URL from `url`, appending all key-value pairs from `params` as URL-encoded parameters.
  63. *
  64. * @type{function(string=, Object=): string}
  65. */
  66. const u = (url = "", params = {}) => {
  67. let result = new URL(url, window.location.href);
  68. Object.entries(params).forEach((kv) => {
  69. let [k, v] = kv;
  70. result.searchParams.set(k, v);
  71. });
  72. return result.toString();
  73. };
  74. /**
  75. * Takes a callback to run when all DOM content is loaded.
  76. *
  77. * Equivalent to `window.addEventListener('DOMContentLoaded', callback)`
  78. *
  79. * @type{function(function())}
  80. */
  81. const r = (callback) => window.addEventListener("DOMContentLoaded", callback);
  82. /**
  83. * Allows a stateful value to be tracked by consumers.
  84. *
  85. * This is the Xeact version of the React useState hook.
  86. *
  87. * @type{function(any): [function(): any, function(any): void]}
  88. */
  89. const useState = (value = undefined) => {
  90. return [
  91. () => value,
  92. (x) => {
  93. value = x;
  94. },
  95. ];
  96. };
  97. /**
  98. * Debounce an action for up to ms milliseconds.
  99. *
  100. * @type{function(number): function(function(any): void)}
  101. */
  102. const d = (ms) => {
  103. let debounceTimer = null;
  104. return (f) => {
  105. clearTimeout(debounceTimer);
  106. debounceTimer = setTimeout(f, ms);
  107. };
  108. };
  109. /**
  110. * Parse the contents of a given HTML page element as JSON and
  111. * return the results.
  112. *
  113. * This is useful when using templ to pass complicated data from
  114. * the server to the client via HTML[1].
  115. *
  116. * [1]: https://templ.guide/syntax-and-usage/script-templates/#pass-server-side-data-to-the-client-in-a-html-attribute
  117. */
  118. const j = (id) => JSON.parse(g(id).textContent);
  119. export { h, t, x, g, j, c, n, u, s, r, useState, d };