UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

131 lines 3.93 kB
let currentInput = 'initial'; let currentIntent = 'initial'; let currentTimestamp = Date.now(); const ignoreMap = [16, 17, 18, 91, 93]; let specificMap = []; let isScrolling = false; const mousePos = { x: 0, y: 0 }; const formInputs = ['input', 'select', 'textarea']; let isSetUp = false; function setUp() { if (isSetUp) { return; } isSetUp = true; const passiveCapture = { passive: true, capture: true }; if (window.PointerEvent) { window.addEventListener('pointerdown', setInput, true); window.addEventListener('pointermove', setIntent, passiveCapture); } else { window.addEventListener('mousedown', setInput, true); window.addEventListener('mousemove', setIntent, passiveCapture); if ('ontouchstart' in window) { window.addEventListener('touchstart', setInput, passiveCapture); window.addEventListener('touchend', setInput, true); } } window.addEventListener('wheel', setIntent, passiveCapture); window.addEventListener('keydown', setInput, true); window.addEventListener('keyup', setInput, true); doUpdate('input'); doUpdate('intent'); } function resolvePointerType(event) { if (event.pointerType === 'pen' || event.pointerType === 'touch') { return 'touch'; } return 'mouse'; } function resolveInputType(event) { const type = event.type; if (type === 'keydown' || type === 'keyup') { return 'keyboard'; } if (type === 'mousedown') { return 'mouse'; } if (type === 'touchstart' || type === 'touchend') { return 'touch'; } if (type === 'pointerdown') { return resolvePointerType(event); } if (type === 'pointermove') { return resolvePointerType(event); } if (type === 'mousemove' || type === 'wheel') { return 'mouse'; } return 'mouse'; } function validateTouch(value) { const now = Date.now(); const isFalsePositive = value === 'mouse' && currentInput === 'touch' && now - currentTimestamp < 200; currentTimestamp = now; return isFalsePositive; } function setInput(event) { const value = resolveInputType(event); const eventKey = event.which; if (value === 'keyboard' && eventKey) { const ignoreMatch = specificMap.length === 0 && !ignoreMap.includes(eventKey); const specificMatch = specificMap.length > 0 && specificMap.includes(eventKey); if (!ignoreMatch && !specificMatch) { return; } } if (validateTouch(value)) { return; } if (currentInput !== value) { currentInput = value; doUpdate('input'); } if (currentIntent !== value) { const activeElem = document.activeElement; if (activeElem !== null && activeElem !== void 0 && activeElem.nodeName && (!formInputs.includes(activeElem.nodeName.toLowerCase()) || activeElem.nodeName.toLowerCase() === 'button' && !activeElem.closest('form'))) { currentIntent = value; doUpdate('intent'); } } } function detectScrolling(event) { if (mousePos.x !== event.screenX || mousePos.y !== event.screenY) { isScrolling = false; mousePos.x = event.screenX; mousePos.y = event.screenY; } else { isScrolling = true; } } function setIntent(event) { const value = resolveInputType(event); if (event.type === 'pointermove' || event.type === 'mousemove') { detectScrolling(event); } const isWheelEvent = event.type === 'wheel' || event.type === 'mousewheel' || event.type === 'DOMMouseScroll'; if ((!isScrolling && !validateTouch(value) || isScrolling && isWheelEvent) && currentIntent !== value) { currentIntent = value; doUpdate('intent'); } } function doUpdate(which) { document.documentElement.setAttribute(`data-what${which}`, which === 'input' ? currentInput : currentIntent); } function specificKeys(arr) { specificMap = arr; } if (typeof document !== 'undefined' && typeof window !== 'undefined') { setUp(); } const whatInput = { specificKeys }; export default whatInput; //# sourceMappingURL=whatInput.js.map