Sha256: 8e7708356567f7bb8d4e0209d9a97acb0ec50d6bcdb45cb1de5e846ee8b89e36

Contents?: true

Size: 1.91 KB

Versions: 7

Compression:

Stored size: 1.91 KB

Contents

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

7 entries across 7 versions & 1 rubygems

Version Path
coveragebook_components-0.12.2 app/assets/js/libs/alpine/directives/tooltip.js
coveragebook_components-0.12.1 app/assets/js/libs/alpine/directives/tooltip.js
coveragebook_components-0.12.0 app/assets/js/libs/alpine/directives/tooltip.js
coveragebook_components-0.11.0 app/assets/js/libs/alpine/directives/tooltip.js
coveragebook_components-0.10.1.beta.2 app/assets/js/libs/alpine/directives/tooltip.js
coveragebook_components-0.10.1.beta.1 app/assets/js/libs/alpine/directives/tooltip.js
coveragebook_components-0.10.1.beta.0 app/assets/js/libs/alpine/directives/tooltip.js