-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added doc setting developing to false Fix toggleTheme method Fix toggleTheme method 0.0.3
- Loading branch information
Leandro Diato
committed
Oct 16, 2017
1 parent
00d6e00
commit f3500a4
Showing
11 changed files
with
339 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"cSpell.words": [ | ||
"Evented" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,162 @@ | ||
# ember-theme-changer | ||
|
||
This README outlines the details of collaborating on this Ember addon. | ||
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 | ||
|
||
* `git clone <repository-url>` this repository | ||
* `cd ember-theme-changer` | ||
* `npm install` | ||
* `bower install` | ||
`ember install ember-theme-changer` | ||
|
||
|
||
## Configuration | ||
|
||
In our example we want to support Dark and Light themes. To accomplish it you must define Ember-Changer available themes in your `config/environment.js` file: | ||
|
||
``` | ||
/* jshint node: true */ | ||
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 | ||
} | ||
}; | ||
return ENV; | ||
}; | ||
``` | ||
|
||
## 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. | ||
|
||
|
||
## Running | ||
``` | ||
// your_component.js: | ||
themeChanger: service(), | ||
chartColor, | ||
.... | ||
* `ember serve` | ||
* Visit your app at [http://localhost:4200](http://localhost:4200). | ||
didReceiveAttrs() { | ||
this._super(...arguments); | ||
## Running Tests | ||
// subscribe to the theme changes event | ||
this.get('themeChanger').onThemeChanged((theme) => { | ||
* `npm test` (Runs `ember try:each` to test your addon against multiple Ember versions) | ||
* `ember test` | ||
* `ember test --server` | ||
// your code to handle theme changes: | ||
if (theme === 'light') { | ||
this.set('chartColor', 'black'); | ||
} else { | ||
this.set('chartColor', 'white'); | ||
} | ||
... | ||
}); | ||
## Building | ||
}, | ||
* `ember build` | ||
For more information on using ember-cli, visit [https://ember-cli.com/](https://ember-cli.com/). | ||
// Its important to unsubscribe to the event when your component will be destroyed | ||
willDestroyElement() { | ||
this.get('themeChanger').offThemeChanged(); | ||
this._super(...arguments); | ||
} | ||
``` |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
import Ember from 'ember'; | ||
|
||
const { | ||
Service, | ||
Evented, | ||
warn, | ||
isEmpty, | ||
isArray, | ||
getOwner, | ||
computed, | ||
inject: { service } | ||
} = Ember; | ||
|
||
const LINK_TAG_ID = 'ember-theme-changer-stylesheet'; | ||
|
||
export default Service.extend(Evented, { | ||
|
||
cookies: service(), | ||
_themeName: null, | ||
defaultTheme: null, | ||
cookieName: 'ember-theme-changer__current-theme', | ||
eventName: 'ember-theme-changer__theme-changed', | ||
themes: [], | ||
|
||
// @private | ||
init() { | ||
this._super(...arguments); | ||
let owner = getOwner(this); | ||
let ENV = owner.factoryFor('config:environment').class; | ||
|
||
let defaultTheme = null; | ||
if (!ENV.theme) { | ||
warn('Ember-theme-changer did not find a theme configuration.\neg: themes: { themes: [\'theme1\', \'theme2\',...] }.', false, | ||
{ id: 'ember-theme-changer.theme' }); | ||
} else { | ||
|
||
if (ENV.theme.themes == null) { | ||
warn('Ember-theme-changer did not find themes in your environment file.\neg: themes: { themes: [\'theme1\', \'theme2\',...] }.', | ||
false, | ||
{ id: 'ember-theme-changer.themes' }); | ||
} else if (isEmpty(ENV.theme.themes)) { | ||
warn('Ember-theme-changer requires themes to be defined. Please add an array of supported themes in your Environment file.\neg: themes: { themes: [\'theme1\', \'theme2\',...] }.', false, | ||
{ id: 'ember-theme-changer.themes.empty' }); | ||
} else if (!isArray(ENV.theme.themes)) { | ||
warn('Ember-theme-changer requires the themes configuration to be an array. Please add an array of supported themes in your Environment file.\neg: themes: { themes: [\'theme1\', \'theme2\',...] }.', | ||
false, | ||
{ id: 'ember-theme-changer.themes.array' }); | ||
} else { | ||
this.set('themes', ENV.theme.themes); | ||
defaultTheme = (ENV.theme || {}).defaultTheme; | ||
if (defaultTheme == null) { | ||
defaultTheme = ENV.theme.themes.get('firstObject'); | ||
warn(`ember-theme-changer did not find a default theme; falling back to "${defaultTheme}".`, | ||
false, { | ||
id: 'ember-theme.changer.default-theme' | ||
}); | ||
} else { | ||
if (!ENV.theme.themes.includes(defaultTheme)) { | ||
const firstTheme = ENV.theme.themes.get('firstObject'); | ||
warn(`ember-theme-changer, default theme \'${defaultTheme}\' is not listed as part of the themes list: \'${ENV.theme.themes}\'. Defaulting to \'${firstTheme}\'.`, | ||
false, { | ||
id: 'ember-theme.changer.invalid-default-theme' | ||
}); | ||
defaultTheme = firstTheme; | ||
} | ||
} | ||
this.set('defaultTheme', defaultTheme); | ||
|
||
// let { cookieName, eventName } = this.getProperties('cookieName', 'eventName'); | ||
if (!isEmpty(ENV.theme.cookieName)) { | ||
this.set('cookieName', ENV.theme.cookieName); | ||
} | ||
if (!isEmpty(ENV.theme.eventName)) { | ||
this.set('eventName', ENV.theme.eventName); | ||
} | ||
|
||
} | ||
} | ||
}, | ||
|
||
// @private | ||
_generateStyleTag() { | ||
|
||
const headTag = document.head; | ||
const linkTag = document.createElement('link'); | ||
linkTag.id = LINK_TAG_ID; | ||
linkTag.rel = 'stylesheet'; | ||
|
||
const { cookies, cookieName, defaultTheme } = this.getProperties('cookies', 'cookieName', 'defaultTheme'); | ||
let themeValue = cookies.read(cookieName) || defaultTheme; | ||
|
||
if (!isEmpty(themeValue)) { | ||
linkTag.href = '/assets/' + themeValue + '.css'; | ||
this.trigger('theme-changed', themeValue); | ||
} | ||
|
||
headTag.appendChild(linkTag); | ||
}, | ||
|
||
// @public | ||
onThemeChanged(callback) { | ||
const eventName = this.get('eventName'); | ||
this.on(eventName, callback); | ||
}, | ||
|
||
// @public | ||
offThemeChanged() { | ||
const eventName = this.get('eventName'); | ||
this.off(eventName); | ||
}, | ||
|
||
// @public | ||
toggleTheme() { | ||
const { themes, theme } = this.getProperties('themes', 'theme'); | ||
const currentIndex = themes.indexOf(theme); | ||
let newTheme = null; | ||
if (currentIndex === theme.length - 1) { | ||
newTheme = themes.get(0); | ||
} else { | ||
newTheme = themes.get(currentIndex+1); | ||
} | ||
|
||
this.set('theme', newTheme); | ||
}, | ||
|
||
// @public | ||
theme: computed({ | ||
get() { | ||
const { cookies, cookieName } = this.getProperties('cookies', 'cookieName'); | ||
let themeValue = cookies.read(cookieName); | ||
if (isEmpty(themeValue)) { | ||
themeValue = this.get('defaultTheme'); | ||
|
||
if (!isEmpty(themeValue)) { | ||
// no theme saved. Using and saving default theme: | ||
cookies.write(cookieName, themeValue, { path: '/', expires: 'Fri, 31 Dec 9999 23:59:59 GMT' }); | ||
const eventName = this.get('eventName'); | ||
this.trigger(eventName, themeValue); | ||
} | ||
} | ||
return themeValue; | ||
}, | ||
set(key, value) { | ||
const { cookies, cookieName, eventName } = this.getProperties('cookies', 'cookieName', 'eventName'); | ||
|
||
// 1- Update the theme value in the cookie | ||
cookies.write(cookieName, value, { path: '/', expires: 'Fri, 31 Dec 9999 23:59:59 GMT' }); | ||
// 2- Uploading the new style | ||
document.getElementById(LINK_TAG_ID).setAttribute("href", '/assets/' + value + '.css'); | ||
// 3- Triggering theme-change notification | ||
this.trigger(eventName, value); | ||
|
||
return value; | ||
} | ||
}) | ||
|
||
}); |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export function initialize(applicationInstance) { | ||
let themes = applicationInstance.lookup('service:theme-changer'); | ||
themes._generateStyleTag(); | ||
} | ||
|
||
export default { | ||
name: 'ember-theme-changer-init', | ||
initialize: initialize | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import ThemeChanger from 'ember-theme-changer/services/theme-changer'; | ||
|
||
export default ThemeChanger; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"compilerOptions":{"target":"es6","experimentalDecorators":true},"exclude":["node_modules","bower_components","tmp","vendor",".git","dist"]} |
Oops, something went wrong.