UNPKG

@botonic/react

Version:

Build Chatbots using React

55 lines 2.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isShadowDOMSupported = exports.onDOMLoaded = exports.getWebchatElement = exports.scrollToBottom = exports.getScrollableArea = exports.getScrollableContent = void 0; const constants_1 = require("../constants"); const getScrollableContent = webchatElement => { return webchatElement.querySelector(constants_1.WEBCHAT.SELECTORS.SCROLLABLE_CONTENT); }; exports.getScrollableContent = getScrollableContent; const getScrollableArea = webchatElement => { const getArea = area => { const botonicScrollableContent = (0, exports.getScrollableContent)(webchatElement); const scrollableArea = botonicScrollableContent && botonicScrollableContent.querySelector(area); return scrollableArea; }; return { full: getArea(constants_1.WEBCHAT.SELECTORS.SIMPLEBAR_CONTENT), visible: getArea(constants_1.WEBCHAT.SELECTORS.SIMPLEBAR_WRAPPER), }; }; exports.getScrollableArea = getScrollableArea; const scrollToBottom = ({ timeout = 200, behavior = 'smooth', host, } = {}) => { const webchatElement = (0, exports.getWebchatElement)(host); if (!webchatElement) return; const frame = (0, exports.getScrollableArea)(webchatElement).visible; if (frame) { setTimeout(() => frame.scrollTo({ top: frame.scrollHeight, behavior: behavior }), timeout); } }; exports.scrollToBottom = scrollToBottom; const getWebchatElement = host => host && host.querySelector(`#${constants_1.WEBCHAT.DEFAULTS.ID}`); exports.getWebchatElement = getWebchatElement; // https://stackoverflow.com/questions/9457891/how-to-detect-if-domcontentloaded-was-fired const onDOMLoaded = callback => { if (/complete|interactive|loaded/.test(document.readyState)) { // In case the document has finished parsing, document's readyState will // be one of "complete", "interactive" or (non-standard) "loaded". callback(); } else { // The document is not ready yet, so wait for the DOMContentLoaded event document.addEventListener('DOMContentLoaded', callback, false); } }; exports.onDOMLoaded = onDOMLoaded; const isShadowDOMSupported = () => { try { return document.head.createShadowRoot || document.head.attachShadow; } catch (e) { return false; } }; exports.isShadowDOMSupported = isShadowDOMSupported; //# sourceMappingURL=dom.js.map