UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

181 lines 6.67 kB
import _defineProperty from "@babel/runtime-corejs3/helpers/esm/defineProperty"; import _pushInstanceProperty from "core-js-pure/stable/instance/push.js"; import React from 'react'; import keycode from "../keycode.js"; import { warn, PLATFORM_MAC, PLATFORM_WIN, PLATFORM_LINUX } from "../helpers.js"; export function isTouchDevice() { if (typeof document !== 'undefined') { let intent = false; try { intent = document.documentElement.getAttribute('data-whatintent'); } catch (e) {} return intent === 'touch'; } return false; } export function defineNavigator() { const handleNavigator = () => { if (typeof document === 'undefined' || typeof window === 'undefined' || typeof navigator === 'undefined') { return; } try { if (!(typeof window !== 'undefined' && window.IS_TEST)) { if (navigator.platform.match(new RegExp(PLATFORM_MAC)) !== null) { document.documentElement.setAttribute('data-os', 'mac'); } else if (navigator.platform.match(new RegExp(PLATFORM_WIN)) !== null) { document.documentElement.setAttribute('data-os', 'win'); } else if (navigator.platform.match(new RegExp(PLATFORM_LINUX)) !== null) { document.documentElement.setAttribute('data-os', 'linux'); } } else { document.documentElement.setAttribute('data-os', 'other'); } } catch (e) { warn(e); } document.removeEventListener('DOMContentLoaded', handleNavigator); }; if (typeof document !== 'undefined' && document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', handleNavigator); } else { handleNavigator(); } } export const processChildren = props => { if (!props) { return null; } if (typeof global !== 'undefined' && Array.isArray(global.registeredElements) && global.registeredElements.length > 0) { let cache = null; Object.entries(props).reverse().map(([key, cb]) => { if (key.includes('render_') && /^render_/.test(key)) { if (typeof cb === 'function') { if (cache) { if (Object.isFrozen(props)) { props = { ...props }; } props.children = cache; } return cache = React.createElement(React.Fragment, { key: key }, cb(props)); } } return null; }).filter(Boolean); if (cache) { return cache; } } const res = typeof props.children === 'function' ? props.children(props) : props.children; if (Array.isArray(res)) { const onlyTexts = res.reduce((pV, cV) => { if (typeof cV === 'string' || typeof cV === 'number') { _pushInstanceProperty(pV).call(pV, cV); } return pV; }, []); if (onlyTexts.length === res.length && onlyTexts.length > 0) { return onlyTexts.join(''); } } return res; }; export const detectOutsideClick = (ignoreElements, onSuccess, options) => new DetectOutsideClickClass(ignoreElements, onSuccess, options); export class DetectOutsideClickClass { constructor(_ignoreElements, _onSuccess, options = {}) { _defineProperty(this, "checkOutsideClick", ({ event, ignoreElements }, onSuccess = null) => { try { const currentElement = event.target; if ((currentElement === null || currentElement === void 0 ? void 0 : currentElement.tagName) === 'HTML' && (event.pageX > document.documentElement.clientWidth - 40 || event.pageY > document.documentElement.clientHeight - 40)) { return; } if (checkIfHasScrollbar(currentElement)) { return; } for (let i = 0, elem, l = ignoreElements.length; i < l; ++i) { const ignoreElement = ignoreElements[i] && 'current' in ignoreElements[i] ? ignoreElements[i].current : ignoreElements[i]; elem = currentElement; if (!ignoreElements[i]) { continue; } do { if (elem === ignoreElement) { return; } elem = elem && elem.parentNode; } while (elem); } if (typeof onSuccess === 'function') { onSuccess(); } } catch (e) { warn(e); } }); if (!this.handleClickOutside && typeof document !== 'undefined' && typeof window !== 'undefined') { if (!Array.isArray(_ignoreElements)) { _ignoreElements = [_ignoreElements]; } this.handleClickOutside = event => { this.checkOutsideClick({ event, ignoreElements: _ignoreElements }, () => typeof _onSuccess === 'function' && _onSuccess({ event })); }; document.addEventListener('mousedown', this.handleClickOutside); this.keydownCallback = event => { const keyCode = keycode(event); if (keyCode === 'esc') { window.removeEventListener('keydown', this.keydownCallback); if (typeof _onSuccess === 'function') { _onSuccess({ event }); } } }; window.addEventListener('keydown', this.keydownCallback); if (options.includedKeys) { this.keyupCallback = event => { const keyCode = keycode(event); if (options.includedKeys.includes(keyCode) && typeof this.handleClickOutside === 'function') { this.handleClickOutside(event, () => { if (this.keyupCallback) window.removeEventListener('keyup', this.keyupCallback); }); } }; window.addEventListener('keyup', this.keyupCallback); } } } remove() { if (this.handleClickOutside && typeof document !== 'undefined') { document.removeEventListener('mousedown', this.handleClickOutside); this.handleClickOutside = null; } if (this.keydownCallback && typeof window !== 'undefined') { window.removeEventListener('keydown', this.keydownCallback); this.keydownCallback = null; } if (this.keyupCallback && typeof window !== 'undefined') { window.removeEventListener('keyup', this.keyupCallback); this.keyupCallback = null; } } } export const checkIfHasScrollbar = elem => { return elem && (elem.scrollHeight > elem.offsetHeight || elem.scrollWidth > elem.offsetWidth) && overflowIsScrollable(elem); }; const overflowIsScrollable = elem => { const style = typeof window !== 'undefined' ? window.getComputedStyle(elem) : {}; return /scroll|auto/i.test((style.overflow || '') + (style.overflowX || '') + (style.overflowY || '')); }; //# sourceMappingURL=component-helper-legacy.js.map