Giter Club home page Giter Club logo

ember-theme-changer's Introduction

ember-theme-changer

This Ember-Addon will help you to switch CSS style files on runtime.

For example, let's say you want to support multiple themes in your app and each of the theme styles is defined in its own CSS file (I'll show you how easy is to accomplish this on Ember). Ember-theme-changer addon provides an easy mechanism to change themes and subscribe to theme changes in your different components to handle those styles that you couldn't define using stylesheet.

Installation

ember install ember-theme-changer

Configuration

In this example we want to support Dark and Light themes. To accomplish it you must define all available themes in your config/environment.js file:

// config/environment.js

module.exports = function(environment) {
  var ENV = {
    /* ... */
    theme: {
      themes: [ 'light', 'dark'], // MANDATORY
      defaultTheme: 'dark', // OPTIONAL
      eventName: 'name of the event to be trigger when the theme changes', // OPTIONAL
      cookieName: 'name of cookie used to save the current theme value', // OPTIONAL
      useAssetMap: [default to true in Prod environment] boolean value to indicate if the assets were fingerprinted and an assetMap.json file is available // OPTIONAL
    }
  };
  return ENV;
};

Fingerprinting assets

If your app is fingerprinting your CSS files with Broccoli (which is default to true in prod environment), you could tell the Addon to use the generated /assets/assetMap.json to load the correct asset.

Enable the asset-map generation in your ember-cli-build file:

fingerprint: {
  enabled: Default: app.env === 'production' - Boolean. Enables fingerprinting if true. True by default if current environment is production.
  generateAssetMap: true
}

More info: https://ember-cli.com/asset-compilation#fingerprinting-and-cdn-urls & https://github.com/rickharrison/broccoli-asset-rev

Pre-requisites

Since v0.3.0 ember-theme-changer relies in ember-cli-head v0.4.0 addon to load the theme in runtime and provide Fastboot compatibility.

If you are not already using ember-cli-head (or another addon that requires it) you must follow these 2 simple steps:

  1. Create a file head.hbs in your app/template folder with this content:
// app/template/head.hbs
<link rel="stylesheet" href="{{model.themeHref}}">

(If you already have a head.hbs file, just add the line: <link rel="stylesheet" href="{{model.themeHref}}">)

  1. Add {{head-layout}} to the top of your application template.

Check ember-cli-head for more info.

Defining themes on Ember.

There are many mechanism to define themes in your application. In the following example I'll be defining style variables but you can use any mechanism that works better for you. Also, I'll be using Sass preprocessor, but the same solution applies with Less:

1st) Writing multiple theme files

// app.scss

// In your app.scss file you can define styles that are dependent on the current theme, or import external styles too
@import "ember-modal-dialog/ember-modal-structure";
@import "ember-modal-dialog/ember-modal-appearance";

body {
  width: 100%
}
// main.scss

// here is where you define styles that depends on the current theme. All the theme dependent values are referenced with variables. eg:
body {
  background-color: $bodyBackgroundColor;
}
// dark.scss

// 1) define all the variables you need
$bodyBackgroundColor: '#fff';
...

// 2) Import your style fle where the variables will be consumed.
@import 'main';

And do the same for the rest of the themes. eg:

// light.scss

// 1) Define variables
$bodyBackgroundColor: '#000';
...
// 2) import style file
@import 'main';

2nd) Generating theme files.

By default, Ember only generates 1 css file based on your app.scss content (or app.less, app.css depending in your your style preprocessor), in this case we want to generate 2 extra files (dark.css & light.css). This is accomplish easily by telling Ember about the extra output files in the ember-cli-build.js file:

// ember-cli-build.js
    outputPaths: {
      app: {
        css: {
          'light': '/assets/light.css',
          'dark': '/assets/dark.css'
        }
      }
    },

As a result, ember will generate 3 css files (app.css, light.css & dark.css).

For more info, please read the official Ember-CLI doc: https://ember-cli.com/user-guide/#configuring-output-paths

Usage

All interaction with the addon is though the themeChanger service.

Change themes

You can circle through the themes invoking the service toggleTheme method, or set an specific value with the theme property.

// your_component.js:

themeChanger: service(),
....

action: {
  switchTheme() {
    this.get('themeSwitcher').toggleTheme();
  },
  setDarkTheme() {
    this.get('themeSwitcher').set('theme', 'dark');
  }
}

Theme-change Event

In some situation you could have some styles defined outside of the CSS boundaries, this addon provides an event you can subscribe to detect changes int he current theme allowing you to execute any code.

// your_component.js:
themeChanger: service(),
chartColor,
....

  didReceiveAttrs() {
    this._super(...arguments);

    // subscribe to the theme changes event
    this.get('themeChanger').onThemeChanged((theme) => {

      // your code to handle theme changes:
      if (theme === 'light') {
        this.set('chartColor', 'black');
      } else {
        this.set('chartColor', 'white');
      }
      ...
    });

  },


  // Its important to unsubscribe to the event when your component will be destroyed
  willDestroyElement() {
    this.get('themeChanger').offThemeChanged();
    this._super(...arguments);
  }

ember-theme-changer's People

Contributors

demee avatar ember-tomster avatar mervinva avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

ember-theme-changer's Issues

Doesn't work with fingerprinted assets

Ember cli defaults create fingerprinted assets for production but the service still attempts to set theme based on the non-fingerprinted name. For example, ember-cli generates light-0aa73b63d391eea318ac06eccff95b90.css for light.scss theme but the service tries to lookup light.css anyway.

Breaks fastboot

This addon breaks fastboot with this error:

ReferenceError: document is not defined
    at Class._generateStyleTag (P:\Project\tmp\broccoli_merge_trees-output_path-rLftrlAj.tmp\assets\addon-tree-output\ember-theme-changer\services\theme-changer.js:81:1)
    at Object.initialize (P:\Project\tmp\broccoli_merge_trees-output_path-rLftrlAj.tmp\assets\my-app\instance-initializers\theme-init.js:10:1)

Maybe it could use ember-cli-head for the fastboot part instead?

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.