<html>

<head>
    <title>Tippy</title>
    <link rel="stylesheet" href="index.css">
</head>

<body>
    <div id="mainContainer">
        <div id="tooltipContainer"></div>
        <div class="sectionContainer">
            <p><i>Hover</i> over the diamond below to see the tooltip.</p>
            <div id="diamond"></div>
        </div>
    <script src="https://unpkg.com/@popperjs/core@2"></script>
    <script src="https://unpkg.com/tippy.js@6"></script>
    <script src="index.js"></script>
</body>

</html>
/* common styles */

body {
    display: grid;
    place-items: center;
    font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;
}

#mainContainer {
    width: 50vw;
    left: 50%;
    top: 50%;
}

div {
    display: grid;
    place-items: center;
}

/* 1st section */

#diamond {
    transform: rotate(45deg);
    background-color: #abcbca;
    width: 40px;
    height: 40px;
}

/* 2nd section */

#textContainer {
    padding: 24px;
    border-radius: 4px;
    display: grid;
    place-items: center;
    background-color: #abcbca;
}

#textContainer>p {
    color: white;
    letter-spacing: 2px;
    font-size: 16px;
}

p::selection {
    background-color: gainsboro;
    color: black;
}

#tooltipContainer {
    position: absolute;
}
// for the 1st section

tippy('#diamond', {
    content: 'Awesome!',
    placement: 'right-end'
});

// for the 2nd section

const [instance] = tippy('#tooltipContainer', {
    content: 'tooltip',
    sticky: true
})

const tooltipContainer = document.getElementById("tooltipContainer");

document.getElementById("textContainer").addEventListener("mouseup", () => {
    let selection = document.getSelection();
    if (!selection.isCollapsed) {
        const {
            left,
            top,
            width,
            height
        } = selection.getRangeAt(0).getBoundingClientRect()

        tooltipContainer.style.left = `${left}px`
        tooltipContainer.style.top = `${top}px`
        tooltipContainer.style.width = `${width}px`
        tooltipContainer.style.height = `${height}px`

        const numLines = selection.toString().length;
        instance.setContent(`Great Job! You selected ${numLines} charcters!`);
        instance.enable()
    }
});

document.addEventListener('mousedown', (event) => {
    console.log("hiding")
    tooltipContainer.style.width = `${0}px`
    tooltipContainer.style.height = `${0}px`
    instance.disable()
})

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.