Sha256: 506840ca200cb57e4118c39635b42d9fdcb6a4af9ea701985a7aa79091663d5b

Contents?: true

Size: 1.91 KB

Versions: 25

Compression:

Stored size: 1.91 KB

Contents

import tippy from "@assets/js/base/tippy";
import buildConfig from "../utils/tippy_modifiers";
import { setData } from "@helpers/alpine";

export default function (Alpine) {
  Alpine.directive(
    "tooltip",
    (el, { modifiers, expression }, { evaluate, evaluateLater, effect }) => {
      const config = buildConfig(modifiers);

      if (!el.__x_tippy) {
        el.__x_tippy = tippy(el, {
          theme: "coco-tooltip",
          ...config,
        });
      }

      let initialContent = null;

      const enableTooltip = () => el.__x_tippy.enable();
      const disableTooltip = () => el.__x_tippy.disable();
      const setupTooltip = (content) => {
        if (!content) {
          disableTooltip();
        } else {
          enableTooltip();

          el.__x_tippy.setContent(content);
          initialContent = initialContent || content;
        }
      };

      // Evaluate initital content immediately if
      // marked as raw, otherwise defer evaluation

      if (modifiers.includes("raw")) {
        setupTooltip(expression);
      } else {
        const getContent = evaluateLater(expression);

        effect(() => {
          getContent((content) => {
            if (typeof content === "object" && content !== null) {
              el.__x_tippy.setProps(content);
              initialContent = initialContent || content.content;
              enableTooltip();
            } else {
              setupTooltip(content);
            }
          });
        });
      }

      // Add some helper methods under the `.tooltip`
      // property for getting/setting tooltip content

      setData(el, {
        tooltip: {
          setContent(content) {
            setupTooltip(content);
          },

          disable: () => disableTooltip(),

          enable: () => enableTooltip(),

          reset() {
            if (initialContent) setupTooltip(initialContent);
          },
        },
      });
    }
  ).before("bind");
}

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
coveragebook_components-0.19.8 app/assets/js/base/alpine/directives/tooltip.js
coveragebook_components-0.19.7 app/assets/js/base/alpine/directives/tooltip.js
coveragebook_components-0.19.6 app/assets/js/base/alpine/directives/tooltip.js
coveragebook_components-0.19.5 app/assets/js/base/alpine/directives/tooltip.js
coveragebook_components-0.19.4 app/assets/js/base/alpine/directives/tooltip.js
coveragebook_components-0.19.3 app/assets/js/base/alpine/directives/tooltip.js
coveragebook_components-0.19.2 app/assets/js/base/alpine/directives/tooltip.js
coveragebook_components-0.19.1 app/assets/js/base/alpine/directives/tooltip.js
coveragebook_components-0.19.0 app/assets/js/base/alpine/directives/tooltip.js
coveragebook_components-0.18.8 app/assets/js/base/alpine/directives/tooltip.js
coveragebook_components-0.18.7 app/assets/js/base/alpine/directives/tooltip.js
coveragebook_components-0.18.0 app/assets/js/base/alpine/directives/tooltip.js
coveragebook_components-0.17.7 app/assets/js/base/alpine/directives/tooltip.js
coveragebook_components-0.17.6 app/assets/js/base/alpine/directives/tooltip.js
coveragebook_components-0.17.5 app/assets/js/base/alpine/directives/tooltip.js
coveragebook_components-0.17.4 app/assets/js/base/alpine/directives/tooltip.js
coveragebook_components-0.17.3 app/assets/js/base/alpine/directives/tooltip.js
coveragebook_components-0.17.2 app/assets/js/base/alpine/directives/tooltip.js
coveragebook_components-0.17.1 app/assets/js/base/alpine/directives/tooltip.js
coveragebook_components-0.17.0 app/assets/js/base/alpine/directives/tooltip.js