Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

ember-responsive

freshbooks104.9kMIT5.0.0

An ember-cli addon that gives you a simple, Ember-aware way of dealing with media queries.

ember-addon, media query

readme

ember-responsive Ember Observer Score

ember-responsive is an ember-cli addon that give you a simple, Ember-aware way of dealing with media queries.

All you need to do is tell it your application's breakpoints and it'll expose the rest for you. Here is an interactive demo

Requirements

ember-responsive needs window.matchMedia() to function, which isn't available in all browsers. Compatibility matrix

There is a polyfill by Paul Irish called matchMedia.js that will add support to older browsers

Getting Started

This is an ember-cli addon so, so all you need to do is

ember install ember-responsive

After that, simply register the breakpoints that are pertinent to your application in app/breakpoints.js:

export default {
  mobile:  '(max-width: 767px)',
  tablet:  '(min-width: 768px) and (max-width: 991px)',
  desktop: '(min-width: 992px) and (max-width: 1200px)',
  jumbo:   '(min-width: 1201px)'
};

This default config has already been provided for you. If you wish to change the values or add new ones, simply create a new app/breakpoints.js in your project and export your chosen config.

Now you can inject the media service in any object with access to the container:

import Component from '@ember/component';
import { inject as service } from '@ember/service';

export default Controller.extend({
  media: service(),
  doSomething() {
    this.get('media.isMobile'); // => true
  }
});

In your templates you have access to the media helper that allows you to query breakpoints easily.

{{#if (media 'isDesktop')}}
  Desktop view!
{{/if}}

You can also bind the list of active media queries to your app's rootElement. This means you won't have to deal with complicated media queries in CSS, instead simply use classes to style the different devices.

In your application.hbs template:

<div class="{{media 'classNames'}}">
  {{outlet}}
</div>

Injection

If you find explicitly injecting the service too repetitive, you can setup an initializer to inject it automatically in every controller and component like this:

// in app/initializers/ember-responsive
export default {
  name: 'responsive',
  initialize(application) {
    application.inject('controller', 'media', 'service:media');
    application.inject('component', 'media', 'service:media');
  }
};

Additional Media Queries

Media queries beyond those based on size will also work in your breakpoints.js file, including:

  portrait: '(orientation: portrait)',
  landscape: '(orientation: landscape)',
  dark: '(prefers-color-scheme: dark)',
  light: '(prefers-color-scheme: light)'

Updating to 2.x

When updating this addon, make sure to run the generate command. Choose no to overriding existing files, unless you want the defaults. This command has to be run when updating to 2.x if your application relies on automatic injection. Because as of version 1.2.9, the addon will generate an initializer to allow users to customize injection.

ember g ember-responsive

Updating to 3.x

The major breaking changes to update to 3.x are so far:

Usage in engines

If you are using engines and you want to share responsive behaviour between the main application and engine, you must pass the 'media' service to the engine app.

Testing Helpers

This project provides a single test helper which works in both integration and acceptance tests to assist in testing content specific to different breakpoints.

Acceptance Tests

...
import { setBreakpoint } from 'ember-responsive/test-support';

...

test('example test', function(assert) {
  setBreakpoint('mobile');
  visit('/');

  andThen(function() {
    // assert something specific to mobile
  });
});

Integration Tests

...
import { setBreakpoint } from 'ember-responsive/test-support';

...

test('it renders', function(assert) {
  setBreakpoint('mobile');

  this.render(hbs`{{your-component}}`);

  // assert something specific to mobile
});

Multiple Breakpoints in Tests

You can set multiple breakpoints to the helper. This is useful if your breakpoints.js file defines breakpoints that overlap.

// in app/breakpoints.js
export default {
  tablet:  '(min-width: 768px)',
  desktop: '(min-width: 992px)',
  jumbo:   '(min-width: 1201px)'
};

// in test file
...
import { setBreakpoint } from 'ember-responsive/test-support';

...

test('it renders', function(assert) {
  setBreakpoint(['tablet', 'desktop']);

  this.render(hbs`{{your-component}}`);

  // assert something specific to desktop, i.e. sizes 992px - 1201px
  // `isTablet` and `isDesktop` will both return true
});

Tests

To run the tests, after cloning do:

npm install
bower install
npm test

License

This library is lovingly brought to you by the FreshBooks developers. We've released it under the MIT license.

changelog

Changelog

[4.0.2]

  • BUGFIX: CP's are not firing on tracked media props (#350 @rreckonerr)

[4.0.1]

  • Make test support setBreakpoint IE11 compatible (#303 @raido)

[4.0.0]

  • Use Tracked based services on Match (#286 @snewcomer)
  • Breaking Change: Bump Node 10

[3.0.5]

  • Update dependencies (#153 @k-fish)

[3.0.4]

  • Update dependencies (#151 @danielchanja)
  • Update dependencies (#150 @danielchanja)

[3.0.3]

  • Move ember-cli-babel back to 6.6 (#148 @calvin-fb)

[3.0.2]

  • Update the package dependencies to address security vulnerabilities. (#changes @danielchanja)

[3.0.1]

  • Fixed init not calling super, which breaks for Ember 3.11 (#145 @ryanto)

[3.0.0]

  • Breaking Change: All test helpers now use the same import: import { setBreakpoint } from 'ember-responsive/test-support';. See README for examples. (#124)
  • Breaking Change: Make auto-injection an opt-in (#124)
  • Breaking Change: The media helper now needs to be passed a breakpoint as a string. {{media.isDesktop}} -> {{media 'isDesktop'}}. (#126, #128)
  • Breaking Change: You have to update to using the new testing api for tests to work properly. https://github.com/emberjs/rfcs/blob/master/text/0232-simplify-qunit-testing-api.md

[2.0.8]

  • Use new testing api (#122 @cibernox)

[2.0.7]

  • Added safeguard to fail on jQuery usage (#120 @cibernox)

[2.0.6]

  • Update ember-cli (#119 @k-fish)

2.0.5

  • Fix breakpoint coherency to match breakpoint widths across the addon. Existing addons with a generated breakpoints.js should not be affected, re-generating or generating for the first time will use the new breakpoints. (#107 @telmaantunes) (#106 @AndreJoaquim)

2.0.4

  • Update dependencies (#98 @calvinlough)

2.0.3

  • Update ember-cli-babel and remove some unneeded packages (#97 @calvinlough)

2.0.2

  • Fix: Add more floating room for ember-getowner-polyfill dependency (#91 @kturney)

2.0.1

  • Fix: update ember-getowner-polyfill to remove deprecation (#86 @kellyselden)
  • Fix: Remove deprecated Ember.K (#89 @cibernox)

2.0.0

  • Breaking Change: Initializer is now generated instead of included. Run ember g ember-responsive to generate the required file. (#83)

1.2.10

  • Fixed: Initializer breaking change, re-add initializer before 2.0 (#82)

1.2.9

  • No changes

1.2.8

  • Changed: Expose automatic injection as a generated file (#78)
  • Fixed: Ember-try scenarios (#78)

1.2.7

  • Fix setting on a destroyed element (#67 @Gaurav0)

1.2.6

  • New test helpers (#65 @blimmer)
  • ES6 syntax (#63 @blimmer)

1.2.4

  • Support fastboot (#56 @tomdale)
  • Remove use of private method lookupFactory (#57 @poteto)
  • fix ember 2.3 deprecations (#53 @minichate)

1.2.3

  • fix compatibility with older versions of ember (#47 @alexbaizeau)

1.2.2

  • fix deprecation warnings (#46 @alexbaizeau)

1.2.1

  • update addon via ember-init (@elwayman02)

1.2.0

  • Adds breakpoints.js to addon. (#40, thanks @elwayman02)

1.1.1

  • Fixes support for Ember 2.1 beta (#37, thanks @jasonmit)
  • Upgrade addon to use Ember 1.13.10
  • Updates Travis CI config and testing to use matrix

1.1.0

  • Upgrade to ember-cli 0.2.3

1.0.2

  • Stop using function prototype extensions

1.0.1

  • Better Readme
  • Passing build

    1.0.0

  • CHANGE: ember-cli addon

  • BREAKING CHANGE: Breakpoints are now defined in app/breakpoints.js