8000 GitHub - sandydoo/ember-is-mobile at v3.0.0
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
This repository was archived by the owner on Apr 2, 2024. It is now read-only.

sandydoo/ember-is-mobile

Repository files navigation

🐹 ember-is-mobile 📱

Build Status

Detect requests from mobile devices in your Ember apps!

Works seamlessly in both the browser and in FastBoot!

This addon leverages isMobile.js for parsing user agent strings. It also exports isMobile.js as an ES6 accessible module.

Installation

ember install ember-is-mobile

Usage

isMobile service

The isMobile service is auto-injected into your app and provides access to the results of the user agent tests provided by isMobile.js. This service works in both the browser and in FastBoot. The FastBoot support is particularly useful if you want to conditionally render large blocks of content to target desktop or mobile devices.

N.B. Don't use this addon as a replacement for good responsive design!

You can query the user agent tests in your controllers, components and routes:

this.get('isMobile.any'); // => true|false

The properties are also available in templates:

{{#if isMobile.any}}
  I'm on a mobile device!
{{/if}}

is-mobile helper

The is-mobile helper can be used as an alternative to isMobile service. It expects one argument – a string specifying the user agent test.

{{#if (is-mobile "any")}}
  I'm on a mobile device
{{/if}}
<div class="{{if (is-mobile "android.phone") "is-android"}} {{if (is-mobile "apple.phone") "is-apple"}}">
  I'm on a mobile device
</div>

The full list of user agent tests provided by isMobile:

  • any
  • phone
  • tablet
  • apple
  • android
  • amazon
  • windows
  • seven_inch
  • other

Importing

This addon also shims isMobile.js, so you can import it yourself if you need to.

In most cases you should use the service instead. If you need to use this addon as a shim only, open an issue and I'll consider a way of making the service injection opt-out.

import isMobile from 'ember-is-mobile';

In the browser, the isMobile object returns the computed user agent tests.

isMobile; // => { apple: {}, windows: {}, ... }

In FastBoot however, the import returns just the isMobile function, since navigator is not available. You can fetch the FastBoot headers yourself and run the method manually. Note that this syntax will only work in FastBoot.

if (this.get('fastboot.isFastBoot')) {
    const headers = this.get('fastboot.request.headers');
    const userAgent = headers.get('user-agent');
    isMobile(userAgent); // => { apple: {}, windows: {}, ... }
}

Upgrading

This addon uses a blueprint to add ismobilejs to your app's dependencies using a blueprint. This step is necessary if you're using FastBoot. To get the latest version from the blueprint, run ember g ember-is-mobile.

License

This project is licensed under the MIT License © Sander Melnikov.

0