All files / src/components/tool-tip ToolTip.ts

27.27% Statements 9/33
0% Branches 0/12
20% Functions 2/10
28.13% Lines 9/32

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 1381x 1x 1x   1x   1x   1x       1x         1x                                                                                                                                                                                                                                               1x
import CustomElement from "../../custom-element/CustomElement";
import applyClasses from "../../custom-element/helpers/css/class/applyClasses";
import defineCustomElement from "../../custom-element/helpers/defineCustomElement";
import { CustomElementPropertyMetadata } from "../../custom-element/interfaces";
import html  from "../../renderer/html";
import { NodePatchingData } from "../../renderer/NodePatcher";
import styles from "./ToolTip.css";
 
export default class ToolTip extends CustomElement {
 
    static get styles(): string {
 
        return styles as any;
    }
 
    static get properties(): Record<string, CustomElementPropertyMetadata> {
 
        return {
 
            /**
             * The position of the tool tip
             */
            position: {
                type: String,
                value: 'bottom',
                options: ['top', 'bottom', 'left', 'right']
            }
        };
    }
 
    render(): NodePatchingData {
 
        return html`<div class="container">
            <span id="trigger">
                <slot name="trigger"></slot>
            </span>       
            <span id="content">
                <slot name="content"></slot>
            </span>
        </div>`;
    }
 
    connectedCallback(): void {
        
        super.connectedCallback?.();
 
        // TODO: Create a global (singleton) resize manager and subscribe to it
        // Also re-position when scroll has occurred
        window.addEventListener('resize', () => this.handleResize());
    }
 
    didMountCallback() {
 
        this._positionContent();
    }
 
    didUpdateCallback() {
 
        this._positionContent();
    }
 
    handleResize() {
 
        this._positionContent();
    }
 
    /**
     * Positions the content since we don't know its dimensions until it is rendered in the DOM
     */
    private _positionContent() {
 
        const trigger = this.document.getElementById("trigger");
 
        const content = this.document.getElementById("content");
 
        const p = this.getFittingPosition(trigger, content, this.position); // Get the position from the trigger
        
        applyClasses(content, { // Keep or update the position of the content
            'top': p === 'top',
            'bottom': p === 'bottom',
            'left': p === 'left',
            'right': p === 'right',
        });
    }
 
    /**
     * Returns the requested position if there is enough room for that
     * @param trigger 
     * @param pos 
     * @returns 
     */
    getFittingPosition(trigger: HTMLElement, content: HTMLElement, pos: string) {
 
        const {
            clientWidth,
            clientHeight
        } = document.documentElement;
 
        const {
            x: triggerX,
            y: triggerY,
            height: triggerHeight,
            width: triggerWidth
        } = trigger.getBoundingClientRect();
 
        const {
            height: contentHeight,
            width: contentWidth
        } = content.getBoundingClientRect();
 
        switch (pos) {
            case 'top':
                {
                    pos = triggerY < triggerHeight ? 'bottom' : 'top';
                }
                break;
            case 'bottom':
                {
                    pos = triggerY + triggerHeight + contentHeight> clientHeight ? 'top' : 'bottom';
                }
                break;
            case 'left':
                {
                    pos = triggerX < triggerWidth ? 'right' : 'left';
                }
                break;
            case 'right':
                {
                    pos = triggerX + triggerWidth + contentWidth > clientWidth ? 'left' : 'right';
                }
                break;
        }
 
        return pos;
    }
}
 
defineCustomElement('gcl-tool-tip', ToolTip);