Sha256: 400ae462df57eac5af5ab51478f617149073b494a7b90fa8596572396ade695e

Contents?: true

Size: 1.75 KB

Versions: 16

Compression:

Stored size: 1.75 KB

Contents

import tippy from "@libs/tippy";

export default function withTooltip(props = {}) {
  function withTooltipMixin(component) {
    const el = component.$root;
    const oldDestroy = component.destroy;

    if (!component.hasData("tippy-content")) {
      component.setData("tippy-disabled", true);
    }

    const content = component.getData("tippy-content");
    const tooltip = tippy(el, {
      theme: "coco-tooltip",

      onShow() {
        if (component.onTooltipShow) {
          return component.onTooltipShow();
        } else {
          return component.refuteData("tippy-disabled", true);
        }
      },

      content() {
        return content;
      },

      ...props,
    });

    Object.assign(
      component,
      Alpine.reactive({
        tooltip: {
          content,
          instance: tooltip,
          originalContent: content,
        },

        getTooltip() {
          return this.tooltip;
        },

        setTooltip(content) {
          this.tooltip.content = content;
        },

        resetTooltip() {
          this.tooltip.content = this.tooltip.originalContent;
        },

        destroy() {
          if (this.tooltip && this.tooltip.instance) {
            this.tooltip.instance.destroy();
            this.tooltip = null;
          }

          if (oldDestroy) {
            oldDestroy.call(this);
          }
        },
      })
    );

    Alpine.effect(() => {
      if (component.tooltip && component.tooltip.instance) {
        const value = component.tooltip.content;
        if (value !== null) {
          component.tooltip.instance.setContent(value);
          component.setData("tippy-content", value);
        }
      }
    });

    return component;
  }

  withTooltipMixin.props = ["tooltip"];

  return withTooltipMixin;
}

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
coveragebook_components-0.7.1 app/assets/js/base/mixins/tooltip.js
coveragebook_components-0.7.0 app/assets/js/base/mixins/tooltip.js
coveragebook_components-0.6.5 app/assets/js/base/mixins/tooltip.js
coveragebook_components-0.6.4 app/assets/js/base/mixins/tooltip.js
coveragebook_components-0.6.3 app/assets/js/base/mixins/tooltip.js
coveragebook_components-0.6.2 app/assets/js/base/mixins/tooltip.js
coveragebook_components-0.6.1 app/assets/js/base/mixins/tooltip.js
coveragebook_components-0.6.0 app/assets/js/base/mixins/tooltip.js
coveragebook_components-0.5.7 app/assets/js/base/mixins/tooltip.js
coveragebook_components-0.5.6 app/assets/js/base/mixins/tooltip.js
coveragebook_components-0.5.5 app/assets/js/base/mixins/tooltip.js
coveragebook_components-0.5.4 app/assets/js/base/mixins/tooltip.js
coveragebook_components-0.5.3 app/assets/js/base/mixins/tooltip.js
coveragebook_components-0.5.2 app/assets/js/base/mixins/tooltip.js
coveragebook_components-0.5.1 app/assets/js/base/mixins/tooltip.js
coveragebook_components-0.5.0 app/assets/js/base/mixins/tooltip.js