Skip to content

Instantly share code, notes, and snippets.

@selvagsz
Created February 5, 2016 14:27
Show Gist options
  • Save selvagsz/5bb7af54f551f76bf5ed to your computer and use it in GitHub Desktop.
Save selvagsz/5bb7af54f551f76bf5ed to your computer and use it in GitHub Desktop.
Theming with html5 template tags & services
import Ember from 'ember';
import config from '../config/environment';
const { Service, computed, get } = Ember;
const defaultTheme = config.themes.defaultTheme;
export default Service.extend({
themeTemplateString: computed(function() {
let themeTemplate = document.getElementById('custom-theme-template');
let templateStyles = themeTemplate.content.querySelectorAll('style')[0];
return templateStyles.innerHTML;
}),
customThemeStylesheet: computed(function() {
let customThemeStylesheet = document.getElementById('custom-theme');
if (!customThemeStylesheet) {
customThemeStylesheet = document.createElement('style');
customThemeStylesheet.setAttribute('id', 'custom-theme');
}
return customThemeStylesheet;
}),
setTheme(theme = defaultTheme || {}) {
let templateString = this.get('themeTemplateString');
let customThemeStylesheet = this.get('customThemeStylesheet');
let compiledStyles = replaceValues(templateString)(theme); // Can also use `Handlebars.compile` instead of pattern matching
customThemeStylesheet.innerHTML = compiledStyles;
document.getElementsByTagName('head')[0].appendChild(customThemeStylesheet);
}
});
<template id="custom-theme-template">
<style>
.main-navs {
background: {{navsBgColor}};
}
.main-navs ul > li > a {
color: {{menuTextColor}};
}
</style>
</template>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment