Namespace: Context:detectInteractionType

Context:detectInteractionType

Source:

Methods

detectInteractionType(returnObservableopt, nullable) → {String|Basic.Observable}

Try to figure out the current type of interaction between the user and the document. This is determined by the input device and is currently limited to either "pointer" or "touch".

On call the function returns an educated guess about the fact what interaction type might be more probable based on browser features and sets up event listeners to update Context module's CURRENT_INTERACTION_TYPE observable (to which you may subscribe to be informed about updates), when interaction type should change while the page is being interacted with. In case a touch occurs we determine touch interaction and on mousemove we determine pointer interaction. If you use this observable to set up a class on your document for example you can even relatively safely handle dual devices like a surface book.

Hint: because touch devices also emit a single mousemove after touchend with a single touch we have to block mousemove detection for 1s after the last touchend. Therefore, it takes up to 1s after the last touch event until we are able to detect the change to a pointer device.

Parameters:
Name Type Attributes Default Description
returnObservable Boolean <optional>
<nullable>
false

if set to true, the call returns Context module's CURRENT_INTERACTION_TYPE observable

Source:
Returns:
interaction type string "pointer" or "touch", or the CURRENT_INTERACTION_TYPE observable
Type
String | Basic.Observable
Example
let interactionTypeGuess = detectInteractionType();
detectInteractionType(true).subscribe(function(type){
    document.body.classList.toggle('touch', type === 'touch');
});