UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

156 lines 5.66 kB
import { warn, PLATFORM_MAC, PLATFORM_WIN, PLATFORM_LINUX } from "../helpers.js"; export function isTouchDevice() { if (typeof document !== 'undefined') { let intent = null; 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)) { const platform = navigator.userAgentData?.platform || navigator.platform; if (platform.match(new RegExp(PLATFORM_MAC)) !== null) { document.documentElement.setAttribute('data-os', 'mac'); } else if (platform.match(new RegExp(PLATFORM_WIN)) !== null) { document.documentElement.setAttribute('data-os', 'win'); } else if (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; } 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') { pV.push(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 { handleClickOutside = null; keydownCallback = null; keyupCallback = null; constructor(ignoreElementsInput, onSuccess, options = {}) { const ignoreElements = Array.isArray(ignoreElementsInput) ? ignoreElementsInput : [ignoreElementsInput]; if (!this.handleClickOutside && typeof document !== 'undefined' && typeof window !== 'undefined') { this.handleClickOutside = event => { this.checkOutsideClick({ event, ignoreElements }, () => typeof onSuccess === 'function' && onSuccess({ event })); }; document.addEventListener('mousedown', this.handleClickOutside); this.keydownCallback = event => { if (event.key === 'Escape') { window.removeEventListener('keydown', this.keydownCallback); if (typeof onSuccess === 'function') { onSuccess({ event }); } } }; window.addEventListener('keydown', this.keydownCallback); if (options.includedKeys) { this.keyupCallback = event => { if (options.includedKeys.includes(event.key) && 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; } } checkOutsideClick = ({ event, ignoreElements }, onSuccess = null) => { try { const currentElement = event.target; if (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] && ignoreElements[i] !== null && '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); } }; } export const checkIfHasScrollbar = elem => { return elem && (elem.scrollHeight > elem.offsetHeight || elem.scrollWidth > elem.offsetWidth) && overflowIsScrollable(elem); }; const overflowIsScrollable = elem => { if (typeof window === 'undefined') { return false; } const style = window.getComputedStyle(elem); return /scroll|auto/i.test((style.overflow || '') + (style.overflowX || '') + (style.overflowY || '')); }; //# sourceMappingURL=component-helper-legacy.js.map