aboutsummaryrefslogtreecommitdiff
path: root/static/js/hoverfix.ts
blob: b6d1a5bf98d4190495eb498df4b4131ee81aad93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
(function () {
  /*/
   *  Element.closest() polyfill
   *  https://developer.mozilla.org/en-US/docs/Web/API/Element/closest
   *  https://github.com/idmadj/element-closest-polyfill#readme
   *  Copyright (C) Abdelmadjid Hammou
   *  Licence: ISC | https://www.isc.org/licenses/
  /*/

  if (typeof Element !== "undefined") {
    if (!Element.prototype.matches) {
      Element.prototype.matches = Element.prototype.msMatchesSelector ||
        Element.prototype.webkitMatchesSelector;
    }
    if (!Element.prototype.closest) {
      Element.prototype.closest = function (s) {
        var el = this;
        do {
          if (el.matches(s)) return el;
          el = el.parentElement || el.parentNode;
        } while (el !== null && el.nodeType === 1);
        return null;
      };
    }
  }

  /*/
   * -----------------------------------------------------------------
  /*/

  const disabled = "0s";

  function walk(children, callback) {
    for (let i = 0; i < children.length; i++) {
      callback(children[i]);
      walk(children[i].children, callback);
    }
  }

  self.addEventListener("mousemove", function (event) {
    if (typeof event.target.closest !== "function") return;
    tree = event.target.closest("figure") || event.target.closest("article");

    if (tree !== null) {
      walk(tree.children, function (element) {
        const delay = self.getComputedStyle(element).getPropertyValue(
          "transition-delay",
        );
        if (delay !== disabled) {
          element.style.setProperty("visibility", "hidden");
        }
      });

      walk(tree.children, function (element) {
        const delay = self.getComputedStyle(element).getPropertyValue(
          "transition-delay",
        );
        if (delay !== disabled) {
          element.style.removeProperty("visibility");
        }
      });
    }
  });
})();