{"id":19533962,"url":"https://github.com/rafgraph/detect-touch-events","last_synced_at":"2025-04-26T14:34:35.714Z","repository":{"id":60775047,"uuid":"62693110","full_name":"rafgraph/detect-touch-events","owner":"rafgraph","description":"Detect if the browser has a touch screen and supports the Touch Events API","archived":false,"fork":false,"pushed_at":"2020-09-12T00:48:09.000Z","size":44,"stargazers_count":8,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-13T21:06:20.361Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://detect-it.rafgraph.dev#detect-touch-events","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rafgraph.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-07-06T05:16:48.000Z","updated_at":"2023-03-09T01:40:39.000Z","dependencies_parsed_at":"2022-10-04T16:16:34.679Z","dependency_job_id":null,"html_url":"https://github.com/rafgraph/detect-touch-events","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rafgraph%2Fdetect-touch-events","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rafgraph%2Fdetect-touch-events/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rafgraph%2Fdetect-touch-events/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rafgraph%2Fdetect-touch-events/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rafgraph","download_url":"https://codeload.github.com/rafgraph/detect-touch-events/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251001271,"owners_count":21520918,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-11T02:11:31.762Z","updated_at":"2025-04-26T14:34:35.396Z","avatar_url":"https://github.com/rafgraph.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Detect Touch Events\n\nDetect if the browser supports the Touch Events API.\n\n[Live detection test][liveDetectionTest]\n\nExports a reference to a singleton object (a micro state machine with an update function) with its state set to if the device supports the Touch Events API, as well as an `update()` function which re-runs the tests and updates the object's state.\n\nNote that `detect-touch-events` is one of the micro state machines used by [`detect-it`][detectItRepo] to determine if a device is `mouseOnly`, `touchOnly`, or `hybrid`.\n\n*For more information on the touch events api, please see [MDN's Touch Events][mdnTouchEvents], or the [W3C Touch Events specification][w3cSpecLatest].*\n\n\n### `detectTouchEvents` micro state machine\n```javascript\nconst detectTouchEvents = {\n  hasSupport: true / false,\n  browserSupportsApi: true / false,\n\n  // re-run all the detection tests and update state\n  update() {...},\n}\n```\n\n### Installing `detect-touch-events`\n```terminal\n$ npm install --save detect-touch-events\n```\n\n### Using `detect-touch-events`\n```javascript\nimport detectTouchEvents from 'detect-touch-events';\n```\n```javascript\n// touch events api is present on a device with a touch screen - *most useful*\ndetectTouchEvents.hasSupport === true;\n\n// browser supports the touch events api, but the device may or may not have a touch screen\ndetectTouchEvents.browserSupportsApi === true;\n\n// updating the state - most apps won't need to use this at all\ndetectTouchEvents.update();\n```\n\n```javascript\n/*\n * note that in the case of a browser that doesn't support touch events,\n * including when using a legacy computer and browser, the default state will be:\n */\nconst detectTouchEvents = {\n  hasSupport: false,\n  browserSupportsApi: false,\n}\n```\n\nNote that `hasSupport` is still just an api presence test and some browsers (notably Firefox) may give a false positive (saying it has support on a device without a touch screen), so it is recommended to use [`detect-it`][detectItRepo] which factors in other tests as well to determine device capabilities.\n\nNote that the `update()` function is run once at the time of import to set the object's initial state, and generally doesn't need to be run again. If it doesn't have access to the `window`, then the state will be `undefined` (`detect-touch-events` will not throw an error), and you will need to call the `update()` function manually at a later time to update its state.\n\n\n### Part of the [`detect-it`][detectItRepo] family\n- [`detect-it`][detectItRepo]\n  - [`detect-hover`][detectHoverRepo]\n  - [`detect-pointer`][detectPointerRepo]\n  - **`detect-touch-events`**\n  - [`detect-passive-events`][detectPassiveEventsRepo]\n\n\n\u003c!-- links --\u003e\n[liveDetectionTest]: https://detect-it.rafgraph.dev/#detect-touch-events\n[w3cSpecLatest]: https://w3c.github.io/touch-events/\n[mdnTouchEvents]: https://developer.mozilla.org/en-US/docs/Web/API/Touch_events\n[detectItRepo]: https://github.com/rafgraph/detect-it\n[detectHoverRepo]: https://github.com/rafgraph/detect-hover\n[detectPointerRepo]: https://github.com/rafgraph/detect-pointer\n[detectPassiveEventsRepo]: https://github.com/rafgraph/detect-passive-events\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frafgraph%2Fdetect-touch-events","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frafgraph%2Fdetect-touch-events","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frafgraph%2Fdetect-touch-events/lists"}