function supportsTouchEvents() { const onTouchStartIsDefined = typeof window !== 'undefined' && window.ontouchstart !== undefined; const maxTouchPointsIsDefined = typeof navigator !== 'undefined' && navigator.maxTouchPoints; return !!(onTouchStartIsDefined || maxTouchPointsIsDefined); } function userAgentSuggestsTouchDevice() { const sampleTouchDevices = [ 'android', 'iemobile', 'iphone', 'ipad', 'ipod', 'blackberry', 'bada', ]; const matchString = sampleTouchDevices.map((device) => `(${device})`).join('|'); const regex = new RegExp(matchString, 'gi'); return typeof navigator !== 'undefined' && !!regex.test(navigator.userAgent); } // Important: this is not fool-proof! It gives false positives and negatives, and will be outdated. // Only use this for small vanity changes where it still works either way. export function isTouchDevice() { return supportsTouchEvents() || userAgentSuggestsTouchDevice(); }