4.1.2 • Published 4 years ago

ember-is-mobile v4.1.2

Weekly downloads
698
License
MIT
Repository
github
Last release
4 years ago

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.

4.1.2

4 years ago

4.1.1

4 years ago

4.1.0

4 years ago

4.0.2

4 years ago

4.0.1

4 years ago

4.0.0

5 years ago

3.0.0

5 years ago

2.0.5

7 years ago

2.0.4

7 years ago

2.0.3

7 years ago

2.0.2

7 years ago

2.0.1

7 years ago

2.0.0

7 years ago

1.0.2

7 years ago

1.0.1

8 years ago

1.0.0

8 years ago