Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ember-responsive

Package Overview
Dependencies
Maintainers
7
Versions
51
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ember-responsive

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

  • 3.0.0-beta.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
17K
decreased by-17.05%
Maintainers
7
Weekly downloads
 
Created
Source

ember-responsive Build Status 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');
  }
};

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

Testing Helpers

This project provides several testing helpers to assist in testing content specific to different breakpoints.

Acceptance Tests

This project provides an acceptance testing helper to assist in testing content specific to different breakpoints.

The provided testing helper has to be added to start-app.js before the following example will work.

Changes required to start-app
// app/helpers/start-app.js
...
import './responsive'; // You need to import helpers to use them in your tests.
...
Acceptance Helper Usage
test('example test', function(assert) {
  setBreakpoint('mobile');
  visit('/');

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

The default breakpoint for testing defaults to desktop. You can modify this by changing _defaultBreakpoint in tests/helpers/responsive.js.

Integration Tests

Since the entire application isn't spun up for an integration tests, the setBreakpoint acceptance test helper won't work. In this case, you'll need to use the setBreakpointForIntegrationTest helper.

To use the setBreakpointForIntegrationTest helper in an integration test:

import { moduleForComponent, test } from 'ember-qunit';
import hbs from 'htmlbars-inline-precompile';
import { setBreakpointForIntegrationTest } from 'your-app-name/tests/helpers/responsive';

moduleForComponent('foo-bar', 'Integration | Component | foo bar', {
  integration: true
});

test('it renders', function(assert) {
  setBreakpointForIntegrationTest(this, 'mobile');
  this.render(hbs`{{foo-bar media=media}}`); // IMPORTANT: you must pass the media service

  // assert something specific to mobile
});

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.

Keywords

FAQs

Package last updated on 03 Apr 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc