ember-is-mobile

4.1.2 • Public • Published

ember-is-mobile

Build Status

Detect requests from mobile devices in your Ember apps.

This addon uses isMobile.js for parsing user agent strings. Works seamlessly in both the browser and in FastBoot.

Install

ember install ember-is-mobile

Compatibility

  • Ember.js v3.16 or above
  • Ember CLI v2.13 or above
  • Node.js v10 or above

Usage

As a service

The isMobile service 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.

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

import Component from '@ember/component';

export default class extends Component {
  @service isMobile;

  constructor() {
    super(...arguments);
    console.log(this.isMobile.any); // => true|false
  }
}
{{#if this.isMobile.any}}
  I'm on a mobile device!
{{/if}}

As a helper

The is-mobile helper can be used as an alternative to isMobile service. It takes an optional 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>

Directly

This addon also re-exports isMobile.js, so you can import it yourself if you need to. In most cases you should use the service instead.

import isMobile from 'ember-is-mobile';

In the browser, you can call the isMobile function with no arguments.

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

In FastBoot, however, you need to fetch the FastBoot headers yourself and run the method manually. Note that this syntax will only work in FastBoot.

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

Contributing

See the Contributing guide for details.

For more information on using ember-cli, visit https://ember-cli.com/.

License

MIT © Sander Melnikov.

Package Sidebar

Install

npm i ember-is-mobile

Weekly Downloads

425

Version

4.1.2

License

MIT

Unpacked Size

9.66 kB

Total Files

21

Last publish

Collaborators

  • sandydoo