3

I wanted to have close button for every tooltip how can I have that?

Note: I don't want to mess up with design , that is why did not try much , Best solution I'm looking here

Below is demo:

tippy('#t1,#t2,#t3,#t4',{
        content: "Error Message",
        delay: 100,
        arrow: true,
        arrowType: 'round',
        size: 'large',
        duration: 500,
        animation: 'scale',
        trigger:'manual',
        placement:'bottom',
        hideOnClick:false,
 });

var settings = [{id:"#t1",pos:"top"},{id:"#t2",pos:"bottom"}, 
                {id:"#t3",pos:"left"},{id:"#t4",pos:"right"}];

settings.forEach(function(sett){
    var tip = document.querySelector(sett.id);
    tippy(tip);
    tip._tippy.set({content:"click x",placement:sett.pos});
   tip._tippy.show();
});
div.tippy-dummy{
    position: relative;
    width: 220px;
    height: 30px;
    background: #333;
    color: #fff;
}

div.tippy-dummy span{
    position: absolute;
    display: inline-block;
    background: #715a5a;
    right: 0;
    top: 0;
}

.tooltip{
      display:flex;
      justify-content:space-between;
      width: 90%;
}
.btn{
   width: 90px;
      height: 30px;
   border: 2px solid #ccc;
   border-radius: 6px;
}
.btn:hover{
    cursor: pointer;
  background:#f05a27;
}    
.tooltip{
     margin-top:80px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/tippy.js/3.1.3/tippy.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tippy.js/3.1.3/tippy.min.js"></script>
<h4>Expected output:</h4>
<div class="tippy-dummy">
   Please click X to hide
   <span>X</span>
</div>


<hr/>


<div class="tooltip">
     <button id="t1" class="btn tippy" title="">top</button>
     <button id="t2" class="btn tippy" title="">bottom</button>
     <button id="t3" class="btn tippy" title="">Left</button>
     <button id="t4" class="btn tippy" title="">Right</button>
    </div>

for better view here is codepen: https://codepen.io/anon/pen/aPXKZM

Please help me thanks in advance!!

EaBengaluru
  • 131
  • 2
  • 17
  • 59

2 Answers2

6

It's actually even easier than Gifford N.'s answer...

<button data-tippy-content="&lt;span class=&quot;closeme&quot;&gt;&amp;times;&lt;/span&gt;There are many like it but this one is mine!!!!!">
  This is my button
</button>
const tippyInstance = tippy('[data-tippy-content]', {
  allowHTML: true,
  interactive: true,
  onShow(instance) {
    instance.popper.querySelector('.closeme').addEventListener('click', () => {
      instance.hide();
    });
  },
  onHide(instance) {
    instance.popper.querySelector('.closeme').removeEventListener('click', () => {
      instance.hide();
    });
  },
});
the-sides
  • 3
  • 1
  • 3
cfx
  • 3,311
  • 2
  • 35
  • 45
2

The action you are looking for is .hide().

To use this action you have to make a couple adjustments to your code:

  1. Add interactive: true to tippy settings.
tippy('#t1,#t2,#t3,#t4',{
   ...
   interactive: true,
   ...
});`
  1. Assign the .hide(); action to the popper property in your settings.forEach function:
settings.forEach(function(sett){
  ...
  tip._tippy.popper.onclick = function() {
    tip._tippy.hide();
  }
});

~

PROTIP:

If you want to hide the popper when clicking a specific element within the popper (X, for example) it just takes a little more markup:

  1. Add some markup to the desired element <button class=\"hide\">X</button>

  2. Use the element in the onclick:

settings.forEach(function(sett){
  ...
var closeBtn = tip._tippy.popper.getElementsByClassName('hide')[0];
closeBtn.onclick = function() {
  tip._tippy.hide();
}

Updated Codepen: https://codepen.io/gnowland/pen/wvBzjym?editors=0010

Gifford N.
  • 412
  • 5
  • 13