UNPKG

js.foresight

Version:

Predicts mouse trajectory to trigger actions as users approach elements, enabling anticipatory UI updates or pre-loading. Made with vanilla javascript and usable in every framework.

31 lines 1.21 kB
export function evaluateRegistrationConditions() { var isTouchDevice = userUsesTouchDevice(); var isLimitedConnection = hasConnectionLimitations(); var shouldRegister = !isTouchDevice && !isLimitedConnection; return { isTouchDevice: isTouchDevice, isLimitedConnection: isLimitedConnection, shouldRegister: shouldRegister }; } /** * Detects if the current device is likely a touch-enabled device. * It checks for coarse pointer media query and the presence of touch points. * * @returns `true` if the device is likely touch-enabled, `false` otherwise. */ function userUsesTouchDevice() { return window.matchMedia("(pointer: coarse)").matches && navigator.maxTouchPoints > 0; } /** * Checks if the user has connection limitations (slow network or data saver enabled). * * @returns {boolean} True if connection is limited, false if safe to prefetch * @example * if (!hasConnectionLimitations()) { * prefetchResource('/api/data'); * } */ function hasConnectionLimitations() { var connection = navigator.connection; if (!connection) return false; return /2g/.test(connection.effectiveType) || connection.saveData; } //# sourceMappingURL=shouldRegister.js.map