npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

svelte-device-info

v1.0.6

Published

informs about a device's form factor and pointing accuracy

Downloads

6,416

Readme

svelte-device-info

informs about a device's form factor and pointing accuracy (not only in Svelte)

NPM users: please consider the Github README for the latest description of this package (as updating the docs would otherwise always require a new NPM package version)

Note: Microsoft Internet Explorer and Microsoft Edge (classic) are NOT supported

Just a small note: if you like this module and plan to use it, consider "starring" this repository (you will find the "Star" button on the top right of this page), so that I know which of my repositories to take most care of.

Installation

svelte-device-info may be used as an ECMAScript module (ESM), a CommonJS or AMD module or from a global variable.

You may either install the package into your build environment using NPM with the command

npm install svelte-device-info

or load the plain script file directly

<script src="https://unpkg.com/svelte-device-info"></script>

Access

How to access the package depends on the type of module you prefer

  • ESM (or Svelte): import Device from 'svelte-device-info'
  • CommonJS: const Device = require('svelte-device-info')
  • AMD: require(['svelte-device-info'], (Device) => {...})

Alternatively, you may access the global variable Device directly.

Note for ECMAScript module users: all module functions and values are exported individually, thus allowing your bundler to perform some "tree-shaking" in order to include actually used functions or values (together with their dependencies) only.

Usage within Svelte

For Svelte, it is recommended to import the package in a module context:

<script context="module">
  import Device from 'svelte-device-info'
</script>

<script>
  console.log('this device is ' + (Device.isMobile ? '' : 'not') + ' mobile')
  
  switch (true) {
    case Device.isPhone:  console.log('this device is a smartphone'); break
    case Device.isTablet: console.log('this device is a tablet');     break
    default:              console.log('this device is neither a smartphone nor a tablet')
  }
    
  console.log('the primary pointing device can' + (
    Device.canHover ? '' : 'not'
  ) + ' "hover" over elements')

  switch (Device.PointingAccuracy) {
    case 'none':   console.log('this device does not support any touch input'); break
    case 'fine':   console.log('this device has a high-resolution touch input'); break
    case 'coarse': console.log('this device has a low-resolution touch input')
  }
  
/**** convertibles may change their PointingAccuracy at any time! ****/
  
  let PointingAccuracyObserver = function (newAccuracy) {
    console.log('this device\'s PointingAccuracy is now "' + newAccuracy + '"')
  }
  
  Device.onPointingAccuracyChanged(PointingAccuracyObserver) // may run multiple times
  Device.offPointingAccuracyChanged(PointingAccuracyObserver)   // deregisters handler

  Device.oncePointingAccuracyChanged((newAccuracy) => {
    console.log('PointingAccuracy has changed to "' + newAccuracy + '"')
  })
</script>

Usage as ECMAscript, CommonJS or AMD Module (or as a global Variable)

Let's assume that you already "required" or "imported" (or simply loaded) the module according to your local environment. In that case, you may use it as follows:

  console.log('this device is ' + (Device.isMobile ? '' : 'not') + ' mobile')
  
  switch (true) {
    case Device.isPhone:  console.log('this device is a smartphone'); break
    case Device.isTablet: console.log('this device is a tablet');     break
    default:              console.log('this device is neither a smartphone nor a tablet')
  }
  
  console.log('the primary pointing device can' + (
    Device.canHover ? '' : 'not'
  ) + ' "hover" over elements')
  
  switch (Device.PointingAccuracy) {
    case 'none':   console.log('this device does not support any touch input'); break
    case 'fine':   console.log('this device has a high-resolution touch input'); break
    case 'coarse': console.log('this device has a low-resolution touch input')
  }
  
/**** convertibles may change their PointingAccuracy at any time! ****/
  
  let PointingAccuracyObserver = function (newAccuracy) {
    console.log('this device\'s PointingAccuracy is now "' + newAccuracy + '"')
  }
  
  Device.onPointingAccuracyChanged(PointingAccuracyObserver) // may run multiple times
  Device.offPointingAccuracyChanged(PointingAccuracyObserver)   // deregisters handler

  Device.oncePointingAccuracyChanged((newAccuracy) => {
    console.log('PointingAccuracy has changed to "' + newAccuracy + '"')
  })

Examples

All examples are available on the Svelte REPL - feel free to play with them!

Background Information

This package determines a few often needed details of the underlying device running a given JavaScript application:

  • is the device a mobile one? a smartphone? a tablet?
  • is the device's primary input device a touch pad or touch screen? does that input device have a low or a high resolution?

In addition, the package informs the application about any change in touch input resolution (which is important for "convertibles" that may switch between notebook and tablet mode)

The package's finding may either be retrieved using JavaScript or by styling a few CSS classes which are added to or removed from the document body depending on the current PointingAccuracy.

JavaScript API

This package offers a JavaScript default export, which may be imported as follows

import Device from 'svelte-device-info'

With such an import, the JavaScript API can be used as follows:

  • Device.isMobile - is true if the underlying device is a mobile one (or false otherwise)
  • Device.isPhone - is true if the underlying device is a smartphone (or false otherwise)
  • Device.isTablet - is true if the underlying device is a tablet (or false otherwise)

Nota bene: the reported device factor may be wrong for mobile devices if their browsers have been configured to act like desktop browsers!

  • Device.canHover - is true if the primary pointing device can "hover" over elements (or false otherwise) 
  • Device.PointingAccuracy - determines the current pointing accuracy of the underlying input device
    • none - indicates the absence of any touch input device
    • fine - indicates the presence of a high-resolution touch input device
    • coarse - indicates the presence of a low-resolution touch input device 
  • Device.onPointingAccuracyChanged(callback) - installs a callback function which is automatically invoked, whenever the device's PointingAccuracy has changed
  • Device.oncePointingAccuracyChanged(callback) - installs a callback function which is automatically invoked once, when the device's PointingAccuracy has changed
  • Device.offPointingAccuracyChanged(callback) - uninstalls a previously installed callback function 
  • Device.observesPointingAccuracy - is true while there is at least one callback function observing the current PointingAccuracy (or false otherwise)

CSS Classes

The following CSS classes are added to document.body depending on the current PoiningAccuracy

  • noPointer - indicates the absence of any touch input device
  • finePointer - indicates the presence of a high-resolution touch input device
  • coarsePointer - indicates the presence of a low-resolution touch input device

Build Instructions

You may easily build this package yourself.

Just install NPM according to the instructions for your platform and follow these steps:

  1. either clone this repository using git or download a ZIP archive with its contents to your disk and unpack it there
  2. open a shell and navigate to the root directory of this repository
  3. run npm install in order to install the complete build environment
  4. execute npm run build to create a new build

You may also look into the author's build-configuration-study for a general description of his build environment.

License

MIT License