Skip to content

Jitsi Meet - Secure, Simple and Scalable Video Conferences that you use as a standalone app or embed in your web application.

5.7 KB
Raw Blame
const getLeftAlignedStyle = (bounds: DOMRect) => {
    return {
        position: 'fixed',
        right: `${window.innerWidth - bounds.x}px`
    };
};

const getRightAlignedStyle = (bounds: DOMRect) => {
    return {
        position: 'fixed',
        left: `${bounds.x + bounds.width}px`
    };
};

const getTopAlignedStyle = (bounds: DOMRect) => {
    return {
        position: 'fixed',
        bottom: `${window.innerHeight - bounds.y}px`
    };
};

const getBottomAlignedStyle = (bounds: DOMRect) => {
    return {
        position: 'fixed',
        top: `${bounds.y + bounds.height}px`
    };
};

const getLeftRightStartAlign = (bounds: DOMRect, size: DOMRectReadOnly) => {
    return {
        top: `${Math.min(bounds.y, window.innerHeight - size.height - 20)}px`
    };
};

const getLeftRightMidAlign = (bounds: DOMRect, size: DOMRectReadOnly) => {
    return {
        bottom: `${window.innerHeight - bounds.y - (bounds.height / 2) - (size.height / 2)}px`
    };
};

const getLeftRightEndAlign = (bounds: DOMRect, size: DOMRectReadOnly) => {
    return {
        bottom: `${Math.min(window.innerHeight - bounds.y - bounds.height, window.innerHeight - size.height)}px`
    };
};

const getTopBotStartAlign = (bounds: DOMRect) => {
    return {
        right: `${window.innerWidth - bounds.x - 6}px`
    };
};

const getTopBotMidAlign = (bounds: DOMRect, size: DOMRectReadOnly) => {
    return {
        right: `${window.innerWidth - bounds.x - (bounds.width / 2) - (size.width / 2)}px`
    };
};

const getTopBotEndAlign = (bounds: DOMRect) => {
    return {
        left: `${bounds.x + bounds.width - 6}px`
    };
};

/**
 * Gets the trigger element's and the context menu's bounds/size info and
 * computes the style to apply to the context menu to positioning it correctly
 * in regards to the given position info.
 *
 * @param {DOMRect} triggerBounds -The bounds info of the trigger html element.
 * @param {DOMRectReadOnly} dialogSize - The size info of the context menu.
 * @param {string} position - The position of the context menu in regards to the trigger element.
 *
 * @returns {Object} = The style to apply to context menu for positioning it correctly.
 */
export const getContextMenuStyle = (triggerBounds: DOMRect,
        dialogSize: DOMRectReadOnly,
        position: string) => {
    const parsed = position.split('-');

    switch (parsed[0]) {
    case 'top': {
        let alignmentStyle = {};

        if (parsed[1]) {
            alignmentStyle = parsed[1] === 'start'
                ? getTopBotStartAlign(triggerBounds)
                : getTopBotEndAlign(triggerBounds);
        } else {
            alignmentStyle = getTopBotMidAlign(triggerBounds, dialogSize);
        }

        return {
            ...getTopAlignedStyle(triggerBounds),
            ...alignmentStyle
        };
    }
    case 'bottom': {
        let alignmentStyle = {};

        if (parsed[1]) {
            alignmentStyle = parsed[1] === 'start'
                ? getTopBotStartAlign(triggerBounds)
                : getTopBotEndAlign(triggerBounds);
        } else {
            alignmentStyle = getTopBotMidAlign(triggerBounds, dialogSize);
        }

        return {
            ...getBottomAlignedStyle(triggerBounds),
            ...alignmentStyle
        };
    }
    case 'left': {
        let alignmentStyle = {};

        if (parsed[1]) {
            alignmentStyle = parsed[1] === 'start'
                ? getLeftRightStartAlign(triggerBounds, dialogSize)
                : getLeftRightEndAlign(triggerBounds, dialogSize);
        } else {
            alignmentStyle = getLeftRightMidAlign(triggerBounds, dialogSize);
        }

        return {
            ...getLeftAlignedStyle(triggerBounds),
            ...alignmentStyle
        };
    }
    case 'right': {
        let alignmentStyle = {};

        if (parsed[1]) {
            alignmentStyle = parsed[1] === 'start'
                ? getLeftRightStartAlign(triggerBounds, dialogSize)
                : getLeftRightEndAlign(triggerBounds, dialogSize);
        } else {
            alignmentStyle = getLeftRightMidAlign(triggerBounds, dialogSize);
        }

        return {
            ...getRightAlignedStyle(triggerBounds),
            ...alignmentStyle
        };
    }
    default: {
        return {
            ...getLeftAlignedStyle(triggerBounds),
            ...getLeftRightEndAlign(triggerBounds, dialogSize)
        };
    }
    }
};

/**
 * Maps the portal element of an open popover to the element which triggers it.
 */
const popoverTriggers = new WeakMap<Element, HTMLElement>();

/**
 * Stores (or clears, if no trigger is given) the trigger of the popover rendered in the given portal element.
 *
 * @param {Element} portal - The element the popover content is rendered in.
 * @param {HTMLElement} trigger - The element which triggers the popover.
 * @returns {void}
 */
export function setPopoverTrigger(portal: Element, trigger?: HTMLElement | null) {
    if (trigger) {
        popoverTriggers.set(portal, trigger);
    } else {
        popoverTriggers.delete(portal);
    }
}

/**
 * Finds the focusable trigger of the popover which contains the given element.
 *
 * @param {Element} element - An element inside a popover.
 * @returns {HTMLElement|undefined} - The element to focus in order to land back on the popover trigger.
 */
export function getPopoverTrigger(element?: Element | null): HTMLElement | undefined {
    for (let current = element; current; current = current.parentElement) {
        const trigger = popoverTriggers.get(current);

        if (trigger) {
            return trigger.matches('[tabindex]:not([tabindex="-1"]), button')
                ? trigger
                : trigger.querySelector<HTMLElement>('[tabindex]:not([tabindex="-1"]), button') ?? undefined;
        }
    }
}