Sha256: 46ba429111c3140f79e38cb49f25c5035d87fca350abeee926dd68a6e34a728c

Contents?: true

Size: 1.9 KB

Versions: 41

Compression:

Stored size: 1.9 KB

Contents

import tippy from "@libs/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

41 entries across 41 versions & 1 rubygems

Version Path
coveragebook_components-0.10.0 app/assets/js/libs/alpine/directives/tooltip.js
coveragebook_components-0.9.1 app/assets/js/libs/alpine/directives/tooltip.js
coveragebook_components-0.9.0 app/assets/js/libs/alpine/directives/tooltip.js
coveragebook_components-0.8.9 app/assets/js/libs/alpine/directives/tooltip.js
coveragebook_components-0.8.8 app/assets/js/libs/alpine/directives/tooltip.js
coveragebook_components-0.8.7 app/assets/js/libs/alpine/directives/tooltip.js
coveragebook_components-0.8.6 app/assets/js/libs/alpine/directives/tooltip.js
coveragebook_components-0.8.5 app/assets/js/libs/alpine/directives/tooltip.js
coveragebook_components-0.8.4 app/assets/js/libs/alpine/directives/tooltip.js
coveragebook_components-0.8.3 app/assets/js/libs/alpine/directives/tooltip.js
coveragebook_components-0.8.2 app/assets/js/libs/alpine/directives/tooltip.js
coveragebook_components-0.8.1 app/assets/js/libs/alpine/directives/tooltip.js
coveragebook_components-0.8.0 app/assets/js/libs/alpine/directives/tooltip.js
coveragebook_components-0.8.0.beta.3 app/assets/js/libs/alpine/directives/tooltip.js
coveragebook_components-0.8.0.beta.2 app/assets/js/libs/alpine/directives/tooltip.js
coveragebook_components-0.8.0.beta.1 app/assets/js/libs/alpine/directives/tooltip.js
coveragebook_components-0.7.10 app/assets/js/libs/alpine/directives/tooltip.js
coveragebook_components-0.7.9 app/assets/js/libs/alpine/directives/tooltip.js
coveragebook_components-0.7.8 app/assets/js/libs/alpine/directives/tooltip.js
coveragebook_components-0.7.7 app/assets/js/libs/alpine/directives/tooltip.js