Git Product home page Git Product logo

ember-responsive's Introduction

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: 768px)',
  tablet:  '(min-width: 769px) and (max-width: 992px)',
  desktop: '(min-width: 993px) 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.

You can then query those breakpoints in your controllers, components, routes, and views:

this.get('media.isMobile'); // => true

Obviously, these properties also propagate to templates:

{{#if media.isDesktop}}
  Desktop view!
{{/if}}

You should 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>

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.

To use the setBreakpoint helper in an acceptance test:

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.

ember-responsive's People

Contributors

alexbaizeau avatar bantic avatar blimmer avatar elwayman02 avatar f3ndot avatar flemse avatar gaurav0 avatar gigr avatar jasonmit avatar meirish avatar minichate avatar mixonic avatar poteto avatar samio avatar tomdale avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.