Advice on using tippy.js

Created on 6 January 2024, 6 months ago
Updated 18 February 2024, 4 months ago

Problem/Motivation

The description says the title attribute "shows native to most browsers, and can be nicely styled with a host of 3th party JS plugins like tippy.js ."... I would really like to use tippy.js in my theme, but am at a bit of a loss of how to do properly.

Proposed resolution

It would be really great if anyone who has done this successfully could share their code/commentary.

πŸ’¬ Support request
Status

Active

Version

3.0

Component

Documentation

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @jacobupal
  • I think I worked it out.

    1. I copied these lines from the webforms module to my theme's .libraries.yml file - I don't think you need the license, version and cdn stuff but I left it in:

    libraries.popperjs:
      remote: https://github.com/floating-ui/floating-ui
      version: '2.11.6'
      license:
        name: MIT
        url: https://github.com/floating-ui/floating-ui/blob/v2.x/LICENSE.md
        gpl-compatible: true
      directory: popperjs
      cdn:
        /libraries/popperjs/dist/umd/: https://unpkg.com/@popperjs/core@2.11.6/dist/umd/
      js:
        /libraries/popperjs/dist/umd/popper.js: {  weight: -20  }
    
    libraries.tippyjs:
      remote: https://github.com/atomiks/tippyjs
      version: '6.3.7'
      license:
        name: MIT
        url: https://github.com/atomiks/tippyjs/blob/master/LICENSE
        gpl-compatible: true
      directory: tippyjs
      cdn:
        /libraries/tippyjs/dist/: https://unpkg.com/tippy.js@6.3.7/dist/
      css:
        component:
          /libraries/tippyjs/dist/tippy.css: { weight: -20 }
      js:
        /libraries/tippyjs/dist/tippy.umd.js: {  weight: -20 }
      dependencies:
        - MY_THEME/libraries.popperjs
    

    2. Then I overwrote the glossify-tooltip.html.twig template in my theme thus:

    <abbr tabindex="0" {% if tip %}aria-describedby="{{ word|clean_id }}-definition"{% endif %} class="glossify-tooltip-tip despace-parent glossary-word" data-glossary-id="{{ word|clean_id }}">{{ word }}</abbr>
    <span id="{{ word|clean_id }}-definition" class="glossary-definition" style="display: none;">
      <dfn><a href="{{ tipurl }}">{{ word }}</a>: </dfn>
      <span>{{ tip }}</span>
    </span>
    

    3. Then I added the following to the JS folder of my theme.

    (function (Drupal) {
      'use strict';
    
      /**
       * Initialize tooltip element support.
       *
       * @type {Drupal~behavior}
       */
      // Check if Drupal and Drupal.behaviors are defined
      if (typeof Drupal !== 'undefined' && typeof Drupal.behaviors !== 'undefined') {
        Drupal.behaviors.glossifyTippyTooltip = {
          attach: function (context, settings) {
            // Initialize Tippy.js
            tippy('.glossary-word', {
              content: function (reference) {
                var id = reference.getAttribute('data-glossary-id');
                return document.getElementById(id + '-definition').innerHTML;
              },    
          allowHTML: true,
          interactive: true,
          trigger: 'focus click'
            });
          }
        };
      }
    })(Drupal, tippy);
    
    document.querySelectorAll('.glossary-word').forEach(function (word) {
      var tooltip = document.getElementById(word.getAttribute('data-glossary-id' + '-definition'));
      // Initialize Tippy.js
      tippy(word, {
        content: tooltip.innerHTML,
        allowHTML: true,
        interactive: true,
        trigger: 'focus click'
      });
    });
    

    4. Added my new JS as a library

    glossify-tippy:
      js:
        js/glossify-tippy.js: { weight: -10 }
      dependencies:
        - MY_THEME/libraries.tippyjs
    

    5. Added all three libraries to my theme's .info.yml file

    libraries:
      - MY_THEME/libraries.popperjs
      - MY_THEME/libraries.tippyjs
      - MY_THEME/glossify-tippy
    
  • πŸ‡ΊπŸ‡ΈUnited States W01F

    This would be fantastic as a submodule... just saying =)

  • Thanks for the suggestion/compliment!

    I can look into doing that in the next couple of months - my current version is a mishmash of the code above and things that only make sense in my current project.

Production build 0.69.0 2024